poj3421 X-factor Chains
题意:
Description
Given a positive integer X, an X-factor chain of length m is a sequence of integers,
1 = X0, X1, X2, …, Xm = X
satisfying
Xi < Xi+1 and Xi | Xi+1 where a | b means a perfectly divides into b.
Now we are interested in the maximum length of X-factor chains and the number of chains of such length.
Input
The input consists of several test cases. Each contains a positive integer X (X ≤ 220).
Output
For each test case, output the maximum length and the number of such X-factors chains.
Sample Input
2 3 4 10 100
Sample Output
1 1 1 1 2 1 2 2 4 6
思路:
一开始想到dp。令dp[i][j]表示长度为i,以j结尾的链的个数,于是dp[i+1][k] += dp[i][j] (j为k的因子),然而复杂度高,并不会优化。
后来发现要想链最长,只能从1开始,每次乘上这个数的某个质因子才行。于是就变成了分解质因子+排列组合的问题。
实现:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 #include <algorithm> 6 #include <map> 7 using namespace std; 8 typedef long long ll; 9 10 ll fac[21]; 11 int x; 12 13 void init() 14 { 15 fac[0] = 1; 16 for (ll i = 1; i <= 20; i++) 17 fac[i] = fac[i - 1] * i; 18 } 19 20 map<int, int> prime_factor(int n) 21 { 22 map<int, int> res; 23 for (int i = 2; i * i <= n; i++) 24 { 25 while (n % i == 0) 26 { 27 res[i]++; 28 n /= i; 29 } 30 } 31 if (n != 1) 32 res[n] = 1; 33 return res; 34 } 35 36 int main() 37 { 38 init(); 39 while (scanf("%d", &x) != EOF) 40 { 41 map<int, int> f = prime_factor(x); 42 map<int, int>::iterator it; 43 ll res = 1; 44 int cnt = 0; 45 for (it = f.begin(); it != f.end(); it++) 46 { 47 res *= fac[it->second]; 48 cnt += it->second; 49 } 50 printf("%d %lld\n", cnt, fac[cnt] / res); 51 } 52 return 0; 53 }
转载于:https://www.cnblogs.com/wangyiming/p/6443982.html
poj3421 X-factor Chains相关推荐
- POJ3421 X-factor Chains【分解质因子+组合数学】
问题链接:POJ3421 X-factor Chains. 题意简述:输入正整数x,求x的因子组成的满足任意前一项都能整除后一项的序列的最大长度,以及所有不同序列的个数. 问题分析: 首先要对x进行因 ...
- Using Markov Chains for Android Malware Detection
If you're chatting with someone, and they tell you "aslkjeklvm,e,zk3l1" then they're speak ...
- c++ Factor泛型编程示例
c++ Factor泛型编程示例 c++ 泛型编程 之Factor (c++ 设计新思维) 一.概述 泛化仿函数是将"请求(函数)封装起来",存储与对象中,该对象是具有" ...
- android约束布局中 链,Android-ConstraintLayout(约束布局)-Chains链(链条布局,Nice)
到Chains这个部分了.之前的新项目做得登录,注册,重置密码等暂时还没用到这种.不过后面可能随着新的设计可能会涉及到.所以赶紧过来看看先.新项目基本就打算全部用约束布局实现了.实际用了也会越来越熟悉 ...
- GPB编辑部招聘启事 2019 Impact Factor破7;CiteScore破10
<基因组蛋白质组与生物信息学报>(Genomics, Proteomics & Bioinformatics,GPB)是由中国科学院北京基因组研究所(国家生物信息中心)和中国遗传学 ...
- Error in Math.factor() : ‘sqrt’ not meaningful for factors
Error in Math.factor() : 'sqrt' not meaningful for factors 目录 Error in Math.factor() : 'sqrt' not me ...
- R语言将多分类数据集转化为二分类数据集,使用条件判断将多分类转化为二分类(transform dataset into a dichotomous factor response dataset)
R语言将多分类数据集转化为二分类数据集,使用条件判断将多分类转化为二分类(transform dataset into a dichotomous factor response dataset) 目 ...
- R语言使用psych包的fa函数对指定数据集进行因子分析(输入数据为相关性矩阵)、使用rotate参数指定进行斜交旋转提取因子、使用factor.plot函数可视化斜交旋转因子分析、并解读可视化图形
R语言使用psych包的fa函数对指定数据集进行因子分析(输入数据为相关性矩阵).使用rotate参数指定进行斜交旋转提取因子.使用factor.plot函数可视化斜交旋转因子分析.并解读可视化图形 ...
- R语言ggplot2可视化:使用ggplot2按照热力图的方式显示全是分类变量的dataframe数据、并自定义因子(factor)的图例颜色legend
R语言ggplot2可视化:使用ggplot2按照热力图的方式显示全是分类变量的dataframe数据.并自定义因子(factor)的图例颜色legend(use heatmap to visuali ...
最新文章
- 找不到具有指定ID的对话框类解决方法
- phy芯片测试寄存器_PCIe 5.0首秀!7nm IP方案已成熟!PCIe 5.0的芯片设计有多难?...
- 中石油训练赛 - 小A进学校(唯一分解定理)
- spock 集成测试_Spock 1.2 –轻松进行集成测试中的Spring Bean模拟
- JAVA中String类以形参传递到函数里面,修改后外面引用不能获取到更改后的值
- vue组件化开发实践
- MySQL Spatial Extensions 地理信息
- 关于vmware虚拟机硬件里没有软盘驱动器,而操作系统里还有的解决方法
- java文件内容比较_怎么用JAVA技术编写一个两文件内容比较的程序?
- STM32定时器周期任务函数编写
- 通过Nginx搭建直播带货平台的直播服务器
- 统计函数耗费时间,简单的图像运算,定义感兴趣区域 第二章
- CVPR2019|Depth-Aware Video Frame Interpolation【论文阅读笔记】
- hbase manager 2.0.9 安装
- 致铭主板好礼等着您拿
- js运算符优先级和~~运算符
- JDK 18 / Java 18 GA 来了
- 数据分析记录(六)--多元线性回归在SPSS中的实现(步骤及指标含义)
- 从零玩转七牛云之CDN-qiniuyunzhicdn
- CG100 保时捷锂电池通病维修 此电池装载在保时捷车上,由于长时间停放,导致电池电压过低,电池处于保护模式,内部程序锁死,无法充电,车辆也无法启动。
热门文章
- PXE 01-PXE介绍
- 《Forward团队-爬虫豆瓣top250项目-设计文档》
- III USP Freshmen ContestH. MaratonIME gets candies
- IOS 本地推送 IOS10.0以上 static的作用 const的作用
- 从零开始编写自己的C#框架(20)——框架异常处理及日志记录
- 错误 1093 You can't specify target table 'table name' for update in FROM clause
- 在Panel上绘图的实现
- TCP 端口监听队列原理
- GridView实现删除时弹出确认对话框
- Ubuntu 19.04 Beta 发布,正式版定于 4 月