其他分享
首页 > 其他分享> > LightOJ - 1370 - Bi-shoe and Phi-shoe(欧拉函数)

LightOJ - 1370 - Bi-shoe and Phi-shoe(欧拉函数)

作者:互联网

链接:

https://vjudge.net/problem/LightOJ-1370

题意:

Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe is a very popular coach for his success. He needs some bamboos for his students, so he asked his assistant Bi-Shoe to go to the market and buy them. Plenty of Bamboos of all possible integer lengths (yes!) are available in the market. According to Xzhila tradition,

Score of a bamboo = Φ (bamboo's length)

(Xzhilans are really fond of number theory). For your information, Φ (n) = numbers less than n which are relatively prime (having no common divisor other than 1) to n. So, score of a bamboo of length 9 is 6 as 1, 2, 4, 5, 7, 8 are relatively prime to 9.

The assistant Bi-shoe has to buy one bamboo for each student. As a twist, each pole-vault student of Phi-shoe has a lucky number. Bi-shoe wants to buy bamboos such that each of them gets a bamboo with a score greater than or equal to his/her lucky number. Bi-shoe wants to minimize the total amount of money spent for buying the bamboos. One unit of bamboo costs 1 Xukha. Help him.

思路:

欧拉函数打表,从小到大挨个尝试花费。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<vector>
#include<map>

using namespace std;
typedef long long LL;
const int INF = 1e9;

const int MAXN = 1e6+10;
const int MOD = 1e9+7;

struct Phi
{
    int v, p;
}phi[MAXN];

int prime[MAXN];
int Cost[MAXN], a[MAXN];
int tot, n;

void Euler()
{
    tot = 0;
    for (int i = 1;i < MAXN;i++)
        phi[i].v = i, phi[i].p = 0;
    phi[1].p = 1;
    for (int i = 2;i < MAXN;i++)
    {
        if (!phi[i].p)
        {
            phi[i].p = i-1;
            prime[tot++] = i;
        }
        for (int j = 0;j < tot && 1LL*i*prime[j] < MAXN;j++)
        {
            if (i%prime[j])
                phi[i*prime[j]].p = phi[i].p*(prime[j]-1);
            else
            {
                phi[i*prime[j]].p = phi[i].p*prime[j];
                break;
            }
        }
    }
}

bool cmp(Phi a, Phi b)
{
    if (a.p != b.p)
        return a.p < b.p;
    return a.v < b.v;
}

int main()
{
    Euler();
    /*
    sort(phi+1, phi+MAXN, cmp);
    int pos = 2;
    for (int i = 1;i < MAXN;i++)
    {
        while(phi[pos].p < i)
            pos++;
        Cost[i] = phi[pos].v;
    }
    */
    int t, cnt = 0;
    scanf("%d", &t);
    while(t--)
    {
        printf("Case %d:", ++cnt);
        scanf("%d", &n);
        for (int i = 1;i <= n;i++)
            scanf("%d", &a[i]);
        sort(a+1, a+1+n);
        LL sum = 0;
        for (int i = 2, j = 1;i < MAXN && j <= n;i++)
        {
            while(phi[i].p >= a[j] && j <= n)
                sum += i, j++;
        }
        printf(" %lld Xukha\n", sum);
    }
    
    return 0;
}

标签:prime,Phi,int,LightOJ,phi,MAXN,shoe,include
来源: https://www.cnblogs.com/YDDDD/p/11846242.html