Codeforces round #272 (Div. 1) A. dreamoon and sums (number theory)

Source: Internet
Author: User
Tags integer division

Question Link

Dreamoon loves summing up something for no reason. One day he obtains two integersAAndBOccasionally. He wants to calculate the sum of all nice integers. Positive IntegerXIs called nice if and, whereKIs some integer number in range [1, random,A].

By we denote the quotient of integer divisionXAndY. By we denote the remainder of integer divisionXAndY. You can read more about these operations here: http://goo.gl/AcsXhT.

The answer may be large, so please print its remainder modulo 1 limit 000 limit 000 limit 007 (109 rows + 7). Can you compute it faster than dreamoon?

Input

The single line of the input contains two integersA,B(1 digit ≤ DigitA, Bytes,BLimit ≤ limit 107 ).

Output

Print a single integer representing the answer modulo 1 0000000 0000000 0000007 (109 rows + rows 7 ).

 

Question: A and B. Let you find the X, Div (X, B)/MoD (X, B) = k that meets the following conditions, where k is in the range of [1, A], where Mod (X, B )! = 0. Then sum all X that meets the condition and calculate the final result.

Official question:

If we fix the value K , And let D Signature = Signature Div ( X , Bytes, B ),M Signature = Signature MoD ( X , Bytes, B ), We have:
D Signature = Signature Mk
X Signature = Signature DB Region + Region M
So we have X Signature = Signature MKB Region + Region M Signature = Signature ( KB Rows + rows 1) rows * Rows )* M .
And we know M Wocould be in range [0, random, B Using-keys 1] because it's a remainder, so the sum X Of that fixed K Wocould be.
Next we shoshould notice that if an integer X Is Nice It can only be Nice For a single particle K Because a given X Uniquely defines Div ( X , Bytes, B ) And MoD ( X , Bytes, B ).
Thus the final answer wocould be sum up for all individual K : Which can be calculated in O ( A ) And will pass the time limit of 1.5 seconds.
Also the formula abve can be expanded.

#include <stdio.h>#include <string.h>#include <iostream>using namespace std ;#define mod 1000000007int 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 (number theory)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.