UVA_10139
The factorial function, n! is defined thus for n a non-negative integer:
0! = 1 n! = n×(n−1)! (n > 0)
We say that a divides b if there exists an integer k such that k×a = b
Input
The input to your program consists of several lines, each containing two non-negative integers, n and m, both less than 231.
Output
For each input line, output a line stating whether or not m divides n!, in the format shown below.
Sample Input
6 9
6 27
20 10000
20 100000
1000 1009
Sample Output
9 divides 6!
27 does not divide 6!
10000 divides 20!
100000 does not divide 20!
1009 does not divide 1000!
m能否被n!整除,题目并不难,细节扣的多。分解m的质因子,然后通过勒让德的结论就能过。
while(n){
n/=x;
sum+=n;
}
被英语语法gank了一波,还要注意的是 0和1 也能整除。
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#define N 1000010
using namespace std;
int vis[N];
int prime[N];
int pn=0,flag;
void gp()
{
for (int i = 2; i <= N; i++) {
if (vis[i]) continue;
prime[pn++] = i;
for (int j = i; j <= N; j += i)
vis[j] = 1;
}
}
int lrd(int n,int x)
{
int sum=0;
while(n)
{
n/=x;
sum+=n;
}
return sum;
}
int main()
{
gp();
//cout<<prime[pn-1]<<endl;
int m,n;
while(~scanf("%d%d",&n,&m))
{
if((n>=m)||(n==0&&m==1))
{
printf("%d divides %d!\n",m,n);
continue;
}
flag=0;
int x=m;
for(int i=0;prime[i]*prime[i]<=m;i++)
{
int cnt=0;
while(x%prime[i]==0)
{
x/=prime[i];
cnt++;
}
if(cnt)
{
int res=lrd(n,prime[i]);
if(res<cnt)
flag=1;
}
}
if((x<=n&&!flag))
printf("%d divides %d!\n",m,n);
else
printf("%d does not divide %d!\n",m,n);
}
}
UVA_10139的更多相关文章
随机推荐
- Jenkins+Ant+Jmeter接口自动化集成测试
一.Jmeter+ant 1.首先我们默认Jmeter脚本已经录制好了,并测试通过,存在(查询模块.jmx)脚本 2.将JMeter所在目录下extras子目录里的ant-JMeter-1.1.1.j ...
- 案例49-crm练习获取客户列表带有分页和筛选功能
1 案例分析 2 书写步骤 1.封装PageBean 2.书写Action 3.书写Service 4.书写Dao 注意清空之前设置的聚合函数 dc.setProjection(null); 5 ...
- hdu 3586 最小代价切断根与叶子联系
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3586 http://blog.csdn.net/woshi250hua/article/details ...
- 浏览器缓存介绍之sessionStorage、localStorage、Cookie
Cookie Cookie 是小甜饼的意思.顾名思义,cookie 确实非常小,它的大小限制为4KB左右,是网景公司的前雇员 Lou Montulli 在1993年3月的发明.它的主要用途有保存登录信 ...
- JSON语法格式
一.JSON数据格式 名称/值对 二.JSON值对数据类型 数字 字符串 逻辑值 数组(在方括号中) 对象 (在花括号中) null eg: { "staff ...
- 在CentOS上配置redis服务
#!/bin/sh # # redis Startup script for Redis Server # # chkconfig: - 80 12 # description: Redis is a ...
- css float布局--右侧上部固定下部自适应大小
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Description Resource Path Location Type Java compiler level does not match the version of the installed Java project facet Unknown Faceted Project Problem (Java Version Mismatch)
project 编译问题,需要三处的jdk版本要保持一致,才能编译通过. 1.在项目上右键properties->project Facets->修改右侧的version 保持一致 2. ...
- 笨办法学Python(二)
习题 2: 注释和井号 程序里的注释是很重要的.它们可以用自然语言告诉你某段代码的功能是什么.在你想要临时移除一段代码时,你还可以用注解的方式将这段代码临时禁用.接下来的练习将让你学会注释: #-- ...
- 显示C++数据的数据类型
#include <typeinfo> using namespace std; ... cout << typeid(d).name() << endl; 其中, ...