POJ 3421 X-factor Chains

这条链依次乘一个因子。因为n<2^20,sqrt(n)分解因子,相同的因子相对顺序取一个。

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
//#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define PB push_back

const int maxn = 1<<20;

ll fac[21];
void calFac(int n = 20)
{
    fac[0] = 1;
    for(int i = 1; i <= n; i++){
        fac[i] = fac[i-1]*i;
    }
}

int primeDecompose(int n,ll &div)
{
    int re = 0; div = 1;

    for (int i = 2; i*i <= n; i++){
        int cnt = 0;
        while (n % i == 0){
            n /= i;
            cnt++;
            re++;
        }
        if(cnt) div *= fac[cnt];
    }
    if(n>1) re++;
    return re;
}

//#define LOCAL
int main()
{
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif
    calFac();
    int X;
    while(~scanf("%d",&X)){
        ll div;
        int len = primeDecompose(X,div);
        ll shc = fac[len]/div;
        printf("%d %I64d\n",len,shc);
    }
    return 0;
}

我尝试用预先筛sqrt(n)以内的素数来加速判断,结果却慢了26ms..

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
//#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define PB push_back

const int maxn = 1<<20;
const int sqrn = 1<<10;//(int)sqrt(maxn+0.5); + 5
const int PrmSz = (1<<8)+5; // (sqrn>>2) + 5

bool isNot[sqrn];
int Prm[PrmSz], tot;

void seive(int n = sqrn) //sqrt(sqrt(maxn))
{
    int m = sqrt(n+0.5);
    isNot[0] = isNot[1] = true;
    for(int i = 2; i <= m; i++){
        if(!isNot[i])
        for(int j = i*i; j <= n; j += i){
            isNot[j] = true;
        }
    }
    for(int i = 2; i <= n; i++){
        if(!isNot[i]) //Prm.PB(i);
            Prm[tot++] = i;
    }
}

ll fac[21];
void calFac(int n = 20)
{
    fac[0] = 1;
    for(int i = 1; i <= n; i++){
        fac[i] = fac[i-1]*i;
    }
}

inline int primeDecompose(int n,ll &div)
{
    int re = 0; div = 1;
    for (int i = 0; i < tot && Prm[i] <= n; i++){
        int p = Prm[i], cnt = 0;
        while (n % p == 0){
            n /= p;
            cnt++;
            re++;
        }
        if(cnt) div *= fac[cnt];
    }
    if(n>1) re++;
    return re;
}

//#define LOCAL
int main()
{
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif
    seive();
    calFac();
    int X;
    while(~scanf("%d",&X)){
        ll div;
        int len = primeDecompose(X,div);
        ll shc = fac[len]/div;
        printf("%d %I64d\n",len,shc);
    }
    return 0;
}
时间: 2024-10-14 01:02:04

POJ 3421 X-factor Chains的相关文章

挑战程序设计竞赛2.6习题:X-factor Chains POJ - 3421

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 ch

POJ 3421 X-factor Chains (因式分解+排列组合)

题意:一条整数链,要求相邻两数前一个整除后一个.给出链尾的数,求链的最大长度以及满足最大长度的不同链的数量. 类型:因式分解+排列组合 算法:因式分解的素因子个数即为链长,链中后一个数等于前一个数乘以某素因子,所以链的数量即为这些因子不全相异的全排列数:A!/(a1!a2!a3!..) #include <cstdio> #include <cmath> #include <iostream> using namespace std; typedef long long

POJ 3421 X-factor Chains | 数论

题意: 给一个x,求最长的排列满足开头是1,结尾是x,前一个数是后一个数的因子 输出长度和这样序列的个数 题解: 把x分解质因数,质因数个数就是答案,接下来考虑怎么求个数 显然这是一个可重集合全排列问题,设有n个元素 答案就是n!/每个元素出现次数的阶乘 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 typedef long long ll; 5 #define N 2000 6 using

POJ 3421 素数+组合数学

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

POJ 3048 Max Factor (筛素数)

Description To improve the organization of his farm, Farmer John labels each of his N (1 <= N <= 5,000) cows with a distinct serial number in the range 1..20,000. Unfortunately, he is unaware that the cows interpret some serial numbers as better tha

数学问题的解决窍门—素数判定

数学问题的解决窍门 素数判定 所谓素数: 指恰好有2个约数的整数. 判定: 因为n的约数都不超过n, 所以只要检查 2 ~ n-1 的所有整数是否整除n就能判定n是不是素数. 在此,如果d 是 n的约数, 那么 n/d也是n的约数.由n = d * n / d 可知 min(d, n/d) <= 根号n? , 所以只要检查 2 ~ ? 根号n 的所有整数就足够了. 同理可知,整数分解和约数枚举都可以在 O(根号n?) 时间完成.(还有更高效的算法) #include <iostream>

POJ 2248 Addition Chains dfs(水)

题意:给出n 构成出满足下列条件 长度最小的数列a[0]=1,a[m]=n, a[0]<a[1]<..<a[m]每个下标k都存在(i,j<k) 满足:a[k]=a[i]+a[j] n<=100 枚举长度 dfs爆搜+剪枝 水过, #include <iostream> #include <cstring> #include <cstdio> using namespace std; const int N=2e5+20; int n,a[N

POJ 2245 Addition Chains(算竞进阶习题)

迭代加深dfs 每次控制序列的长度,依次加深搜索 有几个剪枝: 优化搜索顺序,从大往下枚举i, j这样能够让序列中的数尽快逼近n 对于不同i,j和可能是相等的,在枚举的时候用过的数肯定不会再被填上所以可以去重(记得回溯) #include <iostream> #include <cstring> #include <cstdio> #define INF 0x3f3f3f3f using namespace std; typedef long long ll; inl

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