首页 > 其他分享 >洛谷 P5218 无聊的水题 II

洛谷 P5218 无聊的水题 II

时间:2023-09-10 12:44:51浏览次数:40  
标签:typedef 洛谷 水题 ll long P5218 II define

洛谷传送门

无聊的水题。

根据裴蜀定理,显然能组合出任意值的充要条件是,选出的数的 \(\gcd = 1\)。

设 \(g(i)\) 为在 \(1 \sim n\) 中选出若干个数使得它们 \(\gcd = i\) 的方案数,\(f(i)\) 为在 \(1 \sim n\) 中选出若干个数使得它们 \(\gcd\) 是 \(i\) 的倍数的方案数。我们有:

\[f(i) = \sum\limits_{i \mid j} g(j) = 2^{\left\lfloor\frac{n}{i}\right\rfloor} - 1 \]

\[g(i) = \sum\limits_{i \mid j} \mu(\frac{j}{i}) f(j) \]

因此:

\[g(1) = \sum\limits_{i = 1}^n \mu(i) (2^{\left\lfloor\frac{n}{i}\right\rfloor} - 1) \]

整除分块后使用杜教筛计算 \(\mu(i)\) 前缀和,复杂度 \(O(\sqrt{n} \log n + n^{\frac{2}{3}})\)。

code
// Problem: P5218 无聊的水题 II
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P5218
// Memory Limit: 500 MB
// Time Limit: 6000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;

const int maxn = 30000100;
const int N = 30000000;
const ll mod = 1000000007;

inline ll qpow(ll b, ll p) {
	ll res = 1;
	while (p) {
		if (p & 1) {
			res = res * b % mod;
		}
		b = b * b % mod;
		p >>= 1;
	}
	return res;
}

ll pr[maxn / 10], tot, mu[maxn], n;
bool vis[maxn];
unordered_map<ll, ll> mp;

ll dfs(ll n) {
	if (n <= N) {
		return mu[n];
	}
	if (mp.find(n) != mp.end()) {
		return mp[n];
	}
	ll res = 1;
	for (ll i = 2, j; i <= n; i = j + 1) {
		j = n / (n / i);
		res = (res - (j - i + 1) % mod * dfs(n / i) % mod + mod) % mod;
	}
	return mp[n] = res;
}

void solve() {
	mu[1] = 1;
	for (int i = 2; i <= N; ++i) {
		if (!vis[i]) {
			pr[++tot] = i;
			mu[i] = mod - 1;
		}
		for (int j = 1; j <= tot && i * pr[j] <= N; ++j) {
			vis[i * pr[j]] = 1;
			if (i % pr[j] == 0) {
				mu[i * pr[j]] = 0;
				break;
			}
			mu[i * pr[j]] = mod - mu[i];
		}
	}
	for (int i = 1; i <= N; ++i) {
		mu[i] = (mu[i] + mu[i - 1]) % mod;
	}
	scanf("%lld", &n);
	ll ans = 0;
	for (ll i = 1, j; i <= n; i = j + 1) {
		j = n / (n / i);
		ans = (ans + ((dfs(j) - dfs(i - 1)) % mod + mod) % mod * (qpow(2, n / i) + mod - 1) % mod) % mod;
	}
	printf("%lld\n", ans);
}

int main() {
	int T = 1;
	// scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}

标签:typedef,洛谷,水题,ll,long,P5218,II,define
From: https://www.cnblogs.com/zltzlt-blog/p/17691022.html

相关文章

  • 洛谷P8211 [THUPC2022 初赛] 搬砖
    题目链接以下设\(B\)为一个阈值,同时也表示值域分块的块长。先考虑所有\(b\)都不为\(0\)的情况。对于一组询问,我们设一个\(x\)表示:当前已搬完所有\(a\leqx\)的砖。那么每次只可能是以下两种情况之一:有至少一摞砖在当前这个单位时间内被搬完拿\(x\)加上\(d\),之......
  • 【刷题笔记】45. Jump Game II
    题目Givenanarrayofnon-negativeintegers nums,youareinitiallypositionedatthefirstindexofthearray.Eachelementinthearrayrepresentsyourmaximumjumplengthatthatposition.Yourgoalistoreachthelastindexintheminimumnumberofju......
  • 洛谷P5556 圣剑护符
    题目链接先考虑如何判定一个集合是否存在两个异或和相同的子集\(s,t\),不然解决这道题就是无稽之谈。根据异或的优良性质,不妨在\(s,t\)中分别去掉\(s\capt\),之后从\(s\)中任意移动\(|s|-1\)个元素到\(t\)中去,易发现此时两个集合的元素异或和还是相同。也就是说,我们现......
  • 洛谷100题计划(20/100)
    洛谷100题计划(20/100)P1147连续自然数和-洛谷|计算机科学教育新生态(luogu.com.cn)题意就是找一段连续的区间,使得区间和为\(M\),很容易发现,其实这个区间就是一个等差数列,所以\(区间和=\frac{(首项+末项)\times项数}{2}\),假设首项为\(L\),末项为\(R\),那么可以得出......
  • 【230908-17】▲ABC中,b=2,B=30°,C=45°,则S△ABC=?(2013年全国II卷)
    ......
  • 【IIS】HTTP 错误 405.0 - Method Not Allowed,无法显示您正在查找的页面,因为使用了无
    转自:https://blog.csdn.net/weixin_38211198/article/details/103597330问题HTTP 错误 405.0 - Method Not Allowed无法显示您正在查找的页面,因为使用了无效方法(HTTP 谓词)。 解决在IIS中,找到处理程序映射上面的报错已经指明是WebDAVModule模块,找到该模块  ......
  • 剑指 Offer 53 - II. 0~n-1中缺失的数字
    题目链接:剑指Offer53-II.0~n-1中缺失的数字题目描述:解法思路:代码:funcmissingNumber(nums[]int)int{varresint//注意这里,加1(因为要计算0~n个数的和)n:=len(nums)+1ifn==0{returnres}res=(n-1)*n/2for_,v......
  • IIS10配置读取json
    步骤一:iis必须开启asp支持,如果你的iis默认没有支持asp,需要安装asp步骤二:打开”MIME类型“。点击添加,扩展名写“.json”【不要引号】,MIME类型写”application/json“步骤三:打开”处理程序映射“,点击”添加脚本映射“,请求路径写”*.json”【不要引号】,可执行文件为“C:\Windows\Syste......
  • 单词搜索 II(字典树、数组)、合并两个有序数组(数组、双指针)、验证回文串(双指针、字
    单词搜索II(字典树、数组)给定一个mxn二维字符网格board****和一个单词(字符串)列表words,找出所有同时在二维网格和字典中出现的单词。单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母在一......
  • 【230908-6】已知:a=2^4/3,b=4^2/5,c=25^1/3,则a,b,c的大小关系是?(23年全国III卷)
    ......