题目链接: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. P5021 赛道修建 (NOIP2018)

    传送门 考场上把暴力都打满了,结果文件输入输出写错了.... 当时时间很充裕,如果认真想想正解是可以想出来的.. 问你 长度最小的赛道长度的最大值 显然二分答案 考虑如何判断是否可行 显然对于一个节点 ...

  2. Gym - 101615 D Rainbow Roads dfs序

    题目传送门 题目大意: 给出一颗树,每条边都有一个颜色,对一个点来说,如果其他所有点到这个点的简单路径,相连的边颜色都不同,这个点即合法点,统计所有的合法点. 思路: 对于一个节点来说 1.如果这个节 ...

  3. day_10 函数名,闭包,迭代器

    1. 函数名的使用 1.函数名是一个变量,函数名储存的是函数的内存地址 2.函数名可以赋值给其他变量 3.函数名可以当容器类对象的元素 4.函数名可以当其他函数的参数 5.函数名可以做函数的返回值 2 ...

  4. day29 进程

    1..操作系统知识 顾名思义,进程即正在执行的一个过程.进程是对正在运行程序的一个抽象. 进程的概念起源于操作系统,是操作系统最核心的概念,也是操作系统提供的最古老也是最重要的抽象概念之一.操作系统的 ...

  5. my20_mysql的本地用户无法连接到数据库

    mysql的本地用户无法连接到数据库$ mysql -uadmin -prootroot -hlocalhost -P3309mysql: [Warning] Using a password on ...

  6. 在本地安装oracle-maven库

    mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.4.0 -Dpackaging= ...

  7. OfficeControl插件的用法

    项目中需要用到文档在线编辑的功能,网上找到这篇文章: http://hi.baidu.com/hurtingwings/item/bf83b6343305a94e3075a19e

  8. python 合并重叠数据

  9. std::map Intro

    #include <queue>#include <map>#include <iostream>#include <string.h> class T ...

  10. eclipse中使用git下载项目

    准备工作: 目的:从远程仓库github上down所需的项目 eclipse使用git插件下载github上项目 eclipse版本:eclipse4.5  64位 jdk版本:jdk-1.7 64位 ...