hdoj Co-prime 4135 (容斥原理)
来源:互联网 发布:二次元 社区 论坛源码 编辑:程序博客网 时间:2024/06/05 17:07
Co-prime
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2778 Accepted Submission(s): 1070
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.
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
21 10 23 15 5
Sample Output
Case #1: 5Case #2: 10HintIn the first test case, the five integers in range [1,10] which are relatively prime to 2 are {1,3,5,7,9}.思路:要求在l--r这一范围内与n互质的数的个数,先求出在此范围内不与n互质的数的个数num,然后用总个数减去num即为所求。应用容斥原理解。p数组用来存放n的因子,q数组用来存放各个因子的乘积。#include<stdio.h>#include<string.h>#define ll long longll p[10010];ll q[10010];ll k;void getp(ll n){k=0;ll i,j;for(i=2;i*i<=n;i++){if(n%i==0){p[k++]=i;while(n%i==0)n/=i;}}if(n>1)p[k++]=n;}ll solve(ll n){ll i,j,t=0,kk,sum=0;q[t++]=-1;for(i=0;i<k;i++){kk=t;for(j=0;j<kk;j++)q[t++]=p[i]*q[j]*-1;}for(i=1;i<t;i++)sum+=(n/q[i]);return sum;}int main(){int t,T=1;ll l,r,n;scanf("%d",&t);while(t--){scanf("%lld%lld%lld",&l,&r,&n);getp(n);ll cnt=(r-solve(r))-(l-1-solve(l-1));printf("Case #%d: %lld\n",T++,cnt);}return 0;}
0 0
- 【容斥原理】 HDOJ 4135 Co-prime
- HDOJ 4135 Co-prime 容斥原理
- hdoj 4135 Co-prime 【容斥原理】
- HDOJ 4135 Co-prime (容斥原理)
- hdoj Co-prime 4135 (容斥原理)
- HDOJ 4135 Co-prime(容斥原理)
- HDOJ-----4135Co-prime容斥原理三种实现
- HDOJ 4135 Co-prime (容斥)
- [容斥原理] hdu 4135 Co-prime
- 【HDU】4135 Co-prime 容斥原理
- hdu 4135 Co-prime(容斥原理)
- hdu 4135 Co-prime 容斥原理
- hdu 4135 Co-prime【容斥原理】
- hdu 4135 Co-prime (容斥原理)
- 【容斥原理】HDU 4135 Co-prime
- HDU 4135 Co-prime (容斥原理)
- hdu 4135 Co-prime(容斥原理)
- hdu 4135 Co-prime 容斥原理
- 【转载】卡尔曼滤波的原理说明
- Paxos算法 (土豆视频)
- js中几种实用的跨域方法原理详解
- 用adb查看sqlite数据库
- BenchmarkSQL用于windows上的压测加载数据时出现错误:找不到或无法加载主类LoadData
- hdoj Co-prime 4135 (容斥原理)
- context是如何泄漏的 - Handlers和内部类
- C#数据类型 初步认识
- 2015年最热门前端框架React 入门实例教程
- Apache下rewrite配置
- Android Handler
- MindMapper中怎样实现文字多行输入
- hadoop学习002
- 机器学习领域的会议&大牛