码迷,mamicode.com
首页 > 其他好文 > 详细

产生数

时间:2018-07-29 00:10:49      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:show   bfs   oid   enable   sed   scan   ==   set   ide   

刚开始看到此题,以为是bfs,便高高兴兴的写了一堆。。。

不幸的事发生了,tle,只得仔细撕烤。

突然发现找到一个数字可以变化的次数和,之后在连续乘起来 得到的结果就是变化次数。

高精警告

 

40分tle:

技术分享图片
 1 // luogu-judger-enable-o2
 2 #include<bits/stdc++.h>
 3 namespace ZDY{
 4 #define fir freopen("2.in","r",stdin)
 5 #define ll long long
 6 #define db double
 7 #define Fur(i,x,y) for(int i=x;i<=y;i++)
 8 #define fur(i,x,y) for(i=x;i<=y;i++)
 9 #define Fdr(i,x,y) for(int i=x;i>=y;i--)
10 #define in2(x,y) in(x),in(y)
11 #define in3(x,y,z) in2(x,y),in(z)
12 #define in4(a,b,c,d) in2(a,b);in2(c,d)
13 #define clr(x,y) memset(x,y,sizeof(x))
14 #define cpy(x,y) memcpy(x,y,sizeof(x))
15 #define fl(i,x) for(ll i=head[x],to;to=e[i].to,i;i=e[i].next)
16 #define inf 2147483647
17 }
18 namespace FAST{
19 #define pob (fwrite(fob::b,sizeof(char),fob::f-fob::b,stdout),fob::f=fob::b,0)
20 #define pc(x) (*(fob::f++)=(x),(fob::f==fob::g)?pob:0)
21 #define gc ((*fib::f)?(*(fib ::f++)):(fgets(fib::b,sizeof(fib::b),stdin)?(fib::f=fib::b,*(fib::f++)):-1))
22 namespace fib{char b[300000]= {},*f=b;}namespace fob{char b[300000]= {},*f=b,*g=b+300000-2;}struct foce{~foce(){pob;fflush(stdout);}} _foce;namespace ib{char b[100];}template<class T>inline void in(T &x){x=0;char c;bool f=0;while((c=gc)>9||c<0)if(c==-)f=!f;x=c-48;while((c=gc)<=9&&c>=0)x=x*10+c-48;if(f)x=-x;}template<class T>inline void out(T x){if(x==0){pc(48);return;}if(x<0){pc(-);x=-x;}char *s=ib::b;while(x) *(++s)=x%10,x/=10;while(s!=ib::b) pc((*(s--))+48);}template<class T>inline void outn(T x){out(x);pc(\n);}
23 inline char sc(){char c=gc;while(!((A<=c&&c<=Z)||(a<=c&&c<=z)||(0<=c<=9)))c=gc;return c;}
24 template<class T>inline T ABS(T x){return x>0?x:-x;}
25 template<class T>inline T MAX(T x,T y){return x>y?x:y;}
26 template<class T>inline T MIN(T x,T y){return x<y?x:y;}
27 template<class T>inline T GCD(T x,T y){return y?GCD(y,x%y):x;}
28 template<class T>inline void swap(T x,T y){T t=x;y=t;x=y;}
29 }using namespace FAST;using namespace ZDY;using namespace std;
30 #define N 1000001
31 #define mod 83958397
32 int h=0,t=1,k,d=0,cnt=0,ans=0,head[11];
33 bool v[mod];
34 struct edge{int next,to;}e[30];
35 struct sz{int s[32];}q[N];
36 inline void add(int x,int y){e[++cnt].to=y;e[cnt].next=head[x];head[x]=cnt;}
37 inline int hash(sz x){
38     int s=0;
39     Fur(i,1,d)s=(s*10+x.s[i])%mod;
40     return s; 
41 }
42 inline void bfs(){
43     sz x;
44     int u,ha;
45     while(h<t){
46         x=q[h++];
47         Fur(j,1,d)fl(i,x.s[j]){
48             u=x.s[j];x.s[j]=to;
49             ha=hash(x);
50             if(!v[ha])v[ha]=1,q[t++]=x,ans++;
51             x.s[j]=u;
52         }
53     }
54 }
55 int main(){
56     char c=gc;
57     while(c<0||9<c)c=gc;
58     while(0<=c&&c<=9)q[0].s[++d]=c-48,c=gc;
59     ans=1;v[hash(q[0])]=1;
60     in(k);
61     int x,y;
62     Fur(i,1,k)in2(x,y),add(x,y);
63     bfs();
64     outn(ans);
65 }
View Code

 

ac 0ms:

技术分享图片
 1 // luogu-judger-enable-o2
 2 #include<bits/stdc++.h>
 3 namespace ZDY{
 4 #define fir freopen("2.in","r",stdin)
 5 #define ll long long
 6 #define db double
 7 #define Fur(i,x,y) for(int i=x;i<=y;i++)
 8 #define fur(i,x,y) for(i=x;i<=y;i++)
 9 #define Fdr(i,x,y) for(int i=x;i>=y;i--)
10 #define in2(x,y) in(x),in(y)
11 #define in3(x,y,z) in2(x,y),in(z)
12 #define in4(a,b,c,d) in2(a,b);in2(c,d)
13 #define clr(x,y) memset(x,y,sizeof(x))
14 #define cpy(x,y) memcpy(x,y,sizeof(x))
15 #define fl(i,x) for(ll i=head[x],to;to=e[i].to,i;i=e[i].next)
16 #define inf 2147483647
17 }
18 namespace FAST{
19 #define pob (fwrite(fob::b,sizeof(char),fob::f-fob::b,stdout),fob::f=fob::b,0)
20 #define pc(x) (*(fob::f++)=(x),(fob::f==fob::g)?pob:0)
21 #define gc ((*fib::f)?(*(fib ::f++)):(fgets(fib::b,sizeof(fib::b),stdin)?(fib::f=fib::b,*(fib::f++)):-1))
22 namespace fib{char b[300000]= {},*f=b;}namespace fob{char b[300000]= {},*f=b,*g=b+300000-2;}struct foce{~foce(){pob;fflush(stdout);}} _foce;namespace ib{char b[100];}template<class T>inline void in(T &x){x=0;char c;bool f=0;while((c=gc)>9||c<0)if(c==-)f=!f;x=c-48;while((c=gc)<=9&&c>=0)x=x*10+c-48;if(f)x=-x;}template<class T>inline void out(T x){if(x==0){pc(48);return;}if(x<0){pc(-);x=-x;}char *s=ib::b;while(x) *(++s)=x%10,x/=10;while(s!=ib::b) pc((*(s--))+48);}template<class T>inline void outn(T x){out(x);pc(\n);}
23 inline char sc(){char c=gc;while(!((A<=c&&c<=Z)||(a<=c&&c<=z)||(0<=c<=9)))c=gc;return c;}
24 template<class T>inline T ABS(T x){return x>0?x:-x;}
25 template<class T>inline T MAX(T x,T y){return x>y?x:y;}
26 template<class T>inline T MIN(T x,T y){return x<y?x:y;}
27 template<class T>inline T GCD(T x,T y){return y?GCD(y,x%y):x;}
28 template<class T>inline void swap(T x,T y){T t=x;y=t;x=y;}
29 }using namespace FAST;using namespace ZDY;using namespace std;
30 #define N 10000001
31 #define mod 83958397
32 bool b[10][10];
33 int n,f[10],p[1001]={1},lp=1;
34 char a[32];
35 inline void c(int x){
36     Fur(i,0,lp-1)p[i]*=x;
37     Fur(i,0,lp-1)if(p[i]>9)p[i+1]+=p[i]/10,p[i]%=10;
38     while(p[lp]){if(p[lp]>9)p[lp+1]+=p[lp]/10,p[lp]%=10;lp++;}
39 }
40 int main(){
41     scanf("%s",a);
42     in(n);
43     int x,y;
44     Fur(i,1,n)in2(x,y),b[x][y]=1;
45     Fur(k,1,9)Fur(i,0,9)Fur(j,1,9)if(i!=k&&k!=j)b[i][j]|=b[i][k]&&b[k][j];
46     Fur(i,0,9){
47         b[i][i]=1;
48         Fur(j,0,9)if(b[i][j])f[i]++;
49     }
50     Fur(i,0,strlen(a)-1)c(f[a[i]-48]);
51     Fdr(i,lp-1,0)pc(p[i]+48);
52 }
View Code

 

产生数

标签:show   bfs   oid   enable   sed   scan   ==   set   ide   

原文地址:https://www.cnblogs.com/mimiorz/p/9383823.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!