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

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

Codeforces Round 57- Bài 7

Bài 7: All bus tickets in Berland have their numbers. A number consists of $n$ digits ($n$ is even). Only $k$ decimal digits $d_1,d_2,...,d_k$ can be used to form ticket numbers. If $0$ is among these digits, then numbers may have leading zeroes. For eample, if $n=4$ and only digits $0$ and $4$ can be used, then $0000,4004,4440$ are valid ticket numbers, and $0002,00,44443$ are not.
a ticket is lucky if the sum of first $\frac{n}{2}$ digits is equal to the sum of remaining $\frac{n}{2}$ digits.
Calculate the number of different lucky tickets in Berland. Since the answer may be big, print it modulo $998244353$.
Input
The first line contains two integers $n$ and $k(2\le n\le 2.10^5,1\le k\le 10)$- the number of digits in each ticket number, and the number of ticket number, and the number of different decimal digits that may be used. $n$ is even.
The second line contains a sequence of pairwise distinct integers $d_1,d_2,...,d_k(0\le d_i\le 9)$- the digits that may be used in ticket numbers. The digits are given in arbitrary order.
Output
Print the number of lucky ticket numbers, taken modulo $998244353$.
Examples
input
Copy
4 2
1 8
output
Copy
6
input
Copy
20 1
6
output
Copy
1
input
Copy
10 5
6 1 4 0 3
output
Copy
569725
input
Copy
1000 7
5 4 0 1 8 3 2
output
Copy
460571165
 Solution:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=998244353;
const int maxn = 1000111;
int n, k;
bool a[11];
ll b[maxn], inv[maxn];
int main()
{
 inv[1] = 1;
 for (int i=2; i<=maxn; i++) inv[i] = mod-(mod/i)*inv[mod%i]%mod;
 cin>>n>>k;
 n /= 2;
 int mn = 10;
 vector<int> v;
 for (int i=0; i<k; i++)
 {
  int x;
  cin>>x;
  v.push_back(x);
  mn = min(mn, x);
 }
 for (auto x:v) a[x-mn] = 1;
 b[0] = 1;
 ll ans = 0;
 for (int i=0; i<=n*10; i++)
 {
  ll sum = 0;
  for (int j=1; j<10&&j<=i; j++) sum -= b[i-j+1]*(i-j+1)%mod*a[j];
  for (int j=0; j<10&&j<=i; j++) sum += b[i-j]*a[j+1]*(j+1)%mod*n%mod;
  b[i+1] = sum%mod*inv[i+1]%mod;
  ans = (ans+b[i]*b[i])%mod;
 }
 cout<<(ans+mod)%mod<<endl;
 return 0;
}

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

Codeforces Round 57- Bài 4

Bài 4:  Vasya is preparing a contest, and now he has written a statement for an easy problem. The statement is a string of length n consisting of lowercase Latin latters. Vasya thinks that the statement can be considered hard if it contains a subsequence hard; otherwise the statement is easy. For example, hardhzazrzdhaaaaard can be considered hard statements, while harhart and drah are easy statements.
Vasya doesn't want the statement to be hard. He may remove some characters from the statement in order to make it easy. But, of course, some parts of the statement can be crucial to understanding. Initially the ambiguity of the statement is 0, and removing i-th character increases the ambiguity by a_i (the index of each character is considered as it was in the original statement , so, for example, if you delete character r from hard, and then character d, the index of d is still 4 even though you delete it from the string had).Vasya wants to calculate the minimum ambiguity of the statement, if he removes some characters (possibly zero) so that the statement is easy. Help him to do it!
Recall that subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
Input
The first line contains one integer $n(1\le n\le 10^5)$- the length of the statement.
The second line contains one string $s$ of the length $n$, consisting of lowercase Latin letters- the statement written by Vasya.
The third line contains $n$ integers $a_1,a_2,...,a_n(1\le a_i\le 998244353)$Output
Print minimum possible ambiguity of the statement after Vasya deletes some (possibly zero) characters so the resulting statement is easy.
Examples
input
Copy
6
hhardh
3 2 9 11 7 1
output
Copy
5
input
Copy
8
hhzarwde
3 2 6 9 4 8 7 1
output
Copy
4
input
Copy
6
hhaarr
1 2 3 4 5 6
output
Copy
0
Solution:
#include <bits/stdc++.h>
using namespace std;

string s;
long long n,a,b,c,d;
int main(){
 cin>>n>>s;
 
 for(int i=0; i<n; i++){
  int h;
  cin>>h;
  if(s[i]=='h') a+=h;
  else if(s[i]=='a') b=min(a,b+h);
  else if(s[i]=='r') c=min(b,c+h);
  else if(s[i]=='d') d=min(c,d+h);
 }
 cout<<d;

return 0;
}

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