洛谷P1467 循环数 Runaround Numbers

洛谷P1467 循环数 Runaround Numbers

 

 1 #include <cstdio> 
 2 #include <cmath>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <iostream> 
 6 #include <iomanip> 
 7 #include <string> 
 8 #include <algorithm> 
 9 #define LL long long 
10 #define For(i,j,k) for(int i=j;i<=k;i++) 
11 #define Dow(i,j,k) for(int i=j;i>=k;i--) 
12 using namespace std ;  
13 
14 const int N = 50011 ; 
15 int n,len ; 
16 int bit[11],flag[10] ;  
17 
18 inline int read() 
19 {
20     int x = 0 , f = 1 ; 
21     char ch = getchar() ; 
22     while(ch<'0'||ch>'9') { if(ch=='-') f = -1 ; ch = getchar(); } 
23     while(ch>='0'&&ch<='9') { x = x * 10+ch-48 ; ch = getchar(); } 
24     return x * f ; 
25 }
26 
27 inline void add() 
28 {
29     bit[0]+=1 ; 
30     For(i,0,len-1) 
31         if(bit[i]==10) bit[i]=0,bit[i+1]++ ; 
32         else return ; 
33     if(bit[len]) len++ ; 
34 }
35 
36 inline bool check() 
37 {
38     int pos = 0,cnt = 0 ; 
39     For(i,0,9) flag[i] = 0 ; 
40     For(i,0,len-1) flag[bit[i]]++ ; 
41     For(i,1,9) if(flag[i]>1) return 0 ;  
42     if(flag[0]) return 0 ; 
43     For(i,0,9) flag[i] = 0 ;
44     
45     For(i,0,(len-1)/2) swap(bit[i],bit[len-1-i]) ; 
46     For(i,1,len) {
47         (pos+=bit[pos])%=len ; 
48         if(!flag[bit[pos]]) cnt++ ; 
49         flag[bit[pos]] = 1 ; 
50     }
51     For(i,0,(len-1)/2) swap(bit[i],bit[len-1-i]) ; 
52     if(cnt!=len) return 0 ; 
53     if(pos) return 0 ; 
54     return 1 ; 
55 }
56 
57 inline void write() 
58 {
59     int ans = 0 ; 
60     Dow(i,len-1,0)
61         (ans*=10)+=bit[i] ; 
62     printf("%d\n",ans) ; 
63     exit(0) ; 
64 }
65 
66 int main() 
67 {
68     n = read() ;
69     if(n==0) bit[len++]=0 ; 
70     while(n) {
71         bit[len++]=n%10 ; 
72         n/=10 ; 
73     }  
74     while(1) {
75         add() ; 
76         if(check()) write() ; 
77     }
78     return 0 ; 
79 }

 

posted @ 2017-10-02 22:46  third2333  阅读(286)  评论(0编辑  收藏  举报