P2261 [CQOI2007]余数求和
做法就是分块
k%i=k-(k/i)*i;
对(k/i)分块,一个细节问题就是n,k谁大谁小的问题
#include <iostream> #include <cstdio> #include <queue> #include <algorithm> #include <cmath> #include <cstring> #define inf 2147483647 #define N 1000010 #define p(a) putchar(a) #define For(i,a,b) for(long long i=a;i<=b;++i) //by war //2019.8.21 using namespace std; long long n,k,ans,l,r,t; void in(long long &x){ long long y=1;char c=getchar();x=0; while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();} while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();} x*=y; } void o(long long x){ if(x<0){p('-');x=-x;} if(x>9)o(x/10); p(x%10+'0'); } long long sum(long long l,long long r){ return (l+r)*(r-l+1)/2; } signed main(){ in(n);in(k); ans=n*k;l=1; t=min(n,k); while(l<=t){ r=min(t,k/(k/l)); ans-=sum(l,r)*(k/l); l=r+1; } o(ans); return 0; }