链接:https://www.nowcoder.com/acm/contest/144/C
来源:牛客网

Generation I

时间限制:C/C++ 3秒,其他语言6秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

Oak is given N empty and non-repeatable sets which are numbered from 1 to N.

Now Oak is going to do N operations. In the i-th operation, he will insert an integer x between 1 and M to every set indexed between i and N.

Oak wonders how many different results he can make after the N operations. Two results are different if and only if there exists a set in one result different from the set with the same index in another result.

Please help Oak calculate the answer. As the answer can be extremely large, output it modulo 998244353.

输入描述:

The input starts with one line containing exactly one integer T which is the number of test cases. (1 ≤ T ≤ 20)Each test case contains one line with two integers N and M indicating the number of sets and the range of integers. (1 ≤ N ≤ 1018, 1 ≤ M ≤ 1018, )

输出描述:

For each test case, output "Case #x: y" in one line (without quotes), where x is the test case number (starting from 1) and y is the number of different results modulo 998244353.

示例1

输入

复制

2
2 2
3 4

输出

复制

Case #1: 4
Case #2: 52

C呐就是个排列组合,题意很难懂,反正四个人没看懂,可能时候因为读了几遍不理解,然后看目前AC的不多,就导致了刚刚说的四个人的状态,所以先说一下题意,给出N个集合,有N个操作,第i次操作从1~M中选择一个数,填入i~N的集合中。举个栗子,N=2,M=2

第一次操作选1,填入N1~N2集合中,第二次选1填入N2~N2中

N1: 1

N2:1 1 就是 1 1就是说集合中如果有相同的数字就保留一个。

第一次操作选1,填入N1~N2集合中,第二次选2填入N2~N2中

N1: 1

N2:1 2 就是 1 1 2就是说集合中如果有相同的数字就保留一个。

第一次操作选2,填入N1~N2集合中,第二次选1填入N2~N2中

N1: 2

N2:2 1 就是2 2 1就是说集合中如果有相同的数字就保留一个。

第一次操作选2,填入N1~N2集合中,第二次选2填入N2~N2中

N1: 2

N2:2 2 就是2 2 1就是说集合中如果有相同的数字就保留一个。

总共四种结果。

怎么考虑?

首先选一个数填就是A(m,1)种情况,1 2和2 1不同

选两个数填就是A(m,2)种情况,例如一开始集合都放1 1 1 1 1 1 1...

那么2放到哪里?以1 12 12 12 12 12...为例,不论以后放1还是2最终 都是这个结果。所以问题就转化成了2一开始放到了哪里?

应用隔板法,每个隔板就是一开始放2的位置。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MOD = 998244353;
const LL MAXN = 2e6+888;
LL Fac[MAXN];
LL RFac[MAXN];
LL FastPOW(LL a, LL k) {LL ret = 1;while(k) {if (k&1) {ret = (ret * a) % MOD;}a = (a * a) % MOD;k = (k >> 1);}return ret;
}
void Init() {Fac[0] = 1;for (LL i=1; i<MAXN; ++i) {Fac[i] = Fac[i-1] * i % MOD;}RFac[MAXN-1] = FastPOW(Fac[MAXN-1], MOD-2);for (LL i=MAXN-2; ~i; --i) {RFac[i] = RFac[i+1] * (i+1) % MOD;}
}
int main()
{Init();int T;scanf("%d", &T);LL n, m;for (int cas = 1; cas <= T; ++cas){scanf("%lld %lld", &n, &m);LL tmp1=Fac[m]% MOD;LL tmp2=Fac[n-1]%MOD;LL res = 0;for (LL i=1; i<=min(n,m); ++i){res=(res+((((tmp1*tmp2)%MOD*RFac[m-i])%MOD*RFac[n-i])%MOD*RFac[i-1])%MOD)%MOD;}printf("Case #%d: %lld\n", cas, res);}return 0;
}

直接按公式写代码会发生段错误,因为你预处理不了1e18。那怎么办呢?

化简公式。展开

#include<bits/stdc++.h>using namespace std;
typedef long long ll;
const int p = 998244353;
const int maxn = 1e6+1000;
ll inv[maxn];
ll N, M;int main()
{int T;cin >> T;inv[0] = inv[1] = 1;for(int i=2;i<maxn;++i)inv[i]=(p-p/i)*inv[p%i]%p;for(int cas=1;cas <= T;++cas){cin >> N >> M;ll ans = 0, f = M%p, gg = 1;for(int i=1;i<=N&&i<=M;++i){ans=(ans+f*gg)%p;f=f*((M-i)%p)%p;gg=gg*((N-i)%p)%p*inv[i]%p;}printf("Case #%d: %lld\n", cas, ans);}return 0;
}

Generation I--组合数和数学相关推荐

  1. CSP认证 201312-4有趣的数[C++题解]:组合数、数学

    文章目录 题目解答 题目链接 题目解答 来源:acwing 分析: 题目要求:n位数,只能放0,1,2,3:0必须在1前面,2必须在3前面:0不能放在首位. 由于0和1有限制关系,2和3有限制关系,所 ...

  2. 动态规划求解所有字符的组合数

    一,问题描述 给定若干个字符,求解 这些字符能够表示的最多组合个数.比如{'a','b','c'} 一共有七种组合.(每种组合没有重复的字符 且 组合的种数与顺序无关,如 ab 和 ba 是同一种组合 ...

  3. Codeforces Round #311 (Div. 2) D. Vitaly and Cycle

    题目链接:http://codeforces.com/problemset/problem/557/D 题意:给你包含n个顶点,m条边的不一定连通&&没有自环和重边的无向图,要你找到能 ...

  4. 『数学』--数论--组合数+卢卡斯定理+扩展卢卡斯定理

    组合数: 在N个数中选取M个数,问选的方式有几种? 直接递归暴力简单 #include<cstdio> const int N = 2000 + 5; const int MOD = (i ...

  5. zzuli1728(数学期望,组合数)

    1728: 社交网络 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 232  Solved: 64 SubmitStatusWeb Board Des ...

  6. [2020-09-11 CQBZ/HSZX多校联测 T2] 泰拳警告(组合数+数学期望)

    泰拳警告 description solution code description 题目描述 小七擅长泰拳,某天他打算与小枣切磋拳技,一共需要进行 n 次比赛. 由于双方拳技难分上下,每场比赛小七获 ...

  7. P4370-[Code+#4]组合数问题2【数学,堆】

    正题 题目链接:https://www.luogu.com.cn/problem/P4370 题目大意 求满足m≤n≤am\leq n\leq am≤n≤a的情况下,前kkk大的(nm)\binom{ ...

  8. P6046-纯粹容器【数学期望,组合数】

    正题 题目链接:https://www.luogu.com.cn/problem/P6046 题目大意 nnn个数,每次选择两个相邻的数删除小的那个,求每个数期望存活轮数. 解题思路 相当于一条链每次 ...

  9. [数学最安逸][UVa1638改编][第一类斯特林数+组合数]杆子的排列

    有高为1,2,3,...,n的杆子各一根排成一行.从左边能看到l根,从右边能看到r根,求有多少种可能. (l,r <= 200,n <= 200000) 给出T 组数据 (T <= ...

最新文章

  1. 在Ubuntu 16.04.3 LTS上运行go+https+json示例
  2. Windows下挂载iscsi存储及多路径功能配置
  3. hql可以使用distinct吗_香薰精油可以当香水使用吗
  4. OpenCASCADE:绘制测试线束之基本命令
  5. NFS客户端、服务器协商读写粒度(rsize、wsize)流程 【转】
  6. mysql部署jar_mysql+jar踩坑记录
  7. Why is HttpContext.Current null after await?
  8. mysql 转成树_Mysql树型结构2种方式及相互转换
  9. ios上传图片文件到服务器,iOS 图片以文件形式上传到服务器
  10. 【Day34】Pyhotn之路——网络编程
  11. struts1(转)
  12. SpringMVC教程
  13. scikit-learn学习资源
  14. 服务器被入侵了怎么办
  15. 操作系统文件保护及文件共享
  16. Excel 相同名称或ID的 内容 合并起来 同列不同内容剃加
  17. GitHub 又又又多了一个新主题 —— Dimmed Dark 主题!
  18. MySQL事务--基础(课堂笔记)
  19. HGU3336 Count the string (KMP Next数组的应用)
  20. php同步到百度云,linux 备份定时同步到百度云盘

热门文章

  1. python抛出异常 后如何接住,Python 异常的捕获、异常的传递与主动抛出异常操作示例...
  2. 想自学python看哪位的视频比较好-python自学视频看这个就对了
  3. python工资这么高为什么不学-人人学Python,为什么就业拿高薪的那么少?
  4. python的读音-python怎么读?python的含义和读音!
  5. python用什么来写模块-史上最详细的python模块讲解
  6. 自学python顺序-Django 学习顺序及入门要求?
  7. python刚出来多少薪资-Python薪资待遇到底是多少?老男孩python学习
  8. python是什么类型的编程语言-python和scratch有什么区别
  9. 谁给讲讲语音识别中的CTC方法的基本原理?
  10. php response.write,ASP_RESPONSE.WRITE和lt;%=%的区别,RESPONSE.WRITE与%=%都是ASP程 - phpStudy...