版本比较
P12388 Easy Equation 题解
正文
增加 162 行删除 91 行
1123345567878910111210111213141516171819202122232425262728291314151617181920212223242526272829303132333435363738394041424344314546474849505152535455565758596061626364656667686934353637383940414243444546474849505152535455707172737475767778798081828384858687888990919293949596979899100101102103104105106585960616210710864656667686970109110727311111211311411511611711811912012112212312412512612775767778798081828384858687888990919293949596979899100101102128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169104170171172173174175**[题目传送门](https://www.luogu.com.cn/problem/P12388)**## 写在前面的话
题目大意: $$f(n)=\sum_{i=1}^n\sum_{j=1}^n[popcount(i+j)\gcd(i,j)=\max(i,j)]$$ 即:$f(n)=$ 满足 $popcount(i+j)\gcd(i,j)=\max(i,j)$ 的 $i$,$j$ 对数 $(i$,$j\le k)$。[题目传送门](https://www.luogu.com.cn/problem/P12388)。
求 $f(1)\oplus f(2)\oplus\cdots\oplus f(n)$ 的值。其中 $\oplus$ 是按位异或。省流:TJ 改了 9 遍。
显而易见可以暴力枚举 $i$,$j$,$k$ 再进行判断,一个可过Subtask 1、时间复杂度 $O(n^{3})$ 的代码,如下。```cpp## Subtask 1
暴力求每一个 $f$。复杂度 $O(n^3)$,代码如下:
```#include <bits/stdc++.h>using namespace std;int main(){ int n; cin>>n; int ans=0; for(int k=1;k<=n;k++){ int sum=0; for(int i=1;i<=k;i++){ for(int j=1;j<=k;j++){ //枚举i,j,k if(__builtin_popcount(i+j)*__gcd(i,j)==max(i,j)) sum++; //popcount、gcd的库内函数分别是__builtin_popcount()、__gcd() } } ans^=sum; } cout<<ans; return 0;} #define int long long#define Code using#define by namespace#define homeworking std
Code by homeworking;
int n, ans;int f;
signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for (int k = 1; k <= n; k++) { f = 0; for (int i = 1; i <= k; i++) { for (int j = 1; j <= k; j++) { if (__builtin_popcount(i + j) * __gcd(i, j) == max(i, j)) { // popcount,gcd 的库内函数分别是 __builtin_popcount(),__gcd() f++; } } } ans ^= f; } cout << ans; return 0;}```考虑优化:当 $i$,$j$ 满足 $popcount\left(i+j\right)\cdot\gcd(i,j)=\max(i,j)$ 时,只要 $i$,$j\le k1$,那就有 $i$,$j\le k2\left(k1\le k2\right)$,**即 $f(k)$ 具有单调递增性**,$f(k)-f(k-1)=\sum_{i=1}^k\left[popcount\left(i+k\right)\cdot\gcd(i,k)=k\right]\left(k>1\right)$,一个可过Subtask 2、时间复杂度为 $O(n^{2})$ 的代码,如下。## Subtask 2
考虑优化。由计数原理可得:
$$\begin{aligned}f(n)&=\sum_{i=1}^n\sum_{j=1}^n[\operatorname{popcount}(i+j)\gcd(i,j)=\max(i,j)]\\&=2\sum_{i=1}^n\sum_{j=1}^{i-1}[\operatorname{popcount}(i+j)\gcd(i,j)=i]+\sum_{i=1}^n[\operatorname{popcount}(2i)\cdot i=i]\\&=2\sum_{i=1}^n\sum_{j=1}^{i-1}[\operatorname{popcount}(i+j)\gcd(i,j)=i]+\sum_{i=1}^n[\operatorname{popcount}(2i)=1]\\\end{aligned}$$
所以有:
$$\begin{aligned}\Delta f(n)&=f(n)-f(n-1)\\&=2\sum_{i=1}^{n-1}[\operatorname{popcount}(i+n)\gcd(i,n)=n]+[\operatorname{popcount}(2n)=1]\\\end{aligned}$$
复杂度为 $O(n^2)$,代码如下:
```cpp#include <bits/stdc++.h>using namespace std;int main(){ int n; cin>>n; int ans=0,f=0; for (int k=1;k<=n;++k) { int delta=0; //delta表示f(k)-f(k-1) for (int i=1;i<=k;i++) { int g=__gcd(k,i); if (__builtin_popcount(k+i)*g==k) { delta++; if(i!=k) delta++; //注意当 i!=k时(i,k)、(k,i)都符合要求 } } f+=delta; ans^=f; } cout<<ans<<endl; return 0;#define int long long#define Code using#define by namespace#define homeworking std
Code by homeworking;
int n, ans;int diff, f;
signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int k = 1; k <= n; k++) { diff = 0; for (int i = 1; i <= k; i++) { int d = __gcd(k, i); if (__builtin_popcount(k + i) * d == k) { if (i != k) { diff += 2; } else { diff++; } } } f += diff; ans ^= f; } cout << ans << endl; return 0;}
```此时还能优化的只能出现在 $f(k)$ 的计算式中,考虑 $gcd()$,令 $\gcd(i,j)=h$,$i=x\cdot h$,$j=y\cdot h(\gcd(x,y)=1$,不然 $\gcd(i,j)\neq h$ $)$,可以将原式化为 $f(k)=\sum_{i=1}^k\sum_{j=1}^k\left[popcount\left(\left(x+y\right)\cdot h\right)\cdot h=\max(x,y)\cdot h\right]$,即 $f(k)=\sum_{i=1}^k\sum_{j=1}^k\left[popcount\left(\left(x+y\right)\cdot h\right)=\max(x,y)\right]$,枚举 $x$,$y$,令 $y$ 始终更大即可。另外有两个小细节,一是 $x$,$y\leq24$,原因如下: $\because i$,$j\leq10^{7}$ $\therefore popcount(i,j)\leq24$ $\therefore x$,$y\leq24$## Subtask 3, 4, 5
二就是当 $x=y$ 时可以特判,发现只有 $i$ 是 $2$ 的幂次方时才满足 $popcount\left(2\cdot x\cdot h\right)=x$,原因如下: $\because i=j$ $\therefore x=y$ $\because popcount(i+j)\cdot\gcd(i,j)=\max(i,j)$ $\therefore popcount(2\cdot i)=1$ $\therefore 2\cdot i$ 是 $2$ 的幂次方 $\therefore i$ 是 $2$ 的幂次方考虑对 $\gcd$ 进行优化,令 $\gcd(i,n)=d,i=xd,n=yd$,有 $\gcd(x,y)=1$,则:
AC代码如下:```cpp$$\begin{aligned}\Delta f(n)&=2\sum_{i=1}^{n-1}[d\operatorname{popcount}(dx+dy)=dy]+[\operatorname{popcount}(2n)=1]\\&=2\sum_{i=1}^{n-1}[\operatorname{popcount}(d(x+y))=y]+[\operatorname{popcount}(2n)=1]\\\end{aligned}$$
则只需对 $x,y$ 枚举即可,另外有两个优化细节。
一是 $x,y$ 的范围。由 $i,n\leq10^7$,得到 $\operatorname{popcount}(i+n)\leq\log_2 (2\times10^7+1)\leq24$,从而 $x,y\leq24$。
二是对于第二项的优化。显然,$\operatorname{popcount}(2n)=1$ 等价于 $n$ 是 $2$ 的整数次幂。
复杂度 $O(n)$,AC 代码如下:
```#include <bits/stdc++.h>#define N 10000100using namespace std;int f[N];int main(){ int n; cin>>n; for(int x=1;x<24;x++){ for(int y=x+1;y<=24;y++){//细节一 if(__gcd(x,y)==1){ for(int h=1;h<=n/y;h++){ if(__builtin_popcount((x+y)*h)==y){ f[y*h]+=2; } } } } } for(int i=1;i<=n;i*=2){ f[i]++; }//细节二 int ans=0; for(int i=1;i<=n;i++){ f[i]+=f[i-1]; ans^=f[i]; } cout<<ans; return 0;} #define int long long#define Code using#define by namespace#define homeworking std
Code by homeworking;
const int N = 1e7 + 5;int n,ans;int diff[N],f[N];
signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int x = 1; x <= 24; x++) { for (int y = x + 1; y <= 24; y++) { if (__gcd(x, y) == 1) { for (int d = 1; y * d <= n; d++) { if (__builtin_popcount((x + y) * d) == y) { diff[y * d] += 2; } } } } } for (int i = 1; i <= n; i <<= 1) { diff[i]++; } for (int i = 1; i <= n; i++) { f[i] = f[i - 1] + diff[i]; ans ^= f[i]; } cout << ans << '\n'; return 0;}```~~点个赞再走呗~~
## 说在后面的话
本文使用 @[chw0315_](https://www.luogu.com.cn/user/1427773) 进行润色。
发表一条友善的评论吧!