hdu4944FSF’s game(數論)

來源:互聯網
上載者:User

標籤:http   io   for   ar   代碼   amp   type   ef   

題目:hdu4944FSF’s game(數論)


題目大意:給定N,然後會有N * ( N + 1)/2個等級的矩形,(1 * 1, 2 * 1, 2* 2, ...N * 1, N * 2.. N* N).將這些矩陣分成大小相同的K * K 的正方形,能夠獲得金幣A * B / gcd(A
/ K, B/ K);然後給定N,問能夠得到的總金幣。


解題思路:對於sum(N):N * (N + 1) / 2個等級的矩形的金幣總和 = sum(N - 1) + f(N):(N * 1, N * 2,...N * N)這一些矩形的金幣之和)。

                 例如N = 8: 1* 8/ gcd(8/1, 1/1)  2* 8 / gcd(8/1,2/1) 2 * 8/ gcd(8/2,  2/2) ....8 * 8 / gcd(8/1,8/2) 8 * 8/ gcd(8/2.8/2), 8 * 8/ gcd(8/4, 8/4), 8 * 8 / gcd(8/8, 8/8)

                                     1* 8    2* 8/2 @    2*8     3* 8     4 * 8    4 * 8/[email protected]    4 * 8/4     5 * 8    6 * 8    6*8 /2 @   7 * 8    8 * 8   @8 * 8/2   8 *8/4   8*8 /8       

                                     可以發現gcd(A /K, B/K)是N的因子。另a = gcd, N = n*a.

                                     對於gcd = a

                                           可獲得的金幣G(a):                   1*a * N/ a   + 2 * a * N / a + 3 *a * N/a +...+ na * N /a = (1 +2 + 3 + n) * N

                                    那麼枚舉N的因子n,加起來就是f(N) = sum1..n(G(ai)).


代碼:

#include <cstdio>#include <cstring>typedef unsigned __int64 ll;const int maxn = 500005;const ll M = 1LL<<32;//int1會溢出ll f1[maxn];ll f2[maxn];void init () {for (int i = 1; i < maxn; i++) {for (int j = 1; j * i < maxn; j++) f1[i * j] = (f1[i * j] + (1LL + i) * i / 2) % M;}f2[1] = 1;for (int i = 2; i < maxn; i++)f2[i] = (f2[i - 1] + f1[i] * i) % M; }int main () {int t;int n;init ();scanf ("%d", &t);for (int i = 1; i <= t; i++) {scanf ("%d", &n);printf ("Case #%d: %I64u\n", i, f2[n]); }return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.