BZOJ1257 [CQOI2007]余数之和 (数论分块)
题意:
给定n, k,求$\displaystyle \sum_{i=1}^nk\;mod\;i$
n,k<=1e9
思路:
先转化为$\displaystyle \sum_{i=1}^n(k-i\lfloor\frac{k}{i}\rfloor)=\displaystyle \sum_{i=1}^nk-\sum_{i=1}^ni\lfloor\frac{k}{i}\rfloor$
而k/i在一定范围内是不变的,所以分块求等差数列就可以了
代码:
/************************************************************** Problem: 1257 User: wrjlinkkkkkk Language: C++ Result: Accepted Time:60 ms Memory:1288 kb ****************************************************************/ #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> #include<stack> #include<queue> #include<deque> #include<set> #include<vector> #include<map> #include<functional> #define fst first #define sc second #define pb push_back #define mem(a,b) memset(a,b,sizeof(a)) #define lson l,mid,root<<1 #define rson mid+1,r,root<<1|1 #define lc root<<1 #define rc root<<1|1 #define lowbit(x) ((x)&(-x)) using namespace std; typedef double db; typedef long double ldb; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> PI; typedef pair<ll,ll> PLL; const db eps = 1e-6; const int mod = 1e9+7; const int maxn = 2e6+100; const int maxm = 2e6+100; const int inf = 0x3f3f3f3f; const db pi = acos(-1.0); int main() { ll n, k; scanf("%lld %lld", &n, &k); ll ans = n*k; for(ll l = 1, r = 0; l <= min(k, n); l = r+1){ r = min(n, k/(k/l)); ans -= (((l+r)*(k/l))*(r-l+1))>>1; } printf("%lld",ans); return 0; }