HDU 5768:Lucky7(中国剩余定理 + 容斥原理)
http://acm.hdu.edu.cn/showproblem.php?pid=5768
Lucky7
?? 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.
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).
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 <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <queue>
#include <cmath>
using namespace std;
typedef long long LL;
#define N 20 LL p[N], a[N];
int bit[N];
int n; LL mul(LL a, LL b, LL m)
{
//快速乘法
LL ans = ;
while(b) {
if(b & ) ans = (ans + a) % m;
a <<= ;
a %= m;
b >>= ;
}
return ans;
} LL exgcd(LL a, LL b, LL &x, LL &y)
{
if(b == ) {
x = ;
y = ;
return a;
}
LL r = exgcd(b, a%b, x, y);
int t = x;
x = y;
y = t - a / b * y;
return r;
} LL CRT(LL x, LL y)
{
//中国剩余定理: 找同时满足多个同余式的解
LL M = , ans = ;
for(int i = ; i <= n; i++) {
if(bit[i]) M *= p[i];
}
for(int i = ; i <= n; i++) {
if(bit[i]) {
LL x, y, Mi;
Mi = M / p[i];
exgcd(Mi, p[i], x, y);
x = (x % p[i] + p[i]) % p[i];
ans = (ans + mul(Mi * a[i] % M, x, M) % M + M) % M;
//ans找出来的是在 M 以内的特解即最小正整数解
}
}
//每过 M 可以有一个解
LL res = (y - ans + M) / M - (x - - ans + M) / M;
return res;
} void solve(LL x, LL y)
{
bit[n] = ;
LL ans = ;
int all = << n;
for(int i = ; i < all; i++) {
int tmp = i, k = ;
for(int j = ; j < n; j++) {
bit[j] = tmp & ;
tmp >>= ;
k += bit[j];
}
k = k & ? - : ;
//k是计算包含多少个同余式
//容斥原理: 奇数减,偶数加,具体可以看《组合数学》P108
//计算出不具有性质(满足任意一个同余式)的数的数量
ans += CRT(x, y) * k;
}
printf("%I64d\n", ans);
} int main()
{
int t;
scanf("%d", &t);
for(int cas = ; cas <= t; cas++) {
LL x, y;
scanf("%d%I64d%I64d", &n, &x, &y);
for(int i = ; i < n; i++)
scanf("%I64d%I64d", &p[i], &a[i]);
p[n] = , a[n] = ;
printf("Case #%d: ", cas);
solve(x, y);
}
return ;
}
HDU 5768:Lucky7(中国剩余定理 + 容斥原理)的更多相关文章
- 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 5768 Lucky7
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 题目大意: T组数据,求L~R中满足:1.是7的倍数,2.对n个素数有 %pi!=ai 的数 ...
- HDU 5768 Lucky7 (容斥原理 + 中国剩余定理 + 状态压缩 + 带膜乘法)
题意:……应该不用我说了,看起来就很容斥原理,很中国剩余定理…… 方法:因为题目中的n最大是15,使用状态压缩可以将所有的组合都举出来,然后再拆开成数组,进行中国剩余定理的运算,中国剩余定理能够求出同 ...
- HDU 5768 Lucky7 容斥原理+中国剩余定理(互质)
分析: 因为满足任意一组pi和ai,即可使一个“幸运数”被“污染”,我们可以想到通过容斥来处理这个问题.当我们选定了一系列pi和ai后,题意转化为求[x,y]中被7整除余0,且被这一系列pi除余ai的 ...
- HDU 5768 Lucky7 (中国剩余定理 + 容斥 + 快速乘法)
Lucky7 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crow ...
- hdu 5768 Lucky7 容斥
Lucky7 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crow ...
- ACM/ICPC 之 中国剩余定理+容斥原理(HDU5768)
二进制枚举+容斥原理+中国剩余定理 #include<iostream> #include<cstring> #include<cstdio> #include&l ...
随机推荐
- VMWare ESXi 5.5安装及配置
VMWare ESXi 5.5安装大概过程如下:制作虚拟化ESXi系统的USB启动盘,安装ESXi系统到USB,用USB启动ESXi系统.比较难理解,下面图解过程. 下载UNetbootin (下 ...
- [HDF]hdf-4.2.6类库的使用
HDF文件包括科学数据和VData部分.读取HDF格式的calipso数据,用GDAL可以方便的读取其中的子数据集,但是没有发现GDAL中提供读取Vdata的方法.所以只好考虑借助hdf-4.2.6类 ...
- redmine一键安装
参加项目 bitnami项目 https://bitnami.com/stack/redmine/installer 百度网盘地址为:http://pan.baidu.com/s/1jESnO
- c#异步调用
首先来看一个简单的例子: 小明在烧水,等水烧开以后,将开水灌入热水瓶,然后开始整理家务 小文在烧水,在烧水的过程中整理家务,等水烧开以后,放下手中的家务活,将开水灌入热水瓶,然后继续整理家务 这也是日 ...
- jQuery uploadify上传文件404,500错误
1.如果部署到了IIS7的话,IIS7默认的大小为3000000.修改方法如下: 找到网站双击“请求筛选”——右边找到“编辑功能设置”——将“允许的最大内容长度”改成你想要的就行了. 2.当上传大文件 ...
- <s:iterator> 对list操作的一种方法
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA6IAAAH3CAIAAAAuRnW9AAAgAElEQVR4nOzdf4gk1333+wLDDffhPp
- powershell 参数 [String]Service
此种情况,去掉前面的[String] 在里面操作的时候就会认为是string,并可以自动操作了,否则限定为String类型时,就无法将输入的a,b当作String了, 或者需要添加'a,b'单引号来变 ...
- Ruby界面开发--wxRuby库TextCtrl相关问题
界面库官方教程:(1) 总的各种库函数讲解http://wxruby.rubyforge.org/doc/index.html (2)TextCtrl讲解 http://wxruby.rubyforg ...
- MyEclipse 下 Tomcat启动变慢如何解决
MyEclipse 下 Tomcat启动变慢如何解决 项目使用debug启动有时候会突然变得非常慢.不但启动慢,启动之后连打开项目页面也很慢,是日常的4,5倍.可以有下面的几种解决方法: 1. ...
- 封装page分页类
类: <?php //分页工具类 class Page{ /* * 获取分页字符串 * @param1 string $uri,分页要请求的脚本url ...