【数论】Bi-shoe and Phi-shoe

开了数论的坑,目标是无压力写出铜牌+水平的数论题,更大的锅扔给队友吧 (ಥ﹏ಥ)

本题要用到的欧拉函数:[http://www.cnblogs.com/linyujun/p/5194170.html]

题 意:主要是给定n,找出欧拉函数大于等于n的最小的数。要求常数或者log时间。
解:首先,按欧拉函数值从小到大为第一次序,原值为第二次序排序。由于φ(n)不一定随n增大而增大,也就是说,求m的最小目标值n ,(φ(n) = m) 可能不如大于m的数的目标值小。所以排好序之后选出每个m的最小目标值(暂时),然后把目标值数组从后往前更新,目标值 = min(当前目标值,大于m的数的目标值中最小的)。

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<functional>
#include<sstream>
using namespace std;
typedef long long LL;
const int maxn = 1000010;

int n, k, c;
int phi[maxn];

void Euler(){
    phi[1] = 1;
    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);
            }
        }
    }
}

struct node{
    int id, eu;
    node(int i = 0, int e = 0){
        id = i; eu = e;
    }
};

bool cmp(const node &n1, const node &n2){
    if (n1.eu == n2.eu) return n1.id < n2.id;
    return n1.eu < n2.eu;
}

node p[maxn];
int rk[maxn];
int a[10010];

int main(){
    Euler();
    phi[1] = 0;
    for (int i = 1; i < maxn; i++){
        p[i].id = i;
        p[i].eu = phi[i];
        rk[i] = 0x3fffffff;
    }
    sort(p + 1, p + maxn, cmp);
    int cnt = -1;
    for (int i = 1; i < maxn; i++){
        if (p[i].eu > cnt){
            cnt = p[i].eu;
            rk[cnt] = p[i].id;
        }
        else continue;
    }
    for (int i = maxn-2; i >= 1; i--){
        rk[i] = min(rk[i], rk[i + 1]);
    }
    int t; cin >> t;
    int cas = 1;
    while (t--){
        int n; scanf("%d", &n);
        LL ans = 0;
        for (int i = 0; i < n; i++) {
            scanf("%d", &a[i]);
            ans += rk[a[i]];
        }
        printf("Case %d: %lld Xukha\n", cas++,ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值