combinatorics dp math probabilities *2800

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define oo 1000000010

const int N = 5010;

int fastpower(int num,int po){
	int fres = 1;
	while(po > 0){
		if(po & 1)
			fres = (long long)fres * num % mod;
		po >>= 1;
		num = (long long)num * num % mod;
	}
	return fres;
}

inline void add(int &x,int y){x += y;if(x >= mod) x -= mod;}

inline void sub(int &x,int y){x -= y;if(x < 0) x += mod;}

int n , m , v;

int arr[N];

int dp[N][N];

int main(){
	scanf("%d%d%d",&n,&m,&v);
	for(int i = 0 ;i < n;i++){
		scanf("%d",&arr[i]);
	}

	int cur = fastpower(fastpower(n , m) , mod - 2);
	for(int i = 0 ;i <= n;i++) dp[n][i] = fastpower(n , m - i);

	for(int i = n - 1;i >= 0;i--){
		for(int j = 0 ; j <= min(i , m);j++){
			dp[i][j] = (long long)arr[i] * dp[i + 1][j] % mod;
			add(dp[i][j], (long long)((long long)j * v % mod) * dp[i + 1][j] % mod);
			if(j < m) 
				add(dp[i][j] , (long long)((long long)((long long)(m - j) * (i + 1) % mod) * v % mod) * dp[i + 1][j + 1] % mod);
		}
	}
	cout << (long long)dp[0][0] * cur % mod << endl;

	return 0;
} 


Comments

Submit
0 Comments
More Questions

1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)
1151B - Dima and a Bad XOR
1435B - A New Technique
1633A - Div 7
268A - Games
1062B - Math
1294C - Product of Three Numbers
749A - Bachgold Problem
1486B - Eastern Exhibition
1363A - Odd Selection
131B - Opposites Attract
490C - Hacking Cypher
158B - Taxi
41C - Email address
1373D - Maximum Sum on Even Positions
1574C - Slay the Dragon
621A - Wet Shark and Odd and Even
1395A - Boboniu Likes to Color Balls
1637C - Andrew and Stones
1334B - Middle Class
260C - Balls and Boxes
1554A - Cherry
11B - Jumping Jack
716A - Crazy Computer
644A - Parliament of Berland
1657C - Bracket Sequence Deletion
1657B - XY Sequence
1009A - Game Shopping
1657A - Integer Moves