您的位置:首页 > 大数据 > 人工智能

Educational Codeforces Round 5 E. Sum of Remainders (思维题)

2016-08-10 17:48 399 查看
题目链接:http://codeforces.com/problemset/problem/616/E

题意很简单就不说了。

因为n % x = n - n / x * x

所以答案就等于 n * m - (n/1*1 + n/2*2 ... n/m*m)

在根号n复杂度枚举x,注意一点当m>n时,后面一段加起来就等于0,就不用再枚举了。

中间一段x1 ~ x2 的n/x可能相等,所以相等的一段等差数列求和。

//#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef __int64 LL;
typedef pair <int, int> P;
const int N = 1e5 + 5;
vector <LL> myset; //存储x
LL mod = 1e9 + 7;

int main()
{
LL n, m;
scanf("%lld %lld", &n, &m);
LL k = min(m, n);
myset.push_back(k);
for(LL i = 1; i*i <= n; ++i) {
myset.push_back(i);
if(i * i != n) {
myset.push_back(n/i);
}
}
sort(myset.begin(), myset.end());
LL ans = (n%mod)*(m%mod)%mod, cnt = 0;
for(LL i = 0; i < myset.size() && myset[i] <= k; ++i) {
LL temp = myset[i];
if(cnt) {
LL num;
if((temp - cnt) % 2)
num = ((temp + cnt + 1) / 2 % mod) * ((temp - cnt) % mod) % mod;
else
num = ((temp - cnt) / 2 % mod) * ((temp + cnt + 1) % mod) % mod;
num = ((n / temp) % mod * num) % mod;
ans = ((ans - num) % mod + mod) % mod;
}
else {
ans = (ans - n) % mod;
}
cnt = temp;
}
printf("%lld\n", (ans + mod) % mod);
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: