HDU 4135 Co-prime(容斥原理)
2016-05-04 15:02
351 查看
Co-prime
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3313 Accepted Submission(s): 1286
Problem Description
Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N.
Two integers are said to be co-prime or relatively prime if they have no common positive divisors other than 1 or, equivalently, if their greatest common divisor is 1. The number 1 is relatively prime to every integer.
Input
The first line on input contains T (0 < T <= 100) the number of test cases, each of the next T lines contains three integers A, B, N where (1 <= A <= B <= 1015) and (1 <=N <= 109).
Output
For each test case, print the number of integers between A and B inclusive which are relatively prime to N. Follow the output format below.
Sample Input
2 1 10 2 3 15 5
Sample Output
Case #1: 5 Case #2: 10 求在n在a~b之间的和n互质的个数 容斥原理模板题#include <iostream> #include <string.h> #include <algorithm> #include <stdlib.h> #include <math.h> #include <stdio.h> using namespace std; typedef long long int LL; const LL INF=(LL)1<<62; #define MAX 1000000 LL prime[MAX+5]; LL sprime[MAX+5]; LL q[MAX+5]; bool check[MAX+5]; LL n,a,b,cnt; void eular() { memset(check,false,sizeof(check)); int tot=0; for(LL i=2;i<MAX+5;i++) { if(!check[i]) prime[tot++]=i; for(int j=0;j<tot;j++) { if(i*prime[j]>MAX+5) break; check[i*prime[j]]=true; if(i%prime[j]==0) break; } } } void Divide(LL n) { cnt=0; LL t=(LL)sqrt(1.0*n); for(LL i=0;prime[i]<=t;i++) { if(n%prime[i]==0) { sprime[cnt++]=prime[i]; while(n%prime[i]==0) n/=prime[i]; } } if(n>1) sprime[cnt++]=n; } LL Ex(LL n) { LL sum=0,t=1; q[0]=-1; for(LL i=0;i<cnt;i++) { LL x=t; for(LL j=0;j<x;j++) { q[t]=q[j]*sprime[i]*(-1); t++; } } for(LL i=1;i<t;i++) sum+=n/q[i]; return sum; } int main() { int t; scanf("%d",&t); eular(); int cas=0; while(t--) { scanf("%lld%lld%lld",&a,&b,&n); Divide(n); printf("Case #%d: %lld\n",++cas,(b-Ex(b)-(a-1-Ex(a-1)))); } return 0; }
相关文章推荐
- 时间戳
- /*C#:扩展方法*/ 《自学系列》
- javaweb学习总结(六)——Servlet开发(二)
- win10密码清除技巧
- 并发编程学习总结(一) :java 创建线程的三种方式的优缺点和实例
- (礼拜三log)前端开发:replace函数
- 创建文件并设置NTFS权限
- Java实现冒泡排序算法及对其的简单优化示例
- Win7(64bit)下安装Docker
- python global语句 变量作用域
- 【LeetCode-142】Linked List Cycle II
- 欢迎使用CSDN-markdown编辑器
- HDU Binary Numbers 1390(转换+统计)
- css3盒子布局-定义盒子的弹性空间(box-flex)
- Android WebView
- undefined reference to 问题总结(转载)
- replication_slot and PostgreSQL Replication
- 什么叫大数据,果断让你秒懂
- 构造者模式
- java从头到位实现区域三级下拉框