hdu 4473 Exam (思维题 问题转化)

Exam

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


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 ≤ 10 11).
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
 

Source
2012 Asia Chengdu Regional Contest
 
ps:这题是2012年成都区域赛的一个题  挺考思维的   好题 ! (可惜我是想不到的)  ╮(╯▽╰)╭ 所以下面只能贴别人的思路了

题目意思:

  定义f(x) = 满足(a * b)|x的有序对(a,b)的个数。

  然后输入一个n,求f(1) + f(2) + ... + f(n)

此题的关键在于:

  把原题的条件(a * b)|x 转化为 a * b * y = x

  然后就很好计算了,就是,输入一个n,计算有多少有序对(a, b ,y)满足a*b*y<=n

  不妨设a<=b<=y

  则,a<=n^(1/3) , b<=sqrt(n/a)

  那么

    对于三个数字都相同的情况,只计算一次: 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

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

代码:

#include <cstdio>
#include <cmath>
using namespace std;

long long n,ans;

long long sqrt2(long long nn)    // long long 开平方   
{
    int i,j;
    long long k;
    k=pow(nn*1.0,0.5);
    while(k*k<=nn) k++;
    while(k*k>nn) k--;
    return k;
}
long long sqrt3(long long nn)    // long long 开立方
{
    int i,j;
    long long k;
    k=pow(nn*1.0,1.0/3.0);
    while(k*k*k<=nn) k++;
    while(k*k*k>nn) k--;
    return k;
}
int main()
{
    int i,j,t=0;
    long long a,b,c,s,sq2,sq3;
    while(~scanf("%I64d",&n))
    {
        t++;
        sq2=sqrt2(n);       // 假如直接用sqrt或pow开平方、立方会WA  可能是浮点误差吧
        sq3=sqrt3(n);
        ans=sq3;                    // 第一种情况
        for(i=1;i<=sq2;i++)         // 第二种情况
        {
            s=(n/((long long)(i)*i));
            if(s<1) break;
            if(i<=sq3) ans+=(s*3-3);
            else ans+=s*3;
        }
        for(i=1;i<=sq3;i++)         // 第三种情况
        {
            for(j=i+1;j<=sq2;j++)
            {
                s=(n/((long long)(i)*j)-j);
                if(s<=0) break ;
                ans+=s*6;
            }
        }
        printf("Case %d: %I64d\n",t,ans);
    }
    return 0;
}







你可能感兴趣的:(hdu 4473 Exam (思维题 问题转化))