题目链接: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. [例] 用MappedByteBuffer更新文件内容

    import java.io.IOException; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; impor ...

  2. Array to List

    List<OisDiscountIndex> discountIndexes = Arrays.asList(new OisDiscountIndex[trades.size()]);

  3. Python中的None与 NULL(即空字符)的区别

    None是Python的特殊类型,NoneType对象,它只有一个值None. 它不支持任何运算也没有任何内建方法. None和任何其他的数据类型比较永远返回False. None有自己的数据类型No ...

  4. ubuntu下找不到eth0

    1.,查找不到eth0 2.修改/etc/network/interface 发现没有eth0网卡信息 添加如下 autho eth0 iface eth0 inet dhcp 执行 sudo /et ...

  5. Linux下Java环境安装配置记录

    下载jdk http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 两种安装方式: 第一 ...

  6. 2.1 Rust概念

    标识符 The first character is a letter.The remaining characters are alphanumeric or _.或The first charac ...

  7. 08-----pymysql模块使用

    pymysql的下载和使用 exctue() 之sql注入 增.删.改:conn.commit() 查:fetchone.fetchmany.fetchall   一.pytmysql的下载和使用  ...

  8. 使用Myeclipse导入IDEA项目

    问题描述:使用Myeclipse导入IDEA创建的Web项目,成功导入,但是显示的是一个普通的JAVA项目,无法加载到tomcat下. 解决方案:右键项目Properties,选择Myeclipse- ...

  9. Java基础07-随机数

    第一种方法:导入java.util.Random; import java.util.Random; public class Test1{ public static void main(Strin ...

  10. GitKraken使用教程-基础部分(6)

    4) 放弃本次文件的改动 有些情况下,由于更改代码造成了编译无法通过等错误时,想要放弃这次对文件的修改,将文件还原成上一次提交后的状态,一种简单的恢复文件的方法就是,在Unstaged Files 列 ...