swjtu oj Paint Box 第二类斯特林数
http://swjtuoj.cn/problem/2382/
题目的难点在于,用k种颜色,去染n个盒子,并且一定要用完这k种颜色,并且相邻的格子不能有相同的颜色,
打了个表发现,这个数是s(n, k) * k!
s(n, k)表示求第二类斯特林数。
那么关键是怎么快速求第二类斯特林数。
这里提供一种O(k)的算法。
第二类斯特林数:


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
LL quick_pow(LL a, LL b, LL MOD) { //求解 ab % MOD的值
LL base = a % MOD;
LL ans = ; //相乘,所以这里是1
while (b > ) {
if (b & ) { //用quick_mul防止Miller_Rabin那里溢出。
ans = ans * base % MOD; //如果这里是很大的数据,就要用quick_mul
}
base = base * base % MOD; //notice。每次的base是自己base倍
b >>= ;
}
return ans;
}
LL fac[ + ];
LL C2(LL n, LL m, LL MOD) {
if (n < m) return ; //防止sb地在循环,在lucas的时候
if (n == m) return % MOD;
LL ans1 = ;
LL ans2 = ;
LL mx = max(n - m, m); //这个也是必要的。能约就约最大的那个
LL mi = n - mx;
for (int i = ; i <= mi; ++i) {
ans1 = ans1 * (mx + i) %MOD;
ans2 = ans2 * i % MOD;
}
return (ans1 * quick_pow(ans2, MOD - , MOD) % MOD); //这里放到最后进行,不然会很慢
} LL C(LL n, LL m, LL MOD) {
if (n > ) return C2(n, m, MOD);
return fac[n] * quick_pow(fac[n - m], MOD - , MOD) % MOD * quick_pow(fac[m], MOD - , MOD) % MOD;
}
const int MOD = 1e9 + ;
void add(LL &x, LL y) {
x += y;
x += MOD;
if (x >= MOD) x %= MOD;
} void work() {
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
if (n == ) {
printf("%d\n", m);
return;
}
if (k == ) {
printf("0\n");
return;
}
LL ans = ;
LL res = C(m, k, MOD);
k--;
n--;
for (int i = ; i <= k; ++i) {
if (i & ) {
add(ans, -(C(k, i, MOD) * quick_pow(k - i, n, MOD)) % MOD);
} else {
add(ans, (C(k, i, MOD) * quick_pow(k - i, n, MOD) % MOD));
}
}
ans = ans * quick_pow(fac[k], MOD - , MOD) % MOD;
ans = ans * fac[k + ] % MOD;
ans = ans * res % MOD;
printf("%lld\n", ans);
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
fac[] = ;
for (int i = ; i <= ; ++i) {
fac[i] = fac[i - ] * i % MOD;
}
int t;
scanf("%d", &t);
while (t--) work();
return ;
}
swjtu oj Paint Box 第二类斯特林数的更多相关文章
- 新疆大学(新大)OJ xju 1006: 比赛排名 第二类斯特林数+阶乘
题目链接:http://acm.xju.edu.cn/JudgeOnline/problem.php?id=1006 第二类斯特林数: 第二类Stirling数实际上是集合的一个拆分,表示将n个不同的 ...
- bzoj 5093 [Lydsy1711月赛]图的价值 NTT+第二类斯特林数
[Lydsy1711月赛]图的价值 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 245 Solved: 128[Submit][Status][D ...
- 【BZOJ5093】图的价值(第二类斯特林数,组合数学,NTT)
[BZOJ5093]图的价值(第二类斯特林数,组合数学,NTT) 题面 BZOJ 题解 单独考虑每一个点的贡献: 因为不知道它连了几条边,所以枚举一下 \[\sum_{i=0}^{n-1}C_{n-1 ...
- 【BZOJ4555】求和(第二类斯特林数,组合数学,NTT)
[BZOJ4555]求和(第二类斯特林数,组合数学,NTT) 题面 BZOJ 题解 推推柿子 \[\sum_{i=0}^n\sum_{j=0}^iS(i,j)·j!·2^j\] \[=\sum_{i= ...
- CF932E Team Work(第二类斯特林数)
传送门:CF原网 洛谷 题意:给定 $n,k$,求 $\sum\limits^n_{i=1}\dbinom{n}{i}i^k\bmod(10^9+7)$. $1\le n\le 10^9,1\le k ...
- HDU - 4625 JZPTREE(第二类斯特林数+树DP)
https://vjudge.net/problem/HDU-4625 题意 给出一颗树,边权为1,对于每个结点u,求sigma(dist(u,v)^k). 分析 贴个官方题解 n^k并不好转移,于是 ...
- 【CF961G】Partitions 第二类斯特林数
[CF961G]Partitions 题意:给出n个物品,每个物品有一个权值$w_i$,定义一个集合$S$的权值为$W(S)=|S|\sum\limits_{x\in S} w_x$,定义一个划分的权 ...
- 【CF932E】Team Work(第二类斯特林数)
[CF932E]Team Work(第二类斯特林数) 题面 洛谷 CF 求\(\sum_{i=1}^nC_{n}^i*i^k\) 题解 寒假的时候被带飞,这题被带着写了一遍.事实上并不难,我们来颓柿子 ...
- 【51NOD 1847】奇怪的数学题(莫比乌斯反演,杜教筛,min_25筛,第二类斯特林数)
[51NOD 1847]奇怪的数学题(莫比乌斯反演,杜教筛,min_25筛,第二类斯特林数) 题面 51NOD \[\sum_{i=1}^n\sum_{j=1}^nsgcd(i,j)^k\] 其中\( ...
随机推荐
- PG替换字段中的回车与换行
REPLACE(filed, CHR(10), '') //替换换行符 REPLACE(filed, CHR(13), '') //替换回车符
- ACM学习历程—HDU 5289 Assignment(线段树 || RMQ || 单调队列)
Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered fro ...
- python爬虫知识点总结(三)urllib库详解
一.什么是Urllib? 官方学习文档:https://docs.python.org/3/library/urllib.html 廖雪峰的网站:https://www.liaoxuefeng.com ...
- Cocos2d-x v3.x and Xcode 6.x with ARM 64 Architecture
转自:http://becomingindiedev.blogspot.com.es/2014/12/cocos2d-x-v3x-and-xcode-6x-with-arm-64.html Hi! W ...
- Spring boot 学习 九
一:经过试验发现,如果使用如下的Controller(@RequestBody), 前台POST的请求body只能是JSON,如果是form-data, X-www-form-urlencoded 或 ...
- C#窗体上绘制矩形
先上效果图 鼠标三个事件 private void Form1_MouseDown(object sender, MouseEventArgs e) { //记录开始点 this.mousedown ...
- iView之select获取value和label
使用:label-in-value="true" @on-change="obtainValue" 详见官方文档:https://www.iviewui.com ...
- Primer回顾 标准库类型
string类型的输入操作符: 1.读取并忽略开头所有的空白字符(如空格,换行符,制表符). 2.读取字符直至再次遇到空白字符,读取终止. 用getline读取整行文本 getline.接受两个参 ...
- 3.12-3.16 Hbase集成hive、sqoop、hue
一.Hbase集成hive https://cwiki.apache.org/confluence/display/Hive/HBaseIntegration 1.说明 Hive与HBase整合在一起 ...
- 2-5 Flutter开发环境与Android开发环境设置详解(Windows)
第二个是国内服务器的网址 andoid stuido的一些使用的说明文档 https://developer.android.google.cn/studio/intro 安装Flutter Dart ...