SPOJ:SUM OF PRODUCT(数论)

SUMPRO - SUM OF PRODUCT


Given a number N, find the sum of all products x*y such that N/x = y (Integer Division).
Since, the sum can be very large, please output this modulo 1000000007.

Input

The first line of input file contains an integer T, the number of test cases to follow. Each of the next T lines contain an integer N.

Output

Output T lines containing answer to corresponding test case.

Example

Input:

3
2
4
6

Output:

4
15
33
Constraints:
1 ≤ T ≤ 500
1 ≤ N ≤ 109

Sample Explanation:
Case #1:
2 / 1 = 2
2 / 2 = 1
Answer = 1 * 2 + 2 * 1 = 4
Case #2:
4 / 1 = 4
4 / 2 = 2
4 / 3 = 1
4 / 4 = 1
Answer = 1 * 4 + 2 * 2 + 3 * 1 + 4 * 1 = 15
题意:给定一个数字n,求1*(n/1)+2*(n/2)+3*(n/3)+......+n*(n/n)模1e9+7,这里的除法是整型除法。

思路:sqrt(n)以前的数老老实实地算,同时一边算一边将sqrt(n)后面的处理掉,就能O(sqrt(n))内解决,最后的sqrt(n)那里还要特判一下有没有算漏。

# include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const LL mod = 1e9+7;
int main()
{
    LL t, n;
    scanf("%I64d",&t);
    while(t--)
    {
        scanf("%I64d",&n);
        LL i, l = n, r, ans=n;
        for(i=2; i<=sqrt(n); ++i)
        {
            r = n/i;
            ans = (ans+(l+r+1)*(l-r)*(i-1)/2+r*i)%mod;
            l = r;
        }
        LL rest = l-(--i);
        if(rest)
            ans = (ans + (i+i+rest+1)*rest*i/2)%mod;
        printf("%I64d\n",ans);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值