题目链接: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. 第四章 Appium真机运行测试用例讲解

    -----手机自动化之Appium 手机自动化测试用例虽然可以在模拟器上运行,可是模拟器毕竟和真机还是有区别的.在第二章我们讲到了模拟器上运行测试用例后,我又花了两天的时间,研究了一下真机运行测试用例 ...

  2. matlab的一些关于块分类的函数~~~

    1. nlfilter(General sliding-neighborhood operations) B = nlfilter(A, [m n], fun),这是一个其中A是图像[m  n]是图像 ...

  3. Linux UDEV提权过程

    1.下载攻击脚本 [test@H0f ~]$ wget http://www.extmail .org/source/exploit-udev-8478 --2018-04-02 01:21:00-- ...

  4. hive - load CSV file NULL value 加载csv文件出现结果全是空值

    这个问题的根源是,创建表的时候没有指定列分隔符还有行分隔符. 因此修改建表语句 问题依然重现,此问题苦恼了一个下午,有一次用describe tablename 发现了问题所在,原来是一直没有删除ta ...

  5. Java日志组件2---Log4j(org.apache.log4j.Logger)

    如果我们在项目中,需要记录的东西并不多,而且也不需要有太多区分,使用jdk的自带Log完全可以解决问题.但是,在开发的过程中,大多数项目都比较大,为方便找到程序的bug,都是需要系统的记录日志的.这里 ...

  6. SpringBoot初始教程之Servlet、Filter、Listener配置

    1.介绍通过之前的文章来看,SpringBoot涵盖了很多配置,但是往往一些配置是采用原生的Servlet进行的,但是在SpringBoot中不需要配置web.xml的 因为有可能打包之后是一个jar ...

  7. 吞吐率(Requests per second),缩写RPS

    计算公式:   吞吐率 = 总请求数 / 处理这些请求的总完成时间   Requests per second = Complete requests / Time taken for tests 吞 ...

  8. 性能测试工具Jmeter08-Jmeter断言(检查点)

    断言是在请求的返回层面增加一层判断机制.因为请求成功了,并不代表结果一定正确,因此需要检测机制提高测试准确性. 下面介绍常用的jmeter三种断言 1.响应断言 例如: 模式匹配规则 2.Size A ...

  9. 怎么为android控件边缘添加阴影

    为控件设置一个有阴影感的背景图片即可,可以使用shape 在自定义shape中增加一层或多层,并错开,即可显示阴影效果.为增加立体感,按钮按下的时候,只设置一层.我们可以通过top, bottom, ...

  10. Map集合练习之对字符串中字母出现的次数求和

    不多说,直接上干货! 代码需求 如有这么一个字符串 String str = "fdg+avAdc bs5dDa9c-dfs"; MapTest.java package zhou ...