【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. ...
随机推荐
- 洛谷P1230 智力大冲浪
题目描述 小伟报名参加中央电视台的智力大冲浪节目.本次挑战赛吸引了众多参赛者,主持人为了表彰大家的勇气,先奖励每个参赛者m元.先不要太高兴!因为这些钱还不一定都是你的?!接下来主持人宣布了比赛规则: ...
- laravel 5.5 在构造函数使用Session
public function __construct() { $this->request = request(); // 验证是否登录 $this->middleware(functi ...
- 2018南京ICPCMediocre String Problem 马拉车
hash+二分求出最长公共前缀 然后马拉车+前缀和计数 #include <cstdio> #include <cstring> #include <queue> ...
- 通过ida dump Uinity3D的加密dll
声明: 1.本文转载自:http://www.52pojie.cn/thread-398266-1-1.html,仅供自己保存使用,高手勿喷 2.欢迎学习交流 通过ida dump Uinity3D的 ...
- vmware中无法ping通主机的问题
虚拟机使用NAT方式运行一段时间后,发现无法ping通主机(物理机),显示错误如下 ipconfig如下 查看虚拟机中的网络连接,显示"未识别网络" 分析: 查看了网络上的一些资料 ...
- JS中的new操作符原理解析
var Person = function(name){ this.name = name; } Person.prototype.sayHello = function() { console.lo ...
- js控制treeview默认展开
bootStrapTreeview 在bootstrap的treeview官网,可以找到这个方法,用js控制可以写成:$('#xxx').treeview('collapseNode',{silent ...
- 如何在Lunix云服务器上安装Mysql进行远程连接
说说这个服务器上安装mysql真是一个大坑啊 ! 我也不知道自己怎么心血来潮就买一个百度云服务器,然后就想着吧自己做的一些小项目都跑上去.嘿嘿...其实就是想显摆下,写点小应用给不是编程的朋友们使用 ...
- JS-this的用法
o.onclick=function(){alert(this)}//这个this是指o ------ var arr=[1,2,3,4,5]; arr.a=12; arr.show=function ...
- NAIPC2018-K-Zoning Houses
题目描述 Given a registry of all houses in your state or province, you would like to know the minimum si ...