Description

F(n) = (n % 1) + (n % 2) + (n % 3) + ...... (n % n)。其中%表示Mod,也就是余数。
例如F(6) = 6 % 1 + 6 % 2 + 6 % 3 + 6 % 4 + 6 % 5 + 6 % 6 = 0 + 0 + 0 + 2 + 1 + 0 = 3。
给出n,计算F(n)。

Input

输入1个数N(2 <= N <= 10^12)。

Output

输出F(n)。

Sample Input

6

Sample Output

3
 
这是51NOD上的一道题,由于提交不了,所以搞到我们学校的OJ提交了。
这个题目n达10^12这么大,自然不可能走一遍。
然后由于n%i = n-[n/i]*i
所以sum(n%i) = sum(n-[n/i]*i) = n*n - sum([n/i]*i)。
考虑到取整那部分,当i在连续的一段区间内可能值会不变。
=>[n/i] = [n/j] => j = n/(n/i)这里的除法为取下整。
于是便可以考虑分组运算了。
 
当然可以将i和n/i两组再次合并,这样理论上能减少一倍的时间。
 
由于数据很大,用了C++高精度。
 
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define LL long long using namespace std; const int UNIT = ;
struct Bignum
{
int val[];
int len; Bignum()
{
memset(val, , sizeof(val));
len = ;
} Bignum operator=(const LL &a)
{
LL t, p = a;
len = ;
while (p >= UNIT)
{
t = p - (p/UNIT)*UNIT;
p = p / UNIT;
val[len++] = t;
}
val[len++] = p;
return *this;
} Bignum operator+(const Bignum &a) const
{
Bignum x = a;
int L;
L = a.len > len ? a.len : len;
for (int i = ; i < L; ++i)
{
x.val[i] += val[i];
if (x.val[i] >= UNIT)
{
x.val[i+]++;
x.val[i] -= UNIT;
}
}
if (x.val[L] != )
x.len = L+;
else
x.len = L;
return x;
} Bignum operator-(const Bignum &a) const
{
bool flag;
Bignum x1, x2;
if (*this > a)
{
x1 = *this;
x2 = a;
flag = ;
}
else
{
x1 = a;
x2 = *this;
flag = ;
}
int j, L = x1.len;
for (int i = ; i < L; ++i)
{
if (x1.val[i] < x2.val[i])
{
j = i+;
while (x1.val[j] == )
j++;
x1.val[j--]--;
while (j > i)
x1.val[j--] += UNIT-;
x1.val[i] += UNIT-x2.val[i];
}
else
x1.val[i] -= x2.val[i];
}
while (x1.val[x1.len-] == && x1.len > )
x1.len--;
if (flag)
x1.val[x1.len-] = -x1.val[x1.len-];
return x1;
} Bignum operator*(const Bignum &a) const
{
Bignum x;
int i, j, up;
int x1, x2;
for (i = ; i < len; i++)
{
up = ;
for (j = ; j < a.len; j++)
{
x1 = val[i]*a.val[j] + x.val[i+j] + up;
if (x1 >= UNIT)
{
x2 = x1 - x1/UNIT*UNIT;
up = x1 / UNIT;
x.val[i+j] = x2;
}
else
{
up = ;
x.val[i+j] = x1;
}
}
if (up != )
x.val[i+j] = up;
}
x.len = i + j;
while (x.val[x.len-] == && x.len > )
x.len--;
return x;
} Bignum operator/(const int &a) const
{
Bignum x;
int down = ;
for (int i = len-; i >= ; --i)
{
x.val[i] = (val[i]+down*UNIT) / a;
down = val[i] + down*UNIT - x.val[i]*a;
}
x.len = len;
while (x.val[x.len-] == && x.len > )
x.len--;
return x;
} LL operator%(const LL &a) const
{
LL x = ;
for (int i = len-; i >= ; --i)
x = ((x*UNIT)%a+val[i]) % a;
return x;
} bool operator>(const Bignum &a) const
{
int now;
if (len > a.len)
return true;
else if (len == a.len)
{
now = len - ;
while (val[now] == a.val[now] && now >= )
now--;
if(now >= && val[now] > a.val[now])
return true;
else
return false;
}
else
return false;
}
}ans, tmp, ttmp; LL n; void work()
{
ans = n;
ans = ans*ans;
LL j;
for (LL i = ; i <= n; i++)
{
j = n/(n/i);
tmp = i+j;
ttmp = j-i+;
tmp = tmp*ttmp;
tmp = tmp/;
ttmp = n/i;
tmp = tmp*ttmp;
ans = ans-tmp;
i = j;
}
} void output()
{
for (int i = ans.len-; i >= ; --i)
printf("%d", ans.val[i]);
printf("\n");
} int main()
{
//freopen("test.in", "r", stdin);
while (scanf("%lld", &n) != EOF)
{
work();
output();
}
return ;
}

ACM学习历程—SNNUOJ1132 余数之和(数论)的更多相关文章

  1. ACM学习历程—HDU5668 Circle(数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=5668 这题的话,假设每次报x个,那么可以模拟一遍, 假设第i个出局的是a[i],那么从第i-1个出局的人后,重新 ...

  2. ACM学习历程—HDU5667 Sequence(数论 && 矩阵乘法 && 快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=5667 这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t. 发现t是一个递推式,t(n) = c ...

  3. ACM学习历程—HDU5666 Segment(数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=5666 这题的关键是q为质数,不妨设线段上点(x0, y0),则x0+y0=q. 那么直线方程则为y = y0/x ...

  4. ACM学习历程—HDU5585 Numbers(数论 || 大数)(BestCoder Round #64 (div.2) 1001)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5585 题目大意就是求大数是否能被2,3,5整除. 我直接上了Java大数,不过可以对末尾来判断2和5, ...

  5. ACM学习历程—HDU5637 Transform(数论 && 最短路)

    题目链接:http://codeforces.com/problemset/problem/590/A 题目大意是给两种操作,然后给你一个s,一个t,求s至少需要多少次操作到t. 考虑到第一种操作是将 ...

  6. ACM学习历程—HDU1719 Friend(数论)

    Description Friend number are defined recursively as follows. (1) numbers 1 and 2 are friend number; ...

  7. 51nod 1225 余数之和 数论

    1225 余数之和 题目连接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1225 Description F(n) ...

  8. ACM学习历程—ZOJ3785 What day is that day?(数论)

    Description It's Saturday today, what day is it after 11 + 22 + 33 + ... + NN days? Input There are ...

  9. ACM学习历程—SNNUOJ 1239 Counting Star Time(树状数组 && 动态规划 && 数论)

    http://219.244.176.199/JudgeOnline/problem.php?id=1239 这是这次陕西省赛的G题,题目大意是一个n*n的点阵,点坐标从(1, 1)到(n, n),每 ...

随机推荐

  1. SVM支持向量机

    支持向量机(Support Vector Machine,SVM)是效果最好的分类算法之中的一个. 一.线性分类器: 一个线性分类器就是要在n维的数据空间中找到一个超平面,通过这个超平面能够把两类数据 ...

  2. MySQL 下 ROW_NUMBER / DENSE_RANK / RANK 的实现

    原文链接:http://hi.baidu.com/wangzhiqing999/item/7ca215d8ec9823ee785daa2b MySQL 下 ROW_NUMBER / DENSE_RAN ...

  3. task19-21

    [说明]理想是丰满的,现实很骨感,昨天还说今天有望干掉5个小任务,看来是没可能了,兜兜转转地做了一天也才完成下面的这些 一:今日完成 19.学习Spring,配置Spring和Junit 1)先安装一 ...

  4. python多任务处理

    多任务解析 什么叫“多任务”呢?简单地说,就是操作系统可以同时运行多个任务. 现在,多核CPU已经非常普及了,但是,即使过去的单核CPU,也可以执行 多任务.由于CPU执行代码都是顺序执行的,那么,单 ...

  5. python的进程和线程

    关于进程: An executing instance of a program is called a process.程序的执行实例称为进程. Each process provides the ...

  6. Bootstrap学习-菜单-按钮-导航

    1.下拉菜单(基本用法) 在使用Bootstrap框架的下拉菜单时,必须调用Bootstrap框架提供的bootstrap.js文件.当然,如果你使用的是未编译版本,在js文件夹下你能找到一个名为“d ...

  7. Symfony 安装FOUSerBundle

    第一按照官网安装 : https://symfony.com/doc/current/bundles/FOSUserBundle/index.html#main 可能版本无法安装 : $ compos ...

  8. ubuntu14.04 pygame安装 python2.7

    系统:ubuntu14.04 LTS amd64python版本:2.7.6pygame版本:1.9.1release别这种方法了,这么安装不知道什么原因就出现了问题,在使用pygame.image. ...

  9. js原生函数一些封装

    这是一些js原生封装的函数,主要是为了兼容IE浏览器,如下 获取css样式 function getStyle(ele, prop) { if(window.getComputedStyle) { r ...

  10. 323 id与小数据池

    a = 1000b = 1000print(a == b)== 比较的是数值is 比较的是内存地址.print(a is b)查看内存地址id()print(id(a))print(id(b)) 小数 ...