HDU 4135 Co-prime(容斥原理+分解質因數)
Co-prime
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5239 Accepted Submission(s): 2093
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
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
題意:
求出a-b中與n互質的數量。
POINT:
正難則反,求出a-b中不與n互質的數量。
先把n分解質因數,然後求出a-b中與這些質因數成倍數關係的數量。利用容斥原理。
但是我直接在b-(a-1)中找,無限WA.網上的題解是找出(1-b)的數量,在減去(1-a-1)的數量。不知道我這個為什麼不行,也比對很多資料,都是一樣的,除了A>B的情況,但是題目已經給出A<=B了呀。很奇怪。
void dfs(int ceng,int nowi,LL now)
{
if(ceng==cnt+1) return;
for(int i=nowi+1;i<=cnt;i++)
{
LL d=gcd(now,p[i]);
LL sum=now/d*p[i];
// if(sum>b) return;
if(ceng&1)
{
ans+=b/sum-(a-1)/sum;
}
else
ans-=b/sum-(a-1)/sum;
dfs(ceng+1,i,sum);
}
}
沒什麼區別,就是找出(1-b)的數量,在減去(1-a-1)的數量。
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define LL long long
LL a,b,m;
LL p[1000000];
int cnt;
LL ans;
LL anss;
LL gcd(LL A,LL B)
{
if(A<B) swap(A, B);
return B==0?A:gcd(B,A%B);
}
void prime()
{
cnt=0;
memset(p,0,sizeof p);
for(LL i=2;i*i<=m;i++)
{
if(m%i==0)
{
p[++cnt]=i;
}
while(m%i==0) m/=i;
}
if(m>1)
p[++cnt]=m;
}
void dfs(int ceng,int nowi,LL now)
{
if(ceng==cnt+1) return;
for(int i=nowi+1;i<=cnt;i++)
{
LL d=gcd(now,p[i]);
LL sum=now/d*p[i];
// if(sum>b) return;
if(ceng&1)
{
ans+=b/sum;
}
else
ans-=b/sum;
dfs(ceng+1,i,sum);
}
}
void ddfs(int ceng,int nowi,LL now)
{
if(ceng==cnt+1) return;
for(int i=nowi+1;i<=cnt;i++)
{
LL d=gcd(now,p[i]);
LL sum=now/d*p[i];
// if(sum>b) return;
if(ceng&1)
{
anss+=(a-1)/sum;
}
else
anss-=(a-1)/sum;
ddfs(ceng+1,i,sum);
}
}
int main()
{
int T;
scanf("%d",&T);
int k=0;
while(T--)
{
ans=0;
anss=0;
scanf("%lld %lld %lld",&a,&b,&m);
prime();
dfs(1,0,1);
ddfs(1,0,1);
ans=(b-a+1)-(ans-anss);
printf("Case #%d: %lld\n",++k,ans);
}
}
相關文章
- HDU 4135——Co-prime(容斥原理&&二進位制列舉)
- HDU 4059 The Boss on Mars ( 容斥原理)
- HDU4390Number Sequence(容斥原理)
- HDU4407Sum ( 容斥原理)
- 階乘質因數分解
- HDU 5468 Puzzled Elena(DFS序+容斥原理)
- HDU 1299 Diophantus of Alexandria (公式變形 分解質因數)公式
- HDU2841 Visible Trees (容斥原理)
- HDU 1695-GCD(容斥原理+尤拉函式)GC函式
- 容斥原理——數學知識
- #19. 計數(容斥原理)
- 容斥原理
- POJ 2773 Happy 2006 (分解質因數+容斥+二分 或 歐幾里德演算法應用)APP演算法
- 【模板】容斥原理
- 容斥原理講解
- 藍橋杯題庫 BASIC-16 分解質因數
- 遊戲裡面的容斥原理遊戲
- bzoj 2655: calc [容斥原理 伯努利數]
- POJ1142 Smith Numbers(數論,分治+暴力,質因數分解)MIT
- 容斥原理學習筆記筆記
- HDU 5072 Coprime (單色三角形問題+容斥原理)
- python將輸入的一個正整數分解質因數(map)Python
- 質數判斷、質因子分解、質數篩
- HDU 1695 GCD (容斥 + 莫比烏斯反演)GC
- 大質數分解模板
- Java分解質因數,如輸入8,輸出8=2*2*2Java
- HDU 4497GCD and LCM(素數分解)GC
- 有標號DAG計數 [容斥原理 子集反演 組合數學 fft]FFT
- 容斥
- P1075 [NOIP2012 普及組] 質因數分解
- HDU44979 GCD and LCM (素因子分解+計數)GC
- HDU 5212 Code (容斥 莫比烏斯反演基礎題)
- 反射容斥反射
- 求最大質因數
- 【快速因數分解】Pollard's Rho 演算法演算法
- Codeforces 893E Counting Arrays:dp + 線性篩 + 分解質因數 + 組合數結論
- bzoj 3812: 主旋律 [容斥原理 狀壓DP]
- bzoj1853: [Scoi2010]幸運數字(容斥原理)