1341 - Different Digits

假如只用一个数字的话, 如: a, aaa, aaaa, ...

由鸽巢原理, 必存在一个连续的区间, 他们的和是n的倍数, 证明如下:

     我们考虑n个前n项和,s1, s2, s3...., 如果当中有某个数可被n整除,则结论成立,否则考虑每一个除以n的余数, 余数等于1, 2, 3...n - 1. 有n - 1个余数, 确有n个和, 必定有两个前n项和同余, 设这两个同余的数为bn + r,  cn + r,  则两式相减,得(b - c)n, 是的整数倍,结论成立。

因此最后的结果必定要么就包含一个数字, 要么包含两个数字, 接下来就是解决这问题的利器BFS了。

View Code
  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdio>
  4 #include<queue>
  5 #include<string>
  6 using  namespace std;
  7 
  8 struct node1 {
  9     int i;
 10     int mod;
 11     int k;
 12 };
 13 struct node2 {
 14     int mod;
 15     string s;
 16 };
 17 int n;
 18 bool hash[10][70000];
 19 bool test_one() {
 20     int sum = 0;
 21     memset(hash, 0, sizeof(hash));
 22     queue<node1>Q;
 23     for(int i = 1; i < 10; ++i) {
 24         node1 u;
 25         u.i = i;
 26         u.mod = (i) % n;
 27         u.k = 1;
 28         if(!hash[i][u.mod]) {
 29             hash[i][u.mod] = true;
 30             Q.push(u);
 31         }
 32     }
 33     while(!Q.empty()) {
 34         node1 u = Q.front();
 35         Q.pop();
 36         if(u.mod == 0) {
 37             for(int ii = 0; ii < u.k; ++ii) {
 38                 cout << u.i;
 39             }
 40             cout << endl;
 41             return true;
 42         }
 43         node1 v;
 44         v.mod = (u.mod * 10 + u.i) % n;
 45         v.k = u.k + 1;
 46         v.i = u.i;
 47         if(!hash[u.i][v.mod]) {
 48             hash[u.i][v.mod] = true;
 49             Q.push(v);
 50         }
 51     }
 52     return false;
 53 }
 54 
 55 bool has[70000];
 56 bool test_two(char p, char q, string &ans) {
 57     memset(has, 0, sizeof(has));
 58     queue<node2>Q;
 59     if(p != '0') {
 60         node2 u;
 61         u.mod = (p - '0') % n;
 62         u.s = "";
 63         u.s += p;
 64         if(!has[u.mod])
 65             Q.push(u);
 66     }
 67     if(q != '0') {
 68         node2 u;
 69         u.mod = (q -'0') % n;
 70         u.s = "";
 71         u.s += q;
 72         if(!has[u.mod])
 73             Q.push(u);
 74     }
 75 
 76     while(!Q.empty()) {
 77         node2 u = Q.front();
 78         Q.pop();
 79         if(u.mod == 0) {
 80             ans = u.s;
 81             return true;
 82         }
 83         node2 v;
 84         v.mod = (u.mod * 10 + p - '0') % n;
 85         v.s = u.s + p;
 86         if(!has[v.mod]) {
 87             Q.push(v);
 88             has[v.mod] = true;
 89         }
 90         v.mod = (u.mod * 10 + q - '0') % n;
 91         v.s = u.s + q;
 92         if(!has[v.mod]) {
 93             Q.push(v);
 94             has[v.mod] = true;
 95         }
 96     }
 97     return false;
 98 }
 99 
100 int main() {
101     while(cin >> n && n) {
102         bool flg = false;
103         if(test_one()) continue;
104         string ans = "";
105         for(char i = '0'; i <= '9'; ++i) {
106             for(char j = i + 1; j <= '9'; ++j) {
107                 string tmp;
108                 if(test_two(i, j, tmp)) {
109                     if(ans == "") ans = tmp;
110                     else if(ans.size() == tmp.size()) {
111                         if(ans > tmp) ans = tmp;
112                     }
113                     else if(ans.size() > tmp.size()) ans = tmp;
114                 }
115             }
116         }
117         cout << ans << endl;
118     }
119     return 0;
120 }

 

posted @ 2013-01-29 21:09  ACSeed  Views(124)  Comments(0)    收藏  举报