Poj 3421-X-factor Chains

博客讲述了求解Poj 3421-X-factor Chains问题的过程,从最初的图论思路,到尝试动态规划和记忆化搜索,再到利用素数筛法,最终通过将问题转化为多重集排列数计算得出解决方案。博主在解题过程中不断优化,揭示了数学思维在解决ACM问题中的重要性。

 Poj 3421-X-factor Chains

题意:给定数x,求一条最长的因子链,1 = X0, X1, X2, …, Xm = X,链中xi<xi+1,且xi整除xi+1,求最长的链的长度和其数量。


因为前段时间要进行考试的预习,每天在自习室呆着,很久没写代码了,好不容易考试完,继续我的刷题之旅,结果一写就gg了…..虽说是个简单题,但是太弱,所以卡了很久,决定纪念一下我谜一般的思路过程…..

1、 刚开始做的时候一眼看上去像个图论题,把x的所有把整除关系看成边,x的因子看成顶点,之后dfs进行图的遍历就行了,找到最长的和其长度,因子稍微多点就基本运行不出结果了,于是进行了一个最优性剪枝,感觉一个数据能很快运行出结果了,直接就交上去,然后超时了,也是无语。

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <utility>
#include <vector>
#include <map>
using namespace std;

map<int, vector<int> > mp;
vector<int> d;
map<int, int> l;
int ans, num;

void dfs(int x, int len) {
    if (len < l[x]) return ;
    else l[x] = len;
    if  (len >= ans) {
        if (len == ans) num++;
        else ans = len, num = 1;
    }
    for (int i = 0; i < mp[x].size(); i++) dfs(mp[x][i], len+1);
}
int main() {
    int x;
    while (~scanf("%d", &x)) {
        d.clear(); mp.clear(); l.clear();
        int t = sqrt(x+0.0);
        d.push_back(1), d.push_back(x);
        for (int i = 2; i < t; i++)
            if (x % i == 0) d.push_back(i), d.push_back(x/i);
        if (t*t == x) d.push_back(t);
        sort(d.begin(), d.end());
        for (int i = 0; i < d.size(); i++) {
            for (int j = i+1; j < d.size(); j++)
                if (d[j] % d[i] == 0) mp[d[i]].push_back(d[j]);
        }
        ans = 0, num = 1;
        dfs(d[0], 0);
        printf("%d %d\n", ans, num);
    }
    return 0;
}


2、 想来想去,觉得深搜再怎么剪也肯定不行,想了一下dp,对于一个数x,计算两个值,一个是x为最末尾元素的链的最长长度,另一个是这种长度的数量,状态转移为其因子中那些链最长的,然后记忆化搜索的方式实现比较好,跑了一下数据,比之前的深搜快了几十倍,马上高兴地交了上去,还是超时,复杂度已经很低了,这都超时!无奈了,没办法进行优化了,按理来说不应该啊,题目不是说several个test case吗,而且时间限制还有2s,我跑10多个因子数很多的都只要1s左右,其实题目的数据量应该不会只有several个……

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <utility>
#include <vector>
#include <map>
using namespace std;

typedef pair<int, int> p;
map<int, vector<int> > mp;
map<int, p> l;
vector<int> d;

p dp(int x) {
    if (x == 1 || l[x].first) return l[x];
    vector<p> vec;
    for (int i = 0; i < mp[x].size(); i++) vec.push_back(dp(mp[x][i]));
    int len = vec[0].first, num = vec[0].second;
    for (int i = 1; i < vec.size(); i++) {
        if (len < vec[i].first) len = vec[i].first, num = vec[i].second;
        else if (len == vec[i].first) num += vec[i].second;
    }
    return l[x] = p(len+1, num);
}
int main() {
    /*freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);*/
    int x;
    while (~scanf("%d", &x)) {
        d.clear(); mp.clear(); l.clear();
        l[1] = p(0, 1);
        int t = sqrt(x+0.0);
        d.push_back(1), d.push_back(x);
        for (int i = 2; i < t; i++)
            if (x % i == 0) d.push_back(i), d.push_back(x/i);
        if (t*t == x) d.push_back(t);
        sort(d.begin(), d.end());
        for (int i = 0; i < d.size(); i++) {
            for (int j = i+1; j < d.size(); j++)
                if (d[j] % d[i] == 0) mp[d[j]].push_back(d[i]);
        }
        p ans = dp(x);
        printf("%d %d\n", ans.first, ans.second);
    }
    return 0;
}

3、 经历了前两次的失败,没什么想法了,只能翻开挑战程序设计实践的数论部分随便看看找点思路了,看到习题练习上那题的标签是素数,于是想到从素数下手,很快就想到了素数筛法,利用筛法的思想进行状态转移,对于一个数x,状态转移至其所有的倍数,紫书上面给出了证明,时间复杂度为O(nlogn),不过转念一想,这样应该比刚刚的复杂度还高啊,写完后交上去果然又超时了…..打算做最后的反抗,看还能不能在优化下,其实在求解答案的过程中,比x小的数的所有数的最长链的长度和数量全部计算出来了,根本不需要每次都O(nlogn)计算一下答案,直接预处理一下所有的数,复杂度也只有O(2^20*20),肯定不会超时,交上去终于过了!!!不过跑了900多ms。

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

const int N = 1048576;
typedef pair<int, int> p;
p a[N];

void predeal() {
    for (int i = 1; i <= 524288; i++) {
        for (int j = 2*i; j <= N; j += i) {
            if (a[i].first + 1 > a[j].first) a[j] = p(a[i].first + 1, a[i].second);
            else if (a[i].first + 1 == a[j].first) a[j].second += a[i].second;
        }
    }
}
int main() {
    int x;
    a[1] = p(0, 1);
    predeal();
    while (~scanf("%d", &x))printf("%d %d\n", a[x].first, a[x].second);
    return 0;
}

4、 最后重新思索一下这个题目,写了一个数模拟下计算的过程,发现了更巧也更简单的思路,其实要使得链尽量长,那么链中的元素前后两个数中大的比小的多一个素因子相乘即可,这样链肯定是最长的,那么数量怎么求呢?其实就是选取素因子相乘的排列问题,这样便转化为了多重集排列数的计算,直接套用离散数学书上的公式即可:

设多重集S={n1*a1,n2*a2,….nk*ak},且n=n1+n2+n3+…+nk,则s的排列数为n!/(n1!*n2!*…nk!)

所以对于一个数x,对其进行唯一分解式的处理,找出所有的素因子和他们的数量。交上去110ms….


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
using namespace std;

typedef long long LL;
map<int, int> d;
void prime_factor(int x) {
    for (int i = 2; i * i <= x; i++) {
        while (x % i == 0) x /= i, d[i]++;
    }
    if (x != 1) d[x]++;
}
LL fact(int x) { return x == 1 ? 1 : x * fact(x-1); }
int main() {
    int x;
    while (~scanf("%d", &x)) {
        if (x == 1) { printf("0 1\n"); continue; }
        d.clear(); prime_factor(x);
        int len = 0;
        for (map<int, int>::iterator i = d.begin(); i != d.end(); i++) len += i->second;
        LL num = fact(len);
        for (map<int, int>::iterator i = d.begin(); i != d.end(); i++) num /= fact(i->second);
        printf("%d %I64d\n", len, num);
    }
    return 0;
}

最后重新思索整个过程,还真是感受颇多,一片乱搞,卡了很久才过,从这个简单的数论题中发现了自己数学思维是有多差,不过很多题目都是这样,解题的关键点可以在计算过程中发现或者猜测出来,但是更重要的还是多刷题锻炼这些东西…


1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1011 1012 1013 1014 1015 1017 1018 1019 1028 1032 1035 1040 1042 1045 1046 1047 1050 1056 1061 1062 1063 1065 1067 1068 1080 1083 1088 1089 1091 1094 1095 1102 1111 1113 1117 1118 1125 1126 1127 1129 1130 1131 1132 1141 1142 1144 1149 1151 1157 1159 1160 1163 1164 1166 1174 1177 1182 1183 1186 1188 1189 1190 1191 1195 1200 1201 1207 1218 1226 1251 1256 1258 1260 1273 1274 1276 1283 1298 1305 1306 1308 1315 1316 1319 1321 1323 1324 1325 1328 1338 1339 1364 1389 1401 1422 1423 1426 1455 1458 1459 1469 1477 1485 1511 1517 1519 1523 1552 1562 1564 1565 1573 1579 1651 1654 1655 1656 1658 1659 1663 1664 1699 1700 1703 1716 1730 1737 1740 1753 1771 1797 1799 1804 1833 1837 1840 1844 1861 1887 1906 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1970 1979 1980 1985 1988 1989 2000 2001 2002 2018 2019 2021 2027 2033 2044 2051 2081 2084 2104 2109 2112 2135 2136 2137 2153 2155 2181 2182 2184 2185 2186 2187 2188 2190 2192 2195 2228 2229 2234 2236 2241 2242 2245 2247 2248 2249 2253 2264 2287 2299 2301 2309 2336 2337 2348 2352 2353 2362 2371 2378 2386 2387 2388 2389 2392 2393 2394 2402 2403 2406 2411 2413 2419 2421 2446 2449 2456 2479 2488 2492 2503 2509 2513 2524 2528 2531 2533 2545 2553 2559 2564 2575 2576 2586 2591 2593 2594 2602 2623 2632 2656 2676 2680 2707 2750 2774 2777 2780 2782 2812 2818 2840 2908 2922 2934 2965 2983 2993 2996 3020 3041 3168 3169 3176 3183 3184 3185 3191 3193 3214 3219 3224 3250 3253 3255 3256 3257 3258 3259 3264 3267 3273 3275 3277 3278 3279 3280 3295 3297 3302 3303 3311 3312 3321 3325 3348 3349 3355 3356 3357 3368 3372 3386 3400 3421 3424 3425 3427 3428 3438 3452 3468 3486 3517 3561 3585 3589 3602 3612 3614 3615 3616 3617 3618 3619 3620 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3637 3660 3661 3662 3663 3664 3665 3666 3668 3669 3670 3671 3672 3673 3687
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值