51nod 1225 余数之和


题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1225


F(n) = (n % 1) + (n % 2) + (n % 3) + ...... (n % n)。其中%表示Mod,也就是余数。 
例如F(6) = 6 % 1 + 6 % 2 + 6 % 3 + 6 % 4 + 6 % 5 + 6 % 6 = 0 + 0 + 0 + 2 + 1 + 0 = 3。
给出n,计算F(n), 由于结果很大,输出Mod 1000000007的结果即可。
Input
输入1个数N(2 <= N <= 10^12)。
Output
输出F(n) Mod 1000000007的结果。
Input示例
6
Output示例
3

   解题思路:

       其实题目就是求n*n-sigma(floor(n/i)*i),因为对于每个1<=i<=n,余数是n-floor(n/i)*i,因为n/i的值为2*sqrt(n)个,特殊

  情况有2*sqrt(n)-1个,那就是n/i==i时,并且像约数一样,是相互对应的,并且大于sqrt(n)的只会出现一次,只需要

  枚举1~sqrt(n)就可以了,取k=n/i,对应的2个是k,n/k,于是大于sqrt(n)的就是n/k*k;对于每个n/i==k,他们的i值是递增排  列的,于是可以用等差数列求和,ans=(a1+an)*cou/2,最先出现  的是n/(k+1)+1,最后出现的是n/k,还有就是n/i==k出现的次数,即cou=n/k-n/(k+1)。


代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int mod=1000000007;
long long n;
long long ans;
long long inv(int x,int n)
{
    long long temp=x,ans=1;
    while(n)
    {
       if(n&1)
       ans=(ans*temp)%mod;
       temp=(temp*temp)%mod;
       n=n/2;
    }
    return ans;
}
int main()
{
	scanf("%I64d",&n);
	long long cnt=inv(2,mod-2);
	ans=((n%mod)*(n%mod))%mod;
	//cout<<ans<<endl;
	long long m=(long long)sqrt(n);
	for(long long i=1;i<=m;i++)
	{
	    if(n/i==i)
	    {
	        long long temp=n/i*i;
	        ans=((ans-temp)%mod+mod)%mod;
	        continue;
	    }
	    long long temp=(n/i+n/(i+1)+1)%mod;
	    long long cur=(i*(n/i-n/(i+1)))%mod;
	    temp=((temp*cur)%mod*cnt)%mod;
	    temp=(temp+(n/i)*i)%mod;
	    ans=((ans-temp)%mod+mod)%mod;
	}
	printf("%I64d\n",ans);
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值