1. 程式人生 > >Race to 1 UVA - 11762 (記憶dp概率)

Race to 1 UVA - 11762 (記憶dp概率)

printf efi freopen fff algo signed tdi res vector

技術分享圖片

#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概率)