fzu 2282 Wand
2017-07-23 13:18
477 查看
Problem 2282 Wand
Accept: 8 Submit: 24
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.
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.
For each test case, output the answer mod 1000000007(10^9 + 7).
21 13 1
14
n,k ans=∑(cni+dp[n-i])n>=i>=k;
dp[i]表示i个物品错排的情况数;
错排的状态转移:dp[i]=(i-1)*(dp[i-1]+dp[i-2]);
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int MOD=1000000007;
const int N=10007;
long long int qmod(long long int a,long long int b) {
long long int res = 1ll;
while(b) {
if(b&1) res=res*a%MOD;
b>>=1,a=a*a%MOD;
}
return res;
}
long long int dp
;
long long int fac
,inv
;
void init() {
fac[0]=1;
for(long long int i=1; i<N; i++) fac[i]=(fac[i-1]*i)%MOD;
inv[N-1] = qmod(fac[N-1],MOD-2);
for(long long int i=N-2; i>=0; i--) inv[i]=(inv[i+1]*(i+1))%MOD;
}
long long int C(int n,int m) {
return fac
*inv[m]%MOD*inv[n-m]%MOD;
}
int main() {
int T;
long long int dp[11111];
int i;
init();
dp[0]=1;
dp[1]=0;
for(i=2;i<=10000;i++)
{
dp[i]=(i-1)*(dp[i-1]+dp[i-2]);
dp[i]=dp[i]%1000000007;
}cin>>T;
while(T--)
{
int a,b;
cin>>a>>b;
long long int ans=0;
for(i=b;i<=a;i++)
{
ans+=dp[a-i]*C(a,i)%10
4000
00000007;
ans=ans%1000000007;
}
cout<<ans<<endl;
}
return 0;
}
代码参考自http://blog.csdn.net/mengxiang000000/article/details/75799209
Accept: 8 Submit: 24
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 decidedto 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
21 13 1
Sample Output
14 n,k ans=∑(cni+dp[n-i])n>=i>=k;
dp[i]表示i个物品错排的情况数;
错排的状态转移:dp[i]=(i-1)*(dp[i-1]+dp[i-2]);
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int MOD=1000000007;
const int N=10007;
long long int qmod(long long int a,long long int b) {
long long int res = 1ll;
while(b) {
if(b&1) res=res*a%MOD;
b>>=1,a=a*a%MOD;
}
return res;
}
long long int dp
;
long long int fac
,inv
;
void init() {
fac[0]=1;
for(long long int i=1; i<N; i++) fac[i]=(fac[i-1]*i)%MOD;
inv[N-1] = qmod(fac[N-1],MOD-2);
for(long long int i=N-2; i>=0; i--) inv[i]=(inv[i+1]*(i+1))%MOD;
}
long long int C(int n,int m) {
return fac
*inv[m]%MOD*inv[n-m]%MOD;
}
int main() {
int T;
long long int dp[11111];
int i;
init();
dp[0]=1;
dp[1]=0;
for(i=2;i<=10000;i++)
{
dp[i]=(i-1)*(dp[i-1]+dp[i-2]);
dp[i]=dp[i]%1000000007;
}cin>>T;
while(T--)
{
int a,b;
cin>>a>>b;
long long int ans=0;
for(i=b;i<=a;i++)
{
ans+=dp[a-i]*C(a,i)%10
4000
00000007;
ans=ans%1000000007;
}
cout<<ans<<endl;
}
return 0;
}
代码参考自http://blog.csdn.net/mengxiang000000/article/details/75799209
相关文章推荐
- FZU 2282 Wand(错排+费马小定理)
- 【FZU - 2282】 Wand 【错排+费马小定理】
- FZU 2282 Wand(组合)
- FZU 2282 Wand(错排+费马小定理)
- FZU 2282Problem 2282 Wand(错排+快速幂+费马小定理)
- FZU 2282 Wand (组合数学 错排应用)
- FZU2282-Wand
- FZU 2282 Wand 【组合数学】
- FZU 2282 错排
- FZU 2282 错排问题dp
- Fzu 2282 (错排 组合)
- FZU 2282 错排
- fzu-Wand
- FZU -- 2282 和 DHU -- 2049 【简单错排 + 组合数学】
- FZU 2282 错排
- FZU 2282 错排
- FZU 2282 错排
- FZU 2282 错排
- fzu 2282(错位排列+逆元+快速幂函数)
- FZU 2282 错排