Counting fractions
TimeLimit: 2000MS MemoryLimit: 65536 Kb
Totalsubmit: 39 Accepted: 6
Description
Consider the fraction, n/d, where n and d are positive integers. If n <= d and HCF(n,d)=1, it is called a reduced proper fraction.
If we list the set of reduced proper fractions for d <= 8 in ascending order of size, we get:
1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 1/2, 4/7, 3/5, 5/8, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 7/8
It can be seen that there are 21 elements in this set.
How many elements would be contained in the set of reduced proper fractions for d <= x?
Input
The input will consist of a series of x, 1 < x < 1000001.
Output
For each input integer x, you should output the number of elements contained in the set of reduced proper fractions for d <= x in one line.
Sample Input
3
8
Sample Output
3
21
比赛的时候思维进入怪圈,改了半天都改不过 数论知识点匮乏啊
首先贴一个定理
对正整数n,欧拉函数是小于n的正整数中与n互质的数的数目(φ(1)=1)。此函数以其首名研究者欧拉命名(Euler'so totient function),它又称为Euler's totient function、φ函数、欧拉商数等
#include <iostream>
#include <math.h>
#include <cstring>
#include <cstdio>
#include <map>
#include <vector>
using namespace std;
#define maxn 1011000
typedef long long ll;
const int inf = 1e9+7;
int prime[maxn]; //素数表
ll ans[maxn];
void getprime(){
prime[2]=0;
for(int i = 2 ; i<maxn ; i++)
ans[i]=i;
for(int i = 2 ; i<maxn ; i++){
if(!prime[i]){
p[pnum++]=i;
ans[i]=i-1;
for(int j = i*2 ; j<maxn ; j+=i){
prime[j]=1;
ans[j]=ans[j]/i*(i-1);
}
}
}
}
int main(){
getprime();
int t;
scanf("%d",&t);
while(t--){
int n;
ll sum=0;
scanf("%d",&n);
for(int i = 2 ; i<=n ; i++){
sum+=ans[i];
sum=sum%inf;
}
printf("%lld\n",sum);
}
}