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的更多相关文章

随机推荐

  1. 关闭ubuntu讨厌的内部错误提示

    修改/etc/default/apport 浏览下/etc/init/apport.conf 内容你会发现,控制此服务是否启动的是/etc/default/apport 所以把/etc/default ...

  2. 二叉搜索树-php实现 插入删除查找等操作

    二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的 ...

  3. Java多线程使用wait和notify实现生产者消费者模型

    Java多线程使用wait和notify这两个关键字的学习,通过实现生成者与消费者来成对研究比较科学. 从两个字的意义来讲就是等待与通知这个简单道理. 现在先模拟一个缓存区存储,是用一个list实现的 ...

  4. POJ 3189——Steady Cow Assignment——————【多重匹配、二分枚举区间长度】

     Steady Cow Assignment Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I ...

  5. ubuntu 下安装配置LAMP

    详情见: http://www.linuxeden.com/html/softuse/20130731/141934.html

  6. 利用COM组件实现对WORD书签处写入值

    using System; using System.Collections.Generic; using System.Text; using Microsoft.Office.Interop.Wo ...

  7. 动态LINQ(Lambda表达式)构建

    using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; us ...

  8. springboot整合mybatis+oracle

    第一步 认识springboot :springboot是为了解决配置文件多,各个组件不统一的问题,它省去了很多配置文件,同时实现了spring产品的整合. 创建springboot项目:通过选择sp ...

  9. 如何在Chrome粘贴图片直接上传

    背景 截图或页面复制图片,可以直接通过Ctrl+v 粘贴上传图片 原理 操作:复制(截图)=>粘贴=>上传 监听粘贴事件=>获取剪贴板里的内容=>发请求上传 浏览器:Chrom ...

  10. idea 插件描述(转)

    提供些idea插件的干货.干货.干货! 如何安装idea plug,方法如下图:如果安装不了考虑下是否代理问题: 1,maven helper 以前查看maven依赖都比较麻烦,需要用命令maven ...