传送门


证明自己学过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. 基础篇|一文搞懂RNN(循环神经网络)

    基础篇|一文搞懂RNN(循环神经网络) https://mp.weixin.qq.com/s/va1gmavl2ZESgnM7biORQg 神经网络基础 神经网络可以当做是能够拟合任意函数的黑盒子,只 ...

  2. TUM数据集rgbd_benchmark工具的使用方法

    # 在学习视觉slam过程中,需要对数据集合进行预处理和对slam或者跟踪结果进行评价,TUM提供一组这样的工具,为了自己以后方便查找,于是把它记录下来 一.RGBD_Benchmark工具下载链接: ...

  3. mooc《数据结构》 习题1.8 二分查找

    本题要求实现二分查找算法. 函数接口定义: Position BinarySearch( List L, ElementType X ); 其中List结构定义如下: typedef int Posi ...

  4. (网页)人人都会的35个Jquery小技巧

    转自CSDN: 收集的35个 jQuery 小技巧/代码片段,可以帮你快速开发. 1. 禁止右键点击 $(document).ready(function(){ $(document).bind(&q ...

  5. JavaScript大杂烩15 - 使用JQuery(下)

    前面我们总结了使用各种selector拿到了jQuery对象了,下面就是对这个对象执行指定的行为了. 2. 操作对象 - 行为函数action 执行jQuery内置的行为函数的时候,JQuery自动遍 ...

  6. office远程代码执行(CVE-2017-11882)

    office远程代码执行(CVE-2017-11882) 影响版本: MicrosoftOffice 2000 MicrosoftOffice 2003 MicrosoftOffice 2007 Se ...

  7. [20180403]访问dba_autotask_task无输出问题.txt

    [20180403]访问dba_autotask_task无输出问题.txt --//链接http://www.itpub.net/thread-1911421-1-1.html的讨论,还没注意原先的 ...

  8. Jmeter中默认语言的显示

    1.临时性语言的设置 即设置后只对本次使用有效,重启后恢复默认语言 选择Options—>Choose Language—>选择其他语言(例如:Chinese(Simplified)简体中 ...

  9. jmeter利用自身代理录制脚本

    在利用代理录制脚本时一定要安装java jdk,不然不能录制的. 没有安装过java jdk安装jmeter后打开时会提示安装jdk,但是mac系统中直接打开提示安装jdk页面后下载的java并不是j ...

  10. Oracle EBS FA 获取累计折旧

    FUNCTION get_ltd_deprn(p_asset_id IN NUMBER, p_book_type_code IN VARCHAR2, p_rate_source_rule IN VAR ...