中國剩余定理 已知(n+d)%23=a; (n+d)%28=b; (n+c)%33=i 使33×28×a被23除余1,用33×28×6=5544; 使23×33×b被28除余1,用23×33×19=14421; 使23×28×c被33除余1,用23×28×2=1288。 因此有(5544×p+14421×e+1288×i)% lcm(23,28,33) =n+d 又23、28、33互質,即lcm(23,28,33)= 21252; 所以有n=(5544×p+14421×e+1288×i-d)%21252 本題所求的是最小整數解,避免n為負,因此最後結果為n= [n+21252]% 21252 那麼最終求解n的表達式就是: n=(5544*p+14421*e+1288*i-d+21252)%21252;
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<bitset> using namespace std; int main() { int a , b , c , d ; int temp ; int Case ; cin >> Case ; while( Case-- ) { temp = 0 ; while( cin >> a >> b >> c >> d ) { if( a == - 1 && b == -1 && c == -1 && d == -1 ) break; int n = ( 5544 * a + 14421 * b + 1288 * c ) % 21252 ; int ans = n > d ? n - d : 21252 + n - d ; cout << "Case " << ++temp << ": the next triple peak occurs in " << ans << " days." << endl; } if( Case != 0 ) cout << endl ; } return 0 ; } #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<bitset> using namespace std; int main() { int a , b , c , d ; int temp ; int Case ; cin >> Case ; while( Case-- ) { temp = 0 ; while( cin >> a >> b >> c >> d ) { if( a == - 1 && b == -1 && c == -1 && d == -1 ) break; int n = ( 5544 * a + 14421 * b + 1288 * c ) % 21252 ; int ans = n > d ? n - d : 21252 + n - d ; cout << "Case " << ++temp << ": the next triple peak occurs in " << ans << " days." << endl; } if( Case != 0 ) cout << endl ; } return 0 ; }
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<bitset> using namespace std; int main() { int a , b , c , d ; int temp = 0 ; while( cin >> a >> b >> c >> d ) { if( a == - 1 && b == -1 && c == -1 && d == -1 ) break; int n = ( 5544 * a + 14421 * b + 1288 * c ) % 21252 ; int ans = n > d ? n - d : 21252 + n - d ; cout << "Case " << ++temp << ": the next triple peak occurs in " << ans << " days." << endl; } return 0 ; } #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<bitset> using namespace std; int main() { int a , b , c , d ; int temp = 0 ; while( cin >> a >> b >> c >> d ) { if( a == - 1 && b == -1 && c == -1 && d == -1 ) break; int n = ( 5544 * a + 14421 * b + 1288 * c ) % 21252 ; int ans = n > d ? n - d : 21252 + n - d ; cout << "Case " << ++temp << ": the next triple peak occurs in " << ans << " days." << endl; } return 0 ; }
同樣,這道題的解法就是: 已知(n+d)%23=p; (n+d)%28=e; (n+d)%33=i 使33×28×a被23除余1,用33×28×8=5544; 使23×33×b被28除余1,用23×33×19=14421; 使23×28×c被33除余1,用23×28×2=1288。 因此有(5544×p+14421×e+1288×i)% lcm(23,28,33) =n+d 又23、28、33互質,即lcm(23,28,33)= 21252; 所以有n=(5544×p+14421×e+1288×i-d)%21252 本題所求的是最小整數解,避免n為負,因此最後結果為n= [n+21252]% 21252 那麼最終求解n的表達式就是: n=(5544*p+14421*e+1288*i-d+21252)%21252; 當問題被轉化為一條數學式子時,你會發現它無比簡單。。。。直接輸出結果了。