码迷,mamicode.com
首页 > 其他好文 > 详细

Race to 1 UVA - 11762 (记忆dp概率)

时间:2018-07-22 21:40:15      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:bubuko   define   技术分享   txt   space   open   nbsp   sizeof   mat   

技术分享图片

 

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define rap(a, n) for(int i=a; i<=n; i++)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 1000005, INF = 0x7fffffff;
int primes[maxn], vis[maxn];
double f[maxn];
int ans = 0;
void init()
{
    mem(vis, 0);
    for(int i=2; i<maxn; i++)
    {
        if(vis[i]) continue;
        primes[ans++] = i;
        for(LL j=(LL)i*i; j<maxn; j+=i)
            vis[j] = 1;
    }
}

double dp(int x){
    if (x==1) return 0.0;
    if (f[x]) return f[x];
  //  vis[x]=1;
    int g=0,p=0;
    double res=0;
    for(int j=0; j<ans && primes[j] <= x; j++){
        if (primes[j]>x) break;
        p++;
        if (x%primes[j]==0){ g++; res+=dp(x/primes[j]);}
    }
    f[x] = (res+p)/(double)g;
    return f[x];
}
int main()
{
    init();
    int T, kase = 0;
    mem(f, 0.0);
    cin>> T;
    while(T--)
    {
        int n;
        cin>> n;
        double ans = dp(n);
        printf("Case %d: %.10f\n", ++kase, ans);
    }


    return 0;
}

 

Race to 1 UVA - 11762 (记忆dp概率)

标签:bubuko   define   技术分享   txt   space   open   nbsp   sizeof   mat   

原文地址:https://www.cnblogs.com/WTSRUVF/p/9351484.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!