HDU5768Lucky7
Lucky7
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 933 Accepted Submission(s): 345
?? 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).
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.
好题啊 学到了很多东西...
首先 俄罗斯乘法用于大数取模。中国剩余定理解同模方程组。记住这个解不是唯一的...
容斥原理解决 统计问题
/* ***********************************************
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的更多相关文章
- HDU5768Lucky7(中国剩余定理+容斥定理)(区间个数统计)
When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortun ...
随机推荐
- Hihocoder #1938 最大权闭合子图模板
这里的讲解很不错,适合作为入坑题: Hihocoder#1938 代码: #include<algorithm> #include<iostream> #include< ...
- 在前后端分离的SpringBoot项目中集成Shiro权限框架
参考[1].在前后端分离的SpringBoot项目中集成Shiro权限框架 参考[2]. Springboot + Vue + shiro 实现前后端分离.权限控制 以及跨域的问题也有涉及
- Bootstrap-Table 总结
Bootstrap-Table 总结 jQuery Java Bootstrap-Table JS文件 传参:直接将需要的参数置于 queryParams 方法中,例如 line:formData注意 ...
- 三菱PLC FB库函数调用方法 (Gx Work2版本)
本文以 GxWorks2 软件为例 1.新建使用标签项目的工程文件 2.从其它库所在工程项目中导入库 3.选择库文件及FB功能块 4.插入FB功能块调用
- 【04】 CSS开发注意事项
[04] CSS注意事项 1. 页面编码规范 1.1. 统一使用 UTF-8 编码,用@charset "utf-8"指定页面编码. 1.2. 全局字体设置: windows 7系 ...
- 【05】emmet系列之各种缩写
[01]emmet系列之基础介绍 [02]emmet系列之HTML语法 [03]emmet系列之CSS语法 [04]emmet系列之编辑器 [05]emmet系列之各种缩写 各种缩写 缩写:! & ...
- 用java实现二分搜索<算法分析>
实验目的:1.复习java编程:2.掌握二分搜索技术的基本原理:3.掌握使用java程序进行二分搜索的方法.实验步骤:1.由用户输入5个以上的整数:2.利用二分搜索算法完成对数组的搜索. packag ...
- hdu 2433 Travel (最短路树)
One day, Tom traveled to a country named BGM. BGM is a small country, but there are N (N <= 100) ...
- 桐桐的糖果计划(vijos 1325)
背景 桐桐是一个快乐的小朋友,他生活中有许多许多好玩的事,让我们一起来看看吧…… 描述 桐桐很喜欢吃棒棒糖.他家处在一大堆糖果店的附近. 但是,他们家的区域经常出现塞车.塞人等情况,这导致他不得不等到 ...
- Pull方式解析XML文件
package com.pingyijinren.test; import android.content.Intent; import android.os.Handler; import andr ...