2014 多校07 1001 Prime Tree

Prime Tree

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 122    Accepted Submission(s): 18


Problem Description
Given an integer n, it generates a tree by the following operation.

Initially, there is a node weights n.

If n is not a prime number, the nodes gets a left son and a right son, the left son weights x, where x is a factor of n and x is larger than 1 and smaller than n chosen randomly, and the right son weights n/x. We call this step split.

For each leaf of tree, we do the split operation, until all of the leaves weight a prime number.

For example, suppose n to be 12, and the picture below shows a tree generated.

Now we want to know the expectated height of the tree.
 

Input
There are multiply test cases.

The first line contains an integer T(T<=50000), which denotes the number of test cases.

For each test case, there is an integer n(2<=n<=10^11), denotes the weight of the root.

The test cases are generated almost randomly.
 

Output
For each test case, output “Case #k: ”first, k is the case number, from 1 to T , and then output the expectated height. The answer should be rounded after the sixth decimal place.
 

Sample Input
  
  
3 2 4 36
 

Sample Output
  
  
Case #1: 1.000000 Case #2: 2.000000 Case #3: 3.571429
 

Author
UESTC
 

Source


题意:大意就是随机构建一颗树,数的构造规则就是一开始一个节点,里面是一个数n,然后随机选择n的一个大于1小于n的因子x,作为这个节点的左儿子,右儿子就是n/x,然后对这两个儿子进行同样的操作,知道一个节点的数是素数,就不用分解了。问这棵树的平均高度。

思路:首先肯定先要将n进行素因子分解嘛。然后我们用dp[i][j]表示根节点的数是i,深度是j的概率。于是你发现i貌似很大啊,怎么办。。。其实我们观察一下就会发现,我们的结果跟n的素因子的大小没有关系的,我们的答案只跟素因子有多少个,每个的指数是多少有关系,因为我们取因子的时候,只是将一些素因子的指数“去掉”而已。跟这个素因子是多少毫无关系。所以我们想到了反素数。。。反素数简单来讲就是它的素因子是连续的素数,而且指数单调不升。。。好了,我们把在10^11以内的反素数全搜出来,发现不到3000个,也就是说我们要求的状态不会超过3000个!! 但是这些数毕竟还是会很大的,处理起来不方便,于是我们要对其进行hash,能不能对它们取mod之后各不相同呢?答案是肯定的....为什么? 因为我暴力找到了,就是它33179。好了,我们对于所有的输入的数都分解因子后,转换为对应的反素数,然后对应的ID就是x%33179,接下来就是记忆化搜索了,这一步不难,把x的因子枚举出来,然后搜就行了。具体可以看代码。

代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<string.h>
#include <ctime>
#include<algorithm>
#include <cassert>
#include<cmath>
#include <time.h>
#include<list>
#include <queue>
#include<vector>
using namespace std;
#define LL long long
#define uint unsigned int
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define rrep(i,a,b) for(int i=(a);i>=(b);--i)
#define clr(a,x) memset(a,x,sizeof(a))
#define eps 1e-10
const LL limit=1e11+5;
const int maxp=1000000+5;
const int mod=33179;
int pe[maxp],pk;
bool isp[maxp],vis[mod];
double dp[mod*2][70];
double sum[mod*2][70];
int pw[100],kk;

LL to_rp(LL n)
{
    int cnt; kk=0;
    LL v;
    rep(i,0,pk) {
        v=pe[i];
        if(v*v>n) break;
        if(n%v) continue;
        cnt=0;
        do ++cnt,n/=v;
        while(n%v==0);
        pw[kk++]=cnt;
    }
    if(n!=1) pw[kk++]=1;
    sort(pw,pw+kk); reverse(pw,pw+kk);
    v=1;
    rep(i,0,kk) rep(j,0,pw[i])
       v*=(LL)pe[i];
    return v;
}

void divide(int cur,LL val,LL up,vector<LL>&fac)
{
    if(cur==kk) { if(val>1&&val<up) fac.push_back(val); return; }
    divide(cur+1,val,up,fac);
    rep(i,0,pw[cur]) {
        val*=pe[cur];
        divide(cur+1,val,up,fac);
    }
}

inline int Level(LL x)
{
    int l=0;
    while(x>0) ++l,x>>=1;
    return l;
}
inline LL max(LL a,LL b) { return a>b?a:b; }
int dfs(LL n)
{
    int e=n%mod;
    if(vis[e]) return e;
    vector<LL> fac;
    divide(0,1,n,fac);
//    rep(i,0,fac.size()) fac[i]=to_rp(fac[i]);
//    sort(fac.begin(),fac.end());
//    fac.erase(unique(fac.begin(),fac.end()),fac.end());
    int tot=fac.size();
  //  printf("%lld %d\n",n,tot);
    int a,b,k; double p=1.0/tot;
    LL c,d;
    rep(i,0,tot) {
        c=to_rp(fac[i]);
        a=dfs(c);
   //     printf("%d\n",fac[i]);
        d=to_rp(n/fac[i]);
        b=dfs(d);
        k=Level(max(c,d));
        rep(j,1,k+1) {
            dp[e][j+1]+=dp[a][j]*sum[b][j]*p;
            dp[e][j+1]+=sum[a][j]*dp[b][j]*p;
            dp[e][j+1]-=dp[a][j]*dp[b][j]*p;
        }
    }
    rep(i,1,40) sum[e][i]=sum[e][i-1]+dp[e][i];
    vis[e]=true;
    return e;
}

void pre_init()
{
    rep(i,0,maxp) isp[i]=true;
    rep(i,2,maxp) {
        if(isp[i]) pe[pk++]=i;
        rep(j,0,pk) {
            LL x=(LL)pe[j]*i;
            if(x>=maxp) break;
            isp[x]=false;
            if(i%pe[j]==0) break;
        }
    }
}

int main()
{
    clr(dp,0); clr(vis,0);
    pre_init(); dp[2][1]=1.0; vis[2]=true; rep(i,1,50) sum[2][i]=1.0;
    int T; cin>>T; LL n; double ans;
    rep(cas,1,T+1) {
        scanf("%I64d",&n);
        dfs(n=to_rp(n));
        int k=Level(n);
        n%=mod; ans=0;
        rep(i,0,k+1) ans+=i*dp[n][i];
        printf("Case #%d: %.6lf\n",cas,ans+eps);
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值