原题:
Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occasionally. He wants to calculate the sum of all nice integers. Positive integer x is called nice if and , where k is some integer number in range [1, a].
By we denote the quotient of integer division of x and y. By we denote the remainder of integer division of x and y. You can read more about these operations here: http://goo.gl/AcsXhT.
The answer may be large, so please print its remainder modulo 1 000 000 007 (109 + 7). Can you compute it faster than Dreamoon?
Input
The single line of the input contains two integers a, b (1 ≤ a, b ≤ 107).
Output
Print a single integer representing the answer modulo 1 000 000 007 (109 + 7).
Examples
Input
1 1
Output
0
Input
2 2
Output
8
Note
For the first sample, there are no nice integers because is always zero.
For the second sample, the set of nice integers is {3, 5}.
题意:
给定两个数值a,b。现在有一个x满足x与b的商除以x与b的余数得到的数值记为k,k要求在1到a之间(包括1和a)现在要求你求出所有的x的和,并且结果和1000000007取余。
题解:
这道题目,逆向思维思考一下,反着来求出所有的x,即通过遍历所有的a和b求出所有的x:
for(int i=1;i<b;i++)//a、b遍历顺序随意
{
for(int j=1;j<=a;j++)
{
long long res=i*j*b+i;//res=(j*b+1)*i
sum+=res;
sum=sum%1000000007;
}
}
但是光是这样,那坑定会超时的。所以我们可以通过提取公因式,这里可以提出i,利用等差数列求和即可。
但是这里要注意数据范围,a^3 的范围大约是10的21次方,超过了longlong的范围。
附上AC代码:
#include<cstdio>
using namespace std;
#define LL long long
LL ans = 0;
const LL mod = 1e9+7;
int main()
{
LL a,b;
scanf("%lld%lld",&a,&b);
LL m = (b-1)*b/2;
m%=mod;
for(int i = 1;i<=a;i++)
{
LL tmp = (i*b+1)%mod;//无时无刻不取余,降低数据范围
ans = (ans + m*tmp%mod)%mod;
}
printf("%lld\n",ans);
}
欢迎评论!