HDU 4473 Exam 构造枚举

题目链接: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


Problem Description
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?
 

 

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

 

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

 

Sample Input
1
3
6
10
15
21
28
 

 

Sample Output
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
 1 // File Name: J - Exam
 2 // Author: sheng
 3 // Created Time: 2013年04月25日 星期四 18时58分29秒
 4 
 5 #include <stdio.h>
 6 #include <string.h>
 7 #include <math.h>
 8 using namespace std;
 9 
10 typedef __int64 LL;
11 LL ans, a, b, c, t, n;
12 
13 int main ()
14 {
15     int cas = 0;
16     while (~scanf ("%I64d", &n))
17     {
18         a = pow (n, 1./3);
19         while (a*a*a < n)
20             a ++;
21         while (a*a*a > n)
22             a --;
23         ans = a;
24         for (int i = 1; i <= a; i ++)
25         {
26             t = n/i;
27             b = sqrt(t);
28             while (b*b < t)
29                 b ++;
30             while (b*b > t)
31                 b --;
32             ans += (t/i - i + b - i) * 3;
33             for (int j = i + 1; j <= b; j ++)
34                 ans += (t/j - j) * 6;
35         }
36         printf ("Case %d: %I64d\n", ++ cas, ans);
37     }
38     return 0;
39 }

 

posted on 2013-04-25 22:41  圣手摘星  阅读(188)  评论(0编辑  收藏  举报

导航