【莫比乌斯反演】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 : 500 points

Problem Statement

Consider sequences{A1,...,AN} of length N consisting of integers between 1 and K (inclusive).

There are KN such sequences. Find the sum of gcd(A1,...,AN) over all of them.

Since this sum can be enormous, print the value modulo(109+7).Here gcd(A1,...,AN)denotes the greatest common divisor of A1,...,AN.

Constraints

2N105

1K105

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the sum of gcd(A1,...,AN)over all KN sequences, modulo (109+7).

Sample Input 1

3 2

Sample Output 1

9

gcd(1,1,1)+gcd(1,1,2)+gcd(1,2,1)+gcd(1,2,2)+gcd(2,1,1)+gcd(2,1,2)+gcd(2,2,1)+gcd(2,2,2)=1+1+1+1+1+1+1+2=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(109+7).

题意

给一个长度为N的序列,每一位的值都取1k之间,求所有序列,(单序列)所有数位最大公因数之和,模1e9+7

思路

(苦脸)初学,很难搞懂,照葫芦画瓢,emo老师讲课笔记++;

第一反应应该枚举最大公因数d,则1dk,接下来再看有多少组 gcd(A1,...,AN)==1

所以初步可以写出

ans=d=1kd1Aik[gcd(A1,...,AN)=d]

f(d)=1Aik[gcd(A1,...,AN)=d]

g(d)=d|df(d)=1Aik[d|A1][d|A2]...[d|Ak]

d如果是Ai的倍数的话,他就有n/d种取法

那么求和可得g(d)=1Aikn/dN

就可以知道f(d)=d|dμ(d/d)g(d)=d|dμ(d/d)k/dN

代回原来的式子可得

ans=d=1kdd|dμ(d/d)k/dN

置换两个求和号

ans=d=1kk/dNd|ddμ(d/d)

又因为d|ddμ(d/d)=φ(d)(相当于是用充斥的方法去计算φ(d)的数量)

证明:(暂时咕,马上补)

所以ans=d=1kk/dNφ(d)

复杂度O(klogN)

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;
}
posted @   TomiokapEace  阅读(92)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
点击右上角即可分享
微信分享提示