zoukankan      html  css  js  c++  java
  • CF1097D Makoto and a Blackboard 质因数分解 DP

    Hello 2019 D

    题意:

      给定一个n,每次随机把n换成它的因数,问经过k次操作,最终的结果的期望。

    思路:

      一个数可以表示为质数的幂次的积。所以对于这个数,我们可以分别讨论他的质因子的情况。

      假设质因子x的指数是j,那么这个质因子下一步可以变到的情况就有(j+1)种可能,利用概率DP算出k步操作后每个x的不同幂次的概率,然后求出期望。

      把每个质因子的情况算出来的期望乘起来即可。

      

    #include <algorithm>
    #include  <iterator>
    #include  <iostream>
    #include   <cstring>
    #include   <cstdlib>
    #include   <iomanip>
    #include    <bitset>
    #include    <cctype>
    #include    <cstdio>
    #include    <string>
    #include    <vector>
    #include     <stack>
    #include     <cmath>
    #include     <queue>
    #include      <list>
    #include       <map>
    #include       <set>
    #include   <cassert>
    
    using namespace std;
    #define lson (l , mid , rt << 1)
    #define rson (mid + 1 , r , rt << 1 | 1)
    #define debug(x) cerr << #x << " = " << x << "
    ";
    #define pb push_back
    #define pq priority_queue
     
     
    
    typedef long long ll;
    typedef unsigned long long ull;
    //typedef __int128 bll;
    typedef pair<ll ,ll > pll;
    typedef pair<int ,int > pii;
    typedef pair<int,pii> p3;
     
    //priority_queue<int> q;//这是一个大根堆q
    //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
    #define fi first
    #define se second
    //#define endl '
    '
     
    #define OKC ios::sync_with_stdio(false);cin.tie(0)
    #define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
    #define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
    #define max3(a,b,c) max(max(a,b), c);
    #define min3(a,b,c) min(min(a,b), c);
    //priority_queue<int ,vector<int>, greater<int> >que;
     
    const ll mos = 0x7FFFFFFF;  //2147483647
    const ll nmos = 0x80000000;  //-2147483648
    const int inf = 0x3f3f3f3f;
    const ll inff = 0x3f3f3f3f3f3f3f3f; //18
    const int mod = 1e9+7;
    const double esp = 1e-8;
    const double PI=acos(-1.0);
    const double PHI=0.61803399;    //黄金分割点
    const double tPHI=0.38196601;
     
     
    template<typename T>
    inline T read(T&x){
        x=0;int f=0;char ch=getchar();
        while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
        while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
        return x=f?-x:x;
    } 
    /*-----------------------showtime----------------------*/
                const int maxn = 1e4+9;
                ll dp[maxn][105],inv[102];
                ll n; int k;
    
    
                ll cal(ll a,int n){
                    memset(dp, 0, sizeof(dp));
                    dp[0][n] = 1;
    
                    for(int i=0; i<k; i++){
                        for(int j=0; j<=n; j++){
                            for(int t=0; t<=j; t++){
                                dp[i+1][t] =(dp[i+1][t] + dp[i][j] * inv[j+1]%mod)%mod;
                            }
                        }
                    }
                    
                    ll d = 1,sum = 0;
                    for(int i=0; i<=n; i++){
                        sum = (sum + d * dp[k][i])%mod;
                        d = d * a % mod;
                    }
    
                    return sum;
                }
                
    int main(){
                cin>>n>>k;
                ll ans = 1;
                inv[1] = 1;
                for(int i=2; i<=100; i++){
                    inv[i] = (mod - mod/i)*inv[mod%i]%mod;
                }
    
                for(ll i=2; i*i<=n; i++){
                    if(n%i==0){
                        int cnt = 0;
                        while(n%i==0) n/=i,cnt++;
                        ans = ans * cal(i, cnt)%mod;
                    }
                }   
                if(n>1) ans = ans * cal(n, 1)%mod;
                cout<<ans<<endl;
                return 0;
    }
    View Code
  • 相关阅读:
    POJ 2154
    POJ 1286
    Polycarp's problems
    Greedy Change
    Goods transportation
    Ugly Problem
    Happy Matt Friends
    Dense Subsequence
    Ray Tracing
    Batch Sort
  • 原文地址:https://www.cnblogs.com/ckxkexing/p/10284452.html
Copyright © 2011-2022 走看看