51nod 1225 余数之和(根号n枚举)

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%i=n-floor(n/i)*i

所以F[n]=n*n-sigma(floor(n/i)*i) 

然后就是枚举i咯,但是n有1e12,所以需要根号n的枚举,1-根号n,枚举i,然后根号n到n,枚举floor(n/i)

前面的枚举不需要说了,后面的枚举就是假设tmp=floor(n/i),然后取值的范围就是[n/(tmp+1)+1,n/tmp] 

wa了几发,原来是中间算个数和floor相乘会爆LL,开头估计的时候感觉是不会爆炸的

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")

using namespace std;
#define   MAX           1000005
#define   MAXN          1000005
#define   maxnode       15
#define   sigma_size    30
#define   lson          l,m,rt<<1
#define   rson          m+1,r,rt<<1|1
#define   lrt           rt<<1
#define   rrt           rt<<1|1
#define   middle        int m=(r+l)>>1
#define   LL            long long
#define   ull           unsigned long long
#define   mem(x,v)      memset(x,v,sizeof(x))
#define   lowbit(x)     (x&-x)
#define   pii           pair<int,int>
#define   bits(a)       __builtin_popcount(a)
#define   mk            make_pair
#define   limit         10000

//const int    prime = 999983;
const int    INF   = 0x3f3f3f3f;
const LL     INFF  = 0x3f3f;
const double pi    = acos(-1.0);
const double inf   = 1e18;
const double eps   = 1e-8;
const LL    mod    = 1e9+7;
const ull    mx    = 133333331;

/*****************************************************/
inline void RI(int &x) {
      char c;
      while((c=getchar())<'0' || c>'9');
      x=c-'0';
      while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
 }
/*****************************************************/

int main(){
    //freopen("in.txt","r",stdin);
    LL n;
    cin>>n;
    LL xx=qpow(2LL,mod-2);
    LL ans=(n%mod)*(n%mod)%mod;
    LL ret=sqrt(n);
    LL tmp=n/(ret+1);
    for(int i=1;i<=tmp;i++){
        ans=(ans-n/i*i)%mod;
    }
    //cout<<ans<<endl;
    for(int i=n/(tmp+1);i>0;i--){
        LL a=n/i;
        LL b=n/(i+1)+1;
        cout<<i<<" "<<b<<" "<<a<<endl;
        LL cnt=(((LL)i*((a+b))%mod)*(a-b+1)%mod)*xx%mod;
        ans=(ans-cnt)%mod;
    }
    cout<<(ans%mod+mod)%mod<<endl;
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值