NOIP模拟(10.27)T3 心灵治愈

心灵治愈

题目背景:

10.27 NOIP模拟T3

分析:容斥

 

终于知道了一件事,一道题不够难,还可以通过题目描述来增加其难度级别······

讲题,首先前面一长串就是为了告诉你,最终这n + 1个数gcd1对吧······然后它给定了第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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值