您的位置:首页 > 其它

【欧拉函数】【数论】17.6.2 数学题 题解

2017-06-02 19:28 183 查看


#include <iostream>
#include <cstdio>
#include <cstring>
#define clr(a) memset(a, 0, sizeof(a))
#define LL long long
#ifdef win32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif

using namespace std;

const int MAXN = 10000005;
int t,n,ptot;
int primes[MAXN];
bool isnot[MAXN];

template <class T> inline void read(T &x) {
int flag = 1; x = 0;
char ch = getchar();
while(ch <  '0' || ch >  '9') { if(ch == '-')  flag = -1; ch = getchar(); }
while(ch >= '0' && ch <= '9') { x = (x<<1)+(x<<3)+ch-'0'; ch = getchar(); }
x *= flag;
}

LL gcd(LL a, LL b) { return b == 0 ? a : gcd(b, a%b); }

inline void linear_sieve(int n) {
isnot[1] = true;
for(register int i = 2; i <= n; i++) {
if(!isnot[i]) primes[ptot++] = i;
for(register int t = 0; t < ptot; t++) {
int j = primes[t]*i;
if(j > n) break;
isnot[j] = true;
if(!(i%primes[t])) break;
}
}
}

inline LL Calc_Fn(int N) {
LL ans = 0;
for(LL i = 1; i <= N; i++) ans += N/gcd(i,N);
return ans;
}

int main() {
freopen("maths.in","r",stdin);
freopen("maths.out","w",stdout);
linear_sieve(10000000);
read(t);
while(t--) {
LL ans = 0;
read(n);
printf(AUTO"\n", Calc_Fn(n));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: