Problem Description
Lemon wants to be a hero since he was a child. Recently he is reading a book called “Where Is Hero From” written by ZTY. After reading the book, Lemon sends a letter to ZTY. Soon he recieves a reply.
Dear Lemon,
It is my way of success. Please caculate the algorithm, and secret is behind the answer. The algorithm follows:
Int Answer(Int n)
{
.......Count = 0;
.......For (I = 1; I <= n; I++)
.......{
..............If (LCM(I, n) < n * I)
....................Count++;
.......}
.......Return Count;
}
The LCM(m, n) is the lowest common multiple of m and n.
It is easy for you, isn’t it.
Please hurry up!
ZTY
What a good chance to be a hero. Lemon can not wait any longer. Please help Lemon get the answer as soon as possible.
Input
First line contains an integer T(1 <= T <= 1000000) indicates the number of test case. Then T line follows, each line contains an integer n (1 <= n <= 2000000).
Output
For each data print one line, the Answer(n).
Sample Input
1 1
Sample Output
0
思路:由题意可知:
<1>只有当lcm(i,n)<i*n的时候cnt才会进行累加。
<2>分析可知,只有当i和n互素的时候lcm(i,n)==i*n。
所以该题就转换成了求小于n的数中有几个数与n的最大公约数为1
直接用欧拉函数预选筛求解即可。
AC代码如下:
#include<iostream>
using namespace std;
#define MAX 2000005
#define ll long long
int prime[MAX];
ll eula[MAX];
void eulaPrime() {
int i, j;
for (i = 1; i < MAX; i++) {
eula[i] = i;
}
for (i = 2; i < MAX; i++) {
if (!prime[i]) {
eula[i] = i - 1;
for (j = i + i; j < MAX; j += i) {
prime[j] = 1;
eula[j] /= i;
eula[j] *= (i - 1);
}
}
}
}
int main() {
eulaPrime();
int t;
cin >> t;
while (t--) {
int k;
cin >> k;
cout << k - eula[k] << endl;
}
return 0;
}