hdu 2588 GCD(歐拉函數)+ bzoj 2818 gcd(歐拉篩)

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2588GCD

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

Problem Description
The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the largest divisor common to a and b,For example,(1,2)=1,(12,18)=6.
(a,b) can be easily found by the Euclidean algorithm. Now Carp is considering a little more difficult problem:
Given integers N and M, how many integer X satisfies 1<=X<=N and (X,N)>=M.

Input
The first line of input is an integer T(T<=100) representing the number of test cases. The following T lines each contains two numbers N and M (2<=N<=1000000000, 1<=M<=N), representing a test case.

Output
For each test case,output the answer on a single line.

Sample Input

3
1 1
10 2
10000 72

Sample Output

1
6
260

Source
ECJTU 2009 Spring Contest

分析:

gcd(x,n)>=m
令sa=x,sb=n ,且gcd(a,b)=1
x<=n,a<=b
從而求出φ(b)\varphi(b),對於s,枚舉n的因子

代碼:
#include <bits/stdc++.h>

using namespace std;

#define ll long long 
#define p pair<int,int>
#define fr first
#define sc second
const int N=2e6+5;


ll get_eular(ll n)
{
	ll ans=n;
	ll tmp=sqrt(n+0.5);
	for(int i=2;i<=tmp;i++) if(n%i==0)
	{
		ans=ans/i*(i-1);
		while(n%i==0) n/=i;
	}
	if(n>1) 
		ans=ans/n*(n-1);
	return ans;
}


int main()
{
	int t, n , m;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		ll ans=0;
		for(int i = 1; i*i<=n;i++) if(n%i==0)
		{
			if(i>=m) ans+=get_eular(n/i);
			if(n/i>=m&&n/i!=i) 
				ans+=get_eular(i);
		}
		printf("%lld\n",ans);
	}
	return 0;
}

題目鏈接:https://www.lydsy.com/JudgeOnline/problem.php?id=28182818: Gcd

Time Limit: 10 Sec Memory Limit: 256 MB
Submit: 10589 Solved: 4663
[Submit][Status][Discuss]
Description

給定整數N,求1<=x,y<=N且Gcd(x,y)爲素數的
數對(x,y)有多少對.

Input

一個整數N
Output

如題
Sample Input
4

Sample Output
4
HINT

hint

對於樣例(2,2),(2,4),(3,3),(4,2)

1<=N<=10^7

Source

湖北省隊互測

分析:

此題與南京網絡賽J.sum有點類似,不過要簡單些。
先通過歐拉篩,然後求下和

gcd(x,y)=素數,1<=x,y<=n
令d是在[1,n]之間的素數
令a*d=x,b*d=y,且gcd(a,b)=1;a,b∈[1,n/d],1<=a,b<=n/d
等價於2倍的滿足條件的a,b個數 (a,b∈[1,n/d],gcd(a,b)=1,a<=b)減1(減1是數對(1,1)被重複計算了一次)
問題轉化成求∑φ(i) (i=1..n/d),這個可以在歐拉篩中加個數組,求個和就o了

代碼:

#include <bits/stdc++.h>

using namespace std;

#define ll long long 
#define p pair<int,int>
#define fr first
#define sc second
const int N=1e7+5;
int prime[N+1];
int phi[N+1];
ll sum[N+1];

void get_eular()
{
	memset(prime,0,sizeof(prime));
	phi[1]=1;
	sum[1]=1;
	for(int i=2;i<=N;i++)
	{
		if(!prime[i]) 
		{
			prime[++prime[0]]=i;
			phi[i]=i-1;
		}
		for(int j=1;j<=prime[0]&&i*prime[j]<=N;j++)
		{
			prime[i*prime[j]]=1;
			if(i%prime[j]==0)
			{
				phi[i*prime[j]]=prime[j]*phi[i];
				break;
			}
			phi[i*prime[j]]=(prime[j]-1)*phi[i];
		}
		sum[i]=sum[i-1]+phi[i];
		
	}
}




int main()
{
	get_eular();
	int t, n , m;
	scanf("%d",&n);
	ll ans=0;
	for(int i=1;i<=prime[0]&&prime[i]<=n;i++) 
	{
		ans+=2*sum[n/prime[i]]-1;
	}
	printf("%lld\n",ans);
	
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章