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. 看了就很快学会jQuery

    一.jQuery简介与第一个jQuery程序 1.1.jQuery简介 1.2.jQuery特点 1.3.jQuery版本 1.4.获得jQuery库 1.5.第一个jQuery程序 二.jQuery ...

  2. lua(简单的传参)

    #include <iostream> #include <string.h> extern "C" { /*头文件lua.h定义了Lua提供的基础函数,包 ...

  3. Unity 插件收集(持续更新)

    MGS Machinery Unity绑定机械关节,铰链,机构插件包.    MGS Mechanical Drive 用于绑定场景中的机械驱动器的Unity插件   Unity Wave Propa ...

  4. Gaby Ivanushka(快排)

    Gaby Ivanushka Once upon a time there lived a tsar that has a daughter — Beautiful Vasilisa. There w ...

  5. Angular关于$anchorScroll的定位滚动

    以下是实现定位滚动的代码: <!DOCTYPE html> <html lang="en" ng-app="app"> <head ...

  6. 【python】-- SQLAlchemy操作MySQL

    ORM.SQLAchemy orm英文全称object relational mapping,就是对象映射关系程序,简单来说就是类似python这种面向对象的程序来说一切皆对象,但是使用的数据库却都是 ...

  7. Adam 算法

    简介 Adam 是一种可以替代传统随机梯度下降(SGD)过程的一阶优化算法,它能基于训练数据迭代地更新神经网络权重.Adam 最开始是由 OpenAI 的 Diederik Kingma 和多伦多大学 ...

  8. 【HTTP】初识代理

    Web代理(proxy)位于客户端和服务器端之间.HTTP的代理服务器既是Web服务器端又是Web客户端. 1. 代理和网关的对比 代理连接的是两个或者多个使用相同协议的应用程序. 网关连接的是两个或 ...

  9. ACN经典例题1

    1.韩信点兵 描述相传韩信才智过人,从不直接清点自己军队的人数,只要让士兵先后以三人一排.五人一排.七人一排地变换队形,而他每次只掠一眼队伍的排尾就知道总人数了.输入3个非负整数a,b,c ,表示每种 ...

  10. LeetCode:长度最小的子数组【209】

    LeetCode:长度最小的子数组[209] 题目描述 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子数组,返回 ...