数论 - 组合数学 + 素数分解 --- 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< ...
随机推荐
- 如何清除朗逸保养提示标志INSP
自己消除insp小扳手方法 具体步骤如下1.插入钥匙,不要转动.2.按住显示屏下方右边的黑圆柱按钮 3.钥匙转到2档,通电自检,期间按住按钮不要松手4.过10秒左右,INSP消失,小扳手自己去除 不用 ...
- Linux 求文件交集 差集等
使用comm命令 假设两个文件FILE1和FILE2用集合A和B表示,FILE1内容如下: a b c e d a FILE2内容如下: c d a c 基本上有两个方法,一个是comm命令,一个是g ...
- 用Eclipse运行Android版APP(PhoneGap)时出现:Unable to execute dex: Multiple dex files define
这两天遇到点小问题,做个记录: 症状:运行,调试时都报:Unable to execute dex: Multiple dex files define错误,发布后的APP安装到手机后一运行,就提示: ...
- sql 查询 – left join on
1. 问题引入 主要是为了查询在一个表中出现,而不在另一个表中出现的数据,具体来说: 如下图所示, 有A.B两个表,其中B表的Aid字段参照A表的主键id,为了查询在A表中出现,却没有被B表引 ...
- volley中图片加载
volley图片加载有三种方式: 记得:Volley中已经实现了磁盘缓存了,查看源码得知通过 context.getCacheDir()获取到了 /data/data/<application ...
- Android-NDK编译:cocos2d-x(三) eclipse 导入工程
NDK 编译后,用eclipse导入cocos2d-x工程 菜单[File]-->[New]-->[Project] ,弹出New Project 对话框 窗口下方 选 [Android] ...
- 配置NAT回流导致外网解析到了内网IP
单位有3个域名,用量很大,2014年开始本人研究部署了Bind+DLZ +Mysql的三机智能多链路DNS,非常好用,优点是: 1.使用Mysql管理记录,配置.管理.查询方便. 2.自动判断运营商, ...
- Windows下gvim的快捷键--“冒号+w+回车”真的很累人
发现Windows下的gvim支持Ctrl+S保存,Ctrl+A全选,Ctrl+C复制,Ctrl+V粘贴,Ctrl+Z撤销 不过Ctrl+X貌似不太正常(可以剪切,但是不能粘贴) 可能要在安装目录下的 ...
- 设计模式——"simple Factory"
顾名思义,工厂模式就是类似于生活中的工厂,可以生产我们想要的东西,回到代码,假如我们需要一个实例,直接从工厂中拿即可. eg:假如我们定义一个“球”类的接口 public interface Ball ...
- [转载]斐讯K2 A2版免TTL刷BREED不死Bootloader
1:路由器能正常上网,登陆进路由器 (默认密码admin) 2:用浏览器打开这个网址,如果默认ip不是192.168.2.1修改一下.打开后等待即可自动刷入breed. http://192.168. ...