SPOJ:SUM OF PRODUCT(数论)

来源:互联网 发布:足球比赛分析软件 编辑:程序博客网 时间:2024/06/05 04:10

SUMPRO - SUM OF PRODUCT

#simple-math #numbe

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:

3246

Output:

41533
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;}



原创粉丝点击