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. 鸟哥的Linux私房菜-----6、文件与文件夹管理

  2. PHP手机号码正则表达式

    php用正则表达式判断手机号码的写法:从文章中匹配出所有的手机号就可以preg_match_all(),如果要检查用户输入的手机号是否正确可这样来检查:preg_match(). 用正则匹配手机号码的 ...

  3. fedora找开ftpd服务器并以root登陆

    工作原因需要在federal中弄个vsftpd再用root去登陆(我知道这样不太安全) 确认系统的版本 [root@localhost ~]# uname -a Linux localhost.loc ...

  4. Linux安装Nignx基于域名的多虚拟主机实战

    看这个文章之前,要保证你的Nginx已经安装成功! 如果没有,请移步到下面这个文章,看完后再回来看! https://www.cnblogs.com/apollo1616/p/10214531.htm ...

  5. IE下获取不到Response添加的cookie的解决方法

    原博客地址: http://blog.csdn.net/wjdd1/article/details/16341189 在百度上查询了好久也没有查询到结果,于是自己用ie的开发者工具进行跟踪,JSESS ...

  6. C#DataSet/DataAdapter

    DataReader必须持续连接,所以在调用方法SqlDataReader作为返回类型时候,必须在方法外关闭流,很不方便. DataAdapter用于对数据源检索数据并填充到DataSet中的表.Da ...

  7. Shiro:学习笔记(1)——身份验证

    Shiro——学习笔记(1) 1.核心概念 1.Shiro不会自己去维护用户.维护权限:这些需要我们自己去设计/提供:然后通过相应的接口注入给Shiro.2.应用代码直接交互的对象是Subject,也 ...

  8. chorme 插件

    json-handle: json可视化工具 开发中需要用到json,在浏览器显示的json非常乱,难以理解.有没有让人一目了然的工具,让json看起来非常直观呢,json-handle随之而出,包含 ...

  9. eclipse中,项目无法在tomcat中发布(project facet java version 1.7 is not supported)

    在tomcat中发布项目时无法添加项目,错误信息:project facet java version 1.7 is not supported,如下图 这是由于你的tomcat的jdk版本低于你项目 ...

  10. nginx rewrite标签配置以及用户认证配置

    一.nginx  rewrite标签 rewrite 实现URL的改写主要是实现伪静态 1.  rewrite指令语法 指令语法:rewrite regex replacement[flag] 默认值 ...