Hiển thị các bài đăng có nhãn probabilities. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn probabilities. Hiển thị tất cả bài đăng

Thứ Sáu, 4 tháng 1, 2019

Codeforces Round 57- Bài 6

Bài 6: A permutation of size $n$ is an array of size $n$ such that each integer from $1$ to $n$ occurs exactly once in this array. An inversion in a permutation $p$ is a pair of indices $(i,j)$ such that $i>j$ and $a_i,a_j$. For example, a permutation $[4,1,3,2]$ contains $4$ inversion: $(2,1),(3,1),(4,1),(4,3)$.
You are given a permutation $p$ of size $n$. However, the numbers on some positions are replaced by $-1$. Let the valid permutation be such a replacement of $-1$ in this sequence back to numbers from $1$ to $n$ in such a way that the resulting sequence is a permutation of size $n$.
The given sequence was turned into a valid permutation randomly with the equal probability of getting each valid permutation.
Calculate the expected total number of inversions in the resulting valid permutation.
It can be shown that it is in the form of $\frac{P}{Q}$ where $P$ ans $Q$ are non-negative integers and $Q\ne 0$. Report the value of $P.Q^{-1}(\text{ mod }998244353)$.
Input
The first line contains a single integer $n(1\le n\le 2.10^5)$- the length of the sequence.
The second line contains $n$ integers $p_1,p_2,...,p_n(-1\le p_i\le n,p_i\ne 0)$- the initial sequence.
It is guaranteed that all elements not equal to $-1$ are pairwise distinct.
Output
Print a single integer- the expected total number of inversions in the resulting valid permutation.
It can be shown that it is in the form of $\frac{P}{Q}$ where $P$ and $Q$ are non-negative integers and $Q\ne 0$. Report the value of $P.Q^{-1}(\text{ mod }998244353)$.
Examples
input
Copy
3
3 -1 -1
output
Copy
499122179
input
Copy
2
1 2
output
Copy
0
input
Copy
2
-1 -1
output
Copy
499122177
 Solution:

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=200005,M=998244353;
int num[N],a[N],flag[N],g[N],n,f[N];
void insert(int x){
 for (;x;x-=x&-x)num[x]++;
}
int find(int x){
 int ans=0;
 for (;x<=n;x+=x&-x)ans+=num[x];
 return ans;
}
int ksm(int x,int y){
 if (!y)return 1;
 int z=ksm(x,y/2);
 z*=z;z%=M;
 if (y&1)z*=x;
 return z%M;
}
signed main(){
 scanf("%lld",&n);
 for (int i=1;i<=n;i++)scanf("%lld",&a[i]),flag[a[i]]=1;
 for (int i=n;i;i--){
  f[i]=f[i+1];
  if (a[i]==-1)f[i]++;
  g[i]=g[i+1];
  if (!flag[i])g[i]++;
 }
 int ans=f[1]*(f[1]-1)%M*ksm(4,M-2)%M;
 for (int i=1;i<=n;i++)
  if (a[i]!=-1){
   ans+=find(a[i]);
   insert(a[i]);
   (ans+=(f[1]-g[a[i]])*f[i]%M*ksm(f[1],M-2)+g[a[i]]*(f[1]-f[i])%M*ksm(f[1],M-2))%=M;
  }
 printf("%lld",ans); 
 return 0; 
}

Thứ Năm, 3 tháng 1, 2019

Codeforces Round 57- Bài 5

Bài 5: Hasan loves playing games and has recently discovered a game called TopScore. In this soccer-like game there are $p$ players doing penalty shoot-outs. Winner is the one who scores the most. In case of ties, one of the top-scores will be declared as the winner randomly with equal probability.
They have just finished the game and now are waiting for the result. But there's a tiny problem!. The judges have lost the paper of scores! Fortunately they have calculated sum of the scores before they get lost and also for some of the players they have remembered a lower bound on how much they scored. However, the information about the bounds is private, so Hasan only got to know his bound.
 According to the available data, he knows that his score is at least $r$ and sum of the scores is $s$.
Thus the final state of the game can be represented in form of sequence of $p$ integers $a_1,a_2,...,a_p(0\le a_i)$- player's scores. Hasan is player number $1$, so $a_1\ge r$. Also $a_1+a_2+...+a_p=s$. Two clares are considered different if here exisits some position $i$ such that the value of $a_i$ differs in these states.
Once again, Hasan doesn't know the exact scores (he doesn't know his exact score as well). So he considers each of the final states to be equally probable to achieve.
Help Hasan find the probability of him winning.
It can be shown that it is in the form of $\frac{P}{Q}$ where $P$ and $Q$ are non-negative integers and $Q\ne 0,P\le Q$. Report the value of $P.Q^{-1}(\text{ mod }998244353)$.
Input
The only line contains three integers $p,s$ and $r(1\le p\le 100,0\le r\le s\le 5000)$- the number of players, the sum of scores of all players and Hasan's score, respectively.

Print a single integer - the probability of Hasan winning
Output
It can be shown that it is in the form of $\frac{P}{Q}$ where $P$ and $Q$ are non-negative integers and $Q\ne 0,P\le Q$. Report the value of $P.Q^{-1}(\text{ mod }998244353)$.
Examples
input
Copy
2 6 3
output
Copy
124780545
input
Copy
5 20 11
output
Copy
1
input
Copy
10 30 10
output
Copy
85932500
Solution:
#include <bits/stdc++.h>
using namespace std;

#define ll long long
int p, s, r, M=998244353;
ll iv[5100], f1[5100], f2[5100], ans;

int main() {
 ios::sync_with_stdio(0);
 cin.tie(0);

 iv[1]=f1[0]=f1[1]=f2[0]=f2[1]=1;
 for(int i=2; i<5100; ++i) {
  iv[i]=(M-M/i)*iv[M%i]%M;
  f1[i]=f1[i-1]*i%M;
  f2[i]=f2[i-1]*iv[i]%M;
 }
 cin >> p >> s >> r;
 for(int i=1; i<=p&&s-r*i>=0; ++i)
  ans+=(i&1?1:-1)*f1[s-r*i+p-1]*f2[s-r*i]%M*f2[i]%M*f2[p-i]%M;
 ans=(ans%M+M)*f1[p-1]%M*f1[s-r]%M*f2[s-r+p-1]%M;
 cout << ans;
}

Bài G - Educatioal Round 62

Đề bài: Bạn được cho 1 đồ thị vô hướng đặc biệt. Nó bao gồm $2n$ đỉnh được đánh số từ 1 đến 2n. Dưới đây là một số đặc tính của đồ thị: + ...