标签:人生 cas 存储 输入数据 正文 长度 代码 事先 原创
此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置。
题目链接:http://poj.org/problem?id=1006&lang=zh-CN&change=true
Description
Input
Output
Sample Input
0 0 0 0 0 0 0 100 5 20 34 325 4 5 6 7 283 102 23 320 203 301 203 40 -1 -1 -1 -1
Sample Output
Case 1: the next triple peak occurs in 21252 days. Case 2: the next triple peak occurs in 21152 days. Case 3: the next triple peak occurs in 19575 days. Case 4: the next triple peak occurs in 16994 days. Case 5: the next triple peak occurs in 8910 days. Case 6: the next triple peak occurs in 10789 days.
分析:
这是中国剩余定理的典型例题,然而窝的CRT死活也跑不出答案来QAQ伤心
一怒之下写了暴力,AC。
后来又改了一个多小时,总算是把CRT的代码写对了qwq...
AC代码:
1 #include<cstdio> 2 #include<algorithm> 3 #include<cmath> 4 #include<cstring> 5 6 const int MAXN = 1000005; 7 8 inline void read(int &x) 9 { 10 char ch = getchar(),c = ch;x = 0; 11 while(ch < ‘0‘ || ch > ‘9‘) c = ch,ch = getchar(); 12 while(ch >= ‘0‘ && ch <= ‘9‘) x = (x<<1)+(x<<3)+ch-‘0‘,ch = getchar(); 13 if(c == ‘-‘) x = -x; 14 } 15 16 int main() 17 { 18 int a,b,c,d,cnt = 0,lim = 21252; 19 read(a),read(b),read(c),read(d); 20 while(d != -1) 21 { 22 ++cnt,a %= 23,b %= 28,c %= 33; 23 for(register int i = 1;;++ i) 24 if(i%23 == a && i%28 == b && i%33 == c) 25 { 26 if(i < d) i += lim; 27 printf("Case %d: the next triple peak occurs in %d days.\n",cnt,i-d); 28 break; 29 } 30 read(a),read(b),read(c),read(d); 31 } 32 return 0; 33 }
1 #include<cstdio> 2 #include<algorithm> 3 #include<cmath> 4 #include<cstring> 5 6 const int MAXN = 1000005; 7 8 inline void read(int &x) 9 { 10 char ch = getchar(),c = ch;x = 0; 11 while(ch < ‘0‘ || ch > ‘9‘) c = ch,ch = getchar(); 12 while(ch >= ‘0‘ && ch <= ‘9‘) x = (x<<1)+(x<<3)+ch-‘0‘,ch = getchar(); 13 if(c == ‘-‘) x = -x; 14 } 15 16 int f[5] = {0,23,28,33},c[5],inv[5]; 17 //f存储周期,c存储余数(也就是一开始读入的),inv存储逆元 18 int cnt,ans,lim = 21252; 19 20 void exgcd(int a,int b,int &x,int &y) 21 { 22 if(!b) 23 { 24 x = 1,y = 0; 25 return ; 26 } 27 exgcd(b,a%b,y,x); 28 y -= x*(a/b); 29 } 30 31 inline void init() 32 {//事先把逆元求出来 33 int x,y,b; 34 for(int i = 1;i <= 3;++ i) 35 { 36 x = 0,y = 0,b = lim/f[i]; 37 exgcd(b,f[i],x,y); 38 inv[i] = x; 39 } 40 } 41 42 int main() 43 { 44 init(); 45 for(int i = 1;i <= 3;++ i) 46 read(c[i]),c[i]%=f[i]; 47 read(c[4]); 48 while(c[4] != -1) 49 { 50 ++ cnt; 51 ans = 0; 52 for(int i = 1;i <= 3;++ i) 53 //用CRT公式求解ans值 54 ans = (ans+(lim/f[i])*c[i]*inv[i])%lim; 55 if(ans <= c[4]) ans += lim; 56 printf("Case %d: the next triple peak occurs in %d days.\n",cnt,ans-c[4]); 57 for(int i = 1;i <= 3;++ i) 58 read(c[i]),c[i]%=f[i]; 59 read(c[4]); 60 } 61 return 0; 62 }
标签:人生 cas 存储 输入数据 正文 长度 代码 事先 原创
原文地址:http://www.cnblogs.com/shingen/p/7587609.html