• P2155 [SDOI2008]沙拉公主的困惑


    (color{#0066ff}{ 题目描述 })

    大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为1到N的阶乘,但是,政府只发行编号与M!互质的钞票。房地产第一大户沙拉公主决定预测一下大富翁国现在所有真钞票的数量。现在,请你帮助沙拉公主解决这个问题,由于可能张数非常大,你只需计算出对R取模后的答案即可。R是一个质数。

    (color{#0066ff}{输入格式})

    第一行为两个整数T,R。R<=(10^9+10),T<=10000,表示该组中测试数据数目,R为模 后面T行,每行一对整数N,M,见题目描述 m<=n

    (color{#0066ff}{输出格式})

    共T行,对于每一对N,M,输出1至N!中与M!素质的数的数量对R取模后的值

    (color{#0066ff}{输入样例})

    1 11
    4 2
    

    (color{#0066ff}{输出样例})

    1
    

    (color{#0066ff}{数据范围与提示})

    对于100%的数据,1 < = N , M < = 10000000

    (color{#0066ff}{ 题解 })

    这东西看起来可能有点不好想

    先考虑([1,m!])的贡献,显然是(varphi(m!))

    这好像不太好求。。

    考虑定义

    (egin{aligned}varphi(n)=n*prod_{i=1}^kfrac{p_i-1}{p_i}end{aligned})

    好像(m!)的质因子就是(leq m)的所有质数啊

    这样看来好像简单了不少

    考虑在([m!+1,n!])的部分

    因为a,b互质,a+b和b一定互质(别问我为啥,gcd的东西qwq)

    而且(n!)一定是(m!)的倍数,那么可以分段

    每一段都是(varphi(m!))

    (ans=frac{n!}{m!} varphi(m!))

    弄个前缀乘积就行了(记录(varphi(i!))的ans,具有前缀性质)

    #include<bits/stdc++.h>
    #define LL long long
    LL in() {
        char ch; LL x = 0, f = 1;
        while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
        for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
        return x * f;
    }
    const int maxn = 1e7 + 10;
    int pri[maxn], phi[maxn], tot, fac[maxn]; 
    bool vis[maxn];
    int mod;
    LL ksm(LL x, LL y) {
        LL re = 1LL;
        while(y) {
            if(y & 1) re = re * x % mod;
            x = x * x % mod;
            y >>= 1;
        }
        return re;
    }
    void predoit() {
        fac[1] = 1, phi[1] = 1;
        for(int i = 2; i < maxn; i++) {
            if(!vis[i]) pri[++tot] = i, phi[i] = 1LL * (i - 1) * ksm(i, mod - 2) % mod;
            else phi[i] = 1;
            for(int j = 1; j <= tot && (LL)i * pri[j] < maxn; j++) {
                vis[i * pri[j]] = true;
                if(i % pri[j] == 0) break;
            }
            phi[i] = 1LL * phi[i] * phi[i - 1] % mod;
            fac[i] = 1LL * fac[i - 1] * i % mod;
        }
    }
    int main() {
        int T = in();
        mod = in();
        predoit();
        while(T --> 0) {
            int n = in(), m = in();
            printf("%lld
    ", 1LL * fac[n] * phi[m] % mod);
        }
        return 0;
    }
    
  • 相关阅读:
    LeetCode 1245. Tree Diameter
    LeetCode 1152. Analyze User Website Visit Pattern
    LeetCode 1223. Dice Roll Simulation
    LeetCode 912. Sort an Array
    LeetCode 993. Cousins in Binary Tree
    LeetCode 1047. Remove All Adjacent Duplicates In String
    LeetCode 390. Elimination Game
    LeetCode 1209. Remove All Adjacent Duplicates in String II
    LeetCode 797. All Paths From Source to Target
    LeetCode 1029. Two City Scheduling
  • 原文地址:https://www.cnblogs.com/olinr/p/10303756.html
Copyright © 2020-2023  润新知