190819-Hallro-最短路/dp

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<string>
 5 #include<cstring>
 6 #include<algorithm>
 7 using namespace std;
 8 namespace Moxing{
 9     const int N=5e5+50; 
10     int t,x,dp[N],f[N],y[N],st[N],tp,a[N],num;
11     struct main{
12         main(){
13             scanf("%d",&t);
14             while(t--){
15                 scanf("%d",&x);
16                 memset(dp,0,sizeof(dp));
17                 int i=1;
18                 y[1]=1,dp[0]=1,num=1,a[1]=0;
19                 for(;;i++,y[i]=y[i-1]*10%x){
20                     if(dp[(x-y[i])%x]){
21                         f[0]=i;break ;
22                     }
23                     for(int j=1,k=y[i];j<=num;j++){
24                         if(j!=1) k+=a[j]+x-a[j-1];
25                         while(k>=x) k-=x;
26                         if(!dp[k]) st[++tp]=k;
27                     }
28                     while(tp){
29                         int z=st[tp--];
30                         dp[z]=1,f[z]=i,a[++num]=z;
31                     }
32                 }
33                 for(int j=i,now=0;j;j--){
34                     if(j==f[now]) putchar('1'),now=(now+x-y[j])%x;
35                     else putchar('0');
36                 }
37                 puts("");
38             }
39             exit(0);
40         }
41     }UniversalLove;
42 }
43 int main(){
44     Moxing::main(); 
45 }
View Code
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<string>
 5 #include<cstring>
 6 #include<algorithm>
 7 #include<queue>
 8 #include<stack>
 9 using namespace std;
10 namespace Moxing{
11     const int N=5e5+20;
12     bool vis[N];
13     int last[N],t,n;
14     char schr[N];
15     struct main{
16         main(){
17             scanf("%d",&t);
18             while(t--){
19                 scanf("%d",&n);
20                 fill(vis,vis+1+n,0);
21                 queue<int>q;
22                 vis[1%n]=1,q.push(1%n);
23                 while(q.size()){
24                     int pos=q.front();q.pop();
25                     for(int i=0;i<=1;i++){
26                         int tar=(pos*10+i)%n;
27                         if(vis[tar]) continue ;
28                         vis[tar]=1,last[tar]=pos,schr[tar]='0'+i,q.push(tar);
29                         if(!tar) break ;
30                     }
31                     stack<char>ans;
32                     for(int i=0;last[i];i=last[i]) ans.push(schr[i]);
33                     ans.push('1');
34                     while(ans.size()) putchar(ans.top()),ans.pop();
35                     putchar('\n');
36                 } 
37             }
38             exit(0);
39         }
40     }UniversalLove;
41 }
42 int main(){
43     Moxing::main();
44 }
View Code

 

猜你喜欢

转载自www.cnblogs.com/Moxingtianxia/p/11708543.html