1778D - Flexible String Revisit - CodeForces Solution


combinatorics dp math probabilities

Please click on ads to support us..

C++ Code:

//f[i] = a[i]+b[i]*f[i+]
#include <bits/stdc++.h>
using namespace std;

#define int long long
const int M=998244353, N=1001000; 

int mmul(int i, int t){
  if(t==0)return 1;
  int res=mmul(i, t/2); 
  res=res*res%M;
  if(t&1)res=res*i%M;
  return res;}
int inv(int i){return mmul(i, M-2);}

int a[N], b[N], f[N];

void solve(int n){
  f[0]=0, a[1]=1, b[1]=(n-1)*inv(n)%M;
  for(int i=2; i<n; i++){
    int z= 1 - i*inv(n)%M*b[i-1]%M;
    a[i] = i*inv(n)%M*a[i-1]%M +1;
    a[i]=a[i]%M*inv(z)%M;
    b[i]=(n-i)*inv(n)%M*inv(z)%M;
  }
  f[n]=(a[n-1]+1)*inv(1-b[n-1])%M;
  for(int i=n-1; i>=1; i--)f[i]=(a[i]+b[i]*f[i+1])%M;
return;}

signed main(){
  int t;
  cin >> t;
  while(t--){
    string a, b;
    int cnt=0, n;
    cin >> n >> a >> b;
    for(int i=0; i<n; i++)if(a[i]!=b[i])cnt++;
    solve(n);
    cout << (f[cnt]%M+M)%M << '\n';
  }
return 0;}


Comments

Submit
0 Comments
More Questions

501B - Misha and Changing Handles
1496A - Split it
1666L - Labyrinth
1294B - Collecting Packages
1642B - Power Walking
1424M - Ancient Language
600C - Make Palindrome
1669D - Colorful Stamp
1669B - Triple
1669A - Division
1669H - Maximal AND
1669E - 2-Letter Strings
483A - Counterexample
3C - Tic-tac-toe
1669F - Eating Candies
1323B - Count Subrectangles
991C - Candies
1463A - Dungeon
1671D - Insert a Progression
1671A - String Building
1671B - Consecutive Points Segment
1671C - Dolce Vita
1669G - Fall Down
4D - Mysterious Present
1316B - String Modification
1204A - BowWow and the Timetable
508B - Anton and currency you all know
1672A - Log Chopping
300A - Array
48D - Permutations