FZU 2282Problem 2282 Wand(错排+快速幂+费马小定理)
来源:互联网 发布:mysql如何安装 编辑:程序博客网 时间:2024/06/03 15:07
Accept: 132 Submit: 463
Time Limit: 1000 mSec Memory Limit : 262144 KB
Problem Description
N wizards are attending a meeting. Everyone has his own magic wand. N magic wands was put in a line, numbered from 1 to n(Wand_i owned by wizard_i). After the meeting, n wizards will take a wand one by one in the order of 1 to n. A boring wizard decided to reorder the wands. He is wondering how many ways to reorder the wands so that at least k wizards can get his own wand.
For example, n=3. Initially, the wands are w1 w2 w3. After reordering, the wands become w2 w1 w3. So, wizard 1 will take w2, wizard 2 will take w1, wizard 3 will take w3, only wizard 3 get his own wand.
Input
First line contains an integer T (1 ≤ T ≤ 10), represents there are T test cases.
For each test case: Two number n and k.
1<=n <=10000.1<=k<=100. k<=n.
Output
For each test case, output the answer mod 1000000007(10^9 + 7).
Sample Input
Sample Output
Source
第八届福建省大学生程序设计竞赛-重现赛(感谢承办方厦门理工学院)大概转化为给你一个n代表编号为1-n的有序的n个人,一个k,重新排序之后,询问至少有k人在自己原来位置的方案数。
const int mod = 1e9 + 7;
const int maxn = 10005;
typedef long long LL;
LL dp[maxn];
int n,k;
LL pri[maxn];
LL ni[maxn];
LL pow(LL a,int b)
{
LL ans=1,base=a;
while (b>0)
{
if (b%2==1)
ans=(base*ans)%mod;
base=(base*base)%mod;
b/=2;
}
return ans;
}
void s() //打表
{
pri[0]=1;
ni[0]=1;
for (int i=1;i<=maxn ;i++)
{
pri[i]=pri[i-1]*i%mod; //N!
ni[i]=pow(pri[i],mod-2);
}
}
int main()
{
s();
int T;
scanf("%d", &T);
dp[0]=1,dp[1] = 0;
for(int i = 2; i <= maxn; i++)
{
dp[i] = ((i - 1) * (dp[i - 1] + dp[i - 2])) % mod;
}
while(T--)
{
LL ans=1;
scanf("%d%d", &n, &k);
int cnt = n - k;
if(cnt==0)
{
puts("1");
continue;
}
for(int i=2;i<=cnt;i++)
{
LL sum=((pri[n]*ni[i]%mod)*ni[n-i])%mod;
//printf("%lld\n",sum);
ans+=(sum*dp[i])%mod;
}
printf("%lld\n",ans%mod);
}
return 0;
}
- FZU 2282Problem 2282 Wand(错排+快速幂+费马小定理)
- FZU 2282 Wand(错排+费马小定理)
- FZU 2282 Wand(错排+费马小定理)
- FZU 2282 Wand,利用扩展欧几里得求逆元快速求C(n,m) , 错排公式推导
- FZU 2282 Wand (组合数学 错排应用)
- FZU 2282 Wand(组合)
- fzu 2282 Wand
- FZU 2282 Wand 【组合数学】
- FZU 2282 错排
- fzu-Wand
- fzu2282 wand 排列组合 错排
- FZU 2282 错排问题dp
- FZU 1692 Key problem(构造矩阵+矩阵快速幂)
- FZUOJ 2282 Wand
- FZU - 1692 Key problem 矩阵快速幂
- FZU -- 2282 和 DHU -- 2049 【简单错排 + 组合数学】
- fzu 2282(错位排列+逆元+快速幂函数)
- FZU Problem 2198 快来快来数一数(矩阵快速幂 卡常数 +优化)
- 配置Linux网络和克隆虚拟机并更改配置
- Docker 安装使用(centos)
- Linux 中启动Eclipse 出现异常 Exit code =13解决方法
- 设计模式:单例模式
- 模块化编程
- FZU 2282Problem 2282 Wand(错排+快速幂+费马小定理)
- B. Vladik and Complicated Book
- POJ-2773 欧几里得 + 二分 + 容斥
- concurrent(四)
- TensorFlow 之基于Inception V3的多标签分类 retrain
- win10系统怎么通过ssh连接到本机virtual box中安装的centos7系统
- 机器学习面试问题总结
- 2017年08月08日
- 嵌入式每日学习心得2017.08.08