POJ 2800 Joseph's Problem

Joseph's Problem













Time Limit: 1000MS


Memory Limit: 65536K


Total Submissions: 5789


Accepted: 1436


Description


Joseph likes taking part in programming contests.
His favorite problem is, of course, Joseph's problem.

It is stated as
follows.


There are n persons numbered from 0 to n - 1
standing in a circle. The person numberk, counting from the person number 0, is
executed. After that the person number k of the remaining persons is executed,
counting from the person after the last executed one. The process continues
until only one person is left. This person is a survivor. The problem is, given
n and k detect the survivor's number in the original circle.



Of course, all of you know the way to solve this
problem. The solution is very short, all you need is one cycle:


r :=
0;



for i
from 1 to n do




r := (r + k) mod i;




return r;



Here "x mod y" is the remainder of the division
of x by y, But Joseph is not very smart. He learned the algorithm, but did not
learn the reasoning behind it. Thus he has forgotten the details of the
algorithm and remembers the solution just approximately.

He told his
friend Andrew about the problem, but claimed that the solution can be found
using the following algorithm:


r :=
0;



for i
from 1 to n do




r := r + (k mod i);




return r;



Of course, Andrew pointed out that Joseph was
wrong. But calculating the function Joseph described is also very
interesting.

Given n and k, find
1<=i<=n(k mod i).


Input


The input file contains n and k
(1<= n, k <= 109)
.


Output


Output the sum requested.


Sample Input


5 3


Sample Output


7



 

在题目中有三种情况:

11<k<n

2k == n ;

3k>n;

对于第一种情况我们可以分为1kkn两个子问题来解。

1.11k

     for( i = 1 , sum = 0 ; i <= k ; i ++ ) sum += k %i ;

1.2kn sum =(n-k)*k;

而对于第二种情况就是第一种情况的(1)。但是就这样写的话时明显的tle的。

对于第二种情况也可以分为几个小问题来求解:

2.11k/2

          for( i = k/2 ; i >=1 ; i-- ) sum+= k % i ;

2.2k/2k:

k%k+((k-1)+1)%(k-1)+.......+ (k-m0)+m0)%(k-m0) 其中m0 < k –m0 , m0<k/2, 又因为m0 是整数所以m0 = k/2 – 1 ;

简单的来说:如果k%i ( i ++ )  只要 k/i (i ++ ) 的值相同 , 则 k % i 是以个等差数列 例如:

  

  k = 100 , i = 26

  100 % 26 = 22   100 / 26 = 3

  100 % 27 = 19   100 / 27 = 3

  100 % 28 = 16   100 / 28 = 3

  100 % 29 = 13   100 / 29 = 3

所以 sum = sum(2.1) + sum(2.2)

然而2.1k=10^9时所需的时间也是很大的,所以继续优化:

同理可得到for( i=k/2;i>=1;i-- ) 也可以化简成:

2.1.1( (k/2)*2+m1 )%(k/2)+((k/2-1)*2+m1+2)%(k/2-1)+...+((k/2-x)*2+m1+2*x)%(k/2-x);

这里的m1=k%(k/2),因为在2.2式子中的m0=k/2-1 , m1 = k%(k/2) ,x=k/6-1;

2.1.1的式子是公差为2的等差数列;

2.1.2for(i=k/3;i>=1;i--)
sum+=k%i;

综上可以知道,对于原来的线性搜索便可以拆成若干个等差数列的和。由此可以将式子化简成:

(k%(k/1)+k%(k/2+1))*(k/1-k/2)/2+(k%(k/2)+k%(k/3+1))*(k/2-k/3)/2+…s=k/i;e=k/(i-1);

则上诉式子就变为: sum+=(k%e+k%(s+1))*(e-s)/2; 而它的时间复杂度仅为sqrt(k);

对于第三种情况,既k>n的情况,是第二种情况的特殊情况,区别就是把首相变成k%n而已。

3.11<n<sqrt(k);

3.21<sqrt(k)<n;

 

 

     

所以综合上面的可以知道,我们可以把sum分为三部分:

1、 sum+=(k%e+k%(s+1))*(e-s)/2

2、for(i=1;i<=n&&i<=b;i++) sum+=k%i; 其中b = k / sqrt(k) ;

3、 sum += (n-k)*k ;


 

 1 #include<stdio.h>  
 2 #include<math.h>
 3 long long jos ( long long n , long long k )
 4 {
 5      long long sum = 0 , a = ( long long  ) sqrt ( k ), b=k/a ,i ;
 6      if ( n > k ) sum += ( n - k ) * k ;
 7      for ( i = a ; i > 1 ; i -- )
 8      {
 9          long long s = k / i , e = k / ( i - 1 ) ;
10          if ( s > n ) break ;
11          if ( e > n ) e = n ;
12          sum += ( k % e + k % ( s + 1 ) ) * ( e - s ) / 2 ; 
13      } 
14      for ( i = 1 ; i <= n && i <= b ; i ++ ) sum += k % i ;
15      return sum ;
16  }
17 int main ()
18 {
19     long long n , k ;
20     while ( scanf ( "%I64d%I64d",&n,&k ) != EOF )
21           printf ( "%I64d\n" , jos(n,k) ) ;
22     return 0 ;
23 

posted @ 2011-07-23 15:33  贺佐安  阅读(1513)  评论(0编辑  收藏  举报