標籤:style blog http color io os ar for sp
題目連結
這個題模數的時候挺坑的!!!
題意: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 a, b (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 else30 {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 }
Codeforces Round #272 (Div. 2) C. Dreamoon and Sums (數學 思維)