51NOD1238

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>
//#include<ctime>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;

const int maxn = 5E6 + 50;
typedef long long LL;
const LL Mod = 999987;
const LL mo = 1000000007;

struct data{
	LL Num; int ret; data(){}
	data(LL Num,int ret): Num(Num),ret(ret){}
};

int tot,Ans,Inv_2,Inv_6,phi[maxn],pri[maxn];
bool not_pri[maxn]; LL n;

vector <data> v[Mod];

int Mul(const LL &x,const LL &y) {return x * y % mo;}
int Add(const int &x,const int &y) {return x + y < mo ? x + y : x + y - mo;}
int Dec(const int &x,const int &y) {return x - y < 0 ? x - y + mo : x - y;}

int ksm(int x,int y)
{
	int ret = 1;
	for (; y; y >>= 1)
	{
		if (y & 1) ret = Mul(ret,x);
		x = Mul(x,x);
	}
	return ret;
}

int Sum_2(LL N)
{
	int A = Mul(N % mo,(1LL + N) % mo);
	int B = Mul(2LL * N % mo + 1LL,Inv_6);
	return Mul(A,B);
}

int Sum_3(LL N)
{
	int A = Mul((1LL + N) % mo,Inv_2);
	A = Mul(A,N % mo); return Mul(A,A);
}

int Search(LL N)
{
	int pos = N % Mod;
	for (int i = 0; i < v[pos].size(); i++)
		if (v[pos][i].Num == N) return v[pos][i].ret;
	return -1;
}

void Insert(LL N,int now)
{
	int pos = N % Mod;
	v[pos].push_back(data(N,now));
}

int Calc(LL N)
{
	if (N < maxn) return phi[N];
	LL ret = Search(N);
	if (ret != -1) return ret; ret = Sum_3(N);
	for (LL i = 2,last; i <= N; i = last + 1LL)
	{
		last = N / (N / i);
		int now = Calc(N / i);
		int sum = Dec(Sum_2(last),Sum_2(i - 1LL));
		ret = Dec(ret,Mul(now,sum));
	}
	Insert(N,ret); return ret;
}

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
	#endif
	
	cin >> n; phi[1] = 1;
	for (int i = 2; i < maxn; i++)
	{
		if (!not_pri[i])
			pri[++tot] = i,phi[i] = i - 1;
		for (int j = 1; j <= tot; j++)
		{
			LL Nex = 1LL * i * pri[j];
			if (Nex >= maxn) break;
			not_pri[Nex] = 1;
			if (i % pri[j] == 0)
			{
				phi[Nex] = phi[i] * pri[j];
				break;
			}
			phi[Nex] = phi[i] * phi[pri[j]];
		}
	}
	//cerr << (double)(clock()) / CLOCKS_PER_SEC << endl;
	for (int i = 2; i < maxn; i++)
	{
		phi[i] = Mul(phi[i],Mul(i,i));
		phi[i] = Add(phi[i],phi[i - 1]);
	}
	//cerr << (double)(clock()) / CLOCKS_PER_SEC << endl;
	Inv_2 = ksm(2,mo - 2); Inv_6 = ksm(6,mo - 2);
	
	for (LL i = 1,last; i <= n; i = last + 1LL)
	{
		last = n / (n / i);
		int ret = Calc(n / i);
		int B = Mul(Inv_2,Mul((i + last) % mo,(last - i + 1LL) % mo));
		Ans = Add(Ans,Mul(ret,B));
	}
	cout << Ans << endl;
	//cerr << (double)(clock()) / CLOCKS_PER_SEC << endl;
	return 0;
}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值