2016 Multi-University Training Contest 4 - 1005 (hdu5768)
题目链接: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)的更多相关文章
- 2016 Al-Baath University Training Camp Contest-1
2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...
- 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/ ...
- 2016 Multi-University Training Contest 2 - 1005 Eureka
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5738 题目大意:给定平面上的n个点,一个集合合法当且仅当集合中存在一对点u,v,对于集合中任意点w,均 ...
- 2016 Multi-University Training Contest 2 - 1005 (hdu5738)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5738 题目大意:给定平面上的n个点,一个集合合法当且仅当集合中存在一对点u,v,对于集合中任意点w,均 ...
- 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 ...
- 2016 Al-Baath University Training Camp Contest-1 A
Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Device eth0 does not seem to be present, delaying initialization: Linux Networking
copy centos 报错 Device eth0 does not seem to be present, delaying initialization: Linux Networking # ...
- JS电话、手机号码验证
function isTelephone(inpurStr) { var partten = /^0(([1,2]\d)|([3-9]\d{2}))-\d{7,8}$/; ...
- 9.ORM数据访问
1.Spring对ORM的支持 ORM : 对象关系映射(Object Relational Mapping)是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术基于ORM的数据持久层框架有: ...
- SpEL
Spriing boot stater中根据配置文件中的条件 生成相应的bean, 以适应不同场景 @ConditionalOnExpression中使用SpEl, 支持各种条件表达式 String ...
- python模块之openpyxl扩展
主要是对openpyxl扩展进行扩展,使用归类等 1. 安装 pip install openpyxl 想要在文件中插入图片文件,需要安装pillow,安装文件:PIL-fork-1.1.7.win- ...
- MySQL之concat以及group_concat的用法
本文中使用的例子均在下面的数据库表tt2下执行: 一.concat()函数 1.功能:将多个字符串连接成一个字符串. 2.语法:concat(str1, str2,...) 返回结果为连接参数产生的字 ...
- mc04_IntelliJ IDEA常用设置
字体设置 File --> Settings --> Font 项目编码设置 File --> Settings --> File Encodings 项目依赖 即一个项目引用 ...
- 一些自己编写的简单的js
图片在窗口内弹来弹去的效果 <div class="FrontAdv_float01-default" style="position: absolute;z-in ...
- maya安装不了
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- Django自定义过滤器
1.首先在在settings中的INSTALLED_APPS配置当前app,不然django无法找到自定义的simple_tag. 2.在app中创建templatetags模块(模块名只能是temp ...