hdu 5768 Lucky7 容斥
Lucky7
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5768
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.
Hint
题意
给你l,r,问你l,r中有多少数%7=0且%ai!=bi的
题解:
因为满足任意一组pi和ai,即可使一个“幸运数”被“污染”,我们可以想到通过容斥来处理这个问题。当我们选定了一系列pi和ai后,题意转化为求[x,y]中被7整除余0,且被这一系列pi除余ai的数的个数,可以看成若干个同余方程联立成的一次同余方程组。然后我们就可以很自然而然的想到了中国剩余定理。需要注意的是,在处理中国剩余定理的过程中,可能会发生超出LongLong的情况,需要写个类似于快速幂的快速乘法来处理。
代码
#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;
}
hdu 5768 Lucky7 容斥的更多相关文章
- hdu 5514 Frogs(容斥)
Frogs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- HDU 5213 分块 容斥
给出n个数,给出m个询问,询问 区间[l,r] [u,v],在两个区间内分别取一个数,两个的和为k的对数数量. $k<=2*N$,$n <= 30000$ 发现可以容斥简化一个询问.一个询 ...
- HDU 2588 思维 容斥
求满足$1<=X<=N ,(X,N)>=M$的个数,其中$N, M (2<=N<=1000000000, 1<=M<=N)$. 首先,假定$(x, n)=m$ ...
- HDU 5768 Lucky7 (中国剩余定理+容斥)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5768 给你n个同余方程组,然后给你l,r,问你l,r中有多少数%7=0且%ai != bi. 比较明显 ...
- hdu 5768 Lucky7 中国剩余定理+容斥+快速乘
Lucky7 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem D ...
- HDU 5768 Lucky7(CRT+容斥原理)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5768 [题目大意] 求出一个区间内7的倍数中,对于每个ai取模不等于bi的数的个数. [题解] 首 ...
- HDU 1695 GCD 容斥
GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1695 Description Given 5 integers: a, b, c, d, k ...
- HDU 5514 Frogs 容斥定理
Frogs Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5514 De ...
- hdu 5212 反向容斥或者莫比
http://acm.hdu.edu.cn/showproblem.php?pid=5212 题意:忽略.. 题解:把题目转化为求每个gcd的贡献.(http://www.cnblogs.com/z1 ...
随机推荐
- Shell命令行中特殊字符与其转义详解(去除特殊含义)
特殊符号及其转义 大家都知道在一个shell命令是由命令名和它的参数组成的, 比如 cat testfile, 其中cat是命令名, testfile是参数. shell将参数testfile传递给c ...
- CodeForces Contest #1110: Global Round 1
比赛传送门:CF #1110. 比赛记录:点我. 涨了挺多分,希望下次还能涨. [A]Parity 题意简述: 问 \(k\) 位 \(b\) 进制数 \(\overline{a_1a_2\cdots ...
- Insert Interval & Merge Intervals
Insert Intervals Given a non-overlapping interval list which is sorted by start point. Insert a new ...
- 奈奎斯特定理 and 香农定理
-----------------------整理自<21ic电子网> 奈奎斯特定理(Nyquist's Theorem)和香农定理(Shannon's Theorem)是网络传输中的两个 ...
- crontab每10秒钟执行一次
1.使用sleep 在crontab中加入 * * * * * sleep 10; /bin/date >>/tmp/date.txt* * * * * sleep 20; /bin/da ...
- C# 压缩文件 的创建
using System;using System.IO.Compression; using System.Collections.Generic;using System.Linq;using S ...
- elasticsearch安装marvel插件
Marvel插件要在Elasticsearch和Kibana中同时安装.Step 1: Install Marvel into Elasticsearch: bin/plugin install li ...
- Linux配置Selenium+Chrome+Java实现自动化测试
1.安装chrome sudo apt-get install libxss1 libappindicator1 libindicator7 wget https://dl.google.com/li ...
- 一次“ora-12170 tns 连接超时”的经历
win7 64位系统 oracle 10g 64位 plsql之前连接是好使的,突然连接不上,提示错误“ora-12170 tns 连接超时” 1.ping IP 没有问题 2. ...
- device-pixel-radio
移动web开发之像素和DPR 今天看到一个面试题,为iphone6s的自适应,答案是@media(min-device-width:414px) and(max-device-width:736px) ...