POJ 1006 中国剩余定理
思路:
设第x天高峰同时出现,则x≡p(mod23),x≡e(mod28),x≡i(mod33),根据中国剩余定理,求出乘法逆元,直接套公式求和就行了。
注意:求和后要减去初始天数d,如果是小于等于0,则要加上m=23*28*33
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
#define fo freopen("in.txt","r",stdin)
#define fc fclose(stdin)
#define fu0(i,n) for(i=0;i<n;i++)
#define fu1(i,n) for(i=1;i<=n;i++)
#define fd0(i,n) for(i=n-1;i>=0;i--)
#define fd1(i,n) for(i=n;i>0;i--)
#define mst(a,b) memset(a,b,sizeof(a))
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d %d",&n,&m)
#define ss(s) scanf("%s",s)
#define sddd(n,m,k) scanf("%d %d %d",&n,&m,&k)
#define pans(ans) printf("%d\n",ans)
#define all(a) a.begin(),a.end()
#define sc(c) scanf("%c",&c)
#define we(a) while(scanf("%d",&a)!=EOF)
const int maxn=200005;
const double eps=1e-8;
const double PI = acos(-1.0);
ll kgcd(ll a,ll b,ll &x,ll &y)
{
if(!b)
{
x=1;y=0;return a;
}
ll t=kgcd(b,a%b,y,x);
y-=a/b*x;
return t;
}
ll niyuan(ll a,ll p)
{
ll x,y;
kgcd(a,p,x,y);
return (x%p+p)%p;//对x取正
}
int main()
{
ll cas=0,p,e,i,d;
while(cin>>p>>e>>i>>d)
{
if(p==-1&&e==-1&&i==-1&&d==-1)
break;
ll m=23*28*33;
ll x,y,n1,n2,n3;
n1=niyuan(m/23,23);
n2=niyuan(m/28,28);
n3=niyuan(m/33,33);
ll ans=((m/23*p*n1)%m + (m/28*e*n2) + (m/33*i*n3 )+m)%m-d;
if(ans<=0)
ans+=m;
cout<<"Case "<<++cas<<":"<<" the next triple peak occurs in "<<ans<<" days."<<endl;
}
return 0;
}