Codeforces Round #272 (Div. 2) C. Dreamoon and Sums (數學 思維)

來源:互聯網
上載者:User

標籤: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 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             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 (數學 思維)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.