zoukankan      html  css  js  c++  java
  • 【BJOI】【欧拉函数】Euler

    题目大意

    给你一个数y要求输出一个满足φ(x)=y的最小的x,有T组询问。

    y<=1012
    1<=T<=2

    解题思路

    求φ(x)时有一个众所周知的公式是φ(x)=x∗Πpi−1pi(pi为x的所有质因子),也就是说这我们可以得到等式
    φ(x)=y
    x∗Πpi−1pi=y (等式一)
    x=y∗Πpipi−1 (等式二)
    由等式一可知,因为pi都是x的质因子,所以我们可以把下面的pi都与x约掉,然后我们就发现x的质因子要不就是y的质因子,要不就是y的约数加一。而由等式二可知我们只需要找出Πpipi−1最小的符合要求的解就行了。不难发现符合要求的x可能的质因子只有1000多个,那么我们只需暴力判断每个质数选不选再加个全局最优解的优化就可以了。

    程序

    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    
    using namespace std;
    
    const int MAXN = 7e3;
    typedef long long LL;
    
    LL N, Ans, Fac[MAXN], Pri[MAXN];
    int tot, cnt;
    
    void Prepare(LL N) {
        for (LL i = 1; i * i < N; i ++) {
            if (N % i != 0) continue;
            Fac[++ tot] = i;
            if (1ll * i * i == N) continue;
            Fac[++ tot] = N / i;
        }
    }
    
    bool IsPrime(LL Now) {
        for (LL i = 2; i * i <= Now; i ++) 
            if (Now % i == 0) return 0;
        return 1;
    }
    
    void GetPri() {
        for (int i = 1; i <= tot; i ++) 
            if (IsPrime(Fac[i] + 1)) Pri[++ cnt] = Fac[i] + 1;
    }
    
    bool cmp(LL a, LL b) { return a > b;}
    
    bool Check(LL Num, LL Now) {
        if (Now == 1) return 1;
        if (Now % Pri[Num] == 0) return Check(Num, Now / Pri[Num]);
        return 0;
    }
    
    void Dfs(int Num, LL Now, LL S) {
        if (Num > cnt || S > Ans || Now == 1) return;
        if (Now % (Pri[Num] - 1) != 0) {
            Dfs(Num + 1, Now, S);
            return;
        }
        if (Now % (Pri[Num] - 1) == 0 && Check(Num, Now / (Pri[Num] - 1))) 
            Ans = min(Ans, S / (Pri[Num] - 1) * Pri[Num]);
        LL Ord = (Pri[Num] - 1);
        for (; Now % Ord == 0; Ord *= Pri[Num]) 
            Dfs(Num + 1, Now / Ord, S / (Pri[Num] - 1) * Pri[Num]);
        Dfs(Num + 1, Now, S);
    }
    
    void Solve(LL N) {
        Ans = (N == 1) ? N : N * 10;
        tot = cnt = 0;
        Prepare(N);
        GetPri();
        sort(Pri + 1, Pri + 1 + cnt, cmp);
        Dfs(1, N, N);
    }
    
    int main () {
        int Test;
        scanf("%d", &Test);
        for (; Test; Test --) {
            scanf("%lld", &N);
            Solve(N);
            printf("%lld
    ", Ans);
        }
    }
  • 相关阅读:
    P2764 最小路径覆盖问题
    P2598 [ZJOI2009]狼和羊的故事
    P2472 [SCOI2007]蜥蜴
    P2057 [SHOI2007]善意的投票 / [JLOI2010]冠军调查
    P2050 [NOI2012]美食节
    P1345 [USACO5.4]奶牛的电信Telecowmunication
    P1251 餐巾计划问题
    hihocoder 1584 Bounce
    [CF132C] Logo Turtle
    CF1110D Jongmah
  • 原文地址:https://www.cnblogs.com/leotan0321/p/6081391.html
Copyright © 2011-2022 走看看