跳到正文

题解

CF1485C Floor and Mod

fanfansann约 4 分钟
原文
文章信息与版本

文章信息

发布
2021年6月10日
收录
2025年12月12日
更新
2026年11月8日
篇幅
约 4 分钟
获赞
21
题目
CF1485C

版本

  1. v1当前

整套题目全部题解链接:Codeforces Round #701 (Div. 2) A ~ F ,6题全,超高质量良心题解【每日亿题】2021/2/13

C - Floor and Mod

Problem C Floor and Mod 在这里插入图片描述

Translation

定义一对数 (a,b)(a,b) 是特殊的,当且仅当 ⌊ab⌋=a mod b\lfloor\dfrac{a}{b}\rfloor=a\bmod b

给定 xx 和 yy ,求一共有多少对特殊的数 (a,b)(a,b) ,当 1≤a≤x,1≤b≤y1\le a\le x,1\le b\le y。

Solution

签到题

看数据范围,显然这是一个结论题或者 O(n)O(\sqrt n) 枚举题,我们根据题目中给定的柿子:

⌊ab⌋=a mod b⌊ab⌋=a−⌊ab⌋×ba=⌊ab⌋×(b+1)ab+1=⌊ab⌋\begin{aligned}&\lfloor\dfrac{a}{b}\rfloor=a \bmod b &\\& \lfloor\dfrac{a}{b}\rfloor=a-\lfloor\dfrac{a}{b}\rfloor\times b&\\& a=\lfloor\dfrac{a}{b}\rfloor\times (b+1)&\\& \dfrac{a}{b+1}=\lfloor\dfrac{a}{b}\rfloor\end{aligned}

因为⌊ab⌋\lfloor\dfrac{a}{b}\rfloor 一定是整数,所以 ab+1\dfrac{a}{b+1} 一定是整数,所以 b+1 ∣ ab+1\ |\ a。

所以一个很直观的想法就是枚举 bb ,对于每一个 bb , b+1b+1 的倍数 aa 显然有 xb+1\dfrac{x}{b+1} 个,显然可以整除分块。

但是需要注意题目中的给定的条件为 ⌊ab⌋=a mod b\lfloor\dfrac{a}{b}\rfloor=a\bmod b

即: ⌊ab⌋<b\lfloor\dfrac{a}{b}\rfloor<b

即:ab+1<b⇒a<b2+b\dfrac{a}{b+1}<b\Rightarrow a<b^2+b

即 a∈[0,b2+b−1]a\in [0,b^2+b-1]。那么符合题意的 aa 的个数就是: b2+b−1b+1\dfrac { b^2+b-1}{b+1}

并且 aa 的上限是 xx,故最终的答案为:

∑b=1yans=min⁡{x,b2+b−1}b+1\sum\limits_{b=1}^{y} ans = \dfrac {\min \{x, b^2+b-1\}}{b+1}

我们可以先枚举 b<xb<\sqrt x ,计算 b2+b−1b+1\dfrac { b^2+b-1}{b+1}

剩余的 b=x∼yb=\sqrt x \sim y,显然 min⁡{x,b2+b−1}=x{\min\{x, b^2+b-1\}}=x,直接整除分块即可,其中整除分块的时候,我们需要计算的是 ⌊xb+1⌋\lfloor \dfrac{x}{b+1}\rfloor,我们令 l,rl,r 代表 b+1b+1 即可。

时间复杂度 O(x)O(\sqrt x)

Code

C++
// Problem: C. Floor and Mod
// Contest: Codeforces - Codeforces Round #701 (Div. 2)
// URL: https://codeforces.com/problemset/problem/1485/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const int N = 1e5 + 7;

int n, m, t;
int x, y;

int main()
{
	scanf("%d", &t);
	while(t -- ) {
		scanf("%d%d", &x, &y);
		int a = 1, b = 1;
		ll ans = 0;
		for(; b * b + b - 1 < x && b <= y; ++ b) 
			ans += (b * b + b - 1) / (b + 1);	   

		int l = b + 1, r;  
		for(; l <= x && l <= y + 1; l = r + 1) {
			r = min(x / (x / l), y + 1);
			ans += x / l * (r - l + 1); 
			if(r == y + 1) break;
		} 
		cout << ans << endl;
	}		
	return 0;
}
原文
存档评论1 条抓取于