地址:http://acm.hdu.edu.cn/showproblem.php?pid=5768

Lucky7

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Problem Description
When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortunately fall into the sea. While it was dying, seven dolphins arched its body and sent it back to the shore. It is said that ?? used to surrounded by 7 candles when he faced a extremely difficult problem, and always solve it in seven minutes. 
?? 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.
 
Input
On the first line there is an integer T(T≤20) representing the number of test cases.
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).
 
Output
For each test case, first output "Case #x: ",x=1,2,3...., then output the correct answer on a line.
 
Sample Input
2
2 1 100
3 2
5 3
0 1 100
 
Sample Output
Case #1: 7
Case #2: 14

Hint

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.

 
题意:
  就是问有多少个幸运数,然后幸运数呢,就是能被7整除的数,但是这些数可能被污染,哪些数会被污染呢,就是有n种方法会被污染,每种方法给出一个ai,一个
  pi,如果幸运数%pi等于ai就会被污染,然后给你一个区间,问这个区间有多少个没被污染的幸运数。
 
题解:
  这题一开始就知道是个中国剩余定理,然后看了多篇题解,发现要用容斥+中国剩余定理,然后发现要爆long long,必须要加上一个快速乘法=。=,然后求逆元的时候
  不能用费马小定理=。=,因为快速幂要爆long long,用快速乘法就会T(I good vegetable a),最后只有学了一波欧几里德求逆元TAT。
  先对着标程看了半天他在写啥,然后发现标程是先状态压缩,然后再容斥=。=,看标程就看了好久,我是不是没救了QAQ。
 
代码:
 #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(多校第四场)容斥+中国剩余定理(扩展欧几里德求逆元的)+快速乘法的更多相关文章

  1. 【中国剩余定理】【容斥原理】【快速乘法】【数论】HDU 5768 Lucky7

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 题目大意: T组数据,求L~R中满足:1.是7的倍数,2.对n个素数有 %pi!=ai  的数 ...

  2. bzoj 3782 上学路线 卢卡斯定理 容斥 中国剩余定理 dp

    LINK:上学路线 从(0,0)走到(n,m)每次只能向上或者向右走 有K个点不能走求方案数,对P取模. \(1\leq N,M\leq 10^10 0\leq T\leq 200\) p=10000 ...

  3. HDU 4635 多校第四场 1004 强联通

    我还有什么好说,还有什么好说...... 我是SBSBSBSBSBSBSBSBSBSBSBSBBSBSBSBSBSBSBSBSBS........................ 题意 思路什么的都不 ...

  4. HDU暑假多校第四场J-Let Sudoku Rotate

    一.题意 Sudoku is a logic-based, combinatorial number-placement puzzle, which is popular around the wor ...

  5. HDU 全国多校第四场 题解

    题解 A AND Minimum Spanning Tree 参考代码: #include<bits/stdc++.h> #define maxl 200010 using namespa ...

  6. 2018 HDU多校第四场赛后补题

    2018 HDU多校第四场赛后补题 自己学校出的毒瘤场..吃枣药丸 hdu中的题号是6332 - 6343. K. Expression in Memories 题意: 判断一个简化版的算术表达式是否 ...

  7. 牛客多校第四场sequence C (线段树+单调栈)

    牛客多校第四场sequence C (线段树+单调栈) 传送门:https://ac.nowcoder.com/acm/contest/884/C 题意: 求一个$\max {1 \leq l \le ...

  8. Harvest of Apples (HDU多校第四场 B) (HDU 6333 ) 莫队 + 组合数 + 逆元

    题意大致是有n个苹果,问你最多拿走m个苹果有多少种拿法.题目非常简单,就是求C(n,0)+...+C(n,m)的组合数的和,但是询问足足有1e5个,然后n,m都是1e5的范围,直接暴力的话肯定时间炸到 ...

  9. HDU How many integers can you find 容斥

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

随机推荐

  1. python(一):multiprocessing——死锁

    前言近年来,使用python的人越来越多,这得益于其清晰的语法.低廉的入门代价等因素.尽管python受到的关注日益增多,但python并非完美,例如被人诟病最多的GIL(值得注意的是,GIL并非py ...

  2. AJAX --- 一种创建交互式网页应用的网页开发技术

    目录 AJAX 创建XHR实例 指定readyStatechange事件处理程序 启动请求 发送请求 接收数据 取消XHR请求/响应 AJAX ajax核心技术就是 XMLHttpRequest 对象 ...

  3. laravel5.2 增加Caffienate Modules,实现模块化开发

    1.模块化开发可以把框架分成 Topc前台模块,Topm手机端前台,Admin后台管理模块,每个模块中都有自己的一套Controller,Logic,router等. 2.咖啡因模块是一个简单的包,以 ...

  4. 【转】implements百科

    implements是一个类,实现一个接口用的关键字,它是用来实现接口中定义的抽象方法.实现一个接口,必须实现接口中的所有方法.   中文名 实现 外文名 implements 意    思 类实现一 ...

  5. 在idea中创建maven父子工程,子工程无法导入父工程依赖的问题

    创建maven父子工程时遇到一个问题,当子工程的名称前缀和父工程的名称一样时,子工程会出现一系列的问题.比如我的父工程名称是microservicecloud,子工程名称是microservicecl ...

  6. HDU-5540 Secrete Master Plan

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission( ...

  7. CodeForces 719A Vitya in the Countryside (水题)

    题意:根据题目,给定一些数字,让你判断是上升还是下降. 析:注意只有0,15时特别注意一下,然后就是14 15 1 0注意一下就可以了. 代码如下: #pragma comment(linker, & ...

  8. hihocoder 1582 : Territorial Dispute(凸包)

    传送门 题意 略 分析 求一个凸包即可 1.所有点在凸包上且点数>3,令凸包上第1,3点为'A',其余点为'B' 2.部分点在凸包上,令凸包上点为'A',其余点为'B' 3.无可行情况 附代码 ...

  9. c# 组件无法下断点

    1. c#组件 2.断点能下,且运行中断点本身显示正常(正常实心红点,不是白底红圈) 3.运行中无法进入组件 4.主程序中,组件的实例无法被观测到,调试显示:无法计算得到这个对象 , 但是程序能运行 ...

  10. thinkphp整合swoole

    cli模式下执行thinkphp1.cd 项目根目录2.php index.php admin/index/index --执行 模块/控制器/方法名 异步消息队列1.服务器端核心代码 /** * 脚 ...