码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-11-13 01:05:17      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:typedef   oid   for   money   代码   student   pop   bool   int   

链接:

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;
}

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

标签:typedef   oid   for   money   代码   student   pop   bool   int   

原文地址:https://www.cnblogs.com/YDDDD/p/11846242.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!