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. 关于“importer.GetNPOTScale() == TextureImporter::kNPOTKeep”问题的简单处理方法

    在运行NGUI打包图集的时候碰到下图所示的错误,这个错误导致图片无法正确的规格进行图集生成.结果是图片变成各种诡异的尺寸!! 通过关键字搜索,发现国外有讨论这个问题的解决方案: 将图片转换为textu ...

  2. Hibernate课程 初探一对多映射5-2 Eclipse添加数据库连接

    1 Eclipse新建java项目 2 Windows ==> show view == >other ==>Data source Explorer 3 左侧视图 database ...

  3. 账户密码提示 jq简单事件

    $(".username").focus(function(){ if($(this).val()=="请输入用户名"){ $(this).val(" ...

  4. Eclipse plug-in startup

    Plug-in Startup !SESSION 2013-09-02 16:28:29.546 -----------------------------------------------ecli ...

  5. ASP.NET MVC 音乐商店 - 4. 数据访问

    上一次,我们使用了模拟的数据从控制器发送到视图模板.现在,我们开始使用真正的数据库,在这个教程中,我们将指导如何使用 SQL Server Compact 版的数据库,它经常被称为 SQL CE, 来 ...

  6. eclipse:maven工程下显示不出文件,但系统存在,可能是这个原因

  7. 微软提供的 Web 版 Raspberry Pi 模拟器

    https://docs.microsoft.com/en-gb/azure/iot-hub/iot-hub-raspberry-pi-web-simulator-get-started#overvi ...

  8. MySQL的基础(优化)1

    1,在创建表的时候,为了获得更好的性能,我们可以将表中字段的宽度设得尽可能小 2,在可能的情况下,应该尽量把字段设置为NOT NULL,这样在将来执行查询的时候,数据库不用去比较NULL值 3,对于某 ...

  9. 笨办法学Python(一)

    习题 1: 第一个程序 你应该在练习 0 中花了不少的时间,学会了如何安装文本编辑器.运行文本编辑器.以及如何运行命令行终端,而且你已经花时间熟悉了这些工具.请不要跳过前一个练习的内容直接进行下面的内 ...

  10. 显示、更改ubuntu linux主机名(计算机名)

    在bash中输入hostname可以显示计算机名.Linux和windows都可以使用这条指令. 主机名保存在/etc/hostname文件中 需要进入Root权限才可以修改该文件. sudo ged ...