UVa10484 - Divisibility of Factors(数论)

Given two integers N and D, you will have to find how many of the factors of N! (factorial N) are divisible by D.

 

Input

The input file contains several lines of input. Each line contains two integers N (0<=N<= 100) and D(0<|D|<=2^31-1). Input is terminated by a line containing two zeroes. This line should not be processed.

 

Output

For each line of input produce one line of output. This line contains a single integer, which denotes of many different factors of N! are divisible byD.

 

Sample Input
10 2

9 3

0 0 

 

Sample Output
240

128

主要用到素数筛选法,求出50000内的素数

#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>

using namespace std;

const int N = 50000;

vector<int> vPrime;
bool vis[N];
int n, d;

void calc(int n, map<int, int>& m);
void sieve_of_sundaram();
bool input();
void solve();

int main()
{
#ifndef ONLINE_JUDGE
    freopen("e:\\uva_in.txt", "r", stdin);
#endif

    sieve_of_sundaram();

    while (input()) {

        solve();
    }
    return 0;
}

bool input()
{
    scanf("%d%d", &n, &d);
    if (n == 0 && d == 0) return false;

    d = abs(d);
    return true;
}

void solve()
{
    if (n == 0 && d == 1) {
        printf("1\n");
        return;
    }
    map<int, int> nmap, dmap;
    for (int i = 2; i <= n; i++) calc(i, nmap);

    calc(d, dmap);

    for (map<int, int>::iterator it = dmap.begin(); it != dmap.end(); it++) {
        if (nmap.count(it->first) == 0) {
            printf("0\n");
            return;
        }

        if (nmap[it->first] < it->second) {
            printf("0\n");
            return;
        }

        nmap[it->first] -= it->second;
    }

    long long ans = 1;
    for (map<int, int>::iterator it = nmap.begin(); it != nmap.end(); it++) {
        ans *= (nmap[it->first] + 1);
    }

    printf("%lld\n", ans);
}

void sieve_of_sundaram()
{
    memset(vis, false, sizeof(vis));

    int m = (int)sqrt(N / 2);
    for (int i = 1; i < m; i++) {
        if (vis[i]) continue;
        for (int k = 2 * i + 1, j = 2 * i * (i + 1); j < N; j += k) {
            vis[j] = true;
        }
    }

    vPrime.push_back(2);
    for (int i = 1; i < N / 2; i++) {
        if (!vis[i]) vPrime.push_back(2 * i + 1);
    }
}

void calc(int n, map<int, int>& m)
{
    for (size_t i = 0; i < vPrime.size(); i++) {
        int cnt = 0;
        if (n < vPrime[i]) break;

        if (n % vPrime[i] == 0) {
            while (n % vPrime[i] == 0) {
                n /= vPrime[i];
                cnt++;
            }
            m[vPrime[i]] += cnt;

        }
    }

    if (n != 1) m[n] += 1;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值