题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5768

题目大意:给你区间[L,R],问你[L, R]中有多少个数字x满足x%7=0且x%p[i]≠a[i];

数据范围:1≤L<R≤10^18,0<a[i]<p[i]≤10^5,p[i],a[i]有n对,0≤n≤15;

解题思路:这道题赛场上想到了正解,但是因为一些细节处理上经验不足导致WA到结束(对拍都能过,大数处理的问题)

首先看到所求的条件像是求几个集合的并,而n范围恰好合乎容斥范围,对n个条件做容斥,其中处理交集的时候,即求同时满足几个条件的x时显然需要用中国剩余定理来计算。中国剩余定理求得一个合法解ret之后,所有解就是ret+k*M,M就是当前集合那些p[i]的乘积。这里写个函数算一下[L,R]之间%M=ret的有多少个就好了。

而对于特殊条件%7=0,起初想把该条件变为6个条件%7=1,%7=2...%7=6,这样加上给的n个条件一共21个,复杂度2^21*21加上乱七八糟常数,还有T组数据肯定会TLE,于是考虑直接当成条件%7≠0处理。在求其他条件与这个特殊条件的交的时候就用其他条件的交-其他条件与“%7=0”的交计算即可,这样O(2^16*16)很容易接受。

大概思路就是这样,其中需要注意的点:

1、最好写一个Get(L, R, P, X)函数表示[L,R]区间中有多少个数%P=X,这个函数要写稳。

2、由于这类问题数据范围十分的大,CRT中有一句话ret=(ret+tm*x*a[i])%M是需要写快速乘计算,不然会爆long long,惨死…

3、写快速乘的时候一定要记得幂的位置不能是负数

 #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL; const int MaxN = ;
int T, n, cas;
LL ans, L, R;
LL P[MaxN + ], A[MaxN + ]; void Init()
{
scanf("%d%lld%lld", &n, &L, &R);
for (int i = ; i <= n; i++) scanf("%lld%lld", &P[i], &A[i]);
} LL Get(LL L, LL R, LL p, LL x)
{
LL lm = L % p;
LL lc = L / p;
LL rm = R % p;
LL rc = R / p;
LL ans = rc - lc;
if (lm > x) ans--;
if (rm >= x) ans++;
return ans;
} LL extend_gcd(LL a, LL b, LL &x, LL &y)
{
if (b == ) {
x = ; y = ;
return a;
}else {
LL r = extend_gcd(b, a % b, y, x);
y -= x * (a / b);
return r;
}
} LL qwr(LL x, LL y, LL MOD)
{
x = x % MOD;
LL ans = ;
while (y != ) {
if (y & ) ans = (ans + x) % MOD;
y = y / 2LL;
x = (x + x) % MOD;
}
return ans;
} LL CRT(LL a[], LL m[], int n)
{
LL M = ;
for (int i = ; i <= n; i++) M *= m[i];
LL ret = ;
for (int i = ; i <= n; i++) {
LL x, y;
LL tm = M / m[i];
extend_gcd(tm, m[i], x, y);
ret = (ret + qwr(qwr(x, tm, M), a[i], M)) % M;
}
return (ret + M) % M;
} void Solve()
{
ans = ;
for (int s = ; s <= (( << (n + )) - ); s++) {
LL p[MaxN + ], a[MaxN + ];
if (s == ) ans += (R - L + ) - Get(L, R, , );
else {
int tot = ; LL Fac = ;
for (int i = ; i <= n; i++)
if (s & ( << i)) p[++tot] = P[i], a[tot] = A[i], Fac *= p[tot];
if (s & ) {
LL t = ((tot + ) & ) ? : -;
LL crt1 = CRT(a, p, tot);
a[++tot] = ; p[tot] = ;
LL crt2 = CRT(a, p, tot);
ans += t * (Get(L, R, Fac, crt1) - Get(L, R, Fac * (LL), crt2));
}
else {
LL t = (tot & ) ? : -;
LL crt = CRT(a, p, tot);
ans += t * Get(L, R, Fac, crt);
}
}
}
printf("Case #%d: %lld\n", ++cas, R - L + - ans);
} int main()
{
scanf("%d", &T);
for (int i = ; i <= T; i++) {
Init();
Solve();
}
}

2016 Multi-University Training Contest 4 - 1005 (hdu5768)的更多相关文章

  1. 2016 Al-Baath University Training Camp Contest-1

    2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...

  2. hdu 4939 2014 Multi-University Training Contest 7 1005

    Stupid Tower Defense Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/ ...

  3. 2016 Multi-University Training Contest 2 - 1005 Eureka

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5738 题目大意:给定平面上的n个点,一个集合合法当且仅当集合中存在一对点u,v,对于集合中任意点w,均 ...

  4. 2016 Multi-University Training Contest 2 - 1005 (hdu5738)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5738 题目大意:给定平面上的n个点,一个集合合法当且仅当集合中存在一对点u,v,对于集合中任意点w,均 ...

  5. 2016 Al-Baath University Training Camp Contest-1 E

    Description ACM-SCPC-2017 is approaching every university is trying to do its best in order to be th ...

  6. 2016 Al-Baath University Training Camp Contest-1 A

    Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...

  7. 2016 Al-Baath University Training Camp Contest-1 J

    Description X is fighting beasts in the forest, in order to have a better chance to survive he's gon ...

  8. 2016 Al-Baath University Training Camp Contest-1 I

    Description It is raining again! Youssef really forgot that there is a chance of rain in March, so h ...

  9. 2016 Al-Baath University Training Camp Contest-1 H

     Description You've possibly heard about 'The Endless River'. However, if not, we are introducing it ...

随机推荐

  1. codeforces 985C Liebig's Barrels(贪心)

    题目 题意: 有n * k块木板,每个木桶由k木板组成,每个木桶的容量定义为它最短的那块木板的长度. 任意两个木桶的容量v1,v2,满足|v1-v2| <= d. 问n个木桶容量的最大的和为多少 ...

  2. 06-图2 Saving James Bond - Easy Version (25 分)

    This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...

  3. django 视图view

    视图里一般是函数和类,需要返回响应. 试图分为2种:2. CBV(class base view) FBV(function base view) from django.views import V ...

  4. jsonp跨域请求及本质

    在html页面中,能实现跨域请求的是 第一: <script src="http://localhost:59602/JsonpTest.ashx?callBack=callBack& ...

  5. java课后思考题(四)

    1. 请运行以下示例代码StringPool.java,查看其输出结果.如何解释这样的输出结果?从中你能总结出什么? 输出结果: 结论: 在Java中,内容相同的字串常量(“Hello”)只保存一份以 ...

  6. ubuntu hadoop集群 master免密码登陆到slave节点

    1. 在master节点上安装ssh client,在slave节点上安装ssh server sudo apt-get install openssh-client sudo apt-get ins ...

  7. 使用Ext 创建树

    ext使用的是ext3.4.0版本 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> < ...

  8. 多个ajax请求时控制执行顺序或全部执行后的操作

    1.当确保执行顺序时 (1)请求加async: false,,这样所有的ajax就会同步执行,请求顺序就是代码顺序: (2)$.when   确保所有异步的ajax请求完毕时 $.when($.aja ...

  9. RTT设备与驱动之I2C:

    I2C主从结构(可以有多个主机,但同一时间只能有一个):I2C有两种地址结构7位/10位 总线空闲时,SDA 和 SCL 都处于高电平状态. 开始信号: SCL 为高电平时,主机将 SDA 拉低 结束 ...

  10. LeetCode 167.两数之和(C++)

    给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值 ...