POJ 3421 X-factor Chains (因式分解+排列组合)
2024-10-09 05:38:03
题意:一条整数链,要求相邻两数前一个整除后一个。给出链尾的数,求链的最大长度以及满足最大长度的不同链的数量。
类型:因式分解+排列组合
算法:因式分解的素因子个数即为链长,链中后一个数等于前一个数乘以某素因子,所以链的数量即为这些因子不全相异的全排列数:A!/(a1!a2!a3!..)
#include <cstdio>
#include <cmath>
#include <iostream>
using namespace std; typedef long long LL;
int p[22], a[22];
int cnt; int main() {
int n, i, j;
while (scanf("%d", &n) != EOF) {
cnt = 0;
for (i = 2; i <= sqrt(n); ++i) { // 质因子分解
if (n % i == 0) {
p[cnt] = i;
n /= i;
a[cnt++] = 1;
}
while (n % i == 0) {
a[cnt - 1]++;
n /= i;
}
}
if (n > 1) { // 注意点,容易忘记
p[cnt] = n;
a[cnt++] = 1;
}
LL ans = 1;
int A = 0;
for (i = 0; i < cnt; ++i) {
A += a[i];
}
for (i = a[0] + 1; i <= A; ++i) { // 提前除以a[0]!
ans *= i;
}
for (i = 1; i < cnt; ++i) {
for (j = 2; j <= a[i]; ++j) {
ans /= j;
}
}
printf("%d %lld\n", A, ans);
}
return 0;
}
最新文章
- [LeetCode] Integer to Roman 整数转化成罗马数字
- beaglebone black 烧写系统后释放空间。
- 设计模式-观察者模式(Observer Model)
- C语言中的参数传递
- IsBackground的理解
- ubuntu Screen 的比较详细的命令
- maven pom.xml报错
- java字符串练习题
- [ActionScript 3.0] AS3 对XML的操作,创建、删除、增加节点方法
- HDU4612 Warm up 边双连通分量&;&;桥&;&;树直径
- springside springmvc 的一个SB问题
- [转]Ubuntu 软件安装、查找、卸载--apt-get、apt-cache命令安全
- Windows下PHP(Thread Safe与Non Thread Safe)版本说明
- accp8.0转换教材第9章JQuery相关知识理解与练习
- angular2 实现的小项目
- Open-Source Service Discovery
- android 开发中 sdk 无法更新
- Autodesk系列软件下载
- JS 通过字符串取得对应对象
- Flume和Kafka整合安装
热门文章
- Spring面向切面编程AOP(around)实战
- VUE2.0 饿了吗视频学习笔记(三):VUE2.0取消了v-link
- 序列化时提示There was an error reflecting type &#39;System.Collections.Generic.List`1
- Host is not allowed to connect to this MySQL server解决方法
- js 获取DOM的style属性
- Linux - vim 编辑器
- div 只显示两行超出部分隐藏
- HDU 2522 A simple problem (模拟)
- react框架实现点击事件计数小案例
- VGG 参数分析 转