Joseph's Problem UVA - 1363

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

typedef long long ll;

/*
对拍了半天,写的代码挫的一匹

1.思路   要看到余数的规律,如果k/i=p,如果相邻的几个的 结果都是p的话,最后的余数 就是 -p 的一个递减序列
         要能通过 i,直接找到j,也就是和i 同p的最后一项,这样才能节约时间  也就是k/p,因为最后一项 余0,每一项都(-p)么
2.代码   直接项数,第一项,最后一项  ------>结果 
*/

int main()
{
   // freopen("123.txt","r",stdin);
    //freopen("789.txt","w",stdout);
    ll n,k;
    while(scanf("%lld %lld",&n,&k)==2){

        ll ans=0;
        ll i=1,j,tmp,p;
        while(i<=n){
            p=k/i;
        
            if(!p){
                ans+=(n-i+1)*k;
                break;
            }
            tmp=k%i;
            j=min(n,k/p);
            ll ed=tmp-p*(j-i);
            ans+=(tmp+ed)*(j-i+1)/2;
            i=j+1;
        }
        printf("%lld\n",ans);
   }
   return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36424540/article/details/81086708