AtCoder Beginner Contest 357 D(数学)

题目地址

前言

本来想 vp D~G的,结果赛时 D 还没调出来。一看题解恍然大悟,感觉智商被按在地上摩擦。

My Solution

假设 nk 位,最后结果一定是 n((10k)0+(10k)1+...+(10k)n1)。那么问题就变为了解决:快速求 ((10k)0+(10k)1+...+(10k)n1)

考虑倍增。预处理 fi 表示 ((10k)0+(10k)1+...+(10k)2i1)n 拆解为二进制数累计贡献即可。

注意要用欧拉定理(本题其实就是费马小定理)对指数取模。(xyxy%ϕ(mod)(%mod),因为 mod=998244353 是质数,所以 ϕ(mod)=mod1

My Code

PS:自己造的样例是错的,在那里调了半天。

#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read() {
    int x = 0, f = 1; char ch = getchar();
    while(ch<'0' || ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') { x=(x<<3)+(x<<1)+(ch^48); ch=getchar(); }
    return x * f;
}
const int mod = 998244353;
int f[64];
int Pow(int x,int y) {
	int res = 1, base = x;
	while(y) {
		if(y&1) res = res*base%mod;
		base = base*base%mod;
		y >>= 1;
	}
	return res;
}
signed main()
{
	string s;
	cin >> s;
	int k = s.length();
	int n = stoll(s);
	f[0] = 1;
	for(int i=1;i<64;++i) {
		f[i] = f[i-1]*((1+Pow(10, ((1ll<<(i-1))%(mod-1))*k%(mod-1)))%mod) % mod;
	}
	int cnt = 0;
	for(int j=63;j>=0;--j) { 
		if(!((n>>j)&1ll)) continue;
		cnt = cnt*Pow(10, (((1ll<<(j))%(mod-1))*k)%(mod-1)) % mod;
		cnt = (cnt + f[j]) % mod;
	}
	int ans = (n%mod)*cnt % mod;
	printf("%lld\n",ans);
	return 0;
}

Std Solution

x=10k ,原式 =n(1+x1+...+xn1)=n1xn1x(%mod),注意 11x=(1x)mod2(%mod)(费马小定理)。

Code

注意 n1018,最后算的时候 n 要先取模再乘,不然会直接爆 long long

#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read() {
    int x = 0, f = 1; char ch = getchar();
    while(ch<'0' || ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') { x=(x<<3)+(x<<1)+(ch^48); ch=getchar(); }
    return x * f;
}
const int mod = 998244353;
int Pow(int x,int y) {
	int res = 1, base = x;
	while(y) {
		if(y&1) res = res*base%mod;
		base = base*base%mod;
		y >>= 1;
	}
	return res;
}
int Inv(int x) {
	return Pow(x, mod-2);
}
signed main()
{
	string s;
	cin >> s;
	int k = s.length();
	int n = stoll(s);
	int x = Pow(10, k);
	int ans = (n%mod)*(Pow(x, n)-1)%mod*Inv((x-1+mod)%mod)%mod;
	ans = (ans%mod + mod) % mod;
	printf("%lld",ans);
	return 0;
}
posted @   基地AI  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
点击右上角即可分享
微信分享提示