POJ 3421 X-factor Chains【分解质因子+组合数学】
2016-04-19 17:33
609 查看
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
Sample Output
Source
POJ Monthly--2007.10.06, ailyanlu@zsu
题意:
给出一个数字 n ,问 n 的因子组成的满足任意前一项都能整除后一项的序列的最大长度,以及所有不同序列的个数
题解:
最直观的考虑肯定是第一位是1,随后的数进行讨论
不难发现,按题目要求的话要求序列最长,那么就要尽量使得临近的数的比值尽量小,有个理论说(忘了哪个伟人说的了),任意一个比较大的合数,都能写成若干个素数的积,也就是说,肯定能找到某个数的一个非常小的不可分割的单位(最小素因子?)。
尝试如下:
给出一个数n,那么首先找到这个数最小的素因子x,作为序列的第二个元素,然后,假如,这个x*x 依然能被n 整除,那么x*x 此时肯定是能满足题意要求的最小值,依次判断x*x*x....一直到不满足题意,但是还没完.....不一定能完全除尽,那就要考虑次大的素因子了,继续像这样一直尝试,如果最终未除尽,剩余的部分也要算上,因为这个部分乘上倒数第二项,肯定是n,也就是仍然满足题意的!
表达能力有点水,上实例:
100
1,首项 1,没什么说的
2,为了尽量长,用最小的素数(2)去分割,得到2*2*25,那么就是说,另外可以构造出两项了 2,2*2(4)
3,发现不能整除了,那么寻找100的次小的素因子,找到5,把剩下的25 分解成5*5,那么就可以再构造出两项 2*2*5(20),2*2*5*5(100)
那么最后的结果就是1,2,4,20,100,按题意,最长的可能是 4
对于6怎么来的,组合数学有一条说:
n个物体一共有k 个,m种,其中每一种对应n1,n2,n3,n4....个,那么这些物品全排列的总数目是:
n!/(n1+n2+....+nm)!
个人也不懂,怎么推出来的.....
(以上纯属个人理解,错误之处指出还请大神不吝指出)
/* http://blog.csdn.net/liuke19950717 */
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int main()
{
ll x[25]={1};
for(ll i=1;i<25;++i)
{
x[i]=i*x[i-1];
}
ll n;
while(~scanf("%lld",&n))
{
ll ans=0,base=1;
for(ll i=2;i*i<=n;++i)
{
if(n%i==0)
{
ll cnt=0;
while(n%i==0)
{
++cnt;
n/=i;
}
ans+=cnt;
base*=x[cnt];
}
}
if(n>1)
{
ans+=1;
}
printf("%lld %lld\n",ans,x[ans]/base);
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6329 | Accepted: 1960 |
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
Source
POJ Monthly--2007.10.06, ailyanlu@zsu
题意:
给出一个数字 n ,问 n 的因子组成的满足任意前一项都能整除后一项的序列的最大长度,以及所有不同序列的个数
题解:
最直观的考虑肯定是第一位是1,随后的数进行讨论
不难发现,按题目要求的话要求序列最长,那么就要尽量使得临近的数的比值尽量小,有个理论说(忘了哪个伟人说的了),任意一个比较大的合数,都能写成若干个素数的积,也就是说,肯定能找到某个数的一个非常小的不可分割的单位(最小素因子?)。
尝试如下:
给出一个数n,那么首先找到这个数最小的素因子x,作为序列的第二个元素,然后,假如,这个x*x 依然能被n 整除,那么x*x 此时肯定是能满足题意要求的最小值,依次判断x*x*x....一直到不满足题意,但是还没完.....不一定能完全除尽,那就要考虑次大的素因子了,继续像这样一直尝试,如果最终未除尽,剩余的部分也要算上,因为这个部分乘上倒数第二项,肯定是n,也就是仍然满足题意的!
表达能力有点水,上实例:
100
1,首项 1,没什么说的
2,为了尽量长,用最小的素数(2)去分割,得到2*2*25,那么就是说,另外可以构造出两项了 2,2*2(4)
3,发现不能整除了,那么寻找100的次小的素因子,找到5,把剩下的25 分解成5*5,那么就可以再构造出两项 2*2*5(20),2*2*5*5(100)
那么最后的结果就是1,2,4,20,100,按题意,最长的可能是 4
对于6怎么来的,组合数学有一条说:
n个物体一共有k 个,m种,其中每一种对应n1,n2,n3,n4....个,那么这些物品全排列的总数目是:
n!/(n1+n2+....+nm)!
个人也不懂,怎么推出来的.....
(以上纯属个人理解,错误之处指出还请大神不吝指出)
/* http://blog.csdn.net/liuke19950717 */
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int main()
{
ll x[25]={1};
for(ll i=1;i<25;++i)
{
x[i]=i*x[i-1];
}
ll n;
while(~scanf("%lld",&n))
{
ll ans=0,base=1;
for(ll i=2;i*i<=n;++i)
{
if(n%i==0)
{
ll cnt=0;
while(n%i==0)
{
++cnt;
n/=i;
}
ans+=cnt;
base*=x[cnt];
}
}
if(n>1)
{
ans+=1;
}
printf("%lld %lld\n",ans,x[ans]/base);
}
return 0;
}
相关文章推荐
- mybaits不能出现小于号
- FZOJ 2216 The Longest Straight (尺取法)
- warning C4819 解决方案 warning C4819: The file contains a character that cannot be represented in the cu
- grails框架数据查询
- IntelliJ 自动创建main函数 for循环 try catch模块
- 70. Climbing Stairs
- ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction(死锁)
- 11. Container With Most Water
- HDU 1104 remainder
- SFTP 上传文件报错:SFTP ERROR?Algorithm negotiation fail
- ERROR 1215 (HY000): Cannot add foreign key constraint 解决方法
- [2016-04-19 15:46:03 - IceHoloReader1.0] Installation error: INSTALL_FAILED_CONFLICTING_PROVIDER [20
- Jenkins进阶系列之——02email-ext邮件通知模板
- AssertionFailedError
- 如何用Paint实现文字居中?
- Failed to initialize end point associated with ProtocolHandler
- Jenkins进阶系列之——01使用email-ext替换Jenkins的默认邮件通知
- monodb因 /usr/lib64/libssl.so.10: no version information available原因不能连接的问题
- No valid signing identities (i.e. certificate and private key pair) matching the team ID “(null)” were found.
- Paint及Canvas的简单应用