Lucky7

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 933    Accepted Submission(s): 345

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.

好题啊  学到了很多东西...

首先 俄罗斯乘法用于大数取模。中国剩余定理解同模方程组。记住这个解不是唯一的...

容斥原理解决 统计问题

/* ***********************************************
Author :guanjun
Created Time :2016/7/30 13:10:44
File Name :hdu5768.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std; ll a[],m[];
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){
if(!b){d=a;x=1LL;y=0LL;}
else {ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}
}
ll mult(ll a,ll k,ll m){
ll res=;
while(k){
if(k&1LL)res=(res+a)%m;
k>>=;
a=(a<<)%m;
}
return res;
}
ll china(int n,ll *a,ll *m){
ll M=,d,y,x=;
for(int i=;i<n;i++)M*=m[i];
for(int i=;i<n;i++){
ll w=M/m[i];
ex_gcd(m[i],w,d,d,y);
x=(x+mult(y,mult(w,a[i],M),M))%M;
}
return (x+M)%M;
}
ll p[],yu[];
int n;
ll get_ans(ll x){
if(x==)return ;
ll ans=;
int st=(<<n);
for(int i=;i<st;i++){
int cnt=;
ll cur=;
m[cnt]=;a[cnt]=;
cur*=;cnt++;
for(int j=;j<n;j++){
if(i&(<<j)){
m[cnt]=p[j];
a[cnt]=yu[j];
cnt++;
cur*=p[j];
}
}
ll tmp=china(cnt,a,m);
if(tmp>x)continue;
if(cnt&)ans+=(x-tmp)/cur+;
else ans-=(x-tmp)/cur+;
}
//cout<<ans<<endl;
return ans+x/;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int T,t;
ll l,r;
cin>>T;
for(int t=;t<=T;t++){
scanf("%d %I64d %I64d",&n,&l,&r);
for(int i=;i<n;i++)
scanf("%I64d %I64d",&p[i],&yu[i]);
printf("Case #%d: %I64d\n",t,get_ans(r)-get_ans(l-));
}
return ;
}

HDU5768Lucky7的更多相关文章

  1. HDU5768Lucky7(中国剩余定理+容斥定理)(区间个数统计)

    When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortun ...

随机推荐

  1. forEach的使用方法

    [-] 1  js 数组循环遍历 2  forEach 函数 3  让IE兼容forEach方法 4 如何跳出循环   1.  js 数组循环遍历. 数组循环变量,最先想到的就是 for(var i= ...

  2. CSU1007: 矩形着色

    Description Danni想为屏幕上的一个矩形着色,但是她想到了一个问题.当点击鼠标以后电脑是如何判断填充的区域呢? 现在给你一个平面直角坐标系,其中有一个矩形和一个点,矩形的四条边均是平行于 ...

  3. 【makefile】symbol <函数> : can't resolve symbol 问题分析

    调试程序的时候,在linux编译器上可以编译通过,但是编译生成的firmware烧录到板子上的后出现以下异常: can't resolve symbol,无法解析元素符号. review一下code, ...

  4. list嵌套,int与str的用法,replace

    #*************************replace(待改,改动值),返回很重要 A = [['libai',89]] A[0][0]=A[0][0].replace('a','af') ...

  5. LeetCode(56)Merge Intervals

    题目 Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6], ...

  6. c++中的三角函数

    c++中想求cos或sin: 1.首先得包含头文件,include<math.h> 2.sin(),cos(),中是弧度数,即若是角度a,则应写成cou<<sin(a*pi/1 ...

  7. 分享大牛开发经验,浅谈java程序员职业规划

    在中国有很多人都认为IT行为是吃青春饭的,如果过了30岁就很难有机会再发展下去!其实现实并不是这样子的,在下从事.NET及JAVA方面的开发的也有8年的时间了,在这...... 在中国有很多人都认为I ...

  8. Fiddler基本用法:手机抓包

    from:https://blog.csdn.net/gld824125233/article/details/52588275 电脑最好是笔记本,这样能和手机保持统一局域网内:其他不多说,直接说步骤 ...

  9. expdp,impdp,include,exclude

    The examples below are based on:- the demo schema SCOTT that is created with script: $ORACLE_HOME/rd ...

  10. codeforces 362B

    #include<stdio.h> #include<stdlib.h> int cmp(const void *a,const void *b) { return *(int ...