Kattis之旅——Factovisors

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 = 76000;
ll n, m, s, res, ans, len, T, k, num;
int x, y;
int pr[maxn];

void get_pr(){
    int a[maxn] = {0};
    len = 0;
    for(int i=2; i<maxn; i++) {
        if( a[i]==0 ) {
            pr[len++] = i;
            int j = i;
            while( j < maxn ) {
                a[j] = 1;
                j += i;
            }
        }
    }
}

bool judge(int x, int cnt) {
    int c = 0;
    ll t = n;
    while( t ) {
        c += t/x;
        t /= x;
    }
    return cnt<=c;
}

void input() {
    get_pr();
    while( cin >> n >> m ) {
        if( m == 0 ) {
            printf("%lld does not divide %lld!\n",m,n);
            continue;
        }
        ll t = m;
        bool f = true;
        for(int i=0; i<len && pr[i]<=m; i++) {
            if( m%pr[i] == 0 ) {
                res = 0;
                while( m%pr[i]==0 ) {
                    res ++;
                    m /= pr[i];
                }
                f = f&&judge(pr[i], res);
            }
        }
        if( m!=1 ) f = f&&judge(m, 1);
        if( f ) printf("%lld divides %lld!\n",t,n);
        else printf("%lld does not divide %lld!\n",t,n);
    }
}

int main(){
    input();
    return 0;
}

 

全部评论

相关推荐

Lorn的意义:你这种岗位在中国现在要么牛马天天加班,要么关系户进去好吃好喝,8年时间,真的天翻地覆了,对于资本来说你就说一头体力更好的牛马,哎,退伍没有包分配你真的亏了。
点赞 评论 收藏
分享
06-07 17:17
嘉兴学院 教师
心爱的idea:你孩
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 17:10
什么素质,我请问呢,要掉小珍珠了。。。又憋屈又生气
Steven267:这不喷回去?花钱是大爷,记住这个道理
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务