Codeforces Round #272 (Div. 2) C. Dreamoon and Sums (数学 思维)

题目链接

这个题取模的时候挺坑的!!!

题意:div(x , b) / mod(x , b) = k( 1 <= k <= a)。求x的和

分析:

我们知道mod(x % b)的取值范围为 1  - (b-1)。那么我们可以从这一点入口来进行解题。。

mod (x, b) = 1 时, x  =  b + 1, 2b + 1, 3b + 1..... a * b + 1.

mod (x , b) = 2 时, x =  2b + 2, 4b + 2, 6b + 2, ..... 2a * b + 2. = 2(b + 1), 2(2b + 1), 2(3b + 1)...... 2(a * b + 1).

....

mod (x , b) = b - 1..

可将等式化为:x=k*mod(x,b)*b+mod(x,b).

枚举1-b-1.  发现每一个式子都是等差数列  可得:ans += (a*(2*i+i*a*b+i*b))/2;  但是会发现  s ab (1 ≤ a, b ≤ 107).

中间乘起来的时候会超LL,  而且这个式子要对除2, 其实ai*() 这两个乘数里至少有一个偶数,找到并除2就行了。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <queue>
 6 #include <cmath>
 7 #include <algorithm>
 8 #define LL __int64
 9 const int mo = 1e9+7;
10 const int maxn = 100+10;
11 using namespace std;
12 LL a, b;
13 
14 int main()
15 {
16     LL i;
17     LL ans;
18     while(~scanf("%I64d%I64d", &a, &b))
19     {
20         ans = 0;
21         for(i = 1; i < b; i++)
22         {
23             if((a*i)%2==0)
24             {
25                 LL tmp = (a*i/2)%mo;
26                 ans += (((2+a*b+b)%mo)*tmp)%mo;
27                 ans %= mo;
28             }
29             else
30             {
31                 LL tmp = ((2+a*b+b)/2)%mo;
32                 ans += ((a*i%mo)*tmp)%mo;
33                 ans %= mo;
34             }
35         }
36         printf("%I64d\n", ans);
37     }
38     return 0;
39 }

 

posted @ 2014-10-23 19:08  水门  阅读(201)  评论(0编辑  收藏  举报