POJ3421 X-factor Chains

嘟嘟嘟

题目大意:给一个数x,让你求这样一个最长的序列,以及最长的序列的种数:

1.第0项为1,最后一项为x(序列长度不算这两项)。

2.每一项都是x的因子。

3.对于任意的ai和ai+1,ai < ai+1且ai | ai+1

每一项都是x的因子,那么先把x分解质因数,用这些数凑成的数一定都是x的因子。然后要满足第三条,那么ai+1一定由ai乘以一个质因数得到,所以最长长度就是质因数指数之和tot。

再求方案:先不考虑pici中,ci = 1,那么第一个数有tot种选法,第二个数有tot - 1种……所以总方案数是tot!。再考虑重复的数,就再除以 ci!。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<cstdlib>
 7 #include<cctype>
 8 #include<vector>
 9 #include<stack>
10 #include<queue>
11 using namespace std;
12 #define enter puts("")
13 #define space putchar(‘ ‘)
14 #define Mem(a, x) memset(a, x, sizeof(a))
15 #define rg register
16 typedef long long ll;
17 typedef double db;
18 const int INF = 0x3f3f3f3f;
19 const db eps = 1e-8;
20 //const int maxn = ;
21 inline ll read()
22 {
23     ll ans = 0;
24     char ch = getchar(), last = ‘ ‘;
25     while(!isdigit(ch)) {last = ch; ch = getchar();}
26     while(isdigit(ch)) {ans = ans * 10 + ch - ‘0‘; ch = getchar();}
27     if(last == ‘-‘) ans = -ans;
28     return ans;
29 }
30 inline void write(ll x)
31 {
32     if(x < 0) x = -x, putchar(‘-‘);
33     if(x >= 10) write(x / 10);
34     putchar(x % 10 + ‘0‘);
35 }
36
37 int n;
38 ll fac[21];
39 void init()
40 {
41   fac[1] = 1;
42   for(int i = 2; i < 21; ++i) fac[i] = fac[i - 1] * i;
43 }
44
45 void solve(int n)
46 {
47   int tot = 0;
48   ll und = 1;
49   for(int i = 2; i * i <= n; ++i)
50     {
51       if(!(n % i))
52     {
53       int cnt = 0;
54       for(; !(n % i); n /= i, cnt++);
55       tot += cnt;
56       und *= fac[cnt];
57     }
58     }
59   if(n > 1) tot++;
60   write(tot); space; write(fac[tot] / und); enter;
61 }
62
63 int main()
64 {
65   init();
66   while(scanf("%d", &n) != EOF) solve(n);
67   return 0;
68 }

原文地址:https://www.cnblogs.com/mrclr/p/9774496.html

时间: 2024-10-30 20:33:16

POJ3421 X-factor Chains的相关文章

POJ3421:X-factor Chains——题解

http://poj.org/problem?id=3421 题目大意:一个数列,起始为1,终止为一给定数X,满足Xi < Xi+1 并且Xi | Xi+1. 求出数列最大长度和该长度下的情况数. —————————————— 很简单想到分解X质因数,这样我们每加一个数就是前一个数*其中一个质因数即可. 所以长度为质因数个数. 至于情况数,就是有重复的排列数,去重即可求. (不开longlong见祖宗,十年OI一场空) #include<cstdio> #include<cstri

poj3421 X-factor Chains(重复元素的全排列)

poj3421 X-factor Chains 题意:给定正整数$x(x<=2^{20})$,求$x$的因子组成的满足任意前一项都能整除后一项的序列的最大长度,以及满足最大长度的子序列的个数. 显然最大长度就是$x$的质因数个数(一个一个加上去鸭) 而满足最大长度的子序列个数.... 这不就是可重复元素的全排列吗! 有这么一个公式,设元素总个数$n$,每个重复元素的个数$m_{i}$,共$k$种不同元素 则全排列个数$=\frac{n!}{\prod_{i=1}^{k}m_{i}!}$ 发现$n

poj3421 X-factor Chains——分解质因数

题目:http://poj.org/problem?id=3421 好久没有独立A题了...做点水题还是有助于提升自信心的: 这题就是把 x 质因数分解,质因数指数的和 sum 就是最长的长度,因为每次至少乘一个质因数: 排列方式就是从 sum 个位置里给第一种质因数选几个位置,再在剩下的里面给第二种质因数选几个位置... 也就是 ans = ∏(1<=i<=cnt) C(n,pc[i]),n -= pc[i],其中 cnt 是质因数(种类)个数,pc 是每种质因数的指数,n 就是目前剩下几个

Project Euler:Problem 74 Digit factorial chains

The number 145 is well known for the property that the sum of the factorial of its digits is equal to 145: 1! + 4! + 5! = 1 + 24 + 120 = 145 Perhaps less well known is 169, in that it produces the longest chain of numbers that link back to 169; it tu

WAIT EVENT: latch: cache buffers chains

关于CACHE BUFFERS CHAINS描述 CACHE BUFFERS CHAINS latch is acquired when searching for data blocks cached in the buffer cache. Since the Buffer cache is implemented as a sum of chains of blocks, each of those chains is protected by a child of this latch

POJ3048 Max Factor

本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转载请注明出处,侵权必究,保留最终解释权!   Description To improve the organization of his farm, Farmer John labels each of his N (1 <= N <= 5,000) cows with a distinct s

[2016-02-19][UVA][129][Krypton Factor]

UVA - 129 Krypton Factor Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description You have been employed by the organisers of a Super Krypton Factor Contest in which contestants have very high mental and physica

因子分析 (Factor Analysis Sharp)

In my understanding, factor analysis is a method developed to avoid the mass estimation of the variance-covariance matrix when doing Markowitz Allocation. Factor Analysis breakdown the risk factors in stocks into risk factors in portfolio. It apply t

POJ_3421_X-factor Chains(素数筛法)

X-factor Chains Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5659   Accepted: 1786 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