【51nod】1239 欧拉函数之和
题解
写完上一道就开始写这个,大体上就是代码改了改而已= =
好吧,再推一下式子!
\(\sum_{i = 1}^{n}i = \sum_{i = 1}^{n}\sum_{d | i}\phi(d) = \sum_{i = 1}^{n}\sum_{d = 1}^{\lfloor \frac{n}{i} \rfloor} \phi(d) = \sum_{i = 1}^{n}S(\lfloor \frac{n}{i} \rfloor)\)
\(S(n) = \frac{n(n + 1)}{2} - \sum_{i = 2}^{n} S(\lfloor \frac{n}{i} \rfloor)\)
long long相乘会爆掉,前面的注意特判一下
代码
#include <bits/stdc++.h>
#define MAXN 10000005
//#define ivorysi
#define enter putchar('\n')
#define space putchar(' ')
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eps 1e-8
#define mo 974711
#define pii pair<int,int>
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
res = 0;char c = getchar();T f = 1;
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {putchar('-');x = -x;}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
const int MOD = 1000000007;
struct node {
int64 x;
int next,v;
}E[1000005];
int head[mo + 5],sumE;
int prime[5000005],tot,P[MAXN],phi[MAXN];
bool nonprime[MAXN];
int inc(int a,int b) {
a = a + b;
if(a >= MOD) a -= MOD;
return a;
}
void add(int u,int64 x,int v) {
E[++sumE].x = x;E[sumE].v = v;E[sumE].next = head[u];
head[u] = sumE;
}
void Insert(int64 x,int v) {
add(x % mo,x,v);
}
int Query(int64 x) {
int u = x % mo;
for(int i = head[u] ; i ; i = E[i].next) {
if(E[i].x == x) return E[i].v;
}
return -1;
}
int f(int64 x) {
if(x <= 10000000) return P[x];
int c = Query(x);
if(c != -1) return c;
int res = 0;
for(int64 i = 2 ; i <= x ; ++i) {
int64 r = x / (x / i);
res = inc(1LL * (r - i + 1) * f(x / i) % MOD,res);
i = r;
}
int64 a = x,b = x + 1;
if(a & 1) b /= 2;
else a /= 2;
a %= MOD;b %= MOD;
res = inc(1LL * a * b % MOD,MOD - res);
Insert(x,res);
return res;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
phi[1] = 1;P[1] = 1;
for(int i = 2 ; i <= 10000000 ; ++i) {
if(!nonprime[i]) {
prime[++tot] = i;
phi[i] = i - 1;
}
for(int j = 1 ; j <= tot ; ++j) {
if(prime[j] > 10000000 / i) break;
nonprime[prime[j] * i] = 1;
if(i % prime[j] == 0) {phi[i * prime[j]] = phi[i] * prime[j];break;}
else phi[i * prime[j]] = phi[i] * (prime[j] - 1);
}
P[i] = inc(P[i - 1],phi[i]);
}
int64 x;
read(x);
out(f(x));enter;
return 0;
}
【51nod】1239 欧拉函数之和的更多相关文章
- [51Nod 1244] - 莫比乌斯函数之和 & [51Nod 1239] - 欧拉函数之和 (杜教筛板题)
[51Nod 1244] - 莫比乌斯函数之和 求∑i=1Nμ(i)\sum_{i=1}^Nμ(i)∑i=1Nμ(i) 开推 ∑d∣nμ(d)=[n==1]\sum_{d|n}\mu(d)=[n== ...
- 51nod 1239 欧拉函数之和(杜教筛)
[题目链接] https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1239 [题目大意] 计算欧拉函数的前缀和 [题解] 我们 ...
- 51nod 1239 欧拉函数之和【欧拉函数+杜教筛】
和bzoj 3944比较像,但是时间卡的更死 设\( f(n)=\sum_{d|n}\phi(d) g(n)=\sum_{i=1}^{n}f(i) s(n)=\sum_{i=1}^{n}\phi(i) ...
- 51 NOD 1239 欧拉函数之和(杜教筛)
1239 欧拉函数之和 基准时间限制:3 秒 空间限制:131072 KB 分值: 320 难度:7级算法题 收藏 关注 对正整数n,欧拉函数是小于或等于n的数中与n互质的数的数目.此函数以其首名研究 ...
- 【51nod】1239 欧拉函数之和 杜教筛
[题意]给定n,求Σφ(i),n<=10^10. [算法]杜教筛 [题解] 定义$s(n)=\sum_{i=1}^{n}\varphi(i)$ 杜教筛$\sum_{i=1}^{n}(\varph ...
- 51Nod 1239 欧拉函数前n项和 杜教筛
http://www.51nod.com/Challenge/Problem.html#!#problemId=1239 AC代码 #include <bits/stdc++.h> #de ...
- 51nod1239 欧拉函数之和
跟1244差不多. //由于(x+1)没有先mod一下一直WA三个点我... //由于(x+1)没有先mod一下一直WA三个点我... #include<cstdio> #include& ...
- 欧拉函数之和(51nod 1239)
对正整数n,欧拉函数是小于或等于n的数中与n互质的数的数目.此函数以其首名研究者欧拉命名,它又称为Euler's totient function.φ函数.欧拉商数等.例如:φ(8) = 4(Phi( ...
- 【51nod-1239&1244】欧拉函数之和&莫比乌斯函数之和 杜教筛
题目链接: 1239:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1239 1244:http://www.51nod. ...
随机推荐
- python析构函数
class Test(object): def __init__(self, name): self.name = name print('这是构造函数') def say_hi(self): p ...
- Java修饰符 public、protected、default、private
2.访问修饰符public,private,protected,以及不写(默认)时的区别?答: 修饰符 当前类 同 包 子 类 其他包 public √ √ √ √ protected √ √ √ × ...
- Android通过php插入查询SQL数据库
PHP代码 <?php header("Content-type: text/html; charset=gb2312"); $serverName = "loca ...
- dp ZOJ 3956
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3956 Course Selection System Time Limit ...
- (四)伪分布式下jdk1.6+Hadoop1.2.1+HBase0.94+Eclipse下运行wordCount例子
本篇先介绍HBase在伪分布式环境下的安装方式,然后将MapReduce编程和HBase结合起来使用,完成WordCount这个例子. HBase在伪分布环境下安装 一. 前提条件 已经成功地安装 ...
- AES Java加密 C#解密 (128-ECB加密模式)
在项目中遇到这么一个问题: java端需要把一些数据AES加密后传给C#端,找了好多资料,算是解决了,分享一下: import sun.misc.BASE64Decoder; import sun.m ...
- 51nod1773 A国的贸易
基准时间限制:2 秒 空间限制:524288 KB 分值: 40 A国是一个神奇的国家. 这个国家有 2n 个城市,每个城市都有一个独一无二的编号 ,编号范围为0~2n-1. A国的神奇体现在,他们 ...
- 【Atcoder】AGC022 C - Remainder Game 搜索
[题目]C - Remainder Game [题意]给定n个数字的序列A,每次可以选择一个数字k并选择一些数字对k取模,花费2^k的代价.要求最终变成序列B,求最小代价或无解.n<=50,0& ...
- tf.reduce_sum()_tf.reduce_mean()_tf.reduce_max()
根据官方文档: reduce_sum应该理解为压缩求和,用于降维 tf.reduce_sum(input_tensor,axis=None,keepdims=None,name=None,reduct ...
- pycharts实现可视化
https://blog.csdn.net/u012535605/article/details/80677791http://pyecharts.org/#/zh-cn/prepare (中文官网 ...