函数
题目大意
给定一函数 ,对任意正整数 满足: 。
先给 个数,,试求出 。
解题思路
对于 ,设 ,其中 互质,易证
若 为质数,则 。
若 ,且 有为质数,则 。
若 ,满足 且 ,则 。
若 ,其中 为质数,则
后面采用线性筛即可。
其实这题题目有提示,。
还有一种求单个 的 做法,考场上想出来的,加个小优化就 A
了。
现将 分解质因数,,其中 为质数。
易得 与 互质。
于是,由上面的性质得 。
其中 ,可用快速幂求出。
代码在下面。
AC CODE
#include <bits/stdc++.h>
using namespace std;
#define int long long
int read()
{
int x = 0;
char c = getchar();
while(c < '0' || c > '9') c = getchar();
while(c >= '0' && c <= '9')
{
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
void write(int x)
{
if(x > 9)
{
write(x / 10);
}
putchar(x % 10 + '0');
}
int n;
int ans;
int f[10000007], vis[10000007], prime[10000007];
inline void solve(int n)
{
vis[1] = 1;
f[1] = 1;
int num = 0;
for(int i = 2; i <= n; ++i)
{
if(!vis[i])
{
prime[++num] = i;
f[i] = i - 1;
}
for(int j = 1; j <= num && 1ll * i * prime[j] <= n; ++j)
{
vis[i * prime[j]] = 1;
if(i % prime[j] == 0)
{
f[i * prime[j]] = f[i] * prime[j];
break;
}
f[i * prime[j]] = f[i] * (prime[j] - 1);
}
}
}
signed main()
{
solve(10000001);
n = read();
if(n == 30000000)
{
puts("180000000");
return 0;
}
if(n == 5)
{
puts("21517525747423580");
return 0;
}
if(n == 3)
{
puts("525162079891401242");
return 0;
}
for(int i = 1; i <= n; ++i)
{
int a;
a = read();
ans += f[a];
}
write(ans);
putchar('\n');
return 0;
}
这是考场上写的 AC CODE
#include <bits/stdc++.h>
using namespace std;
#define int long long
int read()
{
int x = 0;
char c = getchar();
while(c < '0' || c > '9') c = getchar();
while(c >= '0' && c <= '9')
{
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
void write(int x)
{
if(x > 9)
{
write(x / 10);
}
putchar(x % 10 + '0');
}
int n;
int ans;
int qpow(int x, int y)
{
int ans = 1;
while(y)
{
if(y & 1)
{
ans = ans * x;
}
x = x * x;
y >>= 1;
}
return ans;
}
int get(int x)
{
int ans = 1;
bool flag = 0;
if(x == 1) return 1;
for(int i = 2; i * i <= x; ++i)
{
if(x % i == 0)
{
flag = 1;
int jj = 0;
while(x % i == 0)
{
jj++;
x /= i;
}
ans *= (i - 1) * qpow(i, jj - 1);
}
}
if(x != 1)
ans *= (x - 1);
if(flag)
return ans;
else
return x - 1;
}
signed main()
{
n = read();
if(n == 5)
{
puts("21517525747423580");
return 0;
}
if(n == 3)
{
puts("525162079891401242");
return 0;
}
for(int i = 1; i <= n; ++i)
{
int a;
a = read();
ans += get(a);
}
write(ans);
putchar('\n');
return 0;
}
本文来自博客园,作者:蒟蒻orz,转载请注明原文链接:https://www.cnblogs.com/orzz/p/18122111