HDU 4944 FSF’s game 一道好题

FSF’s game

Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 727    Accepted Submission(s):
377

Problem Description

FSF has programmed a game.
In this game, players
need to divide a rectangle into several same squares.
The length and width of
rectangles are integer, and of course the side length of squares are
integer.

After division, players can get some coins.
If players
successfully divide a AxB rectangle(length: A, width: B) into KxK squares(side
length: K), they can get A*B/ gcd(A/K,B/K) gold coins.
In a level, you can’t
get coins twice with same method.
(For example, You can get 6 coins from
2x2(A=2,B=2) rectangle. When K=1, A*B/gcd(A/K,B/K)=2; When K=2,
A*B/gcd(A/K,B/K)=4; 2+4=6; )

There are N*(N+1)/2 levels in this game, and
every level is an unique rectangle. (1x1 , 2x1, 2x2, 3x1, ..., Nx(N-1),
NxN)

FSF has played this game for a long time, and he finally gets all
the coins in the game.
Unfortunately ,he uses an UNSIGNED 32-BIT INTEGER
variable to count the number of coins.
This variable may overflow.
We want
to know what the variable will be.
(In other words, the number of coins mod
2^32)

Input

There are multiply test cases.

The first line
contains an integer T(T<=500000), the number of test cases

Each of the
next T lines contain an integer N(N<=500000).

Output

Output a single line for each test case.

For
each test case, you should output "Case #C: ". first, where C indicates the case
number and counts from 1.

Then output the answer, the value of that
UNSIGNED 32-BIT INTEGER variable.

Sample Input

3

1

3

100

Sample Output

Case #1: 1

Case #2: 30

Case #3: 15662489

Hint

In the second test case, there are six levels(1x1,1x2,1x3,2x2,2x3,3x3)
Here is the details for this game:
1x1: 1(K=1); 1x2: 2(K=1); 1x3: 3(K=1); 2x2: 2(K=1), 4(K=2); 2x3: 6(K=1); 3x3: 3(K=1), 9(K=3);
1+2+3+2+4+6+3+9=30

Author

UESTC

题意:略。

思路:对于A*B/gcd(A/k,B/k) 看成 N*x/a ,其中x未知,N已知,a是N的因子。

   (因为a必然是N的因子)

1.现在我们这样转化后,就开始一个一个枚举a了。(我们把a看成了gcd()的整体来看。)

2.对于一个确定的a值,假设为ai,那么我们现在要做的就是找出 (N*x/a )满足要求的x来。

并对它进行求和sum(xi/a)*N;(因为N始终没有变化呀。)

此时 a = gcd(N/k,x/k) 可以转化成  gcd(N,x) = k*a,

那么,对于x的取值范围我们知道,是[1,N],求gcd(N,x)=k*a (k是>0的正整数)

其实就是在[1,N]里,a的倍数,a , 2a , 3a ,4a,,,,,N/a*a , 正确吗?

会不会遗漏,gcd()=k*a,就是代表最大公约数是a的倍数。

    这样的话,我们就对x进行求和了。sum = a(1+2...N/a) = a*(1+N/a)*N/a/2 =>(1+N/a)*N/2;

最后根据式子A*x/a,那么就变成  (1+N/a)*N/a /2 * N;

筛选,dp即可。

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<cstring>
 4 #include<cstdlib>
 5 using namespace std;
 6 typedef __int64 LL;
 7
 8 const int maxn = 5e5+3;
 9 LL p = 1;
10 LL dp[maxn];
11 void init()
12 {
13     int j,tmp;
14     for(j=1;j<=32;j++)p=p*2;
15
16     for(int i=1;i<maxn;i++){
17         tmp = i;
18         for(j=1;(tmp=i*j)<maxn;j++){
19             dp[tmp]=(dp[tmp]+((LL)(1+j)*(LL)j)/2)%p;
20         }
21     }
22     dp[1]=1;
23     for(int i=2;i<maxn;i++){
24         dp[i]=(dp[i-1]+dp[i]*i)%p;
25     }
26 }
27 int main()
28 {
29     int T,n;
30     init();
31     scanf("%d",&T);
32     for(int t=1;t<=T;t++)
33     {
34         scanf("%d",&n);
35         printf("Case #%d: %I64d\n",t,dp[n]);
36     }
37     return 0;
38 }
时间: 2024-10-22 02:38:15

HDU 4944 FSF’s game 一道好题的相关文章

hdu 4944 FSF’s game(数论)

题目链接:hdu 4944 FSF's game 题目大意:给定N,可以用不大于N的长a和宽b,组成N?(N?1)2种不同的矩形,对于每个矩形a?b要计算它的值,K为矩形a,b可以拆分成若干个K?K的正方形.∑a?bgcd(a/k,b/k),输出所有矩形值的和. 解题思路:假设有边a和b,那么k肯定即使a的因子也是b的因子.定义f(n)为矩形最长边等于n的情况下所有矩形值的和.那么f(n)=val(1?n)+val(2?n)+?+val(n?n),枚举n的因子作为k,现在假设有因子k,使得n=k

HDU - 4944 FSF’s game

Problem Description FSF has programmed a game. In this game, players need to divide a rectangle into several same squares. The length and width of rectangles are integer, and of course the side length of squares are integer. After division, players c

HDU 4944 FSF’s game(数论+递推)

#include <cstdio> #include <cstring> typedef unsigned long long ll; const ll MOD = (1ULL<<32); const int N = 500001; int t, n; ll ans[N], frc[N]; void init() { for (ll i = 1; i < N; i++) { for (ll j = i; j < N; j += i) { ll tmp = j

HDU 4944 FSF’s game(计数游戏)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4944 题意: 给定一个长度n, 用长度为 a,b 的边组成一个矩形 (1<=a<=b<=n) 我们可以将其组成n(n+1)/2个不同的矩形, 对于每一个矩形 我们可以得到一个val  val=sigma(a*b/gcd(a/k,b/k)) 其中k为a,b的公因子,求这个val; 分析: 我们定义一个函数calu(i,j)   calu(i,j)的意思是sigma{(i*j/gcd(i/k,

HDU 4944

FSF’s game Problem Description FSF has programmed a game.In this game, players need to divide a rectangle into several same squares.The length and width of rectangles are integer, and of course the side length of squares are integer. After division,

HDOJ 4944 FSF’s game

http://blog.csdn.net/keshuai19940722/article/details/38519681 不明真相的补一发... FSF's game Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 448    Accepted Submission(s): 215 Problem Description FSF

HDU 1862 EXCEL排序 (排序水题)

Problem Description Excel可以对一组纪录按任意指定列排序.现请你编写程序实现类似功能. Input 测试输入包含若干测试用例.每个测试用例的第1行包含两个整数 N (<=100000) 和 C,其中 N 是纪录的条数,C 是指定排序的列号.以下有 N 行,每行包含一条学生纪录.每条学生纪录由学号(6位数字,同组测试中没有重复的学号).姓名(不超过8位且不包含空格的字符串).成绩(闭区间[0, 100]内的整数)组成,每个项目间用1个空格隔开.当读到 N=0 时,全部输入结

HDU 4923 Room and Moor(瞎搞题)

瞎搞题啊.找出1 1 0 0这种序列,然后存起来,这种情况下最好的选择是1的个数除以这段的总和.然后从前向后扫一遍,变扫边进行合并.每次合并,合并的是他的前驱.这样到最后从t-1找出的那条链就是最后满足条件的数的大小. Room and Moor Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 307    Accepted Su

HDU 4908 (杭电 BC #3 1002题)BestCoder Sequence(DP)

题目地址:HDU 4908 这个题是从m开始,分别往前DP和往后DP,如果比m大,就比前面+1,反之-1.这样的话,为0的点就可以与m这个数匹配成一个子串,然后左边和右边的相反数的也可以互相匹配成一个子串,然后互相的乘积最后再加上就行了.因为加入最终两边的互相匹配了,那就说明左右两边一定是偶数个,加上m就一定是奇数个,这奇数个的问题就不用担心了. 代码如下: #include <iostream> #include <stdio.h> #include <string.h&g