hdu 2284 Solve the puzzle, Save the world!(组合数)
来源:互联网 发布:python中pipy 编辑:程序博客网 时间:2024/06/11 10:59
Solve the puzzle, Save the world!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 163 Accepted Submission(s): 104
Problem Description
In the popular TV series Heroes, there is a tagline "Save the cheerleader, Save the world!". Here Heroes continues, "Solve the puzzle, Save the world!".
Finally, alien invaders visit our planet. They are eccentric and launch attack many rounds. Since trust in prime numbers, each round they send out p killers, here p is a prime number. Countries on our planet unite and assemble an armed troop of n population. And fortunately we get a fatal weakness of enemy from a betrayer. If the ways of choosing m warriors from n is a multiple of p, the killer number, we will win. Otherwise, we will lose. As the greatest programmer of our planet, you are invited to write a program to count the number of m(0≤m≤n) such that C(n, m) is a multiple of prime p.
![](http://acm.hdu.edu.cn/data/images/C169-1004-1.gif)
Finally, alien invaders visit our planet. They are eccentric and launch attack many rounds. Since trust in prime numbers, each round they send out p killers, here p is a prime number. Countries on our planet unite and assemble an armed troop of n population. And fortunately we get a fatal weakness of enemy from a betrayer. If the ways of choosing m warriors from n is a multiple of p, the killer number, we will win. Otherwise, we will lose. As the greatest programmer of our planet, you are invited to write a program to count the number of m(0≤m≤n) such that C(n, m) is a multiple of prime p.
![](http://acm.hdu.edu.cn/data/images/C169-1004-1.gif)
Input
Each line will contain an integer n(1≤n≤10^5) and a prime p(2≤p<10^7), separated by a single space. Process to the end of file.
Output
For each test of case, if the world can be saved, output the number of ways, otherwise, output "Where is hero from?"(without quotation), both on a single line.
Sample Input
6 25333 127100000 11
Sample Output
3Where is hero from?92301
题意:外星人派出p个杀手(p为素数),地球人从n个人中选出m个组成军队,如果从n中选出m个人的选法是p的倍数就赢,问有多少种赢的方法。对于数学渣的我参考大神思路:剪枝: 因为p为素数,那么它和它的倍数不可能被比它小的数相乘得到。
#include<stdio.h>int main(){int n,p;while(~scanf("%d%d",&n,&p)){if(n==0 || p>n) {//剪枝 printf("Where is hero from?\n");continue;}int cnt=0,ans=0;for(int i=1;i<=n/2;i++){int temp=n-i+1;while(temp%p==0){//求分子中有几个P因子 cnt++;temp/=p;}temp=i;while(temp%p==0){//求分母中有几个p因子 cnt--;temp/=p; }if(cnt<=0) continue;//分子中p因子比分母多的时候才能整除 if(2*i==n) ans++;//m为n的一半时 else ans+=2; //m和n-m的组合数一样 }if(ans) printf("%d\n",ans);else printf("Where is hero from?\n");}return 0;}
0 0
- Hdu 2284 Solve the puzzle, Save the world! (组合数)
- hdu 2284 Solve the puzzle, Save the world!(组合数)
- hdu 4869 Turn the pokers 策略(组合数)
- HDU 4372 Count the Buildings(组合数+斯特林数)
- 【HDU】4869 Turn the pokers 组合数
- CodeForces 131C The World is a Theatre 组合数
- Codeforces Beta Round #95 (Div. 2) C The World is a Theatre(组合数)
- URAL 1925. British Scientists Save the World
- hdu Save the dwarfs 4314
- hdu 4314 Save the dwarfs
- HDU-4314-Save the dwarfs
- CF692D2:The Wall (medium)(组合数)
- HDU -- 4869 Turn the pokers(组合数,费马小定理)
- [HDU 4372]Count the Buildings(第一类斯特林数+组合数)
- HDU 4260 The End of The World(汉罗塔)
- HDU 4260 The End of The World(汉罗塔)
- 【codechef】Strategy for the World Cup(组合打表)
- HDU 4372 Count the Buildings(组合数学,第一类Stirling数)
- Hashtable和HashMap类有三个重要的不同之处
- 织梦DedeCms如何批量修改文章发布时间
- Android中Math取整的三个方法
- (NO.00002)iOS游戏精灵战争雏形(十)
- Python中的元组,列表与字典
- hdu 2284 Solve the puzzle, Save the world!(组合数)
- Apache配置——监听多个端口 对应多个不同的网站目录
- 操作系统中的页面置换算法
- hdu 2803 The MAX【贪心】
- 容斥基础
- linux shell用法(4)-- shell逻辑符号与c兼容方法
- android.content.res.Resources$NotFoundException: String resource ID #0x0异常
- 数据库小析
- Spring AOP