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 ...
随机推荐
- R9—R常用函数分类汇总
数据结构 一.数据管理 vector:向量 numeric:数值型向量 logical:逻辑型向量 character:字符型向量 list:列表 data.frame:数据框 c:连接为向量或列表 ...
- PHP删除数组中空值
array_filter 函数的功能是利用回调函数来对数组进行过滤,一直都以为用回调函数才能处理, 却没有发现手册下面还有一句,如果没有回调函数,那么默认就是删除数组中值为false的项目 代码: ...
- 月薪20K软件测试自动化岗必问面试题:验证码识别与处理
本文乃Happy老师的得意门生来自java全栈自动化测试4期的小核桃所作.正所谓严师出高徒,笔下有黄金~~让我们一起来征服面试官吧~~ 在做自动化测试的时候,经常会遇到需要输入验证码的地方,有些可以让 ...
- Java实现去火柴游戏
package com.gh.p10; /** * Created by Lenovo on 2014/12/10. */ import java.util.Random; import java.u ...
- thymeleaf:访问静态方法
<p class="left tel" th:if="${#strings.startsWith(T(net.common.util.tool.common.Req ...
- Vue中发送ajax请求——axios使用详解
axios 基于 Promise 的 HTTP 请求客户端,可同时在浏览器和 node.js 中使用 功能特性 在浏览器中发送 XMLHttpRequests 请求 在 node.js 中发送 htt ...
- js中parentNode,parentElement,childNodes,children
首先了解parentNode,parentElement,childNodes,children四大属性之前,必须对Dom树有一定的了解,在Dom文档结构中,HTML页面每一部分都是由节点组成的,节点 ...
- TcxGrid 复选框
- plaidctf-2016 Pwn试题小结
回顾了一下今年plaidctf Pwn部分的题目,感觉还是蛮有意思的,值得研究一下. 1.unix_time_formatter-76 最简单的一道题,考点是UAF.说是UAF但是其实根本就不算是真正 ...
- 第一届CCF软件能力认证
1.相反数 问题描述 有 N 个非零且各不相同的整数.请你编一个程序求出它们中有多少对相反数(a 和 -a 为一对相反数). 输入格式 第一行包含一个正整数 N.(1 ≤ N ≤ 500). 第二行为 ...