题目链接: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 (中国剩余定理+容斥)的更多相关文章

  1. hdu 5768 Lucky7 中国剩余定理+容斥+快速乘

    Lucky7 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem D ...

  2. HDU 5768 Lucky7 (中国剩余定理 + 容斥 + 快速乘法)

    Lucky7 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crow ...

  3. hdu_5768_Lucky7(中国剩余定理+容斥)

    题目链接:hdu_5768_Lucky7 题意: 给你一个区间,问你这个区间内是7的倍数,并且满足%a[i]不等于w[i]的数的个数 乍一看以为是数位DP,仔细看看条件,发现要用中国剩余定理,然后容斥 ...

  4. HDU5768Lucky7(中国剩余定理+容斥定理)(区间个数统计)

    When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortun ...

  5. hdu 5768 Lucky7 容斥

    Lucky7 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crow ...

  6. HDU 5768 Lucky7 容斥原理+中国剩余定理(互质)

    分析: 因为满足任意一组pi和ai,即可使一个“幸运数”被“污染”,我们可以想到通过容斥来处理这个问题.当我们选定了一系列pi和ai后,题意转化为求[x,y]中被7整除余0,且被这一系列pi除余ai的 ...

  7. 【中国剩余定理】【容斥原理】【快速乘法】【数论】HDU 5768 Lucky7

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 题目大意: T组数据,求L~R中满足:1.是7的倍数,2.对n个素数有 %pi!=ai  的数 ...

  8. HDU 5768 Lucky7 (容斥原理 + 中国剩余定理 + 状态压缩 + 带膜乘法)

    题意:……应该不用我说了,看起来就很容斥原理,很中国剩余定理…… 方法:因为题目中的n最大是15,使用状态压缩可以将所有的组合都举出来,然后再拆开成数组,进行中国剩余定理的运算,中国剩余定理能够求出同 ...

  9. HDU 5768 Lucky7(CRT+容斥原理)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5768 [题目大意] 求出一个区间内7的倍数中,对于每个ai取模不等于bi的数的个数. [题解] 首 ...

随机推荐

  1. truncate table 和delete

    delete table 和 truncate table 使用delete语句删除数据的一般语法格式: delete [from] {table_name.view_name} [where< ...

  2. UVa 11076 (有重元素的排列) Add Again

    n个可重复的元素的排列一共有 = All种,其中 假设这些数依次为ai,每种数字有mi个. 从右往左考虑第d位数(d≥0),第i个数字出现的次数为,那么这个数字对所求答案的贡献为 其实可以先一次求出个 ...

  3. POJ 3090 (欧拉函数) Visible Lattice Points

    题意: UVa 10820 这两个题是同一道题目,只是公式有点区别. 给出范围为(0, 0)到(n, n)的整点,你站在原点处,问有多少个整点可见. 对于点(x, y), 若g = gcd(x, y) ...

  4. NSAutoReleasePool

    做iPhone应用开发已经2年多了, 但一些基础的概念性问题只是大致了解, 脑袋中有个模糊的概念. 虽然对平时工作开发没什么影响, 不过时间长了, 心里总是有点虚. 所以从现在开始, 每当我遇到一个模 ...

  5. javascript中的关键字和保留字

    javascript中关键字的问题,将名称替换了下,确实就没有问题了.现在将它的关键字和保留字贴出来,便于日后查看和避免在次出现类似的问题. 1 关键字breakcasecatchcontinuede ...

  6. WinForm 禁止调整大小、禁止最大化窗口

    这个设置代码必须添加到*.designer.cs中,就是自动隐藏的那部分: #region Windows Form Designer generated code 一般窗体设置的代码会生成到最后面, ...

  7. nginx - conf.d vs sites-available

    自己理解: conf.d - 扩展配置文件,用户配置文件 sites-available - 配置 虚拟主机(nginx支持多个虚拟主机,sites-enabled(存放 软链接,指向sites-av ...

  8. RTP协议学习大总结从原理到代码

    from:http://wenku.baidu.com/view/aaad3d136edb6f1aff001fa5.html 一.流媒体概念 流媒体包含广义和狭义两种内涵:广义上的流媒体指的是使音频和 ...

  9. poj 3694 Network

    题意: 添加每条新连接后网络中桥的数目// 超时 先放着了 ,下次改//早上这代码超时了 下午改了,代码在下面#include <iostream> #include <algori ...

  10. 【unity3D】鼠标控制camera的移动、放大(俯视浏览效果、LOL摄像机移动效果)

    在Unity开发中,用鼠标滚轮来实现摄像机的视野范围,鼠标左键拖拉控制摄像机的移动,代码如下: 1.俯视浏览效果 using UnityEngine; using System.Collections ...