数论 - 组合数学 + 素数分解 --- hdu 2284 : Solve the puzzle, Save the world!
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.

5333 127
100000 11
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!的更多相关文章
- HDU 4497 数论+组合数学
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4497 解题思路:将满足条件的一组x,z,y都除以G,得到x‘,y',z',满足条件gcd(x',y' ...
- 数学概念——J - 数论,质因数分解
J - 数论,质因数分解 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- luogu 1865 数论 线性素数筛法
洛谷 1865 数论 线性素数筛法 最基本的线性素数筛法,当做复习欧拉筛法了,没有尝试过使用更暴力的筛法... WA了一次,手抖没打\n 传送门 (https://www.luogu.org/prob ...
- [CodeForces - 1225D]Power Products 【数论】 【分解质因数】
[CodeForces - 1225D]Power Products [数论] [分解质因数] 标签:题解 codeforces题解 数论 题目描述 Time limit 2000 ms Memory ...
- HDU_3071 Gcd & Lcm game 【素数分解 + 线段树 + 状压】
一.题目 Gcd & Lcm game 二.分析 非常好的一题. 首先考虑比较暴力的做法,肯定要按区间进行处理,对于$lcm$和$gcd$可以用标准的公式进行求,但是求$lcm$的时候是肯定 ...
- HDU 4708:Rotation Lock Puzzle
Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU 4708 Rotation Lock Puzzle (简单题)
Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29046 Accepted: 7342 Case ...
- hdu 3864 素数分解
题意:求n是否只有4个因子,如果是就输出除1外的所有因子. 模板题,就不排版了 #include<cstdio> #include<iostream> #include< ...
随机推荐
- 解决adb server端口被占用的问题
先执行adb nodaemon server ,查看adb server的端口是多少 C:\Users\xxxx>adb nodaemon server cannot bind 'tcp:5 ...
- python数据结构之二叉树遍历的实现
本篇是实现二叉树的三种遍历,先序遍历,中序遍历,后序遍历 #!/usr/bin/python # -*- coding: utf-8 -*- class TreeNode(object): def _ ...
- mysql性能瓶颈分析、性能指标、指标搜集方法与性能分析调优工具
本文主要讲解mysql的性能瓶颈分析.性能指标.性能指标信息的搜集工具与方法.分析调优工具的使用. 文章尚未完成. 性能瓶颈: 慢.写速度比读速度慢很多 主要的性能指标: 访问频度, 并发连接量, ...
- jenkins svn E175002错误
JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true -Dsvnkit.http.sslProtocols='SSLv3' -Dsvnkit.http ...
- BCB6 重装后的项目编译莫名问题
我很少用 bcb ,重装 bcb6 后原来的项目居然不能编译成功了,看了一下是控件的问题,但很多控件实际上并不关联的,而 bcb 坚持要你"拥有"当时的控件环境,折腾很久实在是没发 ...
- linux(以ubuntu为例)下Android利用ant自动编译、修改配置文件、批量多渠道,打包生成apk文件
原创,转载请注明:http://www.cnblogs.com/ycxyyzw/p/4555328.html 之前写过一篇<windows下Android利用ant自动编译.修改配置文件.批量 ...
- 老鼠跑猫叫主人惊醒c++观察者模式实现
这个题目算是比较经典的观察者模式了,老鼠作为一个Subject,主动发出跑的动作,紧跟着猫由于老鼠的跑而发出叫声,主人也被惊醒,在这里猫跟主人都是被动的,是观察者角色,代码实现如下: class CS ...
- Windows下移动MariaDB数据目录 (转!)
Windows下移动MariaDB数据目录: http://www.xue163.com/news/940/9403312.html [环境]OS:Windows Server 2008 R2 x64 ...
- JS - IE or not:判断是否为IE浏览器方法
问题:使用JS判断是否为IE浏览器 方法: 1.IE='\v'=='v' (失败!) if('\v'=='v') // true only in IE 2.IE=(!+"\v1" ...
- TIB自动化测试快讯 - Appium手机自动化测试学习资料精选
TIB自动化测试快讯 - Appium手机自动化测试学习资料精选 Appium+Android+Javahttp://automationqa.com/forum.php?mod=viewthre ...