HDOJ 4135 Co-prime 容斥原理

求得n的因数后,简单容斥

Co-prime

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1798    Accepted Submission(s): 685

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}. 
/* ***********************************************
Author        :CKboss
Created Time  :2015年03月27日 星期五 17时52分24秒
File Name     :HDOJ4135.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef long long int LL;

LL A,B,N;
vector<LL> pr;

void getPr()
{
	LL TN=N,now=2;
	pr.clear();

	while(TN!=1)
	{
		if(TN%now==0)
		{
			pr.push_back(now);
			while(TN%now==0) TN/=now;
		}
		now++;
		if(now*now>TN) break;
	}
	if(TN!=1) pr.push_back(TN);
}

LL RET,RET1,RET2;

void dfs(int x,LL n,int mark)
{
	for(int i=x,sz=pr.size();i<sz;i++)
	{
		RET+=mark*(n/pr[i]);
		dfs(i+1,n/pr[i],-mark);
	}
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

	int T_T,cas=1;
	cin>>T_T;
	while(T_T--)
	{
		cin>>A>>B>>N;
		getPr();
		A--; RET=A; dfs(0,A,-1); RET1=RET;
		RET=B; dfs(0,B,-1); RET2=RET;
		cout<<"Case #"<<cas++<<": "<<RET2-RET1<<endl;
	}

    return 0;
}
时间: 2024-10-29 19:06:30

HDOJ 4135 Co-prime 容斥原理的相关文章

HDU 4135 Co-prime(容斥原理 + 基础数论)

传送门 Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3695    Accepted Submission(s): 1461 Problem Description Given a number N, you are asked to count the number of integers between A an

hdu 4135 Co-prime【容斥原理】

Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1668    Accepted Submission(s): 636 Problem Description Given a number N, you are asked to count the number of integers between A and B

HDOJ 5297 Y sequence 容斥原理

Y sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1174    Accepted Submission(s): 260 Problem Description Yellowstar likes integers so much that he listed all positive integers in asce

hdoj 2841Visible Trees(容斥原理)

题意:m*n的格点上有m*n棵树,从(0,0)点可以看到多少棵树 假设x与1到m有num[x]个数互质,即1到m中与x有非1的公约数的个数 为m-num[x], 最后结果就是n*m-(num[1]+...num[n]) #include<stdio.h> #include<vector> #include<string.h> #include<iostream> using namespace std; const int N=100000+10; vect

[容斥原理] hdu 4135 Co-prime

题目链接: 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 a

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

题目链接Hdu4135 Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1412    Accepted Submission(s): 531 Problem Description Given a number N, you are asked to count the number of integers betwe

hdu 4135 Co-prime +hdu 2841 Visible Trees(容斥原理)

Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2263    Accepted Submission(s): 847 Problem Description Given a number N, you are asked to count the number of integers between A and B

hdoj 5072 Coprime 【想法+容斥原理】

题目:hdoj 5072 Coprime 题意:给出n个数,然后让你从其中任意选出三个数满足其中三个数都互质或者都不互质,让你求满足这样选择条件的选择种数. 分析:首先我们从反面考虑这个问题,一个满足条件的选择{ a , b , c },题目要求[(a, b) = (b, c) = (a, c) = 1] or [(a, b) ≠ 1 and (a, c) ≠ 1 and (b, c) ≠ 1],其中(a,b)是a和b的最大公约数. 那么其反面就是,[(a, b) = (b, c) =1 and

hdoj 1016 Prime Ring Problem 【DFS】

策略如题 链接 http://acm.hdu.edu.cn/showproblem.php?pid=1016 代码: #include<stdio.h> #include<string.h> int prime[25] = {1, 1}, n, vis[25]; //vis作用:标记是否用过 int a[25]; void f() //找出来前20的素数 判定为0 { for(int i = 2; i <= 24; i ++){ if(prime[i] == 0) for(i