Count of integers

3.7

18 votes
Algorithms, Euler's totient function, Greatest common divisor, Math, Number Theory, Sieve
Problem

You are given a positive integer n, for Q queries, You are required to determine the number of integers k (1kn) such that gcd(k,n)!=1 and gcd(k,n)!=k.

Input format

  • First line: Integer Q denoting the number of queries
  • Next Q lines: Integer n

Output format

Print Q lines each containing an integer that denotes the answer to that specific query.

Constraints

1Q105

1n106

Sample Input
1
10
Sample Output
3
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For c = {1,3,7,9} gcd(c,10) = 1

For c = {2,5,10} gcd(c,10) = c

gcd(4,10) = 2

gcd(6,10) = 2

gcd(8,10) = 2

So, answer is 3.

Editor Image

?