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 number k, 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∑n i=1(k mod i). Input The input file contains several test cases, each of them consists of a line containing n and k (1 ≤ n,k ≤ 109)
Output
For each test case, output a line containing the sum requested.
Sample Input
5 3
Sample Input
7
#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll long long
#define maxn 205
#define MAX 1000000000
#define ms memset
using namespace std;
ll n,k;
int main()
{
while(scanf("%lld%lld",&n,&k)!=EOF)
{
ll a=sqrt(k*1.0),ans=0,b=k/a;
if(n>k ) ans+=(ll)(n-k)*k;
for(ll i=a;i>1;i--)
{
ll s=k/(i),e=k/(i-1);///维护一个左开右闭的区间
if(s>n) break;
if(e>n) e=n;///注意一下其范围
ans+=(k%(s+1)+k%e)*(e-s)/2;///等差数列求和公式
}
for(ll i=1;i<=b&&i<=n;i++) ans+=(k%i);///在这恶鬼范围内其除数唯一,所以直接求和即可(即等差数列的长度只有1)
printf("%lld\n",ans);
}
return 0;
}