poj 1006(中国剩余定理+模板题)
生活随笔
收集整理的這篇文章主要介紹了
poj 1006(中国剩余定理+模板题)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題意:人自出生起就有體力,情感和智力三個生理周期,分別為23,28和33天。一個周期內有一天為峰值,在這一天,人在對應的方面(體力,情感或智力)表現最好。通常這三個周期的峰值不會是同一天。現在給出三個日期,分別對應于體力,情感,智力出現峰值的日期。然后再給出一個起始日期,要求從這一天開始,算出最少再過多少天后三個峰值同時出現。
設第x天同時出現,則x%23=p%23,x%28=i%28...(等于p,i也是可以的,計算中會mod),然后用x-d即可,因為x可能小于d,所以加上23*28*33再對這個數取模。
1 #include<iostream> 2 #include<string.h> 3 #include<string> 4 #include<sstream> 5 #include<vector> 6 #include<deque> 7 #include<map> 8 #include<algorithm> 9 #include<iomanip> 10 #include<math.h> 11 #include<set> 12 using namespace std; 13 14 typedef long long ll; 15 typedef unsigned long long ull; 16 const int mod = 1000000007; 17 18 //擴展歐幾里得求逆元 19 ll exgcd(ll &x,ll &y,ll a,ll b) 20 { 21 if (b == 0) 22 { 23 x = 1; 24 y = 0; 25 return a; 26 } 27 ll r = exgcd(x, y, b, a%b); 28 ll temp = x; 29 x = y; 30 y = temp - a / b * y; 31 return r; 32 } 33 34 ll inv(ll a,ll b) 35 { 36 ll x, y; 37 ll g = exgcd(x, y, a, b); 38 if (g == 1) 39 { 40 return (x%b + b) % b; 41 } 42 else 43 return -1; 44 } 45 46 ll china(ll *n, ll *m,ll len) 47 { 48 ll M = 1; 49 ll res = 0; 50 for (int i = 0; i < len; i++) 51 M = M * m[i]; 52 for (int i = 0; i < len; i++) 53 { 54 ll t = M / m[i]; 55 res = (res + t * inv(t, m[i])*n[i]) % M; 56 } 57 return (res+M)%M; //防止res為負數(不太懂) 58 } 59 60 int main() 61 { 62 int cas = 1; 63 int p, e, i, d; 64 ll m[3] = {23,28,33}, n[3]; 65 while (cin >> e >> i >> d>>p && p != -1) 66 { 67 n[0] = e % 23; 68 n[1] = i % 28; 69 n[2] = d % 33; 70 ll ans = (china(n, m, 3)-p+21252)%21252; 71 ans = ans ? ans : 21252; 72 cout << "Case " << cas++ << ": the next triple peak occurs in " << ans << " days." << endl; 73 } 74 return 0; 75 }?
轉載于:https://www.cnblogs.com/QingFengDaHui/p/10392435.html
總結
以上是生活随笔為你收集整理的poj 1006(中国剩余定理+模板题)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript异步基础
- 下一篇: c++静态变量的生存期