心灵治愈
题目背景:
分析:容斥
终于知道了一件事,一道题不够难,还可以通过题目描述来增加其难度级别······
讲题,首先前面一长串就是为了告诉你,最终这n + 1个数gcd为1对吧······然后它给定了第n + 1个数m,并且告诉你,其他数小于等于m,那么显然gcd一定就是m的因数了对吧,然后我们强行把m分解掉,m = p1k1 * p2k2 * p3k3* ……,因为m最多只能有14个不同的质因数(2 * 3 * 5 * 7 * … * 41 * 43 > 1015)然后总的方案数显然是mn,然后我们减去gcd为一个质因数的倍数的,加上两个质因数倍数的,减去三个质因数倍数的······也就是容斥原理,然后最后就得到答案了,讲道理,代码比读题简单,复杂度为O(sqrt(n) + 214 * logn)
Source:
/*
created by scarlyw
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <cctype>
#include <queue>
#include <vector>
#include <ctime>
const int MAXN = 100;
const int mod = 1000000000 + 7;
int prime_cnt;
long long prime[MAXN];
long long n, m, ans;
inline void get_prime(long long n) {
for (long long x = 2; x <= sqrt(n); ++x) {
if (n % x == 0) prime[++prime_cnt] = x;
while (n % x == 0) n /= x;
}
if (n != 1) prime[++prime_cnt] = n;
}
inline long long ksm(long long a, long long b) {
long long ans = 1;
a %= mod;
for (; b; b >>= 1, a = a * a % mod)
if (b & 1) ans = ans * a % mod;
return ans;
}
inline void dfs(int cur, long long sum, int cnt) {
if (cur == prime_cnt + 1) {
if (cnt & 1) ans = (ans - ksm(m / sum, n) + mod) % mod;
else ans = (ans + ksm(m / sum, n)) % mod;
return ;
}
dfs(cur + 1, sum * prime[cur], cnt + 1), dfs(cur + 1, sum, cnt);
}
int main() {
freopen("heal.in", "r", stdin);
freopen("heal.out", "w", stdout);
std::cin >> n >> m, get_prime(m);
dfs(1, 1, 0), std::cout << ans;
return 0;
}