您的位置:首页 > 其它

Lucky7(hdu5768)

2016-07-28 23:57 495 查看

Lucky7

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 328 Accepted Submission(s): 130


[align=left]Problem Description[/align]
When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortunately fall into the sea. While it was dying, seven dolphins arched its body and sent it back to the shore. It is said that ?? used to surrounded by 7 candles when he faced a extremely difficult problem, and always solve it in seven minutes.
?? once wrote an autobiography, which mentioned something about himself. In his book, it said seven is his favorite number and he thinks that a number can be divisible by seven can bring him good luck. On the other hand, ?? abhors some other prime numbers and thinks a number x divided by pi which is one of these prime numbers with a given remainder ai will bring him bad luck. In this case, many of his lucky numbers are sullied because they can be divisible by 7 and also has a remainder of ai when it is divided by the prime number pi.
Now give you a pair of x and y, and N pairs of ai and pi, please find out how many numbers between x and y can bring ?? good luck.

[align=left]Input[/align]
On the first line there is an integer T(T≤20) representing the number of test cases.
Each test case starts with three integers three intergers n, x, y(0<=n<=15,0<x<y<1018) on a line where n is the number of pirmes.
Following on n lines each contains two integers pi, ai where pi is the pirme and ?? abhors the numbers have a remainder of ai when they are divided by pi.
It is guranteed that all the pi are distinct and pi!=7.
It is also guaranteed that p1*p2*…*pn<=1018 and 0<ai<pi<=105for every i∈(1…n).

[align=left]Output[/align]
For each test case, first output "Case #x: ",x=1,2,3...., then output the correct answer on a line.

[align=left]Sample Input[/align]

2
2 1 100
3 2
5 3
0 1 100

[align=left]Sample Output[/align]

Case #1: 7
Case #2: 14

Hint

For Case 1: 7,21,42,49,70,84,91 are the seven numbers.
For Case2: 7,14,21,28,35,42,49,56,63,70,77,84,91,98 are the fourteen numbers.

[align=left]Author[/align]
FZU
思路:中国剩余定理+容斥+扩展欧几里得;
比赛时打了将近2个小时,最后还因为快速幂中的一个模没写而超时 GG啊。
其实题解的思路和我的思路有些不同,题解是容斥的时候将7加入一起用中国剩余定理求解,这时求出来的通解直接是7 的倍数,因为%7=0,加入求同余方程组的解;
因为只要符合模这些数中的一条就可以了,如果直接求解会重复,所以用容斥原理。加入其中求得一个通解,记为t+kmod;
然后y<=t+kmod<=x;移项可以求出k的解的个数,然后根据容斥这里是奇减偶加。
然后我的思路是没有加入7求同余。
而是用求出那些个的数的同余方程组的解然后t+kmod=7r;再用扩展欧几里得,求k的通解,但在这之前我先求x<=t+kmod<=y;的k的范围。
扩欧求得的b+7p,代入前面的不等式解p的范围,求得p有多少个,然后容斥奇减偶加p;

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<queue>
#include<stdlib.h>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<math.h>
using namespace std;
typedef long long LL;
typedef struct pp
{
LL x;
LL y;
} ss;
ss ans[20];
LL quick(LL n,LL m,LL mod);
LL mul(LL n, LL m,LL p);
pair<LL,LL>P(LL n,LL m);
LL gcd(LL n, LL m);
LL mm[20];
int main(void)
{
int i,j,k;
scanf("%d",&k);
int __ca=0;
LL n,x,y;
while(k--)
{
__ca++;
scanf("%lld %lld %lld",&n,&x,&y);
LL sum;
sum=y/7-(x-1)/7;
printf("Case #%d: ",__ca);
if(n==0)
{
printf("%lld\n",sum);
}
else
{
LL mod=1;
for(i=0; i<n; i++)
{
scanf("%lld %lld",&ans[i].x,&ans[i].y);
}
LL anw=0;
int s;
for(j=1; j<(1<<n); j++)
{
int cr=0;
for(s=0; s<n; s++)
{
if(j&(1<<s))
{
mm[cr++]=s;
}
}
LL mod=1;
LL acm=0;
for(i=0; i<cr; i++)
{
mod*=ans[mm[i]].x;
}
for(i=0; i<cr; i++)
{
LL mod1=mod/ans[mm[i]].x;
LL ni=quick(mod1,ans[mm[i]].x-2,ans[mm[i]].x);
acm=(acm+mul(mul(mod1,ni,mod),ans[mm[i]].y,mod))%mod;
}
anw=acm;
LL ctx=0;
if(anw>y)
{
continue;
}
else
{
LL  cha=x-anw;
LL nx,ny;
LL cha1=y-anw;
nx=cha/mod;
while(anw+mod*nx<x)
{
nx++;
} if(cha<0)nx=0;
ny=cha1/mod;
{
pair<LL ,LL>AK=P(mod,7);
LL nxx=(AK.first*acm%7+7)%7;
nxx=((-nxx)%7+7)%7;
if(ny>=nxx)
{
LL cx=max(nx-nxx,(LL)0);
LL cy=ny-nxx;
if(cx==0)ctx=cy/7+1;else {ctx=cy/7-(cx-1)/7;}
}
}
}
if(cr%2)
{
sum-=ctx;
}
else sum+=ctx;
} printf("%lld\n",sum);
}
}
return 0;
}
LL gcd(LL n, LL m)
{
if(m==0)
{
return n;
}
else if(n%m==0)
{
return m;
}
else
{
return gcd(m,n%m);
}
}
LL quick(LL n,LL m,LL mod)
{
LL cnt=1;n%=mod;
while(m)
{
if(m&1)
{
cnt=cnt*n%mod;
}
n=n*n%mod;
m/=2;
}
return cnt;
}
LL mul(LL n, LL m,LL p)
{
n%=p;
m%=p;
LL ret=0;
while(m)
{
if(m&1)
{
ret=ret+n;
ret%=p;
}
m>>=1;
n<<=1;
n%=p;
}
return ret;
}
pair<LL,LL>P(LL n,LL m)
{
if(m==0)
{
pair<LL,LL>ak;
ak=make_pair(1,0);
return ak;
}
else
{
pair<LL,LL>A=P(m,n%m);
LL nx=A.second;
LL ny=A.first;
ny=ny-(n/m)*nx;
A.first=nx;
A.second=ny;
return A;
}
}


#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<queue>
#include<stdlib.h>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<math.h>
using namespace std;
typedef long long LL;
typedef struct pp
{
LL x;
LL y;
} ss;
ss ans[20];
LL quick(LL n,LL m,LL mod);
LL mul(LL n, LL m,LL p);
pair<LL,LL>P(LL n,LL m);
LL gcd(LL n, LL m);
LL mm[20];
int main(void)
{
int i,j,k;
scanf("%d",&k);
int __ca=0;
LL n,x,y;
while(k--)
{
__ca++;
scanf("%lld %lld %lld",&n,&x,&y);
LL sum;
sum=y/7-(x-1)/7;
if(n==0)
{
printf("Case #%d: %lld\n",__ca,sum);
}
else
{
LL mod=1;
for(i=0; i<n; i++)
{
scanf("%lld %lld",&ans[i].x,&ans[i].y);
}
LL anw=0;
LL s;
for(j=1; j<(1<<n); j++)
{  LL mod=1;
int cr=0;
for(s=0; s<n; s++)
{
if(j&(1<<s))
{
mm[cr++]=s;
mod*=ans[s].x;
}
}mod*=7;
LL acm=0;
for(i=0; i<cr; i++)
{
LL mod1=mod/ans[mm[i]].x;
LL ni=quick(mod1,ans[mm[i]].x-2,ans[mm[i]].x);
acm=(acm+mul(mul(mod1,ni,mod),ans[mm[i]].y,mod))%mod;
}
acm%=mod;
acm+=mod;
acm%=mod;
anw=acm;
LL ctx=0;
if(anw>y)
{
continue;
}
else
{
if(anw<x)
{
LL ax=x-anw-1;
LL ay=y-anw;
ctx+=ay/mod-ax/mod;
}
else if(anw>=x)
{  LL ay=y-anw;
ctx+=ay/mod+1;
}
}
if(cr%2)
{
sum-=ctx;
}
else sum+=ctx;
} printf("Case #%d: %lld\n",__ca,sum);
}
}
return 0;
}
LL gcd(LL n, LL m)
{
if(m==0)
{
return n;
}
else if(n%m==0)
{
return m;
}
else
{
return gcd(m,n%m);
}
}
LL quick(LL n,LL m,LL mod)
{
LL cnt=1;n%=mod;
while(m>0)
{
if(m&1)
{
cnt=cnt*n%mod;
}
n=n*n%mod;
m/=2;
}
return cnt;
}
LL mul(LL n, LL m,LL p)
{
n%=p;
m%=p;
LL ret=0;
while(m)
{
if(m&1)
{
ret=ret+n;
ret%=p;
}
m>>=1;
n<<=1;
n%=p;
}
return ret;
}
pair<LL,LL>P(LL n,LL m)
{
if(m==0)
{
pair<LL,LL>ak;
ak=make_pair(1,0);
return ak;
}
else
{
pair<LL,LL>A=P(m,n%m);
LL nx=A.second;
LL ny=A.first;
ny=ny-(n/m)*nx;
A.first=nx;
A.second=ny;
return A;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: