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));
}
Copyright:http://www.cnblogs.com/candy99/