[HDU6037] Expectation Division (dynamic programming, search)

[HDU6037] Expectation Division (dynamic programming, search)

Face questions

Vjudge
you have a number \ (n-\) , \ (n-\ Le 10 ^ {24} \) , for convenience will tell you \ (n-\) there after decomposition \ (m \) different quality factors, and the these qualitative factors to you.
Every time you can put \ (n \) became one of its divisors, seeking to become \ (1 \) the desired number of steps.

answer

Violence is first transferred:
\ [F [n-] +. 1 = \ {FRAC. 1} {\ Sigma (n-)} \ sum_ {D |} n-F [D] \]
difficult to find the status of each substance a collection of related number of factors appear, and what is the quality factor is independent.
Found \ (n \) essentially different prime factors at most \ (18 \) after months, we found that each burst prime factors of the number of collection appeared, forcing the smaller prime factors of a larger number of times, complete search found status only \ (172,513 \) months.
So we For each \ (n-\) set calculation answer number of prime factors appears simply to be solved and a high dimensional prefix can be transferred.
Here high-dimensional prefix and the method of finding, provided \ (g [n] [j ] \) means that for \ (n-\) this number (the number of a burst found out, i.e. to meet the number of occurrences of small prime factors not less than a large prime factors of the number of occurrences), its front \ (J \) number of occurrences of a qualitative factors are the same, but \) (J \ prime factors after the occurrence times is less than equal to the current position of all \ (f [n ] \) time and, transferred to enumerate what a minus one on the line.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
#define ll __int128
#define MAX 200200
const ll Limit=(ll)1e12*(ll)1e12;
ll n;int m,Case;
char ch[30];int a[30];
int p[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73};
map<ll,int> M;int tot;ll val[MAX];
double g[MAX][20],f[MAX];
void dfs(int x,int lst,ll s)
{
    val[M[s]=++tot]=s;s*=p[x];
    for(int i=1;i<=lst&&s<=Limit;++i,s*=p[x])dfs(x+1,i,s);
}
int main()
{
    dfs(0,90,1);
    for(int i=2;i<=tot;++i)
    {
        ll x=val[i];for(int j=0;j<18;++j)a[j]=0;
        for(int j=0;j<18;++j)while(x%p[j]==0)++a[j],x/=p[j];
        for(int j=17;~j;--j)
            if(a[j])
            {
                int k=j;while(k<17&&a[k+1]==a[j])++k;
                g[i][j]=g[i][j+1]+g[M[val[i]/p[k]]][j];
            }
        int tmp=1;
        for(int j=0;j<18;++j)tmp*=a[j]+1;
        f[i]=(g[i][0]+tmp)/(tmp-1);
        for(int j=0;j<18;++j)g[i][j]+=f[i];
    }
    while(scanf("%s",ch+1)!=EOF)
    {
        for(int i=1,l=strlen(ch+1);i<=l;++i)n=n*10+ch[i]-48;
        scanf("%d",&m);
        for(int i=0;i<m;++i)
        {
            int p;scanf("%d",&p);a[i]=0;
            while(n%p==0)n/=p,++a[i];
        }
        sort(&a[0],&a[m]);reverse(&a[0],&a[m]);
        for(int i=0;i<m;++i)
            for(int j=1;j<=a[i];++j)
                n*=p[i];
        printf("Case #%d: %.10lf\n",++Case,f[M[n]]);
        for(int i=0;i<m;++i)a[i]=0;n=0;
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/cjyyb/p/11141031.html