Lucky7
Total Submission(s): 811 Accepted Submission(s): 299
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.
Now give you a pair of
x
and
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<108)
on a line where
n
is the number of pirmes.
Following on
It is guranteed that all the pi are distinct and
pi!=7
.
It is also guaranteed that
p1∗p2∗…∗pn<=108
and
0<ai<pi<=105
for 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
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.
Author
FZU
Source
2016 Multi-University Training Contest 4
题目大意。
输入n,l,r,表示n个不等式还有区间[l,r]
之后n个不等式由
mi
ri
组成。(保证
mi
为素数)
表示所需查找的x对于任何一个不等式都要满足
x!=ri(modmi)
现要找出[l,r]中能被7整除且满足n个不等式的数。
因为
mi
为素数,7也是素数。
所以可以用CRT搞一下
先找出能被7整除的,然后减去被7和
mi
同时整出的,加上被7和
mi
还有
mj
同时整除的
也就是容斥那样搞一下。
CRT中直接乘有数据会爆LL,改成拆位乘法取余。
还有种方式是不搞7,因为
==0(mod7)
这一条件都要满足。
可以在求出除了7之外的数的m和r后,用m乘7的逆元来搞一下。
这样可以避开那组数据。
代码如下:
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread(ch) freopen(ch,"r",stdin)
#define fwrite(ch) freopen(ch,"w",stdout)
using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const int mod = 1e9+7;
const double eps = 1e-8;
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 a[33],m[33];
LL tmp[233];
//按位乘法取余
LL mult(LL a,LL b,LL m)
{
LL ans = 0;
int tp = 0;
while(a)
{
tmp[tp++] = a%10;
a /= 10;
}
for(int i = tp-1; i >= 0; --i)
{
ans = (ans*10+(tmp[i]*b)%m)%m;
}
return ans;
}
LL M;
//模线性方程(互素的)
LL CRT(int n,int pos)
{
M = 1;
LL ans = 0;
for(int i = 0; i < n; ++i)
{
if(((pos>>i)&1) == 0) continue;
M *= m[i];
}
for(int i = 0; i < n; ++i)
{
if(((pos>>i)&1) == 0) continue;
LL x,y;
LL Mi = M/m[i];
extend_gcd(Mi,m[i],x,y);
//if(pos == 15) printf("%lld %lld %lld %lld %lld %lld\n",a[i],x,y,Mi,m[i],x*Mi+m[i]*y);
//if(pos == 15 )printf("%lld %lld %lld\n",x,a[i]*Mi,M);
ans = (ans+(mult(mult(Mi,a[i],M),x,M)))%M;
}
if(ans < 0) ans += M;
return ans;
}
bool vis[65536];
LL l,r;
//容斥
LL Rc(int n,int pos)
{
if(vis[pos]) return 0;
vis[pos] = 1;
LL ans = 0;
LL x = CRT(n,pos);
if(r >= x) ans = (r-x)/M+1;
//printf("%d pos:%d ans:%lld x:%lld mod:%lld\n",n,pos,ans,x,M);
LL pre = ans;
if(l >= x)
{
ans -= (l-x)/M+1;
if((l-x)%M == 0) ans++;
//printf("now:%d pos:%d ans:%lld x:%lld mod:%lld\n",n,pos,pre-ans,x,M);
}
for(int i = 1; i < n; ++i)
{
if((pos>>i)&1) continue;
ans += Rc(n,pos|(1<<i));
}
return -ans;
}
int main()
{
//fread("1005.in");
//fwrite("test.out");
int t;
int n;
scanf("%d",&t);
for(int z = 1; z <= t; ++z)
{
scanf("%d%lld%lld",&n,&l,&r);
n++;
m[0] = 7;
a[0] = 0;
for(int i = 1; i < n; ++i)
scanf("%lld%lld",&m[i],&a[i]);
memset(vis,0,sizeof(vis));
printf("Case #%d: %lld\n",z,-Rc(n,1));
}
return 0;
}