跳到正文

题解

Oiclass ASH01. 卡牌 题解

Hope5365约 2 分钟
原文
文章信息与版本

文章信息

发布
2024年7月7日
收录
2025年12月12日
更新
2026年11月10日
篇幅
约 2 分钟

版本

  1. v1当前

先放AC代码:

C++
#include <iostream>
#include <cstdio>
using namespace std;
#define int long long

int t;

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	freopen("card.in","r",stdin);
	freopen("card.out","w",stdout);
	cin >> t;
	while (t --)
	{
		int l,r;
		cin >> l >> r;
		int ans = 0;
		if (r - l <= 3)
		{
			for(int i = l;i <= r;i ++)
				if (i % 4 != 2) ans ++;
//			cout << ans << endl;
		}
		else
			while ((r - l + 1) % 4 != 0) ans += (r % 4 != 2),r --;
		cout << ans + (r - l + 1) / 4 * 3 << endl;
	}
	return 0;
}

俗话说的好,打表过样例,暴力出奇迹。

一看样例,我们就想:只要把区间完全平方数的个数减掉就行了!

但是出题人非常的坏,特意让我们这么想。

我们只要打表一下就会发现,满足条件的规律是101110111...。

于是我们就可以把 LL 和 RR 移到差模 44 等于 00 的区间后,直接计算就可以了。

需要注意的是,需要特判 R−L<=3R-L<=3 的情况。

原文