HDU 4535 吉哥系列故事——礼尚往来
2016-05-29 22:22
316 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4535
Total Submission(s): 1401 Accepted Submission(s): 722
那个江湖人称“叽叽哥”的基哥
每当节日来临,女友众多的叽叽哥总是能从全国各地的女友那里收到各种礼物。
有礼物收到当然值得高兴,但回礼确是件麻烦的事!
无论多麻烦,总不好意思收礼而不回礼,那也不是叽叽哥的风格。
现在,即爱面子又抠门的叽叽哥想出了一个绝妙的好办法:他准备将各个女友送来的礼物合理分配,再回送不同女友,这样就不用再花钱买礼物了!
假设叽叽哥的n个女友每人送他一个礼物(每个人送的礼物都不相同),现在他需要合理安排,再回送每个女友一份礼物,重点是,回送的礼物不能是这个女友之前送他的那个礼物,不然,叽叽哥可就摊上事了,摊上大事了......
现在,叽叽哥想知道总共有多少种满足条件的回送礼物方案呢?
每组数据包含一个正整数n,表示叽叽哥的女友个数为n( 1 <= n <= 100 )。
每组输出占一行。
1
2
4
1
9
思路:组合数学的错置公式。
附上AC代码:
#include <bits/stdc++.h>
//#pragma comment(linker, "/STACK:102400000, 102400000")
using namespace std;
typedef long long ll;
const int maxn = 105;
const int mod = 1000000007;
int d[maxn];
int n;
void init(){
d[0] = 1;
d[1] = 0;
for (int i=2; i<maxn; ++i)
d[i] = 1ll*(i-1)*(d[i-1]+d[i-2])%mod;
}
int main(){
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
init();
int T;
scanf("%d", &T);
while (T--){
scanf("%d", &n);
printf("%d\n", d
);
}
return 0;
}
吉哥系列故事——礼尚往来
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 1401 Accepted Submission(s): 722
Problem Description
吉哥还是那个吉哥那个江湖人称“叽叽哥”的基哥
每当节日来临,女友众多的叽叽哥总是能从全国各地的女友那里收到各种礼物。
有礼物收到当然值得高兴,但回礼确是件麻烦的事!
无论多麻烦,总不好意思收礼而不回礼,那也不是叽叽哥的风格。
现在,即爱面子又抠门的叽叽哥想出了一个绝妙的好办法:他准备将各个女友送来的礼物合理分配,再回送不同女友,这样就不用再花钱买礼物了!
假设叽叽哥的n个女友每人送他一个礼物(每个人送的礼物都不相同),现在他需要合理安排,再回送每个女友一份礼物,重点是,回送的礼物不能是这个女友之前送他的那个礼物,不然,叽叽哥可就摊上事了,摊上大事了......
现在,叽叽哥想知道总共有多少种满足条件的回送礼物方案呢?
Input
输入数据第一行是个正整数T,表示总共有T组测试数据(T <= 100);每组数据包含一个正整数n,表示叽叽哥的女友个数为n( 1 <= n <= 100 )。
Output
请输出可能的方案数,因为方案数可能比较大,请将结果对10^9 + 7 取模后再输出。每组输出占一行。
Sample Input
31
2
4
Sample Output
01
9
Source
2013腾讯编程马拉松复赛第二场(3月30日)Recommend
liuyiding思路:组合数学的错置公式。
附上AC代码:
#include <bits/stdc++.h>
//#pragma comment(linker, "/STACK:102400000, 102400000")
using namespace std;
typedef long long ll;
const int maxn = 105;
const int mod = 1000000007;
int d[maxn];
int n;
void init(){
d[0] = 1;
d[1] = 0;
for (int i=2; i<maxn; ++i)
d[i] = 1ll*(i-1)*(d[i-1]+d[i-2])%mod;
}
int main(){
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
init();
int T;
scanf("%d", &T);
while (T--){
scanf("%d", &n);
printf("%d\n", d
);
}
return 0;
}
相关文章推荐
- 初学ACM - 组合数学基础题目PKU 1833
- 【HDU 5366】The mook jong 详解
- 【HDU 2136】Largest prime factor 详细图解
- 【HDU 1568】Fibonacci 数学公式 详解
- HDU 1568
- HDU1290
- HDU1568(Fobonacci公式)
- HDU ACM Step 2.2.2 Joseph(约瑟夫环问题)
- HDU 1405
- HDU 1297
- hdu 1205
- hdu 2087
- hdu 1016
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
- HDU 5592 ZYB's Premutation 线段树(查找动态区间第K大)
- HDU 5240 Exam (好水的题)
- HDU5237 Base64 大模拟
- HDU 1000
- HDU 1001
- HDU 1016 Prime Ring Problem