POj3421 X-factor Chains(质因数分解+排列组合)
一开始没读懂题意,不太明白 Xi | Xi+1 where a | b means a perfectly divides into b的意思,后来才发现是要满足后一个数是前一个数的倍数
题目要求1 = X0, X1, X2, …, Xm = X,并且后一个数是前一个数的倍数,为了得到最长链,必须将数X进行质因数分解,
假设X=(a[1]^b[1])*...*(a[i]^b[i])*..(a[n]^b[n]),设m=b[1]+b[2]+..b[i]+b[n];
a[i]为X的质因数,b[i]为X的质因数为a[i]的个数,m为总的质因数的个数(质因数不包含1,1不是质数)
接下来就是一个排列组合问题了,注意m为总的质因数的个数
为了得到最长链,由于最后一位为m个质因数相乘,则前一位可以任取m-1个质因数相乘,再前一位可选m-2个质因数相乘,以此类推,第一位X1(非X
0)可任取1个质因数.
由此可知最长链的长度为m(m为总的质因数的个数)(因为任意一个质因数都不能再分解);
设fact(n)为n的阶乘
则最长链的个数=fact(m)/fact(a[1])/fact(a[2]).../fact(a[i])/../fact(a[n]);
一开始交了两次都TLE了,后来直接在质因数分解里面计算阶乘就A了.
TLE代码:
/*
* Created: 2016年03月30日 10时34分42秒 星期三
* Author: Akrusher
*
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define in(n) scanf("%d",&(n))
#define in2(x1,x2) scanf("%d%d",&(x1),&(x2))
#define inll(n) scanf("%I64d",&(n))
#define inll2(x1,x2) scanf("%I64d%I64d",&(x1),&(x2))
#define inlld(n) scanf("%lld",&(n))
#define inlld2(x1,x2) scanf("%lld%lld",&(x1),&(x2))
#define inf(n) scanf("%f",&(n))
#define inf2(x1,x2) scanf("%f%f",&(x1),&(x2))
#define inlf(n) scanf("%lf",&(n))
#define inlf2(x1,x2) scanf("%lf%lf",&(x1),&(x2))
#define inc(str) scanf("%c",&(str))
#define ins(str) scanf("%s",(str))
#define out(x) printf("%d\n",(x))
#define out2(x1,x2) printf("%d %d\n",(x1),(x2))
#define outf(x) printf("%f\n",(x))
#define outlf(x) printf("%lf\n",(x))
#define outlf2(x1,x2) printf("%lf %lf\n",(x1),(x2));
#define outll(x) printf("%I64d\n",(x))
#define outlld(x) printf("%lld\n",(x))
#define outc(str) printf("%c\n",(str))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define mem(X,Y) memset(X,Y,sizeof(X));
typedef vector<int> vec;
typedef long long ll;
typedef pair<int,int> P;
const int dx[]={,,-,},dy[]={,,,-};
const int INF=0x3f3f3f3f;
const ll mod=1e9+;
ll powmod(ll a,ll b) {ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
const bool AC=true; ll all,temp,num;
map <int,int> p;
ll fact(int n){
ll res=;
for(ll i=;i<=ll(n);i++){
res*=i;
}
return res;
}
map<int,int> factor;
map<int,int> prime_factor(int n){
map<int,int> res;
for(int i=;i*i<=n;i++){
while(n%i==){
++res[i];
n/=i;
}
}
if(n!=){
res[n]=; //
}
return res;
}
int main()
{
int n;
while(in(n)==){
all=;temp=;num=;
p=prime_factor(n);
for(int i=;i<=n;i++){ //本身可能也是质因数
if(p[i]!=){
num+=p[i];
temp*=fact(p[i]);
}
}
all=fact(num);
printf("%lld %lld",num,all/temp);
}
return ;
}
以下为AC代码
/*
* Created: 2016年03月30日 10时34分42秒 星期三
* Author: Akrusher
*
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define in(n) scanf("%d",&(n))
#define in2(x1,x2) scanf("%d%d",&(x1),&(x2))
#define inll(n) scanf("%I64d",&(n))
#define inll2(x1,x2) scanf("%I64d%I64d",&(x1),&(x2))
#define inlld(n) scanf("%lld",&(n))
#define inlld2(x1,x2) scanf("%lld%lld",&(x1),&(x2))
#define inf(n) scanf("%f",&(n))
#define inf2(x1,x2) scanf("%f%f",&(x1),&(x2))
#define inlf(n) scanf("%lf",&(n))
#define inlf2(x1,x2) scanf("%lf%lf",&(x1),&(x2))
#define inc(str) scanf("%c",&(str))
#define ins(str) scanf("%s",(str))
#define out(x) printf("%d\n",(x))
#define out2(x1,x2) printf("%d %d\n",(x1),(x2))
#define outf(x) printf("%f\n",(x))
#define outlf(x) printf("%lf\n",(x))
#define outlf2(x1,x2) printf("%lf %lf\n",(x1),(x2));
#define outll(x) printf("%I64d\n",(x))
#define outlld(x) printf("%lld\n",(x))
#define outc(str) printf("%c\n",(str))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define mem(X,Y) memset(X,Y,sizeof(X));
typedef vector<int> vec;
typedef long long ll;
typedef pair<int,int> P;
const int dx[]={,,-,},dy[]={,,,-};
const int INF=0x3f3f3f3f;
const ll mod=1e9+;
ll powmod(ll a,ll b) {ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
const bool AC=true; ll all,temp,num;
int n;
map <int,int> p;
ll fact(int b){ //计算阶乘
ll res=;
for(ll i=;i<=ll(b);i++){
res*=i;
}
return res;
}
void prime_factor(int n){ //质因数分解
map<int,int> res;
for(int i=;i*i<=n;i++){
while(n%i==){
++res[i];
n/=i;
all++; //all是所有质因数的个数
temp*=res[i];//计算重复的组数(在外面计算会超时)
}
}
if(n!=){ //能分解得到的最大质因数
res[n]=;
all++;
}
}
int main()
{
while(in(n)==){
all=;temp=;num=;//注意初始化
prime_factor(n);
num=all;//保存最长链的长度
all=fact(num);//计算总的阶乘
printf("%lld %lld\n",num,all/temp);
}
return ;
}
POj3421 X-factor Chains(质因数分解+排列组合)的更多相关文章
- LightOJ 1028 - Trailing Zeroes (I) 质因数分解/排列组合
题意:10000组数据 问一个数n[1,1e12] 在k进制下有末尾0的k的个数. 思路:题意很明显,就是求n的因子个数,本来想直接预处理欧拉函数,然后拿它减n就行了.但注意是1e12次方法不可行.而 ...
- 2018.10.26 poj3421X-factor Chains(数论+排列组合)
传送门 排列组合入门题. 令X=p1a1p2a2..pkakX=p_1^{a_1}p_2^{a_2}..p_k^{a_k}X=p1a1p2a2..pkak 那么答案1就等于∑i=1kai\ ...
- csu 1801(合数分解+排列组合)
1801: Mr. S’s Romance Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 15 Solved: 5[Submit][Status][W ...
- hdu 4497 GCD and LCM 质因素分解+排列组合or容斥原理
//昨天把一个i写成1了 然后挂了一下午 首先进行质因数分解g=a1^b1+a2^b2...... l=a1^b1'+a2^b2'.......,然后判断两种不可行情况:1,g的分解式中有l的分解式中 ...
- poj 3421 X-factor Chains——质因数分解
题目:http://poj.org/problem?id=3421 记忆化搜索竟然水过去了.仔细一想时间可能有点不对,但还是水过去了. #include<iostream> #includ ...
- POJ3421(质因数分解)
X-factor Chains Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6501 Accepted: 2023 D ...
- HDU 4497 GCD and LCM(分解质因子+排列组合)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4497 题意:已知GCD(x, y, z) = G,LCM(x, y, z) = L.告诉你G.L,求满 ...
- POJ 3421 X-factor Chains (因式分解+排列组合)
题意:一条整数链,要求相邻两数前一个整除后一个.给出链尾的数,求链的最大长度以及满足最大长度的不同链的数量. 类型:因式分解+排列组合 算法:因式分解的素因子个数即为链长,链中后一个数等于前一个数乘以 ...
- [bzoj 1005][HNOI 2008]明明的烦恼(prufer数列+排列组合)
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1005 分析: 首先prufer数列:http://baike.baidu.com/view/1 ...
随机推荐
- phpCMS V9 自定义添加 全局变量{SKIN_PATH}方法
前言:目前v9版本新增{js_path},{css_path},{img_path}三个全局变量,代替2008版本中{skin_path},样式图片脚本分开路径,确实达到了一定在后台管理方便的目的,但 ...
- 手机端禁止iPhone字体放大
/*禁止iphone字体放大 */ html { -webkit-text-size-adjust: none; }
- 【学习笔记】【oc】类的包装类 协议 category
1.类的两种包装类: 将基本数据包装成对象:NSValue:NSNumber; NSValue是NSNumber的父类, NSValue用来封装一些基本数据, NSValue是一个通用的包装类,用来包 ...
- web Service试用简例
1.打开文件,选择新建Asp.Net web服务. 2.出现新建页面如下. using System; using System.Collections.Generic; using System.L ...
- Sicily shortest path in unweighted graph
题目介绍: 输入一个无向图,指定一个顶点s开始bfs遍历,求出s到图中每个点的最短距离. 如果不存在s到t的路径,则记s到t的距离为-1. Input 输入的第一行包含两个整数n和m,n是图的顶点 ...
- (摘)SP2-0750: You may need to set ORACLE_HOME to your Oracle software directory
Linux下安装好Oracle 10g后运行sqlplus出现故障如下:[oracle@localhost oracle]$ ./sqlplusError 6 initializing SQL*Plu ...
- PHP vs Java
http://www.phpddt.com/reprint/php_font-java_end.html http://www.zhihu.com/question/20314377 http://b ...
- Cracking the coding interview--Q2.1
原文: Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this p ...
- c++四则运算代码
//Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--& ...
- mvn 使用中的错误
出现这种错误的时候:mvn Error building POM may not be this project's POM,报的是那个jar 包,就删除那个jar 包,重新mvn clean ins ...