hdu1370-Biorhythms

2014-11-23 23:12:00 · 作者: · 浏览: 5
中国剩余定理
已知(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   
#include   
#include   
#include   
#include   
#include   
  
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
#include
#include
#include
#include
#include

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   
#include   
#include   
#include   
#include   
#include   
  
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
#include
#include
#include
#include
#include

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;
当问题被转化为一条数学式子时,你会发现它无比简单。。。。直接输出结果了。