1. 程式人生 > >CodeForces - 1097D:Makoto and a Blackboard (積性)

CodeForces - 1097D:Makoto and a Blackboard (積性)

Makoto has a big blackboard with a positive integer n written on it. He will perform the following action exactly k

times:

Suppose the number currently written on the blackboard is v

. He will randomly pick one of the divisors of v (possibly 1 and v) and replace v with this divisor. As Makoto uses his famous random number generator (RNG) and as he always uses

58">58

as his generator seed, each divisor is guaranteed to be chosen with equal probability.

He now wonders what is the expected value of the number written on the blackboard after k

steps.

It can be shown that this value can be represented as PQ

where P and Q are coprime integers and

Q≢0(mod109+7)">Q≢0(mod109+7). Print the value of PQ1 modulo 109+7

.

Input

The only line of the input contains two integers n

and k (1n1015, 1k104

).

15">Output

Print a single integer — the expected value of the number on the blackboard after k

steps as PQ1(mod109+7) for P, Q

defined above.

Examples Input
6 1
Output
3
Input
6 2
Output
875000008
Input
60 5
Output
237178099

題意:開始給定一個數N,然後讓你K輪如下操作:等概率的變為當前數的一個因子,求期望。

思路:發現有點像孟德爾定律的資料,其實就是在暗示我們:豌豆的幾種顏色可以單獨算,然後作乘。

事實上就是這樣的,對於每個素數因子,我們假設他在N中的冪為p(a^p),那麼我們可以算出最後冪為1到p的概率。 而不同素因子之間不會相互影響。

所以我們預處理出開始冪為p,K輪後冪為q的次數mp[p][q];然後就可以對於每個素數得到其概率,然後累乘即可。

(注意不要爆ll

 

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
using namespace std;
const int maxn=2000010;
const int Mod=1e9+7;
ll ans,a[maxn],f[maxn],rev[maxn],tot,mp[60][60],M[60][60],sum; int K;
int qpow(ll a,int x){
    int res=1; while(x){
        if(x&1) res=1LL*res*a%Mod;
        a=1LL*a*a%Mod; x>>=1;
    } return res;
}
ll get(int p)
{
    f[p]++; ll tmp=qpow(qpow(f[p],K),Mod-2),res=0;
    rep(i,1,f[p]) {
        res=(res+1LL*mp[f[p]][i]*tmp%Mod)%Mod;
        tmp=tmp*a[p]%Mod;
    }
    return res;
}
void solve(int p)
{
    ll tmp=1; mp[p][p]=1;
    rep(i,1,K){
        rep(j,1,p) M[p][j]=1LL*mp[p][j]*p%Mod,mp[p][j]=0;
        rep(j,1,p) {
            rep(k,1,j) mp[p][k]=(mp[p][k]+1LL*M[p][j]*rev[j]%Mod)%Mod;
        }
    }
}
int main()
{
    ll N,tN; scanf("%lld%d",&N,&K); tN=N;
    rev[0]=1; rep(i,1,51) rev[i]=qpow(i,Mod-2);
    rep(i,1,51) solve(i); ans=1LL;
    for(ll i=2;i*i<=tN;i++){
        if(tN%i==0){
            a[++tot]=i; while(tN%i==0) tN/=i,f[tot]++;
            sum=sum*(f[tot]+1);
        }
    }
    if(tN>1) a[++tot]=tN,f[tot]=1;
    rep(i,1,tot) ans=1LL*ans*get(i)%Mod;
    printf("%lld\n",ans);
    return 0;
}

 

相關推薦

CodeForces - 1097DMakoto and a Blackboard

Makoto has a big blackboard with a positive integer n written on it. He will perform the following action exactly k times: Suppose the number currently w

Codeforces-1076EVasya and a Tree樹狀陣列

E. Vasya and a Tree time limit per test 2 seconds memory limit per test 256 megabytes inputstandard input outputstandard output Vas

CF 1097D Makoto and a Blackboard

算是記一下昨天晚上都想了些什麼 官方題解   點我 簡單題意 給定兩個正整數$n$和$k$,定義一步操作為把當前的數字$n$等概率地變成$n$的任何一個約數,求$k$步操作後的期望數字,模$1e9 + 7$。 $$n \leq 10^{15}, k \leq 10^4$$ 我的思

CF 1097D - Hello 2019 D題: Makoto and a Blackboard

目錄 Catalog Solution: (有任何問題歡迎留言或私聊 && 歡迎交流討論哦 Catalog Problem:傳送門  Portal  原題目描述在最下面。  給一個數n,由k次操作。每次操作等概率的把n變成他的一個因數(\(1\leq x\leq

CF-1097D-Makoto and a Blackboard

這題在比賽的時候耗了我近兩個小時,還是沒做出來。用到逆元和期望。賽前掌握的不夠好,現在看了幾位大佬的程式碼之後把這題補上。 程式碼參考來源:https://blog.csdn.net/qq_36797743/article/details/85834812 1097D&nbs

CodeForces - 1097FAlex and a TV Show bitset & 莫比烏斯容斥

Alex decided to try his luck in TV shows. He once went to the quiz named "What's That Word?!". After perfectly answering the questions "How is a pseudonym

CodeForces - 613DKingdom and its Cities虛樹+DP

for pro void 染色 however == force don tac Meanwhile, the kingdom of K is getting ready for the marriage of the King‘s daughter. However

codeforces#766 D. Mahmoud and a Dictionary 並查集

namespace define scanf 對應關系 force sha ict codeforce sharp 題意:給出n個單詞,m條關系,q個詢問,每個對應關系有,a和b是同義詞,a和b是反義詞,如果對應關系無法成立就輸出no,並且忽視這個關系,如果可以成立則加入這

codeforces#766 D. Mahmoud and a Dictionary 並查集

題意:給出n個單詞,m條關係,q個詢問,每個對應關係有,a和b是同義詞,a和b是反義詞,如果對應關係無法成立就輸出no,並且忽視這個關係,如果可以成立則加入這個約束,並且輸出yes。每次詢問兩個單詞的關係,1,同義詞,2,反義詞,3,不確定 題解:這題思路比較奇特,開闢2*n的並查集的空間,第i+n代表i的

cf1097D. Makoto and a Blackboard(期望dp)

題意 題目連結 Sol 首先考慮當\(n = p^x\),其中\(p\)是質數,顯然它的因子只有\(1, p, p^2, \dots p^x\)(最多logn個) 那麼可以直接dp, 設\(f[i][j]\)表示經過了\(i\)輪,當前數是\(p^j\)的概率,轉移的時候列舉這一輪的\(p^j\)轉移

D Makoto and a Blackboard

積性函式 f ( p ∗

CF1097D Makoto and a Blackboard 題解

題意就是給一個數,每次可以把它替換成為它的一個因數,問替換k次後期望的數為多少 一看就不會做,只好先從簡單的開始著手 如果一個數為質數,肯定非常好處理,有 1

CF1097D Makoto and a Blackboard 函式、概率期望、DP

傳送門 比賽秒寫完ABC結果不會D……最後C還fst了qwq 首先可以想到一個約數個數\(^2\)乘上\(K\)的暴力DP,但是顯然會被卡 在\(10^{15}\)範圍內因數最多的數是\(978217616376000=2^6 \times 3^4 \times 5^3 \times 7^2 \ti

CF1097D Makoto and a Blackboard

線性 lac 當前 ack black 概率 答案 info src 這種題顯然不會無緣無故地套上個期望,所以優先考慮期望的線性性。 也就是說,我們可以考慮最後每個質因子的期望值,累加得到答案。 發現我們計算這個東西的時候只關心某個質因子當前的次數,因此,所有的質因子的期

【HDOJ5528】Count a * b函式

題意:設f(i)為0<=x,y<=i-1且xy%i=0的(x,y)對數,g(i)為sigma f(j) [i%j==0] 給定n,求g(n),答案對2^64取模 T<=2e4,n<=1e9 思路:這題堅定了我要找一個專業數學手的決心…… x,y從[0,i-1]等價於從[1,i]

CodeForces 547EMike and FriendsAC自動機+DFS序+主席樹

src father ast bsp each In ant 如何 PE What-The-Fatherland is a strange country! All phone numbers there are strings consisting of lowercas

CodeForces - 258DLittle Elephant and Broken Sorting概率DP

題意:長度為n的排列,m次交換xi, yi,每個交換x,y有50%的概率不發生,問逆序數的期望  。n, m <= 1000 思路:我們只用維護大小關係,dp[i][j]表示位置i的數比位置j的數大的概率。 那麼每次交換x和y。  假設x<y,那麼區間就有三種: 

CodeForces - 321ECiel and Gondolas 四邊形不等式優化DP

題意:N個人排成一行,分成K組,要求每組的不和諧值之和最小。 思路:開始以為是斜率優化DP,但是每個區間的值其實已經知道了,即是沒有和下標有關的未知數了,所以沒必要用斜率。 四邊形優化。 dp[i][j]表示前j個人分為i組的最小代價。 #include<bits/stdc++.h>

Codeforces 1076E Vasya and a Tree樹狀陣列

題意:給你一顆以1為根節點的樹,初始所有節點的權值為0,然後有m個操作,每個操作將點x的所有距離不超過d的節點權值+1,問經過m次操作後每個節點權值是多少? 思路:如果是一個序列,就可以直接用樹狀陣列做,但這是一顆樹,所以我們可以想辦法把它轉化成序列。我們可以先求出每個節點的dfs序,以及深度和子樹的大小,

CodeForces - 367ESereja and Intervals組合數&&DP

ant lov strong clas require sequence 組合 pri c++ Sereja is interested in intervals of numbers, so he has prepared a problem about interval