列表

详情


NC23928. Berserker's trouble

描述

Jamie was very interested in magic recently. Mabel told him that in order to dynamic planning, he must help her solve a problem. There have a pair of two positive integers not exceeding N,(a,b), which she has forgotten. She remembers that the remainder of a divided by b was greater than or equal to K. If Jamie find the number of possible pairs that she may have had, she will help him learn dynamic planning. Now he wants you to help him calculate this problem.

输入描述

Input contains multiple sets of test data(there are no more than 1000 data sets) .Each input containing 2 integers: N(1≤N≤1e5), K(0≤K≤N-1).

输出描述

For each input, you should output the number of possible pairs that he may have had.

示例1

输入:

5 2
10 0

输出:

7
100

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 156ms, 内存消耗: 336K, 提交时间: 2019-04-04 14:05:03

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	ios::sync_with_stdio(false);
	ll n,k;
	while(cin>>n>>k)
	{
		ll ans=(1+n-k)*(n-k)/2;
		for(ll m=k+1;m<=n;m++)
		{
			ll res=n-m+1;
			ll t=res/m;
			ll det=res%m;
			ans+=t*(m-k);
			ans+=max(0LL,det-k);
		}
		if(k==0)
		ans-=n;
		cout<<ans<<endl;
	}
	
}

C++11(clang++ 3.9) 解法, 执行用时: 51ms, 内存消耗: 472K, 提交时间: 2019-04-14 15:06:18

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
	int n,k;
	while(cin>>n>>k){
		long long ans=0;
		int f=(k!=0);
		for(int i=k+1;i<=n;i++){
			ans+=(n/i)*(i-k);
			if(n%i==0)
				continue;
			ans+=max(n%i-k+f,0);
		}
		cout<<ans<<endl;
	}
	return 0;
}

上一题