[51nod1074] 约瑟夫问题 V2

毫无思路,Orz了一下大佬的思路%%%

大概就是因为k比n小的多,我们知道约瑟夫环有个公式是fn=(fn-1+k) mod n

可以改一下,改成fn+p=(fn+pk) mod (n+p)

但是这样的话就不对了,因为有mod,模数是改变的。

pk肯定大于p。然后我们可以让这个模数等价,就是mod n和mod n+1....是一样的,就可以让fn+pk≤n+p

这样的话解一下不等式,p≤(n-lastans-k)/(k-1)

(会不会有锅啊。。。害怕

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <cmath>
 5 using namespace std;
 6 long long n,k,ans,tp;
 7 int main (){
 8     cin>>n>>k;
 9     for(long long i=2;i<=n;i+=tp+1ll) {
10         tp=(i-ans-k)/(k-1ll);
11         if(i+tp>=n) tp=n-i;
12         //if(!tp) break;
13         ans=(ans+k*(tp+1ll))%(i+tp);
14     }
15     cout<<++ans;
16 }
1074

猜你喜欢

转载自www.cnblogs.com/sdfzhsz/p/9643136.html