51NOD 1238 最小公倍数之和 V3 [杜教筛]
1238 最小公倍数之和 V3
三种做法!!!
见学习笔记,这里只贴代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 4641590, U = 4641588, mo = 1e9+7, inv2 = 500000004, inv6 = 166666668;
inline ll read(){
char c=getchar(); ll x=0,f=1;
while(c<'0' || c>'9') {if(c=='-')f=-1; c=getchar();}
while(c>='0' && c<='9') {x=x*10+c-'0'; c=getchar();}
return x*f;
}
bool notp[N]; int p[N/10]; ll phi[N], sum[N];
inline void mod(ll &x) {if(x>=mo) x-=mo; else if(x<0) x+=mo;}
void sieve(int n) {
phi[1]=1;
for(int i=2; i<=n; i++) {
if(!notp[i]) p[++p[0]] = i, phi[i] = i-1;
for(int j=1; j <= p[0] && i*p[j] <= n; j++) {
int t = i*p[j];
notp[t] = 1;
if(i%p[j] == 0) {phi[t] = phi[i] * p[j]; break;}
phi[t] = phi[i] * (p[j]-1);
}
phi[i] = phi[i] * i %mo * i %mo;
}
for(int i=1; i<=n; i++) mod(sum[i] += sum[i-1] + phi[i]);
}
namespace ha {
const int p = 1001001;
struct meow{int ne; ll val, r;} e[3000];
int cnt, h[p];
inline void insert(ll x, ll val) {
int u = x%p;
for(int i=h[u];i;i=e[i].ne) if(e[i].r == x) return;
e[++cnt] = (meow){h[u], val, x}; h[u] = cnt;
}
inline ll quer(ll x) {
int u = x%p;
for(int i=h[u];i;i=e[i].ne) if(e[i].r == x) return e[i].val;
return -1;
}
} using ha::insert; using ha::quer;
inline ll cal1(ll n) {return n %mo * ((n+1) %mo) %mo * inv2 %mo;}
inline ll cal2(ll n) {return n %mo * ((n+1) %mo) %mo * ((2*n+1) %mo) %mo * inv6 %mo;}
inline ll cal2(ll l, ll r) {ll t = cal2(r) - cal2(l-1); return t<0 ? t+mo : t;}
inline ll cal3(ll n) {ll t = cal1(n); return t * t %mo;}
ll dj_s(ll n) {
if(n <= U) return sum[n];
if(n > U && quer(n) != -1) return quer(n);
ll ans = cal3(n), r;
for(ll i=2; i<=n; i=r+1) {
r = n/(n/i);
mod(ans -= dj_s(n/i) * cal2(i, r) %mo);
}
insert(n, ans);
return ans;
}
ll n;
ll solve(ll n) {
ll ans=0, r;
for(ll i=1; i<=n; i=r+1) {
r = n/(n/i); //printf("hi %lld %lld\n", n/i, dj_s(n/i));
mod(ans += dj_s(n/i) * (cal1(r) - cal1(i-1)) %mo);
}
return ans;
}
int main() {
// freopen("in", "r", stdin);
sieve(U);
n=read();
printf("%lld", solve(n));
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 4641590, U = 4641588, mo = 1e9+7, inv2 = 500000004, inv6 = 166666668;
inline ll read(){
char c=getchar(); ll x=0,f=1;
while(c<'0' || c>'9') {if(c=='-')f=-1; c=getchar();}
while(c>='0' && c<='9') {x=x*10+c-'0'; c=getchar();}
return x*f;
}
inline void mod(ll &x) {if(x>=mo) x-=mo; else if(x<0) x+=mo;}
ll s[N], lp[N];
bool notp[N]; int p[N/10];
void sieve(int n) {
s[1] = 1;
for(int i=2; i<=n; i++) {
if(!notp[i]) {
p[++p[0]] = i;
ll now = 1, i2 = (ll) i*i %mo;
for(ll j=i; j<=n; j*=i)
now = now * i2 %mo -i+1, mod(now), s[j] = now, lp[j] = j;
}
for(int j=1; j <= p[0] && i*p[j] <= n; j++) {
ll t = i*p[j];
notp[t] = 1;
if(i%p[j] == 0) {
if(lp[t] != t) {
lp[t] = lp[i] * p[j];
s[t] = s[t / lp[t]] * s[lp[t]] %mo;
}
break;
}
lp[t] = p[j];
s[t] = s[i] * s[p[j]] %mo;
}
}
for(int i=1; i<=n; i++) s[i] = s[i] * i %mo + s[i-1], mod(s[i]);
}
namespace ha {
const int p = 1001001;
struct meow{int ne; ll val, r;} e[3000];
int cnt, h[p];
inline void insert(ll x, ll val) {
int u = x%p;
for(int i=h[u];i;i=e[i].ne) if(e[i].r == x) return;
e[++cnt] = (meow){h[u], val, x}; h[u] = cnt;
}
inline ll quer(ll x) {
int u = x%p;
for(int i=h[u];i;i=e[i].ne) if(e[i].r == x) return e[i].val;
return -1;
}
} using ha::insert; using ha::quer;
inline ll sum1(ll n) {return n %mo * ((n+1) %mo) %mo * inv2 %mo;}
inline ll sum2(ll n) {return n %mo * ((n+1) %mo) %mo * ((2*n+1) %mo) %mo * inv6 %mo;}
inline ll sum3(ll n) {ll t = sum1(n); return t * t %mo;}
inline ll cal(ll n) {
ll ans=0, r;
for(ll i=1; i<=n; i=r+1) {
r = n/(n/i);
mod(ans += (sum3(r) - sum3(i-1)) * sum1(n/i) %mo);
}
return ans;
}
ll dj_s(ll n) {
if(n <= U) return s[n];
if(n > U && quer(n) != -1) return quer(n);
ll ans = cal(n), r;
for(ll i=2; i<=n; i=r+1) {
r = n/(n/i);
mod(ans -= dj_s(n/i) * ((sum2(r) - sum2(i-1)) %mo) %mo);
}
insert(n, ans);
return ans;
}
ll n;
int main() {
//freopen("in", "r", stdin);
sieve(U);
n=read();
printf("%lld", dj_s(n));
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 4641590, U = 4641588, mo = 1e9+7, inv2 = 500000004, inv6 = 166666668;
inline ll read(){
char c=getchar(); ll x=0,f=1;
while(c<'0' || c>'9') {if(c=='-')f=-1; c=getchar();}
while(c>='0' && c<='9') {x=x*10+c-'0'; c=getchar();}
return x*f;
}
inline void mod(ll &x) {if(x>=mo) x-=mo; else if(x<0) x+=mo;}
bool notp[N]; int p[N/10]; ll s[N];
void sieve(int n) {
s[1] = 1;
for(int i=2; i<=n; i++) {
if(!notp[i]) p[++p[0]] = i, s[i] = 1-i;
for(int j=1; j <= p[0] && i*p[j] <= n; j++) {
notp[i*p[j]] = 1;
if(i%p[j] == 0) {s[i*p[j]] = s[i]; break;}
s[i*p[j]] = s[i] * (1 - p[j]) %mo;
}
s[i] = (s[i-1] + i * s[i] %mo) %mo;
}
}
namespace ha {
const int p = 1001001;
struct meow{int ne; ll val, r;} e[3000];
int cnt, h[p];
inline void insert(ll x, ll val) {
int u = x%p;
for(int i=h[u];i;i=e[i].ne) if(e[i].r == x) return;
e[++cnt] = (meow){h[u], val, x}; h[u] = cnt;
}
inline ll quer(ll x) {
int u = x%p;
for(int i=h[u];i;i=e[i].ne) if(e[i].r == x) return e[i].val;
return -1;
}
} using ha::insert; using ha::quer;
inline ll sum1(ll n) {n %= mo; return n * (n+1) %mo * inv2 %mo;}
inline ll sum2(ll n) {n %= mo; return n * (n+1) %mo * (2*n+1) %mo * inv6 %mo;}
ll dj_s(ll n) {
if(n <= U) return s[n];
if(quer(n) != -1) return quer(n);
ll ans = sum1(n), r, now, last=sum2(1);
for(ll i=2; i<=n; i=r+1, last = now) {
r = n/(n/i); now = sum2(r);
mod(ans -= dj_s(n/i) * (now - last) %mo);
}
insert(n, ans);
return ans;
}
int solve(ll n) {
ll ans=0, r, now, last=0;
for(ll i=1; i<=n; i=r+1, last = now) {
r = n/(n/i); now = dj_s(r); ll t = sum1(n/i);
mod(ans += (now - last) * t %mo * t %mo);
}
return ans;
}
ll n;
int main() {
// freopen("in", "r", stdin);
sieve(U);
n=read();
printf("%d", solve(n));
}
51NOD 1238 最小公倍数之和 V3 [杜教筛]的更多相关文章
- 【51nod】1238 最小公倍数之和 V3 杜教筛
[题意]给定n,求Σi=1~nΣj=1~n lcm(i,j),n<=10^10. [算法]杜教筛 [题解]就因为写了这个非常规写法,我折腾了3天…… $$ans=\sum_{i=1}^{n}\s ...
- [51Nod 1238] 最小公倍数之和 (恶心杜教筛)
题目描述 求∑i=1N∑j=1Nlcm(i,j)\sum_{i=1}^N\sum_{j=1}^Nlcm(i,j)i=1∑Nj=1∑Nlcm(i,j) 2<=N<=10102<=N ...
- 51 Nod 1238 最小公倍数之和 V3 杜教筛
题目链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1238 题意:求$\sum_{i=1}^{n}\sum_{j=1}^{n}l ...
- [51Nod1238]最小公倍数之和 V3[杜教筛]
题意 给定 \(n\) ,求 \(\sum_{i=1}^n \sum_{j=1}^n lcm(i,j)\). \(n\leq 10^{10}\) 分析 推式子 \[\begin{aligned} an ...
- 51nod 1238 最小公倍数之和 V3
51nod 1238 最小公倍数之和 V3 求 \[ \sum_{i=1}^N\sum_{j=1}^N lcm(i,j) \] \(N\leq 10^{10}\) 先按照套路推一波反演的式子: \[ ...
- 51nod 1238 最小公倍数之和 V3 【欧拉函数+杜教筛】
首先题目中给出的代码打错了,少了个等于号,应该是 G=0; for(i=1;i<=N;i++) for(j=1;j<=N;j++) { G = (G + lcm(i,j)) % 10000 ...
- 51Nod 1238 - 最小公倍数之和 V3(毒瘤数学+杜教筛)
题目 戳这里 推导 ∑i=1n∑j=1nlcm(i,j)~~~\sum_{i=1}^{n}\sum_{j=1}^{n}lcm(i,j) ∑i=1n∑j=1nlcm(i,j) =∑i=1n∑j= ...
- 51NOD 1237 最大公约数之和 V3 [杜教筛]
1237 最大公约数之和 V3 题意:求\(\sum_{i=1}^n\sum_{j=1}^n(i,j)\) 令\(A(n)=\sum_{i=1}^n(n,i) = \sum_{d\mid n}d \c ...
- 51nod 237 最大公约数之和 V3 杜教筛
Code: #include <bits/stdc++.h> #include <tr1/unordered_map> #define setIO(s) freopen(s&q ...
随机推荐
- java.lang.NoSuchMethodError: javax.wsdl.xml.WSDLReader.readWSDL(Ljavax/wsdl/xml/WSDLLocator;Lorg/w3c/dom/Element;)Ljavax/wsdl/Definition;
http://stackoverflow.com/questions/6066054/whats-wrong-with-my-apache-cxf-client You likely have a 1 ...
- 使用Eclipse在Excel中找出两张表中相同证件号而姓名或工号却出现不同的的项
1:首先把Excel中的文本复制到txt中,复制如下: A表: 证件号 工号 姓名 310110xxxx220130004 101 傅家宜3101 ...
- TCP协议中三次握手
TCP/IP是互联网相关的各类协议族的总称 TCP/IP协议族分为:应用层,传输层,网络层,数据链路层 应用层:向用户提供应用服务时的通讯的活动 传输层:提供处于网络连接中的两台计算机之间的数据传输 ...
- Linux包管理器
按Linux系统分类 Redhat系列:Redhat(本身就是Centos).Centos.Fedora等,采用Dpkg包管理器 Debian系列:Debian.Ubuntu等,使用RPM包管理器 R ...
- CSS3 background-size 属性
定义和用法 background-size 属性规定背景图像的尺寸. 默认值: auto 继承性: no 版本: CSS3 JavaScript 语法: object.style.background ...
- 邓_ecshop
=========================================== 版本错误: error_reporting(0); ============================== ...
- Codeforces 900C Remove Extra One 模拟
题目链接:900C Remove Extra One 题意: 首先record是指这个数比数列前面的所有数都大,给了n个数(1-n),删掉一个数,让整个数列的record值达到最大. 题解: 刚开始我 ...
- java.lang.NoClassDefFoundError: javax/mail/Authenticator
摘录自:http://stackoverflow.com/questions/1630002/java-lang-noclassdeffounderror-javax-mail-authenticat ...
- Java数据持久层框架 MyBatis之API学习六(Mapper XML 文件详解)
对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...
- Django之modelform组件
一.简介与基本使用 简介:django中的modelform组件同时具有model和form作用,但是耦合度比较高,当项目需要拆分时候就比较困难了,所以在使用modelform时候需要先考虑项目的扩展 ...