跳到正文

题解

AT_abc138_f

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

文章信息

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

版本

  1. v2当前
  2. v1

挖掘取模性质:

  • 当 ⌊yx⌋=1\lfloor \frac{y}{x} \rfloor=1,有 y mod x=y−xy\bmod x=y-x。

  • 当 ⌊yx⌋>1\lfloor \frac{y}{x} \rfloor>1,有 y mod x<y−xy\bmod x<y-x。

挖掘异或性质:y⊕x≥y−xy⊕x\geq y-x。

把两个东西合在一起可以知道:

  • 当 ⌊yx⌋=1\lfloor \frac{y}{x} \rfloor=1,有 y⊕x≥y mod xy⊕x\geq y\bmod x。

  • 当 ⌊yx⌋>1\lfloor \frac{y}{x} \rfloor>1,有 y⊕x>y mod xy⊕x> y\bmod x。

所以当且仅当 ⌊yx⌋=1\lfloor \frac{y}{x} \rfloor=1 时才会有 y mod x=x⊕yy\bmod x=x⊕y,则有 y−x=x⊕yy-x=x⊕y。

对每一位进行考虑,y=0y=0 时 x=0x=0,y=1y=1 时 x=0x=0 或 11。数位 dp 即可。

C++
#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N = 64, mod = 1e9 + 7;
int l, r, dp[N][2][2][2];

int dfs(int k, int f1, int f2, int w) { // f1表示是否为下限l,f2表示是否为上限r
	if (k == -1) return 1;
	if (dp[k][f1][f2][w] != -1) return dp[k][f1][f2][w];
	int st = f1 ? (l >> k & 1) : 0;
	int ed = f2 ? (r >> k & 1) : 1;
	int ans = 0;
	for (int i = st; i <= 1; i++) {
		for (int j = i; j <= ed; j++) {
			if (w && i != j) continue;
			ans += dfs(k - 1, f1 & (i == st), f2 & (j == ed), w & (!j));
			ans %= mod;
		}
	}
	return dp[k][f1][f2][w] = ans;
}

signed main() {
	memset(dp, -1, sizeof dp);
	cin >> l >> r;
	cout << dfs(60, 1, 1, 1) % mod << endl;
}
原文