Sponsors

Tinkoff Challenge – Final Round (Codeforces Round #414, rated, Div. 1 + Div. 2), problem: (G) Replace All Solution In C/C++

#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<queue>
#include<bitset>
#include<string>
#include<cstdio>
#include<cctype>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<cassert>
#include<sstream>
#include<numeric>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define For(i,x,y) for(int i=x,_y=y;i<=_y;++i)
#define Rep(i,x,y) for(int i=x,_y=y;i>=_y;–i)
#define pub push_back
#define pob pop_back
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
inline int read()
{
int x=0;char c=getchar();bool f=0;
for(;c>’9’||c<‘0′;f=c==’-‘,c=getchar());
for(;c>=’0’&&c<=’9’;x=(x<<1)+(x<<3)+c-‘0’,c=getchar());
return f?-x:x;
}
inline void write(int x)
{
if(x<0)putchar(‘-‘),x=-x;
if(x>=10)write(x/10);
putchar(x%10+48);
}
const int mo=1e9+7,N=1e6+5;
int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
}
void renew(int &x,int y)
{
if((x+=y)>=mo)x-=mo;
}
int Pow(int a,int b)
{
int res=1;
for(;b;b>>=1,a=(ll)a*a%mo)if(b&1)res=(ll)res*a%mo;
return res;
}
int fac[2*N],inv[2*N],pw[2*N],p[2*N],g[2*N],phi[2*N];
bool vis[2*N];
void prepare(int n)
{
fac[0]=inv[0]=pw[0]=1;
For(i,1,n)fac[i]=(ll)fac[i-1]*i%mo;
inv[n]=Pow(fac[n],mo-2);
Rep(i,n-1,1)inv[i]=(ll)inv[i+1]*(i+1)%mo;
For(i,1,n)pw[i]=pw[i-1]*2%mo;
g[1]=phi[1]=1;vis[1]=1;
For(i,2,n)
{
if(!vis[i])p[++*p]=i,phi[i]=i-1;
for(int j=1;j<=*p&&(ll)i*p[j]<=n;++j)
{
vis[i*p[j]]=1;
if(i%p[j])phi[i*p[j]]=phi[i]*(p[j]-1);else
{
phi[i*p[j]]=phi[i]*p[j];
break;
}
}
}
For(i,2,n)g[i]=(g[i-1]+phi[i]*2)%mo;
}
int n,m,la,lb,ans;
char a[N],b[N];
int C(int n,int m)
{
return (ll)fac[n]*inv[m]%mo*inv[n-m]%mo;
}
int calc1(int c0,int c1)
{
if(c0<0)c0*=-1,c1*=-1;
int d=gcd(c0,c1);
c0/=d,c1/=d;
return (pw[n/max(c0,c1)+1]-2+mo)%mo;
}
int calc2(int c)
{
int mi=1,res=0;
For(i,1,la)
{
if(a[i]==’?’&&b[i]==’?’)mi=mi*2%mo;else
if(a[i]!=’?’&&b[i]!=’?’&&a[i]!=b[i]){mi=0;break;}
}
renew(c,mo-mi);
renew(res,(ll)(pw[n+1]-2+mo)*(pw[n+1]-2+mo)%mo*mi%mo);
For(i,1,n)renew(res,(ll)g[n/i]*c%mo*pw[i]%mo);
return res;
}
int a0,a1,a2,b0,b1,b2;
int main()
{
prepare(2000000);
scanf(“%s%s%d”,a+1,b+1,&n);
la=strlen(a+1);lb=strlen(b+1);
For(i,1,la)
{
if(a[i]==’A’)++a0;else
if(a[i]==’B’)++a1;else
if(a[i]==’?’)++a2;
}
For(i,1,lb)
{
if(b[i]==’A’)++b0;else
if(b[i]==’B’)++b1;else
if(b[i]==’?’)++b2;
}
int cnt=0;
For(i,-b2,a2)
{
int t1=a0-b0+i,t2=b1-a1+b2-a2+i,c=C(a2+b2,i+b2);
if(!t1&&!t2)
{
renew(ans,calc2(c));
assert(++cnt<2);
}else
if((ll)t1*t2>0)
{
renew(ans,(ll)c*calc1(t1,t2)%mo);
}
}
printf(“%d\n”,ans);
}

One to watch: Macquarie...

Macquarie Predicts Double-Digit Returns for ASX Healthcare Stock Macquarie Group,...

THE FREEPER CANTEEN –...

The Freeper Canteen: A Look That Up, It Must...

215% PENGU Rally Incoming?...

215% PENGU Rally Incoming? Analyst Predicts Imminent Breakout The cryptocurrency...

Junk is the new...

Junk is the New Punk: Why We're Falling Back...

Are weighted vests good...

Unlocking the Power of AI-Generated Content for SEO In today's...

Are weighted vests good...

Unlocking the Power of AI-Generated Content: A Boon or...

One to watch: Macquarie tips double-digit returns for this ASX healthcare stock

Macquarie Predicts Double-Digit Returns for ASX Healthcare Stock Macquarie Group, a prominent Australian financial services company, has issued a bullish prediction for a specific ASX-listed...

THE FREEPER CANTEEN – The Look That Up, It Must Be On My Bucket List Quiz – Wednesday, August 27, 2025

The Freeper Canteen: A Look That Up, It Must Be On My Bucket List The Freeper Canteen, a unique online community, has recently launched a...

215% PENGU Rally Incoming? Analyst Says Token ‘Inches’ From Next Leg Up

215% PENGU Rally Incoming? Analyst Predicts Imminent Breakout The cryptocurrency market is constantly fluctuating, and predicting the next big move is a challenge. However, one...

Junk is the new punk: Why we’re falling back in love with retro tech

Junk is the New Punk: Why We're Falling Back in Love with Retro Tech In a world dominated by sleek, minimalist designs and cutting-edge technology,...

Are weighted vests good for bones and muscle? Fact-checking a fitness trend – NPR

Unlocking the Power of AI-Generated Content for SEO In today's digital landscape, search engine optimization (SEO) is paramount for online success. But creating high-quality,...

Are weighted vests good for bones and muscle? Fact-checking a fitness trend – NPR

Unlocking the Power of AI-Generated Content: A Boon or Bane for SEO? The world of search engine optimization (SEO) is constantly evolving, and the recent...

TSMC cuts Chinese tools from cutting-edge chip production to avoid US ire – Nikkei Asia

TSMC Ditches Chinese Tools for Cutting-Edge Chip Production Taiwan Semiconductor Manufacturing Company (TSMC), the world's leading chipmaker, is reportedly phasing out Chinese-made equipment from its...

Google Messages beta rolling out QR code key verification – 9to5Google

Google Messages Adds QR Code Key Verification for Enhanced Security Google is bolstering the security of its Messages app with a new feature rolling out...

A Secretive US Space Plane Will Soon Test Quantum Navigation Technology

## US Space Plane to Test Revolutionary Quantum Navigation Technology The United States is on the cusp of a significant leap forward in space navigation....