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 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

题意:给你个n,让你求在n的范围内,能否将一个矩形分成若干个相同大小为k的正方形,对应有val值,让你统计在n内的所有可能的分数总值

思路:首先我们来试着求解∑(n*i)/(gcd(n/k, i/k)) {1<=i <= n} ,那么我们可以确定的是如果可以把n*m的矩形分成大小为k的正方形的话,那么k一定是gcd(n, i)的因子,那么对于一项来说因为公式可以变形为(n*i*k)/gcd(n, i) -> n*(i/c1 + i/c2 + ...) {k枚举所有的可能},

就拿6*1, 6*2, 6*3, 6*4, 6*5, 6*6来说,对应的k依次有

1;1、2; 1、3;1、2;1;1,2,6,那么cj是n的因子,那么i/cj就是因子对应的系数,我们再从所有的i来讲,对于因子cj我们可以计算出所有可能的数,比如因子cj,我们可以得到cj, 2*cj, 3*cj, 4*cj....n,那么对应的系数就是我们需要的i/cj,累加起来计算是:

num[cj] = (1+2+...+n/cj)=(1+n/cj)*(n/cj)/2,然后最后是递推:

ans[n]=ans[n-1]+val[n] {val[n]=∑num[cj] {1<=i <= n}}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll __int64
using namespace std;
const int maxn = 500005;
const ll mod = 1ll<<32;

ll num[maxn], dp[maxn];

void cal() {
	for (ll i = 1; i < maxn; i++)
		for (ll j = i; j < maxn; j += i)
			num[j] += (j/i+1) * (j/i) / 2;
}

void init() {
	memset(num, 0, sizeof(num));
	cal();
	dp[1] = 1;
	for (ll i = 2; i < maxn; i++) {
		dp[i] = dp[i-1] + num[i]*i;
		dp[i] = dp[i] % mod;
	}
}

int main() {
	init();
	int t, n, cas = 1;
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		printf("Case #%d: %I64d\n", cas++, dp[n]);
	}
	return 0;
}

HDU - 4944 FSF’s game,布布扣,bubuko.com

时间: 2024-10-24 15:08:04

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(数论+递推)

#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 一道好题

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 int

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 4944 逆序数对

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 题意: 给出一个序列,可以相邻的交换k次,求 k 次之后,逆序数对最少是多少: 分析: 可以发现,无论怎么交换之后,总共的逆序数对只会-1,那么结果就是,将这个序列排整齐时,要两两交换的次数-k:题目就转换为求这个序列的逆序数对有多少: 这样的两两交换好像是冒泡排序,冒泡排序是O(n^2): 正确解法是归并排序:当我们合并两个有序序列时,如果,要将后面的插入到前一个中间,那么这里就有m-i+1

hdu 1869 六度分离

六度分离 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4944    Accepted Submission(s): 1986 Problem Description 1967年,美国著名的社会学家斯坦利·米尔格兰姆提出了一个名为"小世界现象(small world phenomenon)"的著名假说,大意是说,任何2个素

dp --- hdu 4939 : Stupid Tower Defense

Stupid Tower Defense Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1219    Accepted Submission(s): 361 Problem Description FSF is addicted to a stupid tower defense game. The goal of tower