题意:给出正整数n和k,计算j(n, k)=k mod 1 + k mod 2 + k mod 3 + … + k mod n的值其中k mod i表示k除以i的余数。例如j(5, 3)=3 mod 1 + 3 mod 2 + 3 mod 3 + 3 mod 4 + 3 mod 5=0+1+0+3+3=7
题解k%i=k-\(\left\lfloor\frac{k}{i}\right\rfloor\) \(*i\),然后\(\left\lfloor\frac{k}{i}\right\rfloor\)只会有\(\sqrt{n}\)个取值,所以可以通过整除分块来一次性算出相同因子的贡献/************************************************************** Problem: 1257 User: walfy Language: C++ Result: Accepted Time:64 ms Memory:1288 kb****************************************************************/ //#pragma comment(linker, "/stack:200000000")//#pragma GCC optimize("Ofast,no-stack-protector")//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")//#pragma GCC optimize("unroll-loops")#include#define fi first#define se second#define mp make_pair#define pb push_back#define pi acos(-1.0)#define ll long long#define vi vector #define mod 1000003#define C 0.5772156649#define ls l,m,rt<<1#define rs m+1,r,rt<<1|1#define pil pair #define pli pair #define pii pair #define cd complex #define ull unsigned long long#define base 1000000000000000000#define fio ios::sync_with_stdio(false);cin.tie(0) using namespace std; const double eps=1e-6;const int N=1000+10,maxn=1000000+10,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f; int main(){ ll n,k,ans=0; scanf("%lld%lld",&n,&k);// ll ans1=0;// for(ll i=1;i<=n;i++)ans1+=k%i;// printf("%lld\n",ans1); if(n>=k) { ans+=k*(n-k); n=k-1; } ans+=n*k; for(ll i=1,j;i<=n;i=j+1) { j=k/(k/i);// cout< <<" "< <<" "< =n)break; } printf("%lld\n",ans); return 0;}/******************** ********************/