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

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;
}

Codeforces Round 57- Bài 2


Bài 2:  You are given a string $s$ of length $n$ consisting only of lowercase Latin letters
A substring of a string is a contiguous subsequence of that string. So, string "forces" is substring of string "codeforces", but string "coder" is not.
Your task is to calculate the number of ways to remove exactly one substring from this string in such a way that all remaining characters are equal (the number of distinct characters either zero or one).
It is guaranteed that there is at least two different characters in s.
Note that you can remove the whole string and it is correct. Also note that you should remove at least one character.
Since the answer can be rather large (not very large though) print it modulo 998244353.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
Input
The first line of the input contains one integer $n(2\le n\le 2.10^5)$ - the length of the string s. 
The second line of the input contains the string $s$ of length $n$ consisting only of lowercase Latin letters
It is guaranteed that there is at least two different characters in $s$.
Output
Print one integer — the number of ways modulo 998244353 to remove exactly one substring from $s$ in such way that all remaining characters are equal.
Examples
input
Copy
4
abaa
output
Copy
6
input
Copy
7
aacdeee
output
Copy
6
Solution:
#include <bits/stdc++.h>
using namespace std;
int64_t n,i,j;
string s;
int main(){
 cin>>n>>s;
 for(;i<n&&s[i]==s[0];i++);
 for(;j<n&&s[n-1-j]==s[n-1];j++);
 cout<<(s[0]!=s[n-1]?i+j+1:i+j<n?(i+1)*(j+1):n*(n+1)/2)%998244353;
}

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ị: + ...