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 ...
随机推荐
- python 基础知识 列表的 增删改查 以及迭代取值
""" python 列表 通用方法 元组.数组.字典 取值方法 [] 列表中可以存储不同类型的数据 函数 封装了独立的功能可以直接调用 函数名(参数) 方法 和函数类似 ...
- JavaScript的单线程性质以及定时器的工作原理
前些日子还在网上争论过js动画用setTimeout还是setInterval,个人偏向于setTimeout,当动画中牵扯到ajax时用setInterval会有时间偏差,出现一些问题即使用clea ...
- Crypto 模块安装
crypto模块的目的是为了提供通用的加密和哈希算法. AES是一种常用的对称加密算法,加解密都用同一个密钥.crypto模块提供了AES支持,但是需要自己封装好函数,便于使用 方法一: 1,到 ht ...
- ifconfig,netstat command not found
当CentOS7进行最小化安装时,有很多工具包是没有的. [root@vultr ~]# ifconfig -bash: ifconfig: command not found [root@vultr ...
- Hive笔记之宏(macro)
一.啥是宏 宏可以看做是一个简短的函数,或者是对一个表达式取别名,同时可以将这个表达式中的一些值做成变量调用时传入,比较适合于做分析时为一些临时需要用到很多次的表达式操作封装一下取个简短点的别名来调用 ...
- Linux awk工具简单学习记录
awk是一个文本分析工具,它把文件逐行读入,以特定符号将每行切分(默认空格为分隔符),切开的部分再进行各种分析处理. awk其名称得自于它的创始人Alfred Aho .Peter Weinberge ...
- tensorboard遇到的坑
<ul><li>No graph definition files were found.</li></ul> <p>启动命令 tensor ...
- shell脚本练习【转】
1.写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin):分别这两类用户的个数:通过字符串比较来实现: #脚本内容 [root@ce ...
- 打包部署到tomcat
部署到tomcat的方法 注意:在eclipse 或 idea 上需要引入外部tomcat 1.将程序打成war包启动tomcat 2.将target 文件下内容压缩城zip,发布到tomcat RO ...
- 兼容IE FF 获取鼠标位置
由于Firefox和IE等浏览器之间对js解释的方式不一样,firefox下面获取鼠标位置不能够直接使用clientX来获取.网上说的一般都是触发mousemove事件才行.我这里有两段代码,思路都一 ...