首页 > 其他 > 详细

Codeforces Round #272 (Div. 1) A. Dreamoon and Sums(数论)

时间:2014-10-13 21:11:38      阅读:263      评论:0      收藏:0      [点我收藏+]

题目链接

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 bubuko.com,布布扣 and bubuko.com,布布扣, where k is some integer number in range[1, a].

By bubuko.com,布布扣 we denote the quotient of integer division of x and y. By bubuko.com,布布扣 we denote the remainder of integer division of x andy. 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 ab (1 ≤ a, b ≤ 107).

Output

Print a single integer representing the answer modulo 1 000 000 007 (109 + 7).

 

题意 : 给你a,b。让你找出符合以下条件的x,div(x,b)/mod(x,b)=k,其中k所在范围是[1,a],其中mod(x,b)!= 0.然后将所有符合条件的x加和,求最后的结果

官方题解 :

If we fix the value of k, and let d = div(x, b), m = mod(x, b), we have :
d = mk
x = db + m
So we have x = mkb + m = (kb + 1) * m.
And we know m would be in range [0, b - 1] because it‘s a remainder, so the sum of x of that fixed k would be bubuko.com,布布扣.
Next we should notice that if an integer x is nice it can only be nice for a single particular k because a given x uniquely definesdiv(x, b) and mod(x, b).
Thus the final answer would be sum up for all individual kbubuko.com,布布扣 which can be calculated in O(a) and will pass the time limit of 1.5 seconds.
Also the formula above can be expanded to bubuko.com,布布扣

bubuko.com,布布扣
#include <stdio.h>
#include <string.h>
#include <iostream>

using namespace std ;
#define mod 1000000007

int main()
{
    long long a,b ;
    while(~scanf("%I64d %I64d",&a,&b)){
        //    printf("%I64d\n",a*(a+1)/2) ;
        long long sum = (((a*(a+1)/2%mod)*b%mod+a)%mod*(b*(b-1)/2%mod))%mod ;
        printf("%I64d\n",sum) ;
    }
    return 0 ;
}
View Code

 

Codeforces Round #272 (Div. 1) A. Dreamoon and Sums(数论)

原文:http://www.cnblogs.com/luyingfeng/p/4023084.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!