Gym 100548F Color (数论容斥原理+组合数)
题意:给定 m 种颜色,把 n 盆花排成一直线的花涂色。要求相邻花的颜色不相同,且使用的颜色恰好是k种。问一共有几种涂色方法。
析:首先是先从 m 种颜色中选出 k 种颜色,然后下面用的容斥原理,当时没想出来,如果是只用一种颜色,那么肯定不行,如果用两种颜色,可以有这么方法,
2 * (2-1) ^ (n-1)种,如果是只用 i 种那么就是 i * (i-1) ^ (n-1)。然后依次求就好。再就是求组合数的时候,由于太大,不能用递推,所以要用逆元。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <ctime>
#include <cstdlib>
#define debug puts("+++++")
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e6 + 5;
const LL mod = 1e9 + 7;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
inline int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); }
inline int lcm(int a, int b){ return a * b / gcd(a, b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
LL f[maxn];
LL cm[maxn], ck[maxn]; void init(){
f[1] = 1;
for(int i = 2; i < maxn; ++i)
f[i] = (mod - mod/i) * f[mod%i] % mod;
} LL quick_pow(LL a, LL n){
LL ans = 1LL;
while(n){
if(n & 1) ans = ans * a % mod;
a = a * a % mod;
n >>= 1;
}
return ans;
} LL solve(LL n, LL m, LL k){
ck[0] = cm[0] = 1;
for(int i = 1; i <= k; ++i){
cm[i] = cm[i-1] * (m - i + 1) % mod * f[i] % mod;
ck[i] = ck[i-1] * (k - i + 1) % mod * f[i] % mod;
}
LL ans = 0;
LL cnt = 1;
for(int i = k; i >= 1; --i, cnt = -cnt)
ans = (mod + ans + ck[i] * i % mod * quick_pow(i-1, n-1) * cnt % mod) % mod;
return ans * cm[k] % mod;
} int main(){
init();
int T; cin >> T;
LL n, m, k;
for(int kase = 1; kase <= T; ++kase){
scanf("%I64d %I64d %I64d", &n, &m, &k);
printf("Case #%d: %I64d\n", kase, solve(n, m, k));
}
return 0;
}
Gym 100548F Color (数论容斥原理+组合数)的更多相关文章
- 2019.02.09 codeforces gym 100548F. Color(容斥原理)
传送门 题意简述:对n个排成一排的物品涂色,有m种颜色可选. 要求相邻的物品颜色不相同,且总共恰好有K种颜色,问所有可行的方案数.(n,m≤1e9,k≤1e6n,m\le1e9,k\le1e6n,m≤ ...
- Gym 100548F Color 2014-2015 ACM-ICPC, Asia Xian Regional Contest (容斥原理+大数取模)
题意:有N朵花,在M种颜色中选择恰好k种不同的颜色,将这N朵花染色,要求相邻的两朵花颜色不相同. 分析:若限制改为选择不超过k种颜色将N朵花朵染色,则方案数\(f(N,k) = k*(k-1)^{N- ...
- Gym 100548F Color 给花染色 容斥+组合数学+逆元 铜牌题
Problem F. ColorDescriptionRecently, Mr. Big recieved n flowers from his fans. He wants to recolor th ...
- Codeforces 100548F - Color (组合数+容斥)
题目链接:http://codeforces.com/gym/100548/attachments 有n个物品 m种颜色,要求你只用k种颜色,且相邻物品的颜色不能相同,问你有多少种方案. 从m种颜色选 ...
- 【BZOJ 2005】【NOI 2010】能量采集 数论+容斥原理
这题设$f(i)$为$gcd(i,j)=x$的个数,根据容斥原理,我们只需减掉$f(i×2),f(i×3)\cdots$即可 那么这道题:$$ans=\sum_{i=1}^n(f(i)×((i-1)× ...
- HDU 2841 Visible Trees 数论+容斥原理
H - Visible Trees Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- xdoj-1149(多重集合+容斥原理+组合数取模)
#include <iostream> #include <algorithm> #include <cstdio> using namespace std; ty ...
- Bzoj4710 分特产(容斥原理+组合数)
题面 Bzoj 题解 考虑容斥原理,所有人都有特产的方案数等于: 至少零个人没有特产\(-\)至少一个人没有特产\(+\)至少两个人有特产\(-...\) 接着考虑其中一种情况怎么求(假设现在至少有\ ...
- 【BZOJ2839】集合计数 容斥原理+组合数
Description 一个有N个元素的集合有2^N个不同子集(包含空集),现在要在这2^N个集合中取出若干集合(至少一个),使得 它们的交集的元素个数为K,求取法的方案数,答案模1000000007 ...
随机推荐
- 八数码难题 双向搜索(codevs 1225)
题目描述 Description Yours和zero在研究A*启发式算法.拿到一道经典的A*问题,但是他们不会做,请你帮他们.问题描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字 ...
- Codevs 队列练习 合并版
3185 队列练习 1 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 给定一个队列(初始为空),只有两种操作入队和出队,现给出这 ...
- 【2018 Multi-University Training Contest 1】
01:https://www.cnblogs.com/myx12345/p/9362221.html 02:https://www.cnblogs.com/myx12345/p/9382267.htm ...
- P1546||2627 最短网络 Agri-Net 洛谷||codevs
https://www.luogu.org/problem/show?pid=1546 同http://codevs.cn/problem/2627/ 不同算法 题目背景 农民约翰被选为他们镇的镇长! ...
- 洛谷——P2820 局域网
P2820 局域网 题目背景 某个局域网内有n(n<=100)台计算机,由于搭建局域网时工作人员的疏忽,现在局域网内的连接形成了回路,我们知道如果局域网形成回路那么数据将不停的在回路内传输,造成 ...
- MongoDB学习day08--Mongoose索引、Mongoose内置方法、扩展Mongoose Model的静态方法和实例方法
一.Mongoose索引 索引是对数据库表中一列或多列的值进行排序的一种结构, 可以让我们查询数据库变得更快. MongoDB 的索引几乎与传统的关系型数据库一模一样, 这其中也包括一些基本的查询优化 ...
- Spring基于注解的配置概述
以下内容引用自http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration.html: 从Spring 2.5开始 ...
- Eclipse打war包方法以及Eclipse移植项目时JDK版本不匹配Project facet Java version 1.7 is not supported
打包时: 在项目上右键选择Export,如图: 然后选择WAR file,如图所示.接着再:其中web projecct为打出来包的名字, Destination,打包后存的位置,点击Browse.. ...
- 【深度探索C++对象模型】data语义学
class X{}; class Y :public virtual X{}; class Z :public virtual X{}; class A :public Y, public Z{}; ...
- 【APUE】进程间通信之共享存储(mmap函数)
共享内存可以说是最有用的进程间通信方式,也是最快的IPC形式,因为进程可以直接读写内存,而不需要任何数据的拷贝.对于像管道和消息队列等通信方式,则需要在内核和用户空间进行四次的数据拷贝,而共享内存则只 ...