zoukankan      html  css  js  c++  java
  • bzoj 2186 [Sdoi2008]沙拉公主的困惑 欧拉函数

    n>=m,所以就变成了求

    ϕ(m!)n!/m!

    ϕ(m!)=m!(p1)/p......
    p为m!的素因子,即为m内的所有素数,问题就转化为了求
    n!(p1)/p......

    只需要预处理出素数,阶乘,逆元即可

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #define N 10000001
    using namespace std;
    bool bo[N+100];
    long long prime[700000],fc[N+100],ans[N+100],ny[N+100];
    int tot,T,n,m,p;
    void init(){
        fc[1]=1;
        for(int i=2;i<=N;i++)fc[i]=(fc[i-1]*i)%p;
        for(int i=2;i<=N;i++){
            if(!bo[i])prime[++tot]=i;
            for(int j=1;j<=tot&&i*prime[j]<=N;j++){
                bo[i*prime[j]]=1;
                if(!i%prime[j])break;
            }
        }
        ny[1]=1;
        for(int i=2;i<=N&&i<p;i++)ny[i]=(p-p/i)*ny[p%i]%p;
        ans[1]=1;
        for(int i=2;i<=N;i++){
            if(!bo[i])ans[i]=ans[i-1]*(i-1)%p*ny[i%p]%p;
            else ans[i]=ans[i-1];
        }
    }
    int main(){
        scanf("%d%d",&T,&p);
        init();//printf("tot==%d
    ",tot);
        while(T--){
            scanf("%d%d",&n,&m);
            printf("%d
    ",(fc[n]*ans[m])%p);
        }
        return 0;
    }
  • 相关阅读:
    [LeetCode]Word Break
    [LeetCode]singleNumber
    [LeetCode]Palindrome
    新浪博客无法访问
    C++基础之顺序容器
    C++基础之IO类
    [LeetCode]Restore IP Addresses
    [LeetCode]Maximal Rectangle
    [LeetCode]Reverse Linked List II
    ACM 树形数组
  • 原文地址:https://www.cnblogs.com/Ren-Ivan/p/7746692.html
Copyright © 2011-2022 走看看