传送门


证明自己学过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. 从零开始学习html(六)开始学习CSS,为网页添加样式

    一.认识CSS样式 <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type&quo ...

  2. const修饰指针的三种效果

    当用const进行修饰时,根据const位置的不同有三种不同效果. 判断的标准是:const修饰谁,谁的内容就是不可变的. 1 const int *p = &a; const修饰*p, *p ...

  3. (其他)最常用的15大Eclipse开发快捷键技巧

    转自CSDNJava我人生(陈磊兴)   原文出处 引言 做java开发的,经常会用Eclipse或者MyEclise集成开发环境,一些实用的Eclipse快捷键和使用技巧,可以在平常开发中节约出很多 ...

  4. python利用Trie(前缀树)实现搜索引擎中关键字输入提示(学习Hash Trie和Double-array Trie)

    python利用Trie(前缀树)实现搜索引擎中关键字输入提示(学习Hash Trie和Double-array Trie) 主要包括两部分内容:(1)利用python中的dict实现Trie:(2) ...

  5. Video.js web视频播放器

    免费视频播放器videojs中文教程 Video.js是一款web视频播放器,支持html5和flash两种播放方式.更多关于video.js的介绍,可以访问官方网站介绍,我之前也写过一篇关于vide ...

  6. C语言程序试题

    一个无向连通图G点上的哈密尔顿(Hamiltion)回路是指从图G上的某个顶点出发,经过图上所有其他顶点一次且仅一次,最后回到该顶点的路劲.一种求解无向图上哈密尔顿回路算法的基础实现如下: 假设图G存 ...

  7. The Tomcat connector configured to listen on port 8080 failed to start. The port may already be in use or the connector may be misconfigured

    springboot 8080端口被占用报错:The Tomcat connector configured to listen on port 8080 failed to start. The p ...

  8. Linux : Vim 使用与配置 (附 GitHub 自动化配置脚本)

    由于经常使用 vim 编辑配置文件,有时候也会进行使用vim 编写一些脚本和c/c++ 程序,所以配置一个常用的 vim 是很是必要的.这篇博文主要是记录vim使用和配置相关的一些知识点. 关于vim ...

  9. Python实例---模拟微信网页登录(day4)

    第五步: 获取联系人信息---day4代码 settings.py """ Django settings for weixin project. Generated b ...

  10. tcpdump抓包具体分析

    Tcpdump抓包分析过程   一.TCP连接建立(三次握手) 过程 客户端A,服务器B,初始序号seq,确认号ack 初始状态:B处于监听状态,A处于打开状态 A -> B : seq = x ...