HDU - 4473 Exam(思维)

Exam

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


 

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),求数对(a, b) 满足(a * b % x == 0) 的个数

f(1) = 1 * 1;

f(2) = 1 * 1 , 1  * 2, 2 * 1

f(3) = 1 * 1 , 1 * 3, 3 * 1

...

思路:以上式子可以转化成a * b * y = x

假设 a <= b <= y

因为是前n项和,那么就会出现3种情况 :

(1)3种相同

满足就一个,每个➕1即可

(2)2种相同  两个相同 i i a, i a i, a i i

对于i,那么k  = n / i / i 如果k 大于等于 i 那么就有 (k  - 1) * 3种 因为要剪掉k = i 的情况,把它放在k<= i 也是一样

(3)3种不同

对于每一的3种不同的数对,都有6种,所以对于 i , j ,而且k = n / i / j,(按照i < j < k)那么如果k > j ,就有(k - j) * 6种

所以AC代码

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <cmath>
#include <string.h>
using namespace std;
#define ll long long
int main(int argc, const char * argv[]) {
    ll n, ca = 0;
    while(scanf("%lld", &n) != EOF) {
        ll ans = 0;//a * b * y == x   a <= b <= y
        for (ll i = 1; i * i * i <= n; i ++) ans ++;
        for (ll i = 1; i * i <= n; i ++) { //两个相同 i i a, i a i, a i i
            ll k = n / i / i;
            if(k >= i) ans += (k - 1) * 3;
            else ans += k * 3;
        }
        for (ll i = 1; i * i * i <= n; i ++) {//a,b,y各不相同 有6种
            for (ll j = i + 1; j <= n; j ++) {
                ll k = n / i / j;
                if(k > j) ans += (k - j) * 6;
                else break;
            }
        }
        printf("Case %lld: %lld\n", ++ca, ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/henu_jizhideqingwa/article/details/81416399
今日推荐