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:

// Memory   Time
// 1347K 0MS
// by : Snarl_jsb
// 2014-09-09-20.25
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<climits>
#include<cmath>
#define N 1000010
#define LL long long
using namespace std; int main()
{
// freopen("C:\\Users\\ASUS\\Desktop\\cin.cpp","r",stdin);
// freopen("C:\\Users\\ASUS\\Desktop\\cout.cpp","w",stdout);
long long n,p;
while(~scanf("%I64d %I64d",&n,&p))
{
if(!n||p>n)
{
puts("Where is hero from?");
continue;
}
long long tmp,cnt,m,ans;
ans=cnt=0;
for(m=1;m<=n/2;m++)
{
tmp=n-m+1;
while(!(tmp%p))
{
cnt++;
tmp/=p;
}
tmp=m;
while(!(tmp%p))
{
cnt--;
tmp/=p;
}
if(cnt<=0)
continue;
if(m*2==n)
ans++;
else ans+=2;
}
if(ans)
cout<<ans<<endl;
else puts("Where is hero from?");
}
return 0;
}

  

数论 - 组合数学 + 素数分解 --- 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. Base: 一种 Acid 的替代方案

    原文链接: BASE: An Acid Alternative Pdf下载链接: Base 数据库 ACID,都不陌生:原子性.一致性.隔离性和持久性,这在单台服务器就能搞定的时代,很容易实现,但是到 ...

  2. Android Launcher分析和修改13——实现Launcher编辑模式(1) 壁纸更换

    已经很久没更新Launcher系列文章,今天不分析源码,讲讲如何在Launcher里面添加桌面设置的功能.目前很多第三方Launcher或者定制Rom都有简单易用的桌面设置功能.例如小米MIUI的La ...

  3. BabeLua

    http://cn.cocos2d-x.org/tutorial/show?id=507 command : -workdir E:\xg_svn\client\cocos2d-x-2.2.2\pro ...

  4. shell脚本编写方法

    shell脚本编写就如同一门语言,涉及到运行环境.基本语法.变量定义.函数.参数(系统参数).条件判定.执行流程控制 等等问题. 本文就以下几个方面进行描述: 运行环境: shell  shebang ...

  5. 【原创】开源BI领袖-SpagoBI5.X最详细的中文版介绍

    SpagoBI是唯一100%的开源商业智能套件由 Engineering Group的SpagoBI实验室(www.eng.it)开发和管理.它提供了强大的分析能力,从传统的报表和图表功能到自助分析. ...

  6. PS 如何改变一个icon的颜色

    好吧,码农被逼到一定程度也会自己出手的. PS:如何改变图标颜色 新建一个图层,设置前景色为你想要的前景色,按Alt+Delete键用前景色填充该图层,将该图层拖到这个图片所在图层的上方,按Ctrl+ ...

  7. thinkphp 3.2.3+Admin LTE后台框架

  8. SharePoint 2013中以其他用户身份登录的WebPart(免费下载)

    在SharePoint 2013中微软并没有提供在SharePoint 2010中以其他用户身份登录的菜单,这对一般用户影响不大,但对于系统管理员或测试人员或特定人员(如在OA系统中的文员或秘书,常常 ...

  9. CSS代码原则

    css的团队合作规则以及怎样写出高性能的css代码. 一.使用Reset但并非全局Reset 同浏览器元素的默认属性有所不同,使用Reset可重置浏览器元素的一些默认属性,以达到浏览器的兼容.但需要注 ...

  10. 在MACOS上实现交叉编译

    在嵌入式开发过程中,设备的存储空间和运算能力通常会比较低,这时候,比如要编译一个linux的内核,嵌入式设备就不能胜任了,所以,实现交叉编译还是很必要的.通过交叉编译,我们就能够在我们的pc上编译出能 ...