ACM学习历程—SNNUOJ1132 余数之和(数论)
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
Sample Output
#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 余数之和(数论)的更多相关文章
- ACM学习历程—HDU5668 Circle(数论)
http://acm.hdu.edu.cn/showproblem.php?pid=5668 这题的话,假设每次报x个,那么可以模拟一遍, 假设第i个出局的是a[i],那么从第i-1个出局的人后,重新 ...
- ACM学习历程—HDU5667 Sequence(数论 && 矩阵乘法 && 快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=5667 这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t. 发现t是一个递推式,t(n) = c ...
- ACM学习历程—HDU5666 Segment(数论)
http://acm.hdu.edu.cn/showproblem.php?pid=5666 这题的关键是q为质数,不妨设线段上点(x0, y0),则x0+y0=q. 那么直线方程则为y = y0/x ...
- ACM学习历程—HDU5585 Numbers(数论 || 大数)(BestCoder Round #64 (div.2) 1001)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5585 题目大意就是求大数是否能被2,3,5整除. 我直接上了Java大数,不过可以对末尾来判断2和5, ...
- ACM学习历程—HDU5637 Transform(数论 && 最短路)
题目链接:http://codeforces.com/problemset/problem/590/A 题目大意是给两种操作,然后给你一个s,一个t,求s至少需要多少次操作到t. 考虑到第一种操作是将 ...
- ACM学习历程—HDU1719 Friend(数论)
Description Friend number are defined recursively as follows. (1) numbers 1 and 2 are friend number; ...
- 51nod 1225 余数之和 数论
1225 余数之和 题目连接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1225 Description F(n) ...
- 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 ...
- ACM学习历程—SNNUOJ 1239 Counting Star Time(树状数组 && 动态规划 && 数论)
http://219.244.176.199/JudgeOnline/problem.php?id=1239 这是这次陕西省赛的G题,题目大意是一个n*n的点阵,点坐标从(1, 1)到(n, n),每 ...
随机推荐
- 一篇文章彻底弄清ARC始末
本文转载至 http://blog.csdn.net/allison162004/article/details/38758265 自动引用计数(ARC)是编译器的一个特色,提供了Objective- ...
- 服务器端获取表单数据的编码解码问题(servlet)
首先需要明确指出的是,这里的服务器是指tomcat. 在页面没有明确指定编码的情况下,客户端通过input标签和字符串向服务器传递两个值param1和param2.如果直接使用request.getP ...
- 【BZOJ3993】[SDOI2015]星际战争 二分+最大流
[BZOJ3993][SDOI2015]星际战争 Description 3333年,在银河系的某星球上,X军团和Y军团正在激烈地作战.在战斗的某一阶段,Y军团一共派遣了N个巨型机器人进攻X军团的阵地 ...
- js arguments对象
1.表示调用他的函数的参数 : arguments不是一个数组对象, 但是可以用下标的方式来访问, 即 arguments[n] function demo() { console.log(argum ...
- 我的Android进阶之旅------>Android颜色值(RGB)所支持的四种常见形式
Android中颜色值是通过红(Red).绿(Green).蓝(Blue)三原色,以及一个透明度(Alpha)值来表示的,颜色值总是以井号(#)开头,接下来就是Alpha-Red-Green-Blue ...
- http请求(get 和 post 请求)与响应
版权声明:欢迎转载 https://blog.csdn.net/chenmoquan/article/details/36656101 一.http请求 http请求基本格式 ============ ...
- 了解CentOS服务器的基本信息
简单描述了如何从CPU.内存.硬盘性能.负载方面去了解自己工作的服务器性能.这个很重要,必须了解机器的方方面面才能提高在自己运维工作效率. 一.查看linux服务器cpu详情 查看物理cpu个数: [ ...
- LeetCode:区域和检索【303】
LeetCode:区域和检索[303] 题目描述 给定一个整数数组 nums,求出数组从索引 i 到 j (i ≤ j) 范围内元素的总和,包含 i, j 两点. 示例: 给定 nums = [ ...
- 一对多 添加表单 cocoon
gem 'cocoon' - javascript "cocoon.js" https://note.youdao.com/web/#/file/XCiivnE/note/WEB4 ...
- golang 获取指定目录下的子文件列表
GO语言按照深度遍历文件 原创 2016年07月20日 09:45:19 标签: go语言 / 遍历 / string 1971 常规方法不使用pathfilepath包 go的filepath包 g ...