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 ...
随机推荐
- li分两列显示
只要控制了li的宽度,利用浮动就能实现<style type="text/css"> .my ul { width: 210px; } .my li { width: ...
- shape-outside 矩形之外的另一种思路
http://docs.webplatform.org/wiki/css/properties/shape-outside
- 【C++】cmdline——轻量级的C++命令行解析库
1.说明 cmdline是一个轻量级的c++命令行参数解析工具,全部源码只有一个cmdline.h头文件. 2.代码 20171210_命令行进行解析.cpp // 20171210_命令行进行解析. ...
- Mysql导入脚本失败,提示需要SUPER权限
1.删除: /*!50013 DEFINER=`root`@`localhost` SQL SECURITY DEFINER */ 2.查看增删函数有没有重复 3.删除: set GLOBAL log ...
- 使用/dev/uinput的简要介绍(含demo程序)【转】
转自:https://blog.csdn.net/zhongkunjia/article/details/75142699 uinput机制有2个很大的优点: 1) 不用自己写驱动(比如弄个红外遥控器 ...
- linux批量关闭进程
ps aux | grep gunicorn_api | awk '{print $2}' | xargs kill -9 gunicorn 换成你的关键字即可.
- springcloud使用Hystrix实现微服务的容错处理
使用Hystrix实现微服务的容错处理 容错机制 如果服务提供者相应非常缓慢,那么消费者对提供者的请求就会被强制等待,知道提供者相应超时.在高负载场景下,如果不作任何处理,此类问题可能会导致服务消费者 ...
- java虚拟机规范(se8)——java虚拟机结构(三)
2.6. 栈帧 栈帧用于存储数据和部分结果,同样也用于执行动态链接,返回方法的值和分派异常. 当方法被调用的时候会创建一个新的栈帧.当一个方法调用结束时,它对应的栈帧就被销毁了,不管是正常调用结束还是 ...
- Android工程方法数超过65535的解决办法
Error:Execution failed for task ':ttt:transformClassesWithDexForDebug'.com.android.build.api.transfo ...
- 原生js实现ajax跨域(兼容IE8,IE9)
html设置meta标签兼容360兼容模式和IE怪异模式 <meta http-equiv="X-UA-Compatible" content="IE=9;IE=8 ...