1151F - Sonya and Informatics - CodeForces Solution


combinatorics dp matrices probabilities *2300

Please click on ads to support us..

Python Code:

import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def matmul(a, b, n):
    c = []
    for i in range(n):
        for j in range(n):
            x = 0
            for k in range(n):
                x += a[n * i + k] * b[n * k + j] % mod
                x %= mod
            c.append(x)
    return c

n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = pow(10, 9) + 7
s = n - sum(a)
m = min(s, n - s) + 1
x = [0] * (m * m)
b = list(a)
b.sort()
for i in range(m):
    for j in range(n):
        for l in range(j + 1, n):
            if b[j] == b[l]:
                x[i * m + i] += 1
                continue
            b[j], b[l] = b[l], b[j]
            c = 0
            for u in range(s):
                c += b[u]
            x[i * m + c] += 1
            b[j], b[l] = b[l], b[j]
    if i ^ (m - 1):
        b[i], b[-i - 1] = b[-i - 1], b[i]
y = [min(i % (m + 1), 1) ^ 1 for i in range(m * m)]
z = pow(n * (n - 1) // 2, k, mod)
invz = pow(z, mod - 2, mod)
p = 1
while k:
    if k & p:
        y = matmul(x, y, m)
        k ^= p
    x = matmul(x, x, m)
    p *= 2
ans = y[sum(a[:s]) * m] * invz % mod
print(ans)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const ll mod=1e9+7;
ll qpow(ll a,ll b){ll res=1;while(b){if(b&1)res=res*a%mod;a=a*a%mod,b>>=1;}return res;}
const int N=102;
int n,k,m,x,a[N];
int A[N],B[N],C[N];
struct mul{
	int n;
    ll g[N][N];
	mul(int n_){n=n_;memset(g,0,sizeof g);}
    mul operator *(mul &y){
        mul c(y.n);
        for(int i=0;i<n;i++)
        for(int k=0;k<n;k++)
        for(int j=0;j<n;j++)
        c.g[i][j]=(c.g[i][j]+g[i][k]*y.g[k][j])%mod;
        return c;
    }
};
inline mul ksm(mul a,ll b){
    mul res(a.n);
    for(int i=0;i<m+1;i++)res.g[i][i]=1;
    while(b){
        if(b&1)res=res*a;
        a=a*a,b>>=1;
    }
    return res;
}
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]),m+=!a[i];
	for(int i=0;i<=m;i++)A[i]=(m-i)*(m-i)%mod,B[i]=i*(n-2*m+i)%mod,C[i]=((n-1)*n/2-A[i]+mod-B[i]+mod)%mod;
	mul ans(m+1);
	ans.g[0][0]=C[0],ans.g[1][0]=A[0];
	ans.g[m][m]=C[m],ans.g[m-1][m]=B[m];
	for(int i=1;i<m;i++)ans.g[i][i]=C[i],ans.g[i-1][i]=B[i],ans.g[i+1][i]=A[i];
	ans=ksm(ans,k);
	ll cnt=0;
	for(int i=1;i<=m;i++)x+=!a[i];
	for(int i=0;i<=m;i++)cnt=(cnt+ans.g[i][x])%mod;
	printf("%lld",ans.g[m][x]*qpow(cnt,mod-2)%mod);
	return 0;
}
 		  			   		 			 	  						  	


Comments

Submit
0 Comments
More Questions

810A - Straight A
1433C - Dominant Piranha
633A - Ebony and Ivory
1196A - Three Piles of Candies
299A - Ksusha and Array
448B - Suffix Structures
1092B - Teams Forming
1166C - A Tale of Two Lands
544B - Sea and Islands
152B - Steps
1174D - Ehab and the Expected XOR Problem
1511A - Review Site
1316A - Grade Allocation
838A - Binary Blocks
1515D - Phoenix and Socks
1624D - Palindromes Coloring
1552F - Telepanting
1692G - 2Sort
1191A - Tokitsukaze and Enhancement
903A - Hungry Student Problem
52B - Right Triangles
1712A - Wonderful Permutation
1712D - Empty Graph
1712B - Woeful Permutation
1712C - Sort Zero
1028B - Unnatural Conditions
735B - Urbanization
746C - Tram
1278B - A and B
1353D - Constructing the Array