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 2^31.

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!

给一个整数n和一个数m,求n的阶乘是否可以整除m。

将m分解质因数,然后对每个质因数在n中进行判断是否含有相同或者更多。

 //Asimple
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = ;
ll n, m, s, res, ans, len, T, k, num;
int x, y;
int pr[maxn]; void get_pr(){
int a[maxn] = {};
len = ;
for(int i=; i<maxn; i++) {
if( a[i]== ) {
pr[len++] = i;
int j = i;
while( j < maxn ) {
a[j] = ;
j += i;
}
}
}
} bool judge(int x, int cnt) {
int c = ;
ll t = n;
while( t ) {
c += t/x;
t /= x;
}
return cnt<=c;
} void input() {
get_pr();
while( cin >> n >> m ) {
if( m == ) {
printf("%lld does not divide %lld!\n",m,n);
continue;
}
ll t = m;
bool f = true;
for(int i=; i<len && pr[i]<=m; i++) {
if( m%pr[i] == ) {
res = ;
while( m%pr[i]== ) {
res ++;
m /= pr[i];
}
f = f&&judge(pr[i], res);
}
}
if( m!= ) f = f&&judge(m, );
if( f ) printf("%lld divides %lld!\n",t,n);
else printf("%lld does not divide %lld!\n",t,n);
}
} int main(){
input();
return ;
}

Kattis之旅——Factovisors的更多相关文章

  1. Kattis之旅——Prime Reduction

    A prime number p≥2 is an integer which is evenly divisible by only two integers: 1 and p. A composit ...

  2. Kattis之旅——Chinese Remainder

    Input The first line of input consists of an integers T where 1≤T≤1000, the number of test cases. Th ...

  3. Kattis之旅——Fractional Lotion

    Freddy practices various kinds of alternative medicine, such as homeopathy. This practice is based o ...

  4. Kattis之旅——Rational Arithmetic

    Input The first line of input contains one integer, giving the number of operations to perform. Then ...

  5. Kattis之旅——Number Sets

    You start with a sequence of consecutive integers. You want to group them into sets. You are given t ...

  6. Kattis之旅——Divisible Subsequences

    Given a sequence of positive integers, count all contiguous subsequences (sometimes called substring ...

  7. Kattis之旅——Prime Path

    The ministers of the cabinet were quite upset by the message from the Chief of Security stating that ...

  8. Kattis之旅——Inverse Factorial

    题目意思就是已知n的阶乘,求n. 当输入的阶乘小于10位数的时候,我们可以用long long将字符串转化成数字,直接计算. 而当输入的阶乘很大的时候,我们就可以利用位数去大概的估计n. //Asim ...

  9. Kattis之旅——Perfect Pth Powers

    We say that x is a perfect square if, for some integer b, x = b2. Similarly, x is a perfect cube if, ...

随机推荐

  1. Spark SQL内置函数

    Spark SQL内置函数官网API:http://spark.apache.org/docs/latest/api/scala/index.html#org.apache.spark.sql.fun ...

  2. 使用Python监控Linux系统

    一.Python编写的监控工具 一.多功能系统资源统计工具dstat 1.dstat介绍 dstat是一个用Python语言实现的多功能系统资源统计工具,用来取代Linux下的vmstat.iosta ...

  3. Java 基础 面向对象之关键字内部类代码块修饰符

    final final概念 继承的出现提高了代码的复用性,并方便开发.但随之也有问题,有些类在描述完之后,不想被继承,或者有些类中的部分方法功能是固定的,不想让子类重写.可是当子类继承了这些特殊类之后 ...

  4. SVM高斯核为何会将特征映射到无穷维?【转载】

    转自:https://www.zhihu.com/question/35602879 1.问题: SVM中,对于线性不可分的情况下,我们利用升维,把低维度映射到到维度让数据变得“更可能线性可分”,为了 ...

  5. 日期格式化(类似QQ邮箱中的邮件列表显示日期)

    日期格式化(类似QQ邮箱中的邮件列表显示日期) public static string FormatDateDisplay(DateTime _datetime) { var ts = DateTi ...

  6. the import XXXX cannot be resolved 解决方法

    明明XXX类完全没问题 突然就报错了 解决方法: 原因一:一个项目引用了兄弟项目的类,报错The import XXX cannot be resolved 解决办法:需要在引用的兄弟项目右键选择Ma ...

  7. unity3d-代码控制游戏角色控制器移动

    先上一个gif看看效果.因为图片大小限制.所以录制的比较小.个人认为效果比较牵强.特别是里面的逻辑代码. 不过我还是认为一切是为了先实现,因为我是刚接触的新手. 工程结构图 这次实现的效果是: 1:摄 ...

  8. 关于随机数、方法重载和System.out.println()的认识

    (1)使用纯随机数发生器编写一个指定数目内数字的程序(类真随机数) 源代码: package Demo1; public class trueRandom { long Multiplier = 45 ...

  9. phpstudy的使用

    1.第一步是下载phpstudy,你可以百度去下载,也可以通过下面我分享的网盘下载 链接:https://pan.baidu.com/s/1E_CXIrKv1N-jrlA4KCovZA 密码:mkx9 ...

  10. JAVA多线程常用例子

    详情见该页面ttps://www.cnblogs.com/pureEve/p/6524366.html