题目1:
来源:Prime Independence LightOJ - 1356
**分析:**欧拉线性筛模板,注意此题不需要二分查找,对于输入的x,可以从x+1开始找phi>=x的第一个数,貌似不会t
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
const ll mod = 1e9 + 7;
const int maxn = 3e6 + 10;
const int inf = 0x3f3f3f3f;
ll phi[maxn];
void init()
{
phi[1] = 1;
for(int i = 2; i <= 3e6; i++)
{
if(!phi[i])
{
for(int j = i; j <= 3e6; j += i)
{
if(!phi[j]) phi[j] = j;
phi[j] = phi[j] / i * (i - 1);
}
}
}
}
int main()
{
init();
int t; cin >> t;
int cases = 0;
while(t--)
{
int n; cin >> n;
ll sum = 0;
while(n--)
{
ll x; cin >> x;
int pos = x + 1;
while(phi[pos] < x) pos++;
sum += pos;
}
printf("Case %d: %lld Xukha\n",++cases,sum);
}
}
题目2:
**分析:**欧拉线性筛模板,打表前缀和
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 10;
const int inf = 0x3f3f3f3f;
int phi[maxn];
ll sum[maxn];
void init()
{
phi[1] = 0;
for(int i = 2; i < maxn; i++)
if(!phi[i])
{
for(int j = i; j < maxn; j += i)
{
if(!phi[j]) phi[j] = j;
phi[j] = phi[j] / i * (i - 1);
}
}
for(int i = 1; i < maxn; i++) sum[i] = sum[i - 1] + phi[i];
}
int main()
{
ll n;
init();
while(cin >> n && n)
{
cout << sum[n] << endl;
}
}