POJ2992 Divisors(因子个数)

题意:给n和k,求组合C(n,k)的因子个数。

这道题,若一开始先预处理出C[i][j]的大小,再按普通方法枚举2~sqrt(C[i][j])来求解对应的因子个数,会TLE。
所以得用别的方法。

在说方法前,先说一个n!的性质:
n!的素因子分解中的素数p的个数为
n/p+n/(p^2)+...+n/(p^k)+...

《ACM-ICPC程序设计系列 数论及应用》上的方法,200+ms:
首先先求解435以内的素因子。
然后预处理出j!中每个素因子的个数,公式如下:
num[j][i]=j/prime[i]+num[j/prime[i]][i];

设n!中素因子p的个数为:a=n/p+n/(p^2)+...+n/(p^k)+...
那么(n/p)!中素因子p的个数为:b=n/(p^2)+...+n/(p^k)+...
很显然a=b+n/p,因此可以利用上述递推公式预处理出所有的j!中每个素因子的个数。

接下来就可以预处理出C(i,j)的因子个数,然后一切就好办了。

#include <iostream>
#include <cstdio>
#include <cstring>
#define ll long long

using namespace std;
const int maxn=435;
bool isprime[maxn];
int prime[maxn];
int num[maxn][maxn];
ll C[maxn][maxn];
int cnt=0;

void init()
{
    int cnt=0;
    memset(isprime,true,sizeof(isprime));
    for(int i=2;i<maxn;i++){
        if(isprime[i]){
            prime[cnt++]=i;
            for(int j=i*2;j<maxn;j+=i){
                isprime[j]=false;
            }
        }
    }
    memset(num,0,sizeof(num));
    for(int i=0;i<cnt;i++){
        for(int j=1;j<maxn;j++){
            num[j][i]=j/prime[i]+num[j/prime[i]][i];
        }
    }
    for(int i=1;i<maxn;i++)
    {
        for(int j=1;j<i;j++)
        {
            C[i][j]=1;
            for(int k=0;k<cnt;k++)
            {
                int d=num[i][k]-num[i-j][k]-num[j][k];
                if(d)
                    C[i][j]*=d+1;
            }
        }
    }
}

int main()
{
    init();
    int n,k;
    while(scanf("%d%d",&n,&k)!=EOF){
        if(n==k ||k==0)
            printf("1\n");
        else
            printf("%lld\n",C[n][k]);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Fy1999/p/9788241.html

时间: 2024-08-30 05:53:37

POJ2992 Divisors(因子个数)的相关文章

UVA 294 - Divisors 因子个数

Mathematicians love all sorts of odd properties of numbers. For instance, they consider 945 to be an interesting number, since it is the first odd number for which the sum of its divisors is larger than the number itself. To help them search for inte

POJ 2992 Divisors 求组合数因子个数

题目来源:POJ 2992 Divisors 题意:... 思路:素数分解的唯一性 一个数可以被分解成若干素数相乘 p1^x1*p2^x2*...*pn^xn 根据乘法原理 因子数为 (x1+1)*(x2+1)*...*(xn+1) 不能直接求出组合数 会溢出 也不能把每个乘的数分解因子 这样会超时 C(N,M)=N!/(M!*(N-M)!) 另dp[i][j] 代表为i的阶乘中j因子的个数(j是素数) 那么i素数的个数为dp[n][i]-dp[m][i]-dp[n-m][i] 最后for循环从

Almost All Divisors(求因子个数及思维)

---恢复内容开始--- We guessed some integer number xx. You are given a list of almost all its divisors. Almost all means that there are all divisors except 11and xx in the list. Your task is to find the minimum possible integer xx that can be the guessed nu

Divisors_组合数因子个数

Description Your task in this problem is to determine the number of divisors of Cnk. Just for fun -- or do you need any special reason for such a useful computation? Input The input consists of several instances. Each instance consists of a single li

Easy Number Challenge(暴力,求因子个数)

Easy Number Challenge Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 236B Appoint description:  System Crawler  (2016-04-26) Description Let's denote d(n) as the number of divisors of a

POJ-2992 Divisors(数学知识)

Divisors Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12085   Accepted: 3600 Description Your task in this problem is to determine the number of divisors of Cnk. Just for fun -- or do you need any special reason for such a useful comp

HDOJ(HDU) 2521 反素数(因子个数~)

Problem Description 反素数就是满足对于任意i(0< i < x),都有g(i) < g(x),(g(x)是x的因子个数),则x为一个反素数.现在给你一个整数区间[a,b],请你求出该区间的x使g(x)最大. Input 第一行输入n,接下来n行测试数据 输入包括a,b, 1<=a<=b<=5000,表示闭区间[a,b]. Output 输出为一个整数,为该区间因子最多的数.如果满足条件有多个,则输出其中最小的数. Sample Input 3 2 3

POJ 2992 求组合数的因子个数

求C(n,k)的因子个数 C(n,k) = (n*(n-1)*...*(n-k+1))/(1*2*...*k) = p1^k1 * p2^k2 * ... * pt^kt 这里只要计算出分子中素数因子个数减去分母中的个数 然后每一种因子都有 (cnt+1)种取的可能,乘一下就出来了 但是不能逐个因子分解,试了两次都错了,后来初始的时候,先将这432个数提前预处理分解好保存到vector中 然后用的时候直接提取就行 不然会因为数据量太大超时的 1 #include <iostream> 2 #i

Acdream1084 寒假安排 求n!中v因子个数

题目链接:点击打开链接 寒假安排 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 128000/64000 KB (Java/Others) SubmitStatistic Next Problem Problem Description 寒假又快要到了,不过对于lzx来说,头疼的事又来了,因为众多的后宫都指望着能和lzx约会呢,lzx得安排好计划才行. 假设lzx的后宫团有n个人,寒假共有m天,而每天只能跟一位后宫MM约会,并且由于后宫