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
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.
#include <bits/stdc++.h>
using namespace std;
const int N=20;
long long a[N],m[N],M[N],C[N];
int n;
long long L,R;
long long quickplus(long long m,long long n,long long k)//返回m*n%k
{
long long b = 0;
if( m >= k ) m %= k;
if( n >= k ) n %= k;
while (n > 0)
{
if (n & 1){
b += m;
if( b >= k ) b -= k;
}
n = n >> 1LL;
m += m;
if( m >= k) m -= k;
}
return b;
}
long long qpow(long long x,long long y,long long MM)
{
long long ret=1LL;
for(;y;y>>=1LL)
{
if(y&1LL) ret = quickplus( ret , x , MM );
x = quickplus( x , x , MM );
}
return ret;
}
long long solve()
{
long long ans=0;
for(int i=0;i<(1<<n);i++)
{
int cnt=0;
long long MM=1LL,ret=0;
for(int j=0;j<n;j++)
if( i >> j & 1 )
{
MM*=m[j];
cnt++;
}
MM*=m[n];
for(int j=0;j<n;j++)
if( i >> j & 1)
{
M[j]=MM/m[j];
C[j]=qpow(M[j],m[j]-1,MM);
}
M[n]=MM/m[n];
C[n]=qpow(M[n],m[n]-1,MM);
for(int j=0;j<n;j++)
if(i&(1<<j))
{
ret+=quickplus(C[j],a[j],MM);
if( ret >= MM ) ret -= MM;
}
ret+=quickplus(C[n],a[n],MM);
if( ret >= MM ) ret -= MM;
if( (cnt&1) == 0 )
{
if( R >= ret) ans+=((R-ret)/MM+1);
if( L >= ret) ans-=((L-ret)/MM+1);
}
else
{
if( R >= ret) ans-=((R-ret)/MM+1);
if( L >= ret) ans+=((L-ret)/MM+1);
}
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
for(int o=1;o<=T;o++)
{
long long x,y;
scanf("%d%I64d%I64d",&n,&x,&y);
for(int i=0;i<n;i++) scanf("%I64d%I64d",m+i,a+i);
a[n]=0,m[n]=7LL;
R=y,L=x-1;
printf("Case #%d: %I64d\n",o,solve());
}
return 0;
}