【莫比乌斯反演】E - Sum of gcd of Tuples (Hard)-ABC162
题目链接(https://atcoder.jp/contests/abc162/tasks/abc162_e)
Time Limit: 2 sec / Memory Limit: 1024 MB
Score : points
Problem Statement
Consider sequences of length consisting of integers between and (inclusive).
There are such sequences. Find the sum of over all of them.
Since this sum can be enormous, print the value modulo.Here denotes the greatest common divisor of .
Constraints
All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
Output
Print the sum of over all sequences, modulo .
Sample Input 1
3 2
Sample Output 1
9
Thus, the answer is 9.
Sample Input 2
3 200
Sample Output 2
10813692
Sample Input 3
100000 100000
Sample Output 3
742202979
Be sure to print the sum modulo.
题意
给一个长度为的序列,每一位的值都取到之间,求所有序列,(单序列)所有数位最大公因数之和,模
思路
(苦脸)初学,很难搞懂,照葫芦画瓢,emo老师讲课笔记++;
第一反应应该枚举最大公因数,则,接下来再看有多少组
所以初步可以写出
设
如果是的倍数的话,他就有种取法
那么求和可得
就可以知道
代回原来的式子可得
置换两个求和号
又因为(相当于是用充斥的方法去计算的数量)
证明:(暂时咕,马上补)
所以
复杂度
AC代码
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3f
#define int long long
#define ull unsigned long long
#define PII pair<int,int>
#define endl '\n'
using namespace std;
typedef long long ll;
const int N = 1e5 + 100;
const int mod = 1e9 + 7;
const double pi = acos(-1.0);
int t, n, k;
int a[N],b[N];
int gcd(int a, int b) {return b ? gcd(b, a % b) : a;}
int quickpow(int a, int b) {//快速幂
int res = 1;
while (b > 0) {
if (b & 1) res = res * a % mod;
b >>= 1;
a = a * a % mod;
}
return res;
}
void init() {//初始化
for (int i = 0; i <= n; i++) {
a[i] = 0;
b[i] = 0;
}
return;
}
void solve() {
init();
int ans = 0;
for (int i = 1; i <= k; i++) {//求┕k/d'┙^N
a[i] = quickpow(k / i, n) % mod;
}
for (int i = k; i >= 1; i--) {//求φ(d')
b[i] = a[i];
for (int j = i << 1; j <= k; j += i) {
b[i] -= b[j];
}
b[i] = (b[i] + mod) % mod;
}
for (int i = 1; i <= k;i ++) {//相乘累加
ans += (b[i] * i) % mod;
}
cout << (ans + mod) % mod << endl;//取模输出
return;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
while (cin >> n >> k) {
solve();
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用