X-factor Chains [POJ3421] [素数]
Description
给定一个正整数X, 一个长度为m的X-因子链是由m+1个整数组成的。其中
1 = X0, X1, X2, …, Xm = X 满足Xi < Xi+1 且 Xi 整除 Xi+1 。
现在要求X-因子链的最大长度和最大长度有多少条?
Input
多组数据,每一组数据一个正整数X (X ≤ 220).
Output
对于每组数据,输出X-因子链的最大长度和最大长度有多少条
Sample Input
2
3
4
10
100
Sample Output
1 1
1 1
2 1
2 2
4 6
Analysis
1.子链的长度就是分解质因数后每个质因数个数的和。为什么?如果出现一个合数,必定可以再分解使子链加长。
2.剩下的工作就是dfs,注意每一步只能选泽一种质数去分解。分解到1记一个贡献
Code
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define RG register int
#define rep(i,a,b) for(RG i=a;i<=b;++i)
#define per(i,a,b) for(RG i=a;i>=b;--i)
#define ll long long
#define inf (1<<29)
#define maxn 1048580
#define lim 1048578
using namespace std;
int n,cnt,pt,ans,ans1;
int isp[maxn],p[maxn];
int num[maxn][];
inline int read()
{
int x=,f=;char c=getchar();
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} void pre()
{
rep(i,,lim)
{
if(!isp[i]) p[++cnt]=i;
for(RG j=;j<=cnt&&p[j]*i<=lim;j++)
{
isp[i*p[j]]=;
if(!(i%p[j])) break;
}
}
} void work()
{
int res=n;
rep(i,,cnt)
{
if(!(res%p[i])){
num[++pt][]=p[i],num[pt][]=;
while(!(res%p[i])) ++num[pt][],res/=p[i],++ans1;
}
if(res==) break;
}
} void dfs(int res)
{
if(res==){ans++;return;}
for(int i=;i<=pt;i++)
{
if(num[i][])
{
num[i][]--;
dfs(res/num[i][]);
num[i][]++;
}
}
} int main()
{
pre();
while(~scanf("%d",&n))
{
pt=ans=ans1=;
work();
dfs(n);
printf("%d %d\n",ans1,ans);
}
return ;
}
X-factor Chains [POJ3421] [素数]的更多相关文章
- X-factor Chains(POJ3421 素数)
X-factor Chains Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6212 Accepted: 1928 D ...
- 杭电 2136 Largest prime factor(最大素数因子的位置)
Largest prime factor Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- UVA 11610 Reverse Prime (数论+树状数组+二分,难题)
参考链接http://blog.csdn.net/acm_cxlove/article/details/8264290http://blog.csdn.net/w00w12l/article/deta ...
- HDU题解索引
HDU 1000 A + B Problem I/O HDU 1001 Sum Problem 数学 HDU 1002 A + B Problem II 高精度加法 HDU 1003 Maxsu ...
- poj3421 X-factor Chains(重复元素的全排列)
poj3421 X-factor Chains 题意:给定正整数$x(x<=2^{20})$,求$x$的因子组成的满足任意前一项都能整除后一项的序列的最大长度,以及满足最大长度的子序列的个数. ...
- HDOJ/HDU 2710 Max Factor(素数快速筛选~)
Problem Description To improve the organization of his farm, Farmer John labels each of his N (1 < ...
- HDOJ(HDU) 2136 Largest prime factor(素数筛选)
Problem Description Everybody knows any number can be combined by the prime number. Now, your task i ...
- POj3421 X-factor Chains(质因数分解+排列组合)
POj3421X-factor Chains 一开始没读懂题意,不太明白 Xi | Xi+1 where a | b means a perfectly divides into b的意思,后来才发现 ...
- Max Factor(素数筛法)题解
Max Factor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
随机推荐
- hive建表范例
建表范例:支持update和delete create table aaa( id string, visitor_name string ) clustered by(id) into bucket ...
- MySQL查看库表的大小
MySQL数据库空间使用情况查询 如果想知道MySQL数据库中每个表占用的空间.表记录的行数的话,可以打开MySQL的 information_schema 数据库.在该库中有一个 TABLES 表, ...
- CMake在Visual Studio下保持目录结构
CMake在Visual Studio下保持目录结构 原理 主要通过CMAKE自带函数source_group来设定. 需要把add_executable()函数进行封装,包裹一层source_gro ...
- XmlSerializer 实现序列化CDATA
[XmlIgnore] public string GuestRemarks { get; set; } [XmlElement("GuestRemarks")] public X ...
- 树递归写法ref实现
using System; using System.Collections.Generic; using System.Linq; namespace ConsoleAppTest { class ...
- 在CentOS 7+ 安装Kubernetes入门(单Master)
TL;DR; ***,***,***,重要的事情说三次.如果不会***,这篇文章就没有看下去的意义.作为一个技术人员如果不愿意折腾,很难有所作为.作为一个单纯的技术人员,最好把心思放在技术上,做到真正 ...
- selenium相关:通过location 和 size 获取元素所在像素位置和尺寸,截取图片ROI
1.实验 #https://captcha.luosimao.com/demo/ chrome default: location 不滚动,直接返回相对整个html的坐标 {'x': 15.0, 'y ...
- 清除DNS缓存(解决能上QQ但是无法上网页问题)
ipconfig/displaydnsipconfig/flushdns
- oracle授权表权限给用户
命令:grant xxx权限 on Table to USERgrant select,insert,update,delete,all on 表名 to 用户名例如:将test表的查询权限赋予to ...
- dubbo spring bean id冲突
service-security-provider应用有provider和consumer配置文件 其中secutrity-consumer引用两个服务 <dubbo:reference int ...