[HDU] [1787] GCD Again

Problem Description

Loading...

Problem Analysis

An application of Euler’s Totient Function
Except that Euler’s Totient Function computes
the count of numbers in 1~N that are coprime with N
while this problem asks for the count of those that are not coprime

Actually, you just need to do a subtraction

Note that because of the data range and memory limit
this problem cannot be solved with a sieve
so just use the formula directly

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
#include<string.h>
int f(int n) {
	int res=n,a=n;
	for(int i=2; i*i<=a; i++) {
		if(a%i==0) {
			res=res/i*(i-1);
			while(a%i==0)
				a/=i;
		}
	}
	if(a>1)
		res=res/a*(a-1);
	return res;
}
int main() {
	int n;
	while(scanf("%d",&n),n)
		printf("%lld\n",n-f(n)-1);
}

Problem link:【杭电】[1787]GCD Again

Loading...