NC23928. Berserker's trouble
描述
输入描述
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; }