传送门


证明自己学过exLucas

这题计算的是本质不相同的排列数量,不难得到答案是\(\frac{n!}{\prod\limits_{i=1}^m w_i! \times (n - \sum\limits_{i=1}^m w_i)!}\)

但是模数不一定是质数,于是用exLucas计算即可。

#include<bits/stdc++.h>
#define int long long
//This code is written by Itst
using namespace std;

int peo[7] , ans[10][2];
int cnt , N , M , P;

inline int poww(int a , int b , int mod = 1e15){
    int times = 1;
    while(b){
        if(b & 1)
            times = times * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return times;
}

void exgcd(int a , int b , int &x , int &y){
    !b ? (x = 1 , y = 0) : (exgcd(b , a % b , y , x) , y -= a / b * x);
}

inline int inv(int a , int b){
    a %= b;
    int x , y;
    exgcd(a , b , x , y);
    return (x + b) % b;
}

int jc(int N , int p , int mod){
    if(!N)
        return 1;
    int times = 1;
    for(int j = 1 ; j < mod ; ++j)
        if(j % p)
            times = times * j % mod;
    times = poww(times , N / mod , mod);
    for(int j = N / mod * mod + 1 ; j <= N ; ++j)
        if(j % p)
            times = times * j % mod;
    return times * jc(N / p , p , mod) % mod;
}

int cntp(int N , int p){
    int sum = 0;
    while(N >= p)
        sum += (N /= p);
    return sum;
}

void calc(int p , int k){
    int mod = poww(p , k) , c = cntp(N , p);
    for(int j = 1 ; j <= M ; ++j)
        c -= cntp(peo[j] , p);
    ans[++cnt][0] = mod;
    if(c < k){
        ans[cnt][1] = poww(p , c) * jc(N , p , mod) % mod;
        for(int j = 1 ; j <= M ; ++j)
            ans[cnt][1] = ans[cnt][1] * inv(jc(peo[j] , p , mod) , mod) % mod;
    }
}

signed main(){
    #ifndef ONLINE_JUDGE
    //freopen("in" , "r" , stdin);
    //freopen("out" , "w" , stdout);
    #endif
    cin >> P >> N >> M;
    for(int i = 1 ; i <= M ; ++i){
        cin >> peo[i];
        peo[M + 1] += peo[i];
    }
    if(peo[M + 1] > N){
        puts("Impossible");
        return 0;
    }
    peo[M + 1] = N - peo[M + 1];
    ++M;
    int tmp = P;
    for(int i = 2 ; i * i <= tmp ; ++i)
        if(tmp % i == 0){
            int cnt = 0;
            while(tmp % i == 0){
                ++cnt;
                tmp /= i;
            }
            calc(i , cnt);
        }
    if(tmp - 1)
        calc(tmp , 1);
    int sum = 0;
    for(int i = 1 ; i <= cnt ; ++i)
        sum = (sum + ans[i][1] * (P / ans[i][0]) % P * inv(P / ans[i][0] , ans[i][0])) % P;
    cout << sum;
    return 0;
}

Luogu2183 礼物 ExLucas、CRT的更多相关文章

  1. BZOJ 2142 礼物 组合数学 CRT 中国剩余定理

    2142: 礼物 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1450  Solved: 593[Submit][Status][Discuss] ...

  2. 【BZOJ2142】礼物 组合数+CRT

    [BZOJ2142]礼物 Description 小E从商店中购买了n件礼物,打算送给m个人,其中送给第i个人礼物数量为wi.请你帮忙计算出送礼物的方案数(两个方案被认为是不同的,当且仅当存在某个人在 ...

  3. [CSP-S模拟测试]:visit(组合数学+exLucas+CRT)

    题目传送门(内部题6) 输入格式 第一行包含两个整数$T$,$MOD$:第二行包含两个整数$n$,$m$,表示$dirty$房子的位置. 输出格式 一行一个整数,表示对$MOD$取模之后的答案. 样例 ...

  4. 【Foreign】无聊的计算姬 [Lucas][BSGS]

    无聊的计算姬 Time Limit: 10 Sec  Memory Limit: 256 MB Description Input Output Sample Input 6 2 2 3 4 3 2 ...

  5. BZOJ_2142_礼物_扩展lucas+组合数取模+CRT

    BZOJ_2142_礼物_扩展lucas+组合数取模 Description 一年一度的圣诞节快要来到了.每年的圣诞节小E都会收到许多礼物,当然他也会送出许多礼物.不同的人物在小E 心目中的重要性不同 ...

  6. CRT and exlucas

    CRT 解同余方程,形如\(x \equiv c_i \ mod \ m_i\),我们对每个方程构造一个解满足: 对于第\(i\)个方程:\(x \equiv 1 \ mod \ m_i\),\(x ...

  7. Algorithm: CRT、EX-CRT & Lucas、Ex-Lucas

    中国剩余定理 中国剩余定理,Chinese Remainder Theorem,又称孙子定理,给出了一元线性同余方程组的有解判定条件,并用构造法给出了通解的具体形式. \[ \begin{aligne ...

  8. 4.18 省选模拟赛 无聊的计算器 CRT EXBSGS EXLucas

    算是一道很毒瘤的题目 考试的时候码+调了3h才搞定. op==1 显然是快速幂. op==2 有些点可以使用BSGS 不过后面的点是EXBSGS. 这个以前学过了 考试的时候还是懵逼.(当时还是看着花 ...

  9. Luogu2183【国家集训队】礼物

    题面 题解 易得答案为 $$ \sum_{i=1}^m\binom{n-\sum_{j=1}^{i-1}w_j}{\sum_{j=1}^iw_j} $$ 扩展$\text{Lucas}$即可 代码 # ...

随机推荐

  1. Tomcat 部署外部系统

    /usr/local/tomcat/conf/Catalina/localhost/resource.xml <?xml version='1.0' encoding='utf-8' ?> ...

  2. NO.1食品超市经营管理的数据方案

    背景 丸悦是一家日资企业,经营管理方式有着很强的日本文化风格:讲流程.重细节.丸悦2013年进入中国,沿袭固有经营管理方式,并且只选择日本供应商合作,日常经营出现诸多摩擦,最终多方原因导致年亏损300 ...

  3. Nginx 反向代理工作原理简介与配置详解

    Nginx反向代理工作原理简介与配置详解   by:授客  QQ:1033553122   测试环境 CentOS 6.5-x86_64 nginx-1.10.0 下载地址:http://nginx. ...

  4. mac下载的excel如果带有超链接,url被转义问题

    注释的代码是file开头的,这种链接在mac系统进行跳转url会转义 hyperlink 还有一种就是http这种就可以正常跳转了. String sLink = basePath + "/ ...

  5. [20171110]sql语句相同sql_id可以不同吗.txt

    [20171110]sql语句相同sql_id可以不同吗.txt --//提一个问题,就是sql语句相同sql_id可以不同吗?--//使用dbms_shared_pool.markhot就可以做到. ...

  6. python第三十一天-----类的封装、继承,多态.....

    封装 封装最好理解了.封装是面向对象的特征之一,是对象和类概念的主要特性. 封装,也就是把客观事物封装成抽象的类,并且类可以把自己的数据和方法只让可信的类或者对象操作,对不可信的进行信息隐藏. cla ...

  7. gitlab hooks配置

    1.邮件格式过滤 pre-recieive rev_type=commit # Only check the first commit information due to a lot of comm ...

  8. gitlab 和 github 配置 SSH Keys

    gitlab 文档上给了很好的配置的例子:https://gitlab.com/help/ssh/README#locating-an-existing-ssh-key-pair 针对mac 下的使用 ...

  9. MySQL内连接(INNER JOIN)

    MySQL INNER JOIN子句介绍 MySQL INNER JOIN子句将一个表中的行与其他表中的行进行匹配,并允许从两个表中查询包含列的行记录. INNER JOIN子句是SELECT语句的可 ...

  10. ubuntu服务器配置

    首先设置Ubuntu更新源 https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/ sudo cp /etc/apt/sources.list /etc/a ...