题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=4135

Co-prime

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1176    Accepted Submission(s): 427

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

Hint

In the first test case, the five integers in range [1,10] which are relatively prime to 2 are {1,3,5,7,9}.

 

Source
 

Recommend
lcy  

题目意思:
给一个a,b,n求在[a,b]内有多少个与n互质的数。

解题思路:

简单的容斥原理。

反面思考。先求出与n不互质的,也就是包括n的质因数的。然后就能够想到先把n分解质因数。

然后能够想到先预处理1000000内的质数。

然后求出1~b满足要求的个数,减去1~a-1满足要求的个数,答案即为最后结果。

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std; #define N 1000000
int prime[130000],cnt;
bool isprime[N+10];
int pp[110000],cnt0;
ll a,b,n,ans1,ans2; void init() //预处理出1~1000000内的质数
{
cnt=0;
for(int i=1;i<=N;i++)
isprime[i]=true; for(int i=2;i<=N;i++)
{
if(!isprime[i])
continue;
prime[++cnt]=i;
for(int j=i*2;j<=N;j+=i)
isprime[j]=false;
}
//printf("cnt:%d\n",cnt);
} void Cal(ll cur) //分解出cur的质因数
{
cnt0=0; for(int i=1;prime[i]*prime[i]<=cur;i++)
{
if(cur%prime[i]==0)
{
pp[++cnt0]=prime[i];
while(!(cur%prime[i]))
cur/=prime[i];
}
}
if(cur!=1)
pp[++cnt0]=cur;
} void dfs(ll hav,int cur,int num) //容斥原理求出互质的
{
if(cur>cnt0||(hav>a&&hav>b))
return ;
for(int i=cur;i<=cnt0;i++)
{
ll temp=hav*pp[i];
if(num&1)
{
ans1-=b/temp;
ans2-=a/temp;
}
else
{
ans1+=b/temp;
ans2+=a/temp;
} dfs(temp,i+1,num+1);
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
init();
//system("pause");
int t; scanf("%d",&t);
for(int ca=1;ca<=t;ca++)
{
scanf("%I64d%I64d%I64d",&a,&b,&n);
Cal(n);
//printf("cnt0:%d\n",cnt0);
a--;
ans1=b,ans2=a; for(int i=1;i<=cnt0;i++)
{
ans1-=b/pp[i];
ans2-=a/pp[i]; dfs(pp[i],i+1,2);
}
printf("Case #%d: %I64d\n",ca,ans1-ans2);
}
return 0;
}

 

[容斥原理] hdu 4135 Co-prime的更多相关文章

  1. HDU 4135 容斥

    问a,b区间内与n互质个数,a,b<=1e15,n<=1e9 n才1e9考虑分解对因子的组合进行容斥,因为19个最小的不同素数乘积即已大于LL了,枚举状态复杂度不会很高.然后差分就好了. ...

  2. hdu 4135 Co-prime (素数打表+容斥原理)

    题目链接 题意:问从A到B中与N互素的个数. 题解: 利用容斥原理:先求出与n互为素数的个数. 可以先将 n 进行素因子分解,然后用区间 x 除以 素因子,就得到了与 n 的 约数是那个素因子的个数, ...

  3. 容斥原理学习(Hdu 4135,Hdu 1796)

    题目链接Hdu4135 Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  4. HDU 4135 Co-prime(容斥原理)

    Co-prime 第一发容斥,感觉挺有意思的 →_→ [题目链接]Co-prime [题目类型]容斥 &题意: 求(a,b)区间内,与n互质的数的个数. \(a,b\leq 10^{15}\) ...

  5. HDU 4135 容斥原理

    思路: 直接容斥 //By SiriusRen #include <cstdio> using namespace std; #define int long long ; int cas ...

  6. HDU 4135

    http://acm.hdu.edu.cn/showproblem.php?pid=4135 求[A,B]内与N互素的数字个数 首先对N分解质因数,对于一个质因数,1-n与它不互素的数字个数是n/(这 ...

  7. 容斥 - HDU 4135 Co-prime

    Co-prime Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=4135 推荐: 容斥原理 Mean: 给你一个区间[l,r]和一 ...

  8. hdu 5901 count prime & code vs 3223 素数密度

    hdu5901题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5901 code vs 3223题目链接:http://codevs.cn/problem ...

  9. hdu 4135 Co-prime(容斥)

    Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

随机推荐

  1. 10. Condition 控制线程通信

    1. 是什么 ? 2. 示例 package com.gf.demo09; import java.util.concurrent.locks.Condition; import java.util. ...

  2. eclipse编写js代码没有提示

    安装插件 点击Help,选择Eclipse Marketplace... 搜索js,安装AngularJS Eclipse 重启eclipse,右键项目,选择Configure(配置),选择Conve ...

  3. 【20190226】JavaScript-知识点记录:dom0级事件,dom2级事件

    DOM0级事件处理程序: 通过将元素的事件处理程序属性(如onclick)的值设置为一个函数来指定事件处理程序的方法称为DOM0级方法,它被认为是元素的方法,这时候的事件处理程序是在元素的作用域中运行 ...

  4. 【代码笔记】Web-JavaScript-JavaScript switch语句

    一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  5. Floyd算法_MATLAB

    %求图中任意两点之间的最短距离与最短路径 %floyd算法通用程序,输入a为赋权邻接矩阵 %输出为距离矩阵D,和最短路径矩阵path function D=floyd(a) n=size(a,);%行 ...

  6. 关于js脚本宿主对脚本代码的绑定

    脚本代码绑定,Unity3D是这样做的.为了体现Unity3D的基于对象设计,Unity3D可以为每个对象绑定多个脚本文件,可以是js,可以是cs,也可以是boo. threejs/editor也有与 ...

  7. MyBatis(傻瓜式)框架

    log4j的配置文件: 使用一个log4j.properties的配置文件,会设定log4j的设置信息,例如日志级别,日志输出方式,日志格式等等: # Set root category priori ...

  8. jQuery如何判断input元素是否获得焦点(点击编辑时)

    问题提出 如果你要判断input元素是否获得焦点,或者是否处在活动编辑状态,使用jQuery的 hasFocus() 方法或 is(':focus') 方法貌似都无效!搜索网上给出的办法,几乎净是采用 ...

  9. ERP承接新后台优惠规则问题

    一.后台在哪配置优惠规则? 1.设置优惠时间段: 2.添加优惠活动: 关于自动和手动: 自动:创建后,ERP同步数据后即生效.     点餐,活动会自动生效,自动计算金额. 手动:创建后,ERP需要手 ...

  10. Cygwin下编译的程序不使用Cygwin.dll即可运行的命令 及常用命令简介

    cc -mno-cygwin foo.c 1.$ ps PS的相关用法: QuoteUsage ps [-aefl] [-u uid]-f = show process uids, ppids-l = ...