求N(10^14)以内与N互质的数的和(容斥原理,或者欧拉函数)

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int mod=1000000007;
const int maxn=205;
const int inv=500000004;
LL n;
LL ms(LL x)
{
    x %= mod;
    return (x * (x + 1) % mod) * inv % mod;
}

void solve()
{
   LL t=n;
   vector<LL>v;
   for(int i=2;i<=sqrt(n);i++)
   {
      if(t%i==0)
      {
         v.push_back(i);
         while(t%i==0)
         {
            t/=i;
         }
      }
   }
   if(t>1)
      v.push_back(t);
   int len=v.size();
   LL ans=0;
   for(int i=1;i<(1<<len);i++)
   {
      int cnt=0;
      LL temp=1;
      for(int j=0;j<len;j++)
      {
         if((i>>j)&1)
         {
            cnt++;
            temp*=v[j];
         }
      }
      LL num=n/temp;
      if(cnt&1)
      {
         ans = (ans + (temp%mod) * ms(num) % mod) % mod;
      }
      else
      {
         ans=(ans- (temp%mod) * ms(num) % mod+mod)%mod;
      }
   }
   cout << ans << endl;
}
int main()
{
   while(cin>>n)
   {
      solve();
   }
   return 0;
}
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#define INF 0x3f3f3f3f
#define LL long long 
using namespace std;
const int mod=1e9+7;
const int maxn=1e5+5;
const int inv=5e8+4;
LL n;
LL phi(LL n)
{
	LL ans=n;
	for(LL i=2;i<=sqrt(n);i++)
	{
		if(n%i==0)
		{
			ans=ans/i*(i-1);
			while(n%i==0)
			{
				n/=i;
			}
		}
	}
	if(n>1)
		ans=ans/n*(n-1);
	return ans;
}
int main()
{
	ios::sync_with_stdio(false);
	//cout << phi(100000000000000) << endl;
	while(cin>>n)
	{
		LL t=n;
		t%=mod;
		LL ans=((t*(t+1)%mod-(t*(phi(n)%mod))%mod+mod)%mod)*inv;
		ans%=mod;
		cout << ans << endl;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值