# 中国剩余定理+容斥_____Lucky7( hdu 5768 2016多校第四场 )

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.


#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std;
typedef long long ll;
int k;
ll left,right,ans;
ll m[16],a[16],M[40000],s[40000];
ll gcd(ll a,ll b)
{
return b==0?a:gcd(b,a%b);
}
ll gcd(ll a,ll b,ll &x,ll &y)
{
ll t,ret;
if(!b)
{
x = 1;
y = 0;
return a;
}
ret=gcd(b,a%b,x,y);
t=x;
x=y;
y=t-a/b*y;
return ret;
}
ll mul(ll a,ll b,ll mod)
{
if(b < 0) return (-mul(a,-b,mod)%mod+mod);
if(b == 0) return 0;
if(b == 1) return a%mod;
if(b%2 == 0) return mul((a+a)%mod,b/2,mod);
return (mul((a+a)%mod,b/2,mod)+a)%mod;
}
ll lmes(ll a[],ll m[],int n,int num,ll& tm)
{
ll ret = 0;
tm = 7;
for(int i = 0 ; i < n ; ++i){
if(num&(1<<i))
{
tm*=m[i];
}
}
for(int i = 0 ; i < n ; ++i)
{
if(!(num&(1<<i)))continue;
ll mi = tm/m[i],x,y;
gcd(mi,m[i],x,y);
ret = (ret + mul(mul(a[i],x,tm),mi,tm))%tm;
}
if(ret<0) ret = (ret+tm);
return ret;
}
int bitcount(int num)
{
int c =0;
for (c =0; num; ++c)
{
num &= (num -1) ; // 清除最低位的1
}
return c ;
}
void init()
{
for(int i = 1 ; i < (1<<k) ; ++ i)
{
s[i] = lmes(a,m,k,i,M[i]);
}
ans = right/7 - left/7;
}
ll cnt(ll ta,ll tm)
{
ll cnt1 = right/tm + (right%tm>=ta?1:0);
ll cnt2 = left/tm + (left%tm>=ta?1:0);
//  printf("%lld %lld %lld %lld\n",ta,tm,cnt1,cnt2);
return cnt1 - cnt2;
}
void work()
{
init();
for(int i = 1 ; i < (1<<k) ; ++i)
{
if(bitcount(i)%2==0)
{
ans += cnt(s[i],M[i]);
}
else
{
ans -= cnt(s[i],M[i]);
}
}
printf("%lld\n",ans);
}
int main()
{
int t,_case=0;
scanf("%d",&t);
while(t--)
{
scanf("%d%lld%lld",&k,&left,&right);
left --;
int tot = 0;
bool flag = false;
for(int i = 0 ; i < k ; i ++)
{
scanf("%lld%lld",&m[tot],&a[tot]);
if(m[tot] % 7 == 0)
{
if(a[tot] == 0) flag = true;
else tot --;
}
tot++;
}
k = tot;
printf("Case #%d: ",++_case);
if(flag)printf("0\n");
else
work();
}
return 0;
}


• 本文已收录于以下专栏：

## HDU 5768 Lucky7(2016 Multi-University Training Contest 4 -1005)——中国剩余定理 + 容斥原理

[传送门](http://acm.hdu.edu.cn/showproblem.php?pid=5768)Lucky7Time Limit: 2000/1000 MS (Java/Others)   ...

## HDU 5768 Lucky7 中国剩余定理+状压+容斥+快速乘法

Lucky7 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total S...

## hdu5768 Lucky7 中国剩余定理 + 容斥原理

/* 题目描述：给出n(0<=n<=15)对pi , ai （ 0 < ai < pi , 且p1*p2*...*pn < 1e18），问在x到y(0 < x < y < 1e18)的范围内有...

## HDU5768 Lucky7[中国剩余定理+容斥定理]

E - Lucky7 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub...

## HDU - 5768 容斥原理 + 中国剩余定理

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)