HDU 5768 Lucky7 (中国剩余定理+容斥)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5768
给你n个同余方程组,然后给你l,r,问你l,r中有多少数%7=0且%ai != bi.
比较明显的中国剩余定理+容斥,容斥的时候每次要加上个(%7=0)这一组。
中间会爆longlong,所以在其中加上个快速乘法(类似快速幂)。因为普通的a*b是直接a个b相加,很可能会爆。但是你可以将b拆分为二进制来加a,这样又快又可以防爆。
//#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
const int N = 1e5 + ;
LL a[] , b[], fuck = 1e18; LL Fmul(LL a , LL n , LL mod) { //快速乘法
LL res = ;
while(n) {
if(n & ) {
res = (res + a) % mod;
}
a = a * % mod;
n >>= ;
}
return res;
} LL exgcd(LL a , LL b , LL &x , LL &y) {
LL res = a;
if(!b) {
x = ;
y = ;
}
else {
res = exgcd(b , a % b , x , y);
LL temp = x;
x = y;
y = temp - a / b * y;
}
return res;
} LL CRT(LL a[] , LL m[] , LL n) {
LL M = , res = ;
for(LL i = ; i <= n ; i++) {
M = M * m[i];
}
for(LL i = ; i <= n ; i++) {
LL x , y , Mi = M / m[i];
exgcd(Mi , m[i] , x , y);
x = (x % m[i] + m[i]) % m[i];
res = (res + Fmul(x * a[i] % M , Mi , M) + M) % M;
}
if(res < )
res += M;
return res;
} LL Get(LL n, LL x, LL y) {
if(x > n)
return ;
else {
n -= x;
return (LL)( + n / y);
}
} int main()
{
int t, n;
LL l , r , md[], di[];
scanf("%d", &t);
for(int ca = ; ca <= t; ++ca) {
scanf("%d %lld %lld", &n, &l, &r);
di[] = , md[] = ;
for(int i = ; i <= n; ++i) {
scanf("%lld %lld", a + i, b + i);
}
LL res1 = , res2 = ;
int limit = ( << n) - ;
for(int i = ; i <= limit; ++i) {
int temp = i, index = ;
LL mul = ;
for(int j = ; temp ; ++j) {
if(temp & ) {
di[++index] = a[j];
md[index] = b[j];
mul *= a[j];
}
temp >>= ;
}
if(index % ) {
res1 -= Get(l - , CRT(md, di, index), mul);
res2 -= Get(r, CRT(md, di, index), mul);
}
else {
res1 += Get(l - , CRT(md, di, index), mul);
res2 += Get(r, CRT(md, di, index), mul);
}
}
printf("Case #%d: %lld\n",ca, r/ - (l-)/ - (res2 - res1));
}
return ;
}
HDU 5768 Lucky7 (中国剩余定理+容斥)的更多相关文章
- hdu 5768 Lucky7 中国剩余定理+容斥+快速乘
Lucky7 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem D ...
- HDU 5768 Lucky7 (中国剩余定理 + 容斥 + 快速乘法)
Lucky7 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crow ...
- hdu_5768_Lucky7(中国剩余定理+容斥)
题目链接:hdu_5768_Lucky7 题意: 给你一个区间,问你这个区间内是7的倍数,并且满足%a[i]不等于w[i]的数的个数 乍一看以为是数位DP,仔细看看条件,发现要用中国剩余定理,然后容斥 ...
- HDU5768Lucky7(中国剩余定理+容斥定理)(区间个数统计)
When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortun ...
- hdu 5768 Lucky7 容斥
Lucky7 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crow ...
- HDU 5768 Lucky7 容斥原理+中国剩余定理(互质)
分析: 因为满足任意一组pi和ai,即可使一个“幸运数”被“污染”,我们可以想到通过容斥来处理这个问题.当我们选定了一系列pi和ai后,题意转化为求[x,y]中被7整除余0,且被这一系列pi除余ai的 ...
- 【中国剩余定理】【容斥原理】【快速乘法】【数论】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(CRT+容斥原理)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5768 [题目大意] 求出一个区间内7的倍数中,对于每个ai取模不等于bi的数的个数. [题解] 首 ...
随机推荐
- 无法加载 DLL“rasapi32.dll”: 动态链接库(DLL)初始化例程失败。
无法加载 DLL“rasapi32.dll”: 动态链接库(DLL)初始化例程失败. 在Asp.Net项目中使用WebClient或HttpWebRequest时出现以上错误 解决方案:把以下代码放在 ...
- QQ在线图标 离线 QQ开通在线QQ服务 QQ陌生人直接聊天
如图 永远都显示离线,即使QQ在线也显示离线的原因和解决方法 1:打开 这个页面 提示你开通 你就点击一下开通 这样头像就可以正常显示 离线 和在线了 http://wp.q ...
- Darwin Streaming Server 6.0.3安装、订制、插件或模块
How to setup Darwin Streaming Server 6.0.3 on 32 or 64 bit Linux platforms, add custom functionality ...
- HWM的实验
HWM是数据段中使用空间和未使用空间之间的界限,假如现有自由链表上的数据块不能满足需求,Oracle把HWM指向的数据块加入到自由链表上,HWM向前移动到下一个数据块.简单说,一个数据段中,HWM左边 ...
- 《C++ Primer 4th》读书笔记 第12章-类
原创文章,转载请注明出处:http://www.cnblogs.com/DayByDay/p/3936473.html
- Android 调用系统的邮箱app发送邮件
第一种: String[] email = { "3802**92@qq.com" }; // 需要注意,email必须以数组形式传入 Intent intent = new In ...
- Hibernate向MySQL插入中文数据--乱码解决
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/exam?useUnicod ...
- Jquery获取第一个子元素
如获取id为divId的div下的第一个子div $("#divId").children("div").get(0) 但得到的是一个dom对象,如果要得到Jq ...
- android中sqlite3常用命令
1)打开数据库 在adb shell模式下执行命令sqlite3 + 数据库名称,例如打开email中的EmailProvider.db数据库: 2)sqlite3特殊命令 大多数候,sqlite3读 ...
- BaseAdapter中重写getview的心得以及发现convertView回收的机制
以前一直在用BaseAdapter,对于其中的getview方法的重写一直不太清楚.今天终于得以有空来探究它的详细机制. 下面先讲讲我遇到的几个问题: 一.View getview(int posit ...