先把板子留下,之后再来写证明…
Biorhythms HDU - 1370
除数互质
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
#define LL long long
void gcd(LL a,LL b,LL &d,LL &x,LL &y)
{
if(b==0)
{
d=a;x=1;y=0;
}
else {
gcd(b,a%b,d,y,x);
y-=(a/b)*x;
}
}
int n=3;
LL m[10],a[10];
LL china(LL M)
{
LL d,y,z=0,x=0;
for(int i=0;i<n;i++)
{
LL w=M/m[i];
gcd(m[i],w,d,d,y);
x=(x+y*w*a[i])%M;
}
return (x+M)%M;
}
int main()
{
int T;
scanf("%d",&T);
int d;
int M = 21252;
m[0]=23,m[1]=28,m[2]=33;
int case1=1;
while(scanf("%I64d %I64d %I64d %d",&a[0],&a[1],&a[2],&d)!=EOF)
{
if(a[0]==-1&&a[1]==-1&&a[2]==-1&&d==-1) break;
int c=china(M);
c-=d;
if(c<=0) c+=M;
printf("Case %d: the next triple peak occurs in %d days.\n",case1++,c);
}
return 0;
}
除数不一定互质
X问题 HDU - 1573
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>
using namespace std;
const int maxn = 20;
int a[maxn],n[maxn];
int gcd(int aa,int bb)
{
if(bb==0) return aa;
else return gcd(bb,aa%bb);
}
void exgcd(int a1,int b1,int &g,int &x,int &y)
{
if(b1==0)
{
x=1;y=0;
g=a1;
return ;
}
else {
exgcd(b1,a1%b1,g,y,x);
y-=(a1/b1)*x;
return ;
}
}
bool merge1(int a1,int n1,int a2,int n2,int &aa,int &bb)
{
int d=gcd(n1,n2),k,x,y;
int c=a2-a1;
if(c%d) return false;
c=(c%n2+n2)%n2;
c/=d,n1/=d,n2/=d;
exgcd(n1,n2,k,x,y);
x=(x%n2+n2)%n2;
c*=x;c=(c%n2+n2)%n2;
c*=n1*d;c+=a1;
bb=n1*n2*d;
aa=(c%bb+bb)%bb;
return true;
}
int gg=0;
int china(int len)
{
int a1=a[0],n1=n[0];
for(int i=1;i<len;i++)
{
int aa,nn;
int a2=a[i],n2=n[i];
if(!merge1(a1,n1,a2,n2,aa,nn)) return -1;
a1=aa,n1=nn;
}
gg=n1;
return (a1%n1+n1)%n1;
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
memset(a,0,sizeof(a));
memset(n,0,sizeof(n));
int nn,m;
scanf("%d %d",&nn,&m);
for(int i=0;i<m;i++)
scanf("%d",&n[i]);
for(int i=0;i<m;i++)
scanf("%d",&a[i]);
int k=china(m);
// cout<<"k="<<k<<endl;
if(k==-1||nn<k) printf("0\n");
else {
int num=(nn-k)/gg+1;
if(k==0)num--;
printf("%d\n",num);
}
}
return 0;
}