您的位置:首页 > 大数据 > 人工智能

2016 Multi-University Training Contest 4 1005Lucky7

2016-07-29 15:12 337 查看


Lucky7

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 637    Accepted Submission(s): 236


Problem Description

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.

 

Input

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).

 

Output

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

 

Sample Input

2
2 1 100
3 2
5 3
0 1 100

 

Sample Output

Case #1: 7
Case #2: 14

HintFor 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.

 

Author

FZU

 

Source

2016 Multi-University Training Contest 4

 
【题意】在给你的(x,y)的范围中,能被7整除的是幸运的数字,不过有些幸运的数字被污染了,给出n对p,a  能被p除然后余数是a的,是被污染的。
问在给定范围内有多少个数是幸运的。

【分析】通过给定的条件 我们共可以列出n+1个模线性方程  ,那么只要容斥一下:枚举几个在一起,利用解方程求解,奇的加偶的减就行了。
即:假使n=2 那么ans=(总的7的倍数的个数)+(条件1)+(条件2)-条件(1,2)。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<vector>

#define F first
#define S second
#define mp make_pair
using namespace std;
typedef long long LL;

LL extend_gcd(LL a,LL b,LL &x,LL &y)
{
if(a==0 && b==0) return -1;
if(b==0 )
{
x=1;
y=0;
return a;
}
LL d=extend_gcd(b,a%b,y,x);
y-= a/b*x;
return d;
}

LL m[20],a[20];

bool solve(LL &m0,LL &a0,LL m,LL a)
{
LL y,x;
LL g=extend_gcd(m0,m,x,y);
if(abs(a-a0)%g) return false;
x *= (a-a0)/g;
x %= m/g;
a0 = (x*m0 + a0);
m0 *= m/g;
a0 %= m0;
if(a0 < 0) a0+=m0;
return true ;
}

bool MLES(LL &m0,LL &a0,LL n,int x)
{
bool flag=true;
m0=1;
a0=0;
for(int i=0; i<=n; i++)
{
if(i!=0&&(x&(1<<(i-1)))==0) continue;
if(!solve(m0,a0,m[i],a[i]))
{
flag=false;
break;
}
}
return flag;
}
bool judge(int x)
{
int sum=0;
while(x)
{
if(x&1) sum++;
x>>=1;
}
if(sum%2) return true;
return false;
}
int main()
{
LL T,n,l,r;
int u=1;
scanf("%lld",&T);
while(T--)
{
scanf("%lld%lld%lld",&n,&l,&r);
for(int i=1; i<=n; i++)
{
scanf("%lld%lld",&m[i],&a[i]);
}
m[0]=7;
a[0]=0;
LL x,y ;
LL ans=0,sum;
for(int i=1; i<(1<<n); i++)
{
sum=0;
if(MLES(x,y,n,i))
{
if(r>=y)
{
sum++;
sum+=(r-y)/x;
}
if(l>=y)
{
sum--;
sum-=(l-y)/x;
}
if(l%x==y) sum++;
if(judge(i))
{
ans+=sum;
}
else ans-=sum;
}
}
sum =0;
if(MLES(x,y,n,0))
{
if(r>=y)
{
sum++;
sum+=(r-y)/x;
}
if(l>=y)
{
sum--;
sum-=(l-y)/x;
}
if(l%x==y) sum++;
ans=sum-ans;
}
printf("Case #%d: %lld\n",u++,ans);
}
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: