您的位置:首页 > 其它

HDU 4473 Exam 构造枚举

2013-04-25 22:41 761 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4473

Exam

Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 329 Accepted Submission(s): 131


[align=left]Problem Description[/align]
Rikka is a high school girl suffering seriously from Chūnibyō (the age of fourteen would either act like a know-it-all adult, or thinks they have special powers no one else has. You might google it for detailed explanation) who, unfortunately, performs badly at math courses. After scoring so poorly on her maths test, she is faced with the situation that her club would be disband if her scores keeps low.
Believe it or not, in the next exam she faces a hard problem described as follows.
Let’s denote f(x) number of ordered pairs satisfying (a * b)|x (that is, x mod (a * b) = 0) where a and b are positive integers. Given a positive integer n, Rikka is required to solve for f(1) + f(2) + . . . + f(n).
According to story development we know that Rikka scores slightly higher than average, meaning she must have solved this problem. So, how does she manage to do so?

[align=left]Input[/align]
There are several test cases.
For each test case, there is a single line containing only one integer n (1 ≤ n ≤ 1011).
Input is terminated by EOF.

[align=left]Output[/align]
For each test case, output one line “Case X: Y” where X is the test case number (starting from 1) and Y is the desired answer.

[align=left]Sample Input[/align]

1

3

6

10
15
21

28

[align=left]Sample Output[/align]

Case 1: 1
Case 2: 7

Case 3: 25

Case 4: 53

Case 5: 95

Case 6: 161

Case 7: 246

解题思路: 这题的意思是求f【x】的前n项和,其中 f【x】是满足 x%(a*b) == 0条件的有向数组<a, b>的个数(比如(1,2)与(2,1)不同算两个)

思路:转化题意,那么要求的就是 a*b*y == x 由于x <= n的所以,输入一个n,我们就只需把a*b*y <= n的a,b,y全排列

我们可以假设a <= b <= y

那么

    对于三个数字都相同的情况,只计算一次: i i i

    对于三个数字中有两个相同的情况,计算3次: i i j, i j i, j i i

    对于均不相同的情况,计算6次: a b y ,a y b ,b a y ,b y a, y a b ,y b a

另外要注意的是, 直接用pow(n,m) 求得的值会 四舍五入.要注意 保证 m*m <= n or m*m*m <= n 中最大的m

程序如下:注意在HDOJ上使用%I64d,但是现场赛使用%lld

View Code

// File Name: J - Exam
// Author: sheng
// Created Time: 2013年04月25日 星期四 18时58分29秒

#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;

typedef __int64 LL;
LL ans, a, b, c, t, n;

int main ()
{
int cas = 0;
while (~scanf ("%I64d", &n))
{
a = pow (n, 1./3);
while (a*a*a < n)
a ++;
while (a*a*a > n)
a --;
ans = a;
for (int i = 1; i <= a; i ++)
{
t = n/i;
b = sqrt(t);
while (b*b < t)
b ++;
while (b*b > t)
b --;
ans += (t/i - i + b - i) * 3;
for (int j = i + 1; j <= b; j ++)
ans += (t/j - j) * 6;
}
printf ("Case %d: %I64d\n", ++ cas, ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: