标签:
Path:新生赛
A.Number Sequence
Description:
A number sequence is defined as follows:
f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
Given A, B, and n, you are to calculate the value of f(n).
Analyse:
自动找周期法。
CODE:
int main() { int a,b,n; cir[1]=cir[2]=1; while(cin>>a>>b>>n) { if(!a&&!b&&!n) break; int num=0,i,j; for(i=3;;i++) { cir[i]=(a*cir[i-1]+b*cir[i-2])%7; //cout<<cir[i]<<"--"<<cir[i-1]<<endl; for(j=2;j<i;j++) { if(cir[i-1]==cir[j-1]&&cir[i]==cir[j]) { num=i-j; break; } } if(num) break; } if(n<=j) printf("%d\n",cir[n]); else printf("%d\n",cir[j+((n-j)%num?(n-j)%num:num)]); } return 0; }
C. 无耻的出题人
Description:
给一个long long 的数N,让你求各位之和。
法一:| N | 模10法,巨坑的是最小负数是 -(1<<31)而正数最大只有 (1<<31)-1....需要特判
<span style="color:#3333FF;">n==-9223372036854775808</span>
法二:正解是字符串读取。。好吧,这样应该不会有坑跳!!
while(cin>>n) { LL ans=0; if(n==-9223372036854775808){ //法一坑 printf("89\n"); continue; } if(n<0) n=-n; while(n) { ans+=n%10; n/=10; } printf("%lld\n",ans); }
D. Coprimes
Description:
给数N,求小于N,且与与N,互质的数。
Analyse:
直接暴力for循环就好。
CODE:
int gcd(int a,int b) { return b==0?a:gcd(b,a%b); }
E. Catch That Cow
Description:
给数轴上两点N,K,要求从N走到K,每min可以从X动到 X-1 or X+1 or 2*X
Analyse:
原本想直接找规律,不过还是有小trick,自己始终没找到。
正解:0<=N<=100000,2^20次方也不到,所以bfs直接搜索,也只有几十层。//脑洞不能堵啊,madan!
CODE:
typedef long long LL; const int N=100007; int dp[2*N]; void bfs(int n,int m) { mem(dp,-1); queue<int> que; dp[n]=0; que.push(n); while(!que.empty()) { int c=que.front(); que.pop(); //cout<<c<<"-"<<dp[c]<<endl; if(c==m) break; if(c<m) { if(dp[c+1]==-1) { dp[c+1]=dp[c]+1; que.push(c+1); } if(c&&dp[c-1]==-1) { dp[c-1]=dp[c]+1; que.push(c-1); } if(c<m&&dp[2*c]==-1) { dp[2*c]=dp[c]+1; que.push(2*c); } } else { if(dp[c-1]==-1) { dp[c-1]=dp[c]+1; que.push(c-1); } } } } int main() { int n,m; while(scanf("%d%d",&n,&m)==2) { bfs(n,m); printf("%d\n",dp[m]); } return 0; }
广东工业大学2015新生赛round2(//自己脑洞堵了,madan!)
标签:
原文地址:http://blog.csdn.net/code_or_code/article/details/44801531