TJU 4087组合数打表快速幂容斥原理
来源:互联网 发布:广电手游新规 知乎 编辑:程序博客网 时间:2024/05/22 03:35
Time Limit: 1.0 Seconds Memory Limit:65536K
Total Runs: 119 Accepted Runs:32
Tuhao and his two small partners participated in the tournament.But in the end, they lost the chance to finish a mathematical problem.After the competition,Tuhao recalled that year in high school math class on a variety of tragedy.
One day, the math teacher give Tuhao a question:if you have 5 boxes in a line(every boxes are different) and you have 4 kinds of balls.you must put an ball in every box, and all balls can take unlimited. How many kinds of way of placing? Tuhao solve this problem easily.But he has another question to you.if we have N boxes, and we have m kind of balls, and you can't use all kinds of balls because Tuhao must have one kind ball at least to play with his partners...
Input
There will be multiple cases to consider. The first input will be a number T(0 < T ≤ 50) indicating how many cases with which you will deal. Following this number will be pairs of integers giving values for N and M, in that order. You are guaranteed that 1 ≤ N < 500, 1 ≤ M < 10, Each N M pair will occur on a line of its own. N and M will be separated by a single space.
Output
For each case display a line containing the case number (starting with 1 and increasing sequentially), and the kinds of way of placing. The desired format is illustrated in the sample shown below.(p.s. the answer should be module 200000007)
Sample Input
21 18 3
Sample Output
Case 1: 0Case 2: 765
代码来自向神,表示感谢~
#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>using namespace std;typedef long long ll;const long long md = 200000007;const int N = 505, M = 20;ll c[M + 5][M + 5];void init()//C组合数打表{ c[0][0] = 1; for (int i = 1; i < M; i++) { c[i][0] = 1; for (int j = 1; j <= i; j++) { c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % md; } }}ll powmod(ll a, ll n){ ll y = 1; while (n) { if (n & 1) y = y * a % md; if (n >>= 1) a = a * a % md; } return y;}int n, m;ll solve(){ scanf("%d%d", &n, &m); ll ans = 0; ll op = 1; for (int i = m - 1; i > 0; i--) { ans = (ans + c[m][i] * powmod(i, n) * op) % md; op *= -1; } ans = (ans % md + md) % md; return ans;}int main(){ // freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin); int T, C = 0; init(); scanf("%d", &T); while (T--) { printf("Case %d: %lld\n", ++C, solve()); } return 0;}
- TJU 4087组合数打表快速幂容斥原理
- TJU 4087
- 【DP】 TJU 4087
- 【TJU】4087 box DP
- TJU-3848 Game(矩阵乘法快速幂)
- TJU 3474
- TJU Sequence
- TJU Vacation
- TJU Change
- TJU Antimonotonicity
- TJU Price
- 【容斥原理+逆元+组合数+费马小定理+快速幂】UVALive
- 组合数快速求解
- 组合数快速算法
- 快速组合数
- 快速组合数
- 快速组合排列算法
- 快速幂+组合数
- java的传值问题
- Using Version Control with Unity
- Spring 七大核心模块
- 阿里RocketMQ Quick Start
- 08extjs ajax编写多级联动菜单
- TJU 4087组合数打表快速幂容斥原理
- eclipse中格式化代码快捷键Ctrl+Shift+F失效的解决办法
- css 对超出文本显示省略号
- C语言 实现 面向对象编程 OOC
- 部分素材下载【补充】
- 这才是9天时是拉动把电崩
- mac os命令
- QTP正则表达式--邮箱动态链接
- android开发