欧拉函数相关(不定期更新)

题目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:

来源:Farey Sequence POJ - 2478

**分析:**欧拉线性筛模板,打表前缀和

代码:

#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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值