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. 最新版ffmpeg源码分析

    最新版ffmpeg源码分析一:框架 (ffmpeg v0.9) 框架 最新版的ffmpeg中发现了一个新的东西:avconv,而且ffmpeg.c与avconv.c一个模样,一研究才发现是libav下 ...

  2. python环境中运行程序

    运行Python程序,我们比较常用的是直接在Windows命令提示窗口或者Linux终端或shell窗口中,直接:Python *.py,或者在Linux环境下,在投不中,加入: #!/usr/bin ...

  3. WOL远程开机

    最近在一直都在研究PC机硬件和软件相结合的软件,硬件信息都是通过C++与驱动结合获取.对于一个好久都没有接触C++的人来说看这些东西太费劲了,必须的重新捡一下C++的基础知识,必然也少不了C知识,底层 ...

  4. Activity intent经常使用的 FLAG

    Intent.FLAG_ACTIVITY_NEW_TASK 默认的跳转类型,会重新创建一个新的Activity,不过与这种情况,比方说Task1中有A,B,C三个Activity,此时在C中启动D的话 ...

  5. golang全文搜索--使用sphinx

    不多废话,测试环境 `ubuntu 13.10` ## 安装 sudo apt-get install sphinxsearch ## 配置 nano /etc/sphinxsearch/sphinx ...

  6. 【规范】javascript 变量命名规则

    javascript 有三大经典的变量命名法:匈牙利命名法,驼峰式命名法和帕斯卡命名法.今天主要介绍下这三种命名方式. 匈牙利命名法 语法 变量名 = 类型 + 对象描述 类型指变量的类型 对象描述指 ...

  7. mvc 返回 xml

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  8. chrome start.js报错

    是由 chrome 插件 “电脑管家广告过滤” 引起的 并且,在用户电脑上还出现了这个插件拦截正常请求的情况 如果同时报以下错误: Uncaught TypeError: Cannot read pr ...

  9. 修改 Semantic UI 的默认字体

    Semantic UI 默认使用的是谷歌提供的字体,并且是直接使用了谷歌的官方链接.由于大家都知道的原因,谷歌网站在国内访问速度很差,甚至根本无法访问,还有就是可能会在离线环境下使用 Semantic ...

  10. Pro ASP.NET MVC –第五章 使用Razor

    Razor是微软在MVC3中引入的视图引擎的名字,在MVC4中对其进行了改进(尽管改动非常小).视图引擎处理ASP.NET内容.寻找指令,典型地用于插入动态数据并输出到浏览器中.微软维持了两个视图引擎 ...