Path: Freshman Race
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 is to calculate the value of f (n).
Analyse:
Auto-find cycle method.
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. A shameless person
Description:
Give a long long number n, and let you ask for the sum.
Law One: | N | Modulo 10 method, The pit is the smallest negative number is-(1<<31) and the positive maximum is only (1<<31)-1 .... Need a special sentence
<span style= "color: #3333FF;" >n==-9223372036854775808</span>
Method Two: The positive solution is a string read. Well, this should not be a pit jump!!
while (Cin>>n) { LL ans=0; if (n==-9223372036854775808) { //FA a pit printf ("89\n"); Continue; } if (n<0) n=-n; while (n) { ans+=n%10; n/=10; } printf ("%lld\n", ans); }
D. Coprimes
Description:
Give the number n, ask for less than N, and with the number of N, coprime.
Analyse:
Direct violence for the loop is good.
CODE:
int gcd (int a,int b) { return b==0?a:gcd (b,a%b);}
E. Catch that Cow
Description:
Give the axis two points n,k, ask from N to K, each min can move from X to X-1 or x+1 or 2*x
Analyse:
Originally want to find the law directly, but still have small trick, oneself always did not find.
Positive solution: 0<=n<=100000,2^20, so the BFS direct search, but also only dozens of layers. Brain hole can't jam, 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;}
Guangdong University of Technology 2015 freshman Race Round2 (//own brain hole blocked, madan! )