Solve the puzzle, Save the world!

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.
 
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 2
5333 127
100000 11
 
Sample Output
3
Where is hero from?
92301
 

Mean:

给你两个数n和p,现在要你从n中选出m个数,如果n取m能够被p整除,那么为可行方案,问可行方案数有多少种。

analyse:

如果数据小的话确实是道水题,当然只要想到这个方法,一样也是水题。

如果我们用传统的方法:枚举m,然后求c(n,m),再看能不能被p整除这样暴力来做的话,光是c(n,m)就存不下,更别提判断整除了。

这题用了一个巧妙的方法,由于题目说p是一个素数,那么我们可直接来统计c(n,m)分子分母的素因子,然后相减之后就是c(n,m)的p因子了。

Time complexity:O(n)

Source code:

  1. // Memory Time
  2. // 1347K 0MS
  3. // by : Snarl_jsb
  4. // 2014-09-09-20.25
  5. #include<algorithm>
  6. #include<cstdio>
  7. #include<cstring>
  8. #include<cstdlib>
  9. #include<iostream>
  10. #include<vector>
  11. #include<queue>
  12. #include<stack>
  13. #include<map>
  14. #include<string>
  15. #include<climits>
  16. #include<cmath>
  17. #define N 1000010
  18. #define LL long long
  19. using namespace std;
  20.  
  21. int main()
  22. {
  23. // freopen("C:\\Users\\ASUS\\Desktop\\cin.cpp","r",stdin);
  24. // freopen("C:\\Users\\ASUS\\Desktop\\cout.cpp","w",stdout);
  25. long long n,p;
  26. while(~scanf("%I64d %I64d",&n,&p))
  27. {
  28. if(!n||p>n)
  29. {
  30. puts("Where is hero from?");
  31. continue;
  32. }
  33. long long tmp,cnt,m,ans;
  34. ans=cnt=0;
  35. for(m=1;m<=n/2;m++)
  36. {
  37. tmp=n-m+1;
  38. while(!(tmp%p))
  39. {
  40. cnt++;
  41. tmp/=p;
  42. }
  43. tmp=m;
  44. while(!(tmp%p))
  45. {
  46. cnt--;
  47. tmp/=p;
  48. }
  49. if(cnt<=0)
  50. continue;
  51. if(m*2==n)
  52. ans++;
  53. else ans+=2;
  54. }
  55. if(ans)
  56. cout<<ans<<endl;
  57. else puts("Where is hero from?");
  58. }
  59. return 0;
  60. }

  

数论 - 组合数学 + 素数分解 --- hdu 2284 : Solve the puzzle, Save the world!的更多相关文章

  1. HDU 4497 数论+组合数学

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4497 解题思路:将满足条件的一组x,z,y都除以G,得到x‘,y',z',满足条件gcd(x',y' ...

  2. 数学概念——J - 数论,质因数分解

    J - 数论,质因数分解 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

  3. luogu 1865 数论 线性素数筛法

    洛谷 1865 数论 线性素数筛法 最基本的线性素数筛法,当做复习欧拉筛法了,没有尝试过使用更暴力的筛法... WA了一次,手抖没打\n 传送门 (https://www.luogu.org/prob ...

  4. [CodeForces - 1225D]Power Products 【数论】 【分解质因数】

    [CodeForces - 1225D]Power Products [数论] [分解质因数] 标签:题解 codeforces题解 数论 题目描述 Time limit 2000 ms Memory ...

  5. HDU_3071 Gcd & Lcm game 【素数分解 + 线段树 + 状压】

    一.题目  Gcd & Lcm game 二.分析 非常好的一题. 首先考虑比较暴力的做法,肯定要按区间进行处理,对于$lcm$和$gcd$可以用标准的公式进行求,但是求$lcm$的时候是肯定 ...

  6. HDU 4708:Rotation Lock Puzzle

    Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  7. HDU 4708 Rotation Lock Puzzle (简单题)

    Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  8. 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 29046   Accepted: 7342 Case ...

  9. hdu 3864 素数分解

    题意:求n是否只有4个因子,如果是就输出除1外的所有因子. 模板题,就不排版了 #include<cstdio> #include<iostream> #include< ...

随机推荐

  1. 笔记 - 本地拦截genymotion或者Android模拟器的网络请求

    我们在主机上面运行了Burp或者fiddler,那么代理已经监听在本机的8080端口了. 那么我们需要在模拟器中进行如下设置: 1.在设置中,长按当前连接的wifi网络,弹出如下: 2. 点击修改网络 ...

  2. PowerShell读取Windows产品密钥

    之前大多数人可能用过VBS读取Windows产品密钥的VBS脚本,VBS脚本通常都比较隐晦.难懂,今天忙里偷闲,随手写了一个用于读取Windows产品密钥的PowerShell脚本. 代码如下: == ...

  3. Sql Server:不允许 ASSIGNMENT 语句中包含 FOR XML 子句

    编写函数的时候遇到“不允许 ASSIGNMENT 语句中包含 FOR XML 子句”错误,开始以为数据库函数里不可以写 FOR XML 子句,仔细看了看总觉得这么写别扭索性改了一种写法就通过了. BE ...

  4. 介绍几个工作开发中封装的好用的android自定义控件

    首先看效果图, 看下这两个界面,第一个中用到了一个自定义的FlowRadioGroup,支持复合子控件,自定义布局: 第二个界面中看到了输入的数字 自动4位分割了吧:也用到了自定义的DivisionE ...

  5. Object c 基础知识

    文件类型说明:.h 头文件,用于定义类.实例变量及类中的方法等定义信息(interface)..m 源文件,定义方法体,可实现objce-c和c方法(implementation)..mm c++源文 ...

  6. MSSQL获得表的字段名称及其参数

    SELECT ColumnsName = c.name, [Description] = ex.value, ColumnType = t.name, [Length]=c.max_length FR ...

  7. 使用UIKit制作卡牌游戏(三)ios游戏篇

    译者: Lao Jiang | 原文作者: Matthijs Hollemans写于2012/07/13 转自朋友Tommy 的翻译,自己只翻译了这第三篇教程. 原文地址: http://www.ra ...

  8. 十五、EnterpriseFrameWork框架核心类库之系统启动入口与初始化

    本章内容是讲三种开发模式,web模式.Winform模式和Wcf模式的系统启动入口有什么区别,以及启动后系统初始化的内容:为什么要把这些单独提出来讲一章,因为我觉得本章非常重要,我们都知道程序中的ma ...

  9. 《Programming with Objective-C》第三章 Working with Objects

    Object和普通变量的区别 If you’re used to using terms like the stack and the heap, a local variable is alloca ...

  10. 获取当前 Python 版本

    方法一 >>> from platform import python_version >>> print python_version() 2.7.8 方法二 & ...