Divisors

题目链接(点击)

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 line containing two integers n and k (0 ≤ k ≤ n ≤ 431), separated by a single space.

Output

For each instance, output a line containing exactly one integer -- the number of distinct divisors of Cnk. For the input instances, this number does not exceed 2 63 - 1.

Sample Input

5 1
6 3
10 4

Sample Output

2
6
16

思路:

之前用求逆元的方法可以将组合数的最终值求出来,但是这个题目并没有给出要mod的值,所以先将结果求出来然后用唯一分解定理求因子个数的思路行不通。(即使求出来,结果也肯定会爆LL,导致根本无法将整个数保留下来求因子个数)

后来尝试将阶乘的前几个的值求出来然后求因子个数最后将 每个个数 相乘就得到结果

例如:C(9,3)

= 9 * 8 * 7

3 * 2 * 1

先将   9/3=3 的因子个数 2

8/2=4 的因子个数 3

7/1=7 的因子个数 2

然后   相乘 即: 2*3*2=12 即为答案

但是这样也是错的,因为这样 可能会导致后面的数分母不能化为1

例如: C(10,5)

= 10 * 9 * 8 * 7 * 6

5  * 4 * 3 * 2 * 1

先将10/5=2的 因子个数 2

9/4= ??这就错了

*************************************************************************************************************************************************

后来听同学的思路:

1.将1~431的每个素数个数用数组存起来

例: 10

sum[10][2]=1;

sum[10][3]=0;

sum[10][5]=1;

:

:

同时最好可以在打表的时候将n!对应素数个数直接存起来(特别好实现):

 num[i][prime[j]]=num[i-1][prime[j]]+count1  ///就可以了

2.求出分母的每个素数的个数

3.求出分子的每个素数的个数

4.将上面的数目对应相减就是最终值通过唯一分解定理得出的素数乘积

例:C(9,3)

9 =3^2               3=3^1

8=2^3                2=2^1

7=7^1                1=2^0

即:   3^2 * 2^3 * 7^1

3^1 * 2^1 * 2^0

结果: 3^1 * 7^1 * 2^2

所以:   (1+1)* (1+1) * (2+1)=12;

*************************************************************************************************************************************************

AC代码:

#include<stdio.h>
typedef long long LL;
int prime[505],flag[505];
void allprime()
{
int count=0;
for(int i=2;i<=431;i++){
if(flag[i]==0){
prime[count++]=i;
}
for(int j=0;j<count&&prime[j]*i<=431;j++){
flag[i*prime[j]]=1;
if(i%prime[j]==0){
break;
}
}
}/// 82个
} //欧拉筛素数
int num[505][505];
int up,down;
int main()
{
allprime();
for(int i=1;i<=431;i++){
int num1=i;
for(int j=0;j<=82;j++){
int count1=0;
while(num1%prime[j]==0){
count1++;
num1/=prime[j];
}
num[i][prime[j]]=num[i-1][prime[j]]+count1;
}
} //打表
//printf("*%d\n",num[9][3]);
int n,k;
while(~scanf("%d%d",&n,&k)){
LL sum=1;
for(int i=0;i<=82;i++){
up=num[n][prime[i]]-num[n-k][prime[i]];
down=num[k][prime[i]];
sum*=(up-down+1);
}
printf("%lld\n",sum);
}
return 0;
}

Divisors (求解组合数因子个数)【唯一分解定理】的更多相关文章

  1. HDU-1492-The number of divisors(约数) about Humble Numbers -求因子总数+唯一分解定理的变形

    A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, ...

  2. POJ1845Sumdiv(求所有因子和 + 唯一分解定理)

    Sumdiv Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 17387   Accepted: 4374 Descripti ...

  3. Divisors_组合数因子个数

    Description Your task in this problem is to determine the number of divisors of Cnk. Just for fun -- ...

  4. POJ 2992 Divisors (求因子个数)

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

  5. 2018.09.28 牛客网contest/197/A因子(唯一分解定理)

    传送门 比赛的时候由于变量名打错了调了很久啊. 这道题显然是唯一分解定理的应用. 我们令P=a1p1∗a2p2∗...∗akpkP=a_1^{p_1}*a_2^{p_2}*...*a_k^{p_k}P ...

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

    ---恢复内容开始--- We guessed some integer number xx. You are given a list of almost all its divisors. Alm ...

  7. UVA294DIvisors(唯一分解定理+约数个数)

    题目链接 题意:输入两个整数L,U(L <= U <= 1000000000, u - l <= 10000),统计区间[L,U]的整数中哪一个的正约数最多,多个输出最小的那个 本来 ...

  8. hdu4497-GCD and LCM-(欧拉筛+唯一分解定理+组合数)

    GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...

  9. B - Common Divisors (codeforces)数论算法基本定理,唯一分解定理模板

    You are given an array aa consisting of nn integers. Your task is to say the number of such positive ...

随机推荐

  1. Java Web之路一:过滤器(Filter)

    一.过滤器(Filter)简介 过滤器是对web资源进行拦截,做一些处理后再交给下一个过滤器或Servlet处理,主要可以拦截request和response 过滤器是以一种组件的形式与web程序绑定 ...

  2. 第二篇-用Flutter手撸一个抖音国内版,看看有多炫

    前言 继上一篇使用Flutter开发的抖音国际版 后再次撸一个国内版抖音,大部分功能已完成,主要是Flutter开发APP速度很爽,  先看下图 项目主要结构介绍 这次主要的改动在api.dart 及 ...

  3. GRpc添加客户端的四种方式

    随着微服务的发展,相信越来越多的.net人员也开始接触GRpc这门技术,大家生成GRpc客户端的方式也各不相同,今天给大家介绍一下依据Proto文件生成Rpc客户端的四种方式 前提:需要安装4个Nug ...

  4. 50个SQL语句(MySQL版) 问题七

    --------------------------表结构-------------------------- student(StuId,StuName,StuAge,StuSex) 学生表 tea ...

  5. 【HIVE】(1)建表、导入数据、外部表、导出数据

    导入数据 1). 本地 load data local inpath "/root/example/hive/data/dept.txt" into table dept; 2). ...

  6. 前端HTML 定位position 绝对定位 相对定位

    >>>position:absolute;绝对定位 当前元素相对于父级元素位置[该父级元素必须也设定了position,不然会继续往上找祖先元素,直到body为止]的定位 >& ...

  7. 这才是你需要的最基础的.Net基础面试题(通俗易懂,最基础的.Net)2

    51. 委托回调静态方法和实例方法有何区别? 当一个实例方法被调用时,需要通过实例对象来访问,绑定一个实例方法到委托必须同时让委托得到实例方法的代码段和实例对象的信息,这样在委托被回调时候.NET才能 ...

  8. Java实现 LeetCode 678 有效的括号字符串(暴力+思路转换)

    678. 有效的括号字符串 给定一个只包含三种字符的字符串:( ,) 和 *,写一个函数来检验这个字符串是否为有效字符串.有效字符串具有如下规则: 任何左括号 ( 必须有相应的右括号 ). 任何右括号 ...

  9. Java实现 LeetCode 1两数之和

    1. 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这 ...

  10. Java实现 LeetCode 122 买卖股票的最佳时机 II

    122. 买卖股票的最佳时机 II 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意: ...