Codeforces 542D Superhero's Job dp (看题解)

Superhero's Job

首先要看出来是个积性函数, 我这个都没看出来, 我好菜啊。

看出来之后随便dp一下就好了。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);

using namespace std;

const int N = 7000 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-8;
const double PI = acos(-1);

template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}

LL fac[N]; int n, cntp;
LL A;
LL p[N];
LL who[N];
vector<LL> G[N];

LL dp[N];

LL check(LL x) {
    int cnt = 0;
    x--;
    LL who = -1;
    for(LL i = 2; i * i <= x; i++) {
        if(x % i) continue;
        cnt++; who = i;
        while(x % i == 0) x /= i;
        if(cnt > 1) return false;
    }
    if(x > 1) {
        cnt++;
        who = x;
    }
    return (cnt == 1) ? who : 0;
}

int main() {
    scanf("%lld", &A);
    if(A == 1) return puts("1"), 0;
    for(LL i = 1; i * i <= A; i++) {
        if(A % i) continue;
        if(i > 2)fac[++n] = i;
        if(i * i < A && A / i > 2) fac[++n] = A / i;
    }
    if(!n) return puts("0"), 0;
    fac[++n] = 1;
    sort(fac + 1, fac + 1 + n);
    for(int i = 1; i <= n; i++) {
        who[i] = check(fac[i]);
        if(who[i]) p[++cntp] = who[i];
    }
    sort(p + 1, p + cntp + 1);
    cntp = unique(p + 1, p + cntp + 1) - p - 1;
    for(int i = 1; i <= n; i++) {
        if(who[i]) {
           G[lower_bound(p + 1, p + cntp + 1, who[i]) - p].push_back(fac[i]);
        }
    }
    dp[1] = 1;
    for(int i = 1; i <= cntp; i++) {
        for(int j = n; j >= 1; j--) {
            if(!dp[j]) continue;
            for(auto& t : G[i]) {
                if(1.0 * t * fac[j] > 1e18) continue;
                int pos = lower_bound(fac + 1, fac + 1 + n, t * fac[j]) - fac;
                if(pos <= n && fac[pos] == t * fac[j]) {
                    dp[pos] += dp[j];
                }
            }
        }
    }
    printf("%lld\n", dp[n]);
    return 0;
}

/*
*/

 

posted @ 2019-05-12 01:50  NotNight  阅读(138)  评论(0编辑  收藏  举报