题解:CF2169D2 Removal of a Sequence (Hard Version)

· · 题解

不难发现,本题相当于要进行 x 次迭代(从 k 到 k'),满足 \displaystyle k'-\lfloor\frac{k'}{y}\rfloor=k,且 y\nmid k'。

画图是最大突破口。画出函数 \displaystyle x-\lfloor\frac{x}{y}\rfloor 的图像,画完图之后认为每 y 个数值域上增加 y-1,那么想要增长到 k,需要增长 \displaystyle\lfloor\frac{k}{y-1}\rfloor 段,那么有

$\displaystyle=\frac{k-k\bmod(y-1)}{y-1}(y-1)+k\bmod(y-1)+\lfloor\frac{k}{y-1}\rfloor- [(y-1)\mid k]=k+\lfloor\frac{k}{y-1}\rfloor-[(y-1)\mid k]=k+\lfloor\frac{k-1}{y-1}\rfloor$。这样,我们把迭代式写成了无敌的增量式。 下一个突破口显然是本质不同增量 $\displaystyle\lfloor\frac{k-1}{y-1}\rfloor$ 的个数。此时一个不难发现的做法是,我们对于每一个增量 $t$,求出当增量保持为 $t$ 时的迭代终点。也就是对于现有的 $k,t$,求一个最大的 $i$ 使得 $\displaystyle\lfloor\frac{k+it-1}{y-1}\rfloor=\lfloor\frac{k-1}{y-1}\rfloor$。不难求得 $i$。 不难发现当 $y\leq\sqrt V$ 时迭代次数不超过 $\sqrt V$,而当 $y>\sqrt V$ 时,$k$ 单次迭代的本质不同增量集合大小不超过 $\sqrt V$。所以我们直接暴力做就可通过这个题(别忘记判 $k$ 与 $10^{12}$ 的关系来对前面一种情况剪枝)。复杂度 $O(t\sqrt V)$。 ```cpp #pragma GCC optimize("Ofast,unroll-loops") #include<bits/stdc++.h> using namespace std; #define int long long #define MAXN 400005 int x,y,k; inline int read(){ int x = 0; char ch = getchar(); while( ch < '0' || ch > '9' ) ch = getchar(); while( ch >= '0' && ch <= '9' ) x = x * 10 + ch - 48,ch = getchar(); return x; } inline void solve(){ x = read(),y = read(),k = read(); if( y == 1 ){ puts("-1"); return; } while( 1 ){ int t = ( k - 1 ) / ( y - 1 ); if( !t ){ printf("%lld\n",k); return; } int rem = ( y - 1 ) - ( k - 1 ) % ( y - 1 ); if( rem == 0 ) rem = y - 1; int ned = ( rem - 1 ) / t + 1; if( x <= ned ){ int aim = k + t * x; if( aim > (int)1e12 ){ puts("-1"); return; } printf("%lld\n",aim); return; } x -= ned,k += ned * t; if( k > (int)1e12 ){ puts("-1"); return; } } } signed main(){ int testcase = read(); while( testcase -- ) solve(); return 0; } ```