UVA 11481 - Arrange the Numbers 数学
Consider this sequence {1, 2, 3, . . . , N}, as a initial sequence of first N natural numbers. You can
earrange this sequence in many ways. There will be N! different arrangements. You have to calculate
the number of arrangement of first N natural numbers, where in first M (M ≤ N) positions, exactly
K (K ≤ M) numbers are in its initial position.
Example:
For, N = 5, M = 3, K = 2
You should count this arrangement {1, 4, 3, 2, 5}, here in first 3 positions 1 is in 1-st position and
3 in 3-rd position. So exactly 2 of its first 3 are in there initial position.
But you should not count this {1, 2, 3, 4, 5}.
Input
The first line of input is an integer T (T ≤ 1000) that indicates the number of test cases. Next T line
contains 3 integers each, N (1 ≤ N ≤ 1000), M, and K.
Output
For each case, output the case number, followed by the answer modulo 1000000007. Look at the sample
for clarification.
Sample Input
1
5 3 2
Sample Output
Case 1: 12
题意:给你 n,m,k, 表示a[i] = 1,2....,n 经过变换后-> 前m个数中只有任意 k个数满足 i = a[i]问你方案数
题解:我们 先在前m个数中任意选k个数是满足不变的 即 C(m,k);
再枚举后n-m个中有多少个数的位置是不变的,C(n−m,x),这样就有n−k−x个数为乱序排列。
对于y个数乱序排序,我们考虑dp做法,假设已经 求出 y-1,y-2个数的乱序排序数,那么 dp[y] = (y-1)*(dp[y-1]+dp[y-2]);(详见上一题)
//meek///#include<bits/stdc++.h>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include<iostream>
#include<bitset>
#include<vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std ;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define fi first
#define se second
#define MP make_pair
typedef long long ll; const int N = +;
const int M = ;
const int inf = 0x3f3f3f3f;
const ll MOD = ; int n, m, k;
ll dp[N], c[N][N];
void init () {
for (int i = ; i < N; i++) {
c[i][] = c[i][i] = ;
for (int j = ; j < i; j++)
c[i][j] = (c[i-][j-] + c[i-][j]) % MOD;
}
dp[] = ;
dp[] = ;
dp[] = ;
for (ll i = ; i < N; i++)
dp[i] = ((dp[i-] + dp[i-]) % MOD * (i-)) % MOD;
} ll solve () {
ll ans = ;
int t = n - m;
for(int i = ;i <= n-m; i++) ans += (c[t][i]*dp[n-k-i]), ans %= MOD;
return (ans * c[m][k]) % MOD;
}
int main () {
init();
int cas = , T;
scanf("%d", &T);
while(T--) {
scanf("%d%d%d", &n, &m, &k);
printf("Case %d: %lld\n", cas++, solve());
}
return ;
}
代码
UVA 11481 - Arrange the Numbers 数学的更多相关文章
- UVA 11481 Arrange the Numbers(组合数学 错位排序)
题意:长度为n的序列,前m位恰好k位正确排序,求方法数 前m位选k个数正确排,为cm[m][k],剩余m - k个空位,要错排,这m - k个数可能是前m个数中剩下的,也可能来自后面的n - m个数 ...
- UVa 11481 Arrange the Numbers (组合数学)
题意:给定 n,m,k,问你在 1 ~ n 的排列中,前 m 个恰好有 k 个不在自己位置的排列有多少个. 析:枚举 m+1 ~ n 中有多少个恰好在自己位置,这个是C(n-m, i),然后前面选出 ...
- UVA 11582 Colossal Fibonacci Numbers(数学)
Colossal Fibonacci Numbers 想先说下最近的状态吧,已经考完试了,这个暑假也应该是最后刷题的暑假了,打完今年acm就应该会退了,但是还什么都不会呢? +_+ 所以这个暑假,一定 ...
- uva 10712 - Count the Numbers(数位dp)
题目链接:uva 10712 - Count the Numbers 题目大意:给出n,a.b.问说在a到b之间有多少个n. 解题思路:数位dp.dp[i][j][x][y]表示第i位为j的时候.x是 ...
- UVA 10539 - Almost Prime Numbers(数论)
UVA 10539 - Almost Prime Numbers 题目链接 题意:给定一个区间,求这个区间中的Almost prime number,Almost prime number的定义为:仅 ...
- light oj 1095 - Arrange the Numbers排列组合(错排列)
1095 - Arrange the Numbers Consider this sequence {1, 2, 3 ... N}, as an initial sequence of first N ...
- UVa 11481 (计数) Arrange the Numbers
居然没有往错排公式那去想,真是太弱了. 先在前m个数中挑出k个位置不变的数,有C(m, k)种方案,然后枚举后面n-m个位置不变的数的个数i,剩下的n-k-i个数就是错排了. 所以这里要递推一个组合数 ...
- POJ2402/UVA 12050 Palindrome Numbers 数学思维
A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example,the ...
- UVA 11461 - Square Numbers 数学水题
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
随机推荐
- Android清除本地数据缓存代码案例
Android清除本地数据缓存代码案例 直接上代码: /* * 文 件 名: DataCleanManager.java * 描 述: 主要功能有清除内/外缓存,清除数据库,清除shar ...
- Eclipse常用快捷键使用
Eclipse的编辑功能非常强大,掌握了Eclipse快捷键功能,能够大大提高开发效率.Eclipse中有如下一些和编辑相关的快捷键. 1. [ALT+/] 此快捷键为用户编辑的好帮手 ...
- Java动态替换InetAddress中DNS的做法简单分析2
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.i ...
- iPhone的震动 基于SDK8.0 Swift实现
导入AudioToolbox.framework包 在swift文件中 import AudioToolbox AudioServicesPlaySystemSound(SystemSoundID. ...
- MongoDB学习笔记-创建、更新、删除文档
创建 MongoDB中使用insert方法来向集合插入文档,然后保存到MongoDB中. db.foo.insert({"hehe":"呵呵"} ...
- ubuntu14.04建立交叉编译环境, 注意事项
ubuntu14.04建立交叉编译环境, 注意事项 ~$ arm-linux-gcc/opt/FriendlyARM/toolschain/4.4.3/bin/arm-linux-gcc: 15: e ...
- Labview实现脉波调制( PAM )
Labview实现脉波调制( PAM ) 根据定义为脉冲振幅调制 生成一个正弦信号,得到其幅值输入给一个方波信号的幅值 这个等价于一个方波信号和一个正弦信号相乘 得到的PAM信号通过观察其频率可以发现 ...
- VC++编程之对话框贴图
基于对话框的程序写好后,为对话框贴上个图片让界面更加美观(我承认做界面,MFC显得力不从心,不如QT). 其实很简单,我们以位图为例,选好我们需要的位图资源(bmp),假若自己的图片不是位图资源,可以 ...
- 硬件相关-ADC原理(未完成)
一.模数转换的一般步骤: 1)采样和保持 为了把模拟信号转换成对应的数字信号,必须首先将模拟量每隔一定时间抽取一次样值,使时间上连续变化的模拟量变为一个时间上断续变化的模拟量,这个过程称为采样. 为了 ...
- ibatis查找date类型
ibatis从oracle查询的时候,返回的类型是java.sql.Date,这个类型是不带时分秒的,我们只有在写sql的时候用to_char来转换.