题解:AT_abc460_e [ABC460E] - x + y ≡ x + y

· · 题解

纯数学题。

发现 concat(x,y)=x\times 10^d + y,其中 d 为 y 的位数。

带入得 x\times 10^d + y \equiv x +y \pmod M。

由同余的基本性质得

x\times 10^d \equiv x \pmod M x\times (10^{d}-1) \equiv 0 \pmod M

即 M\mid x\times (10^{d}-1)。

接下来是奇思妙想。

令 g= \gcd (M,10^{d}-1)。

由整除的基本性质得 \dfrac{M}{g}\mid x\times \dfrac{10^{d}-1}{g}。

由于 \dfrac{M}{g} 与 \dfrac{10^{d}-1}{g} 互质则 \dfrac{M}{g}\mid x。

因 g = \gcd (M,10^{d}-1) 只与位数有关所以枚举 d。

$y$ 的合法个数为 $\min(10^{d+1},N)-10^{d}+1$。 由乘法原理可得当 $y$ 长度为 $d$ 是方案数为 $\dfrac{N\times g}{M}\times (\min(10^{d+1},N)-10^{d}+1)$。 **AC Code:** ```cpp /** * @Author: xmy201315 * @Date: 2025.11.15 * @Last Modified by: xmy201315 * @Last Modified time: 2026.05.30 */ #include <bits/stdc++.h> #define N 300010 #define sz(s) s.size() #define mod 1000000007 #define P 998244353 #define ll long long #define all(x) x.begin(), x.end() #define pb push_back #define per(i, l, r) for (ll i = l; i >= r; i--) #define rep(i, l, r) for (ll i = l; i <= r; i++) #define in insert using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin>>t; while(t--){ int p[1000],l=0; ll N1,M; cin>>N1>>M; __int128 n=N1,m=M; __int128 d=0; __int128 x=n,p10=1,ans=0; while(x)x/=10,d++; rep(i,1,d){ __int128 g=__gcd(m,p10*10-1); __int128 b_d=n/(m/g); if(i<d)ans=(ans+9*p10%P*b_d%P)%P; else ans=(ans+(n-p10+1)%P*b_d%P)%P; p10*=10; } while(ans)p[++l]=ans%10,ans/=10; per(i,l,1)cout<<p[i]; if(l==0){ cout<<"0\n"; continue; } l=0; cout<<'\n'; } cerr << clock() * 1.0 / CLOCKS_PER_SEC * 1000 << 'm'<<'s'<<'\n'; } ```