HDU 5768Lucky7(多校第四场)容斥+中国剩余定理(扩展欧几里德求逆元的)+快速乘法
地址:http://acm.hdu.edu.cn/showproblem.php?pid=5768
Lucky7
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
?? once wrote an autobiography, which mentioned something about himself. In his book, it said seven is his favorite number and he thinks that a number can be divisible by seven can bring him good luck. On the other hand, ?? abhors some other prime numbers and thinks a number x divided by pi which is one of these prime numbers with a given remainder ai will bring him bad luck. In this case, many of his lucky numbers are sullied because they can be divisible by 7 and also has a remainder of ai when it is divided by the prime number pi.
Now give you a pair of x and y, and N pairs of ai and pi, please find out how many numbers between x and y can bring ?? good luck.
Each test case starts with three integers three intergers n, x, y(0<=n<=15,0<x<y<1018) on a line where n is the number of pirmes.
Following on n lines each contains two integers pi, ai where pi is the pirme and ?? abhors the numbers have a remainder of ai when they are divided by pi.
It is guranteed that all the pi are distinct and pi!=7.
It is also guaranteed that p1*p2*…*pn<=1018 and 0<ai<pi<=105for every i∈(1…n).
2 1 100
3 2
5 3
0 1 100
Case #2: 14
For Case 1: 7,21,42,49,70,84,91 are the seven numbers.
For Case2: 7,14,21,28,35,42,49,56,63,70,77,84,91,98 are the fourteen numbers.
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<cstring>
#include<queue>
#include<set>
#include<string>
#include<map>
#define inf 9223372036854775807
#define INF 9e7+5
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef double db;
typedef long long LL;
const int maxn = 1e2 + ;
const int mod = 1e9 + ;
const db eps = 1e-;
ll n, l, r, ai[maxn], pi[maxn]; int Bitcount(ll x) {
return x ? Bitcount(x >> ) + (x&1LL) : ;
} //求当前状态选择的方程的个数 ll q_mul(ll a, ll b, ll mod) {
ll ret = ;
while (b) {
if (b & ) ret = (ret + a) % mod;
b >>= ;
a = (a + a) % mod;
}
return ret;
} // 快速乘法 void Ex_Gcd(ll a, ll b, ll &x, ll &y){
ll d;
if(b==){
x=,y=;
return;
}
Ex_Gcd(b,a%b,y,x);
y-=a/b*x;
} // 这里直接抄的标程的函数。。 ll CRT(ll p[], ll a[], ll cnt) {
ll M = ;
ll res = ;
for (int i = ; i <= cnt; i++) M *= p[i];
for (int i = ; i <= cnt; i++) {
ll m = M / p[i], x, y;
Ex_Gcd(m, p[i], x, y);
res = (res + q_mul(q_mul(x, m, M), a[i], M)) % M;
}
return (res + M) % M;
} ll solve(ll x) {
ll p[maxn], m[maxn], ans = ;
p[] = , m[] = ;
if (!x) return ;
for (ll i = ; i < (1LL << n); i++) {
ll num = Bitcount(i), t = , cnt = ;
for (ll j = ; j < n; j++) {
if (i & (1LL << j)) {
p[++cnt] = pi[j];
m[cnt] = ai[j];
t *= pi[j];
}
}
ll res = CRT(p, m, cnt);
if (res > x) continue;
if (num & ) ans -= (x - res) / t + ; //容斥一下
else ans += (x - res) / t + ;
}
return ans + x / ;
} int main() {
//cin.sync_with_stdio(false);
//freopen("tt.txt", "r", stdin);
//freopen("isharp.out", "w", stdout);
int t, cas = ; cin >> t;
while (t--) {
cin >> n >> l >> r;
for (int i = ; i < n; i++)
cin >> pi[i] >> ai[i];
printf("Case #%d: %I64d\n", cas++, solve(r) - solve(l-));
}
return ;
}
HDU 5768Lucky7(多校第四场)容斥+中国剩余定理(扩展欧几里德求逆元的)+快速乘法的更多相关文章
- 【中国剩余定理】【容斥原理】【快速乘法】【数论】HDU 5768 Lucky7
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 题目大意: T组数据,求L~R中满足:1.是7的倍数,2.对n个素数有 %pi!=ai 的数 ...
- bzoj 3782 上学路线 卢卡斯定理 容斥 中国剩余定理 dp
LINK:上学路线 从(0,0)走到(n,m)每次只能向上或者向右走 有K个点不能走求方案数,对P取模. \(1\leq N,M\leq 10^10 0\leq T\leq 200\) p=10000 ...
- HDU 4635 多校第四场 1004 强联通
我还有什么好说,还有什么好说...... 我是SBSBSBSBSBSBSBSBSBSBSBSBBSBSBSBSBSBSBSBSBS........................ 题意 思路什么的都不 ...
- HDU暑假多校第四场J-Let Sudoku Rotate
一.题意 Sudoku is a logic-based, combinatorial number-placement puzzle, which is popular around the wor ...
- HDU 全国多校第四场 题解
题解 A AND Minimum Spanning Tree 参考代码: #include<bits/stdc++.h> #define maxl 200010 using namespa ...
- 2018 HDU多校第四场赛后补题
2018 HDU多校第四场赛后补题 自己学校出的毒瘤场..吃枣药丸 hdu中的题号是6332 - 6343. K. Expression in Memories 题意: 判断一个简化版的算术表达式是否 ...
- 牛客多校第四场sequence C (线段树+单调栈)
牛客多校第四场sequence C (线段树+单调栈) 传送门:https://ac.nowcoder.com/acm/contest/884/C 题意: 求一个$\max {1 \leq l \le ...
- Harvest of Apples (HDU多校第四场 B) (HDU 6333 ) 莫队 + 组合数 + 逆元
题意大致是有n个苹果,问你最多拿走m个苹果有多少种拿法.题目非常简单,就是求C(n,0)+...+C(n,m)的组合数的和,但是询问足足有1e5个,然后n,m都是1e5的范围,直接暴力的话肯定时间炸到 ...
- HDU How many integers can you find 容斥
How many integers can you find Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 ...
随机推荐
- CodeForces242D:Connected Components (不错的并查集)
We already know of the large corporation where Polycarpus works as a system administrator. The compu ...
- Code-NFine:下来框和复选框
ylbtech-Code-NFine:下来框和复选框 1.返回顶部 1. 1.1 html $("#F_OrganizeId").bindSelect({ url: "/ ...
- JDK7中匿名内部类中使用局部变量要加final,JDK8中不需要,但jdk会默认加上final
今天看书的时候看到了局部内部类,书上说局部内部类可以访问局部变量,但是必须是final的.因为局部变量在方法调用之后就消失了,使用final声明的话该局部变量会存入堆中,和内部类有一样的声明周期.但是 ...
- Integrate Your Code with the Frameworks---整合你的代码和框架
Back to Frameworks Integrate Your Code with the Frameworks When you develop an app for OS X or iOS, ...
- (二十七)分类信息的curd-分类信息修改
修改分类步骤分析: 1.在list.jsp页面上点击修改(编辑) /store/adminCategory?method=getById&cid=??? 2.在getById方法中 获取c ...
- NYOJ3——多边形重心问题
多边形重心问题 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述:在某个多边形上,取n个点,这n个点顺序给出,按照给出顺序将相邻的点用直线连接, (第一个和最后一个连接) ...
- Codeforces703B Mishka and trip
题意: 就是有n个点,本来相邻点之间就有一条边,1和n之间也有一条,然后给你几个特殊点,说这些特殊点和其他所有点都连起来了,然后算一个所有边的权值和,每条边的权值等于两个点的c相乘. 思路: 水题啊- ...
- 第六篇 .NET高级技术之拆箱装箱
拆箱.装箱 值类型赋值给Object类型变量的时候,会发生装箱:包装成Object.ValueType不也是继承自Object吗(CLR内部处理): Object类型变量赋值给值类型赋值的时候会发生拆 ...
- hud3371 Connect the Cities 简单最小生成树
//我看过Discuss说不能用克鲁斯卡尔因为有很多边 //但是只能用G++过,C++的确超时 #include <stdio.h> #include <string.h> # ...
- zabbix网络发现主机
1 功能介绍 默认情况下,当我在主机上安装agent,然后要在server上手动添加主机并连接到模板,加入一个主机组. 如果有很多主机,并且经常变动,手动操作就很麻烦. 网络发现就是主机上安装了age ...