hdu 4473 Exam

来源:互联网 发布:淘宝模特短发红人 编辑:程序博客网 时间:2024/05/20 11:52
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
13610152128
 

Sample Output
Case 1: 1Case 2: 7Case 3: 25Case 4: 53Case 5: 95Case 6: 161Case 7: 246
 

Source
2012 Asia Chengdu Regional Contest
 


 

 

题解:看题花了好长时间呢,还是没看懂,最后借鉴了一下别人的解释。

       题目的意思是说f(x)是满足x%(a*b)=0的(a,b)的有序对的数目,求解f(1)+f(2)+...+f(n)的数值,此时思考一下则可以明白就是求满足a*b*y<=n的(a,b,y)的数目(想一想,为什么的小于等于)。

       假设a<=b<=y,则可以写出他们的取值范围a<=pow(n,1.0/3.0);b<=sqrt(n/a);y>=b;

       因为是有序对,所以a=b=y是sum+=1;

                                       a=b!=y||a!=b=y时,sum+=3;

                                       a!=b!=y时,sum+=6;(想一想,为什么呢?)

       因为枚举必然超时,所以选择使用算法解决:a=b=y的数目是pow(n,1.0/3.0)

                                                                               a=b!=y||a!=b=y的数目是n/(a*a)-a+b-a

                                                                               a!=b!=y的数目是n/(a*b)-b   

         

    此时只要枚举a就可以了。

代码:

#include <iostream>#include <cstdio>#include <cmath>using namespace std;int main(){    __int64 A,B,C,sum,n,i,j,t=1;           //记住只能用__int64,使用unsigned long long会严重超时    while(~scanf("%I64d",&n))          //使用%I64d输入    {        A=pow((double)n,1.0/3.0);        while(A*A*A<n) A++;        if(A*A*A>n) A--;        sum=A;                for(i=1; i<=A; i++)        {            B=n/i;            C=sqrt(B);            while(C*C<B) C++;            if(C*C>B) C--;                        sum+=(B/i-i+C-i)*3;            for(j=i+1; j<=C; j++)                sum+=(B/j-j)*6;        }        printf("Case %I64d: %I64d\n",t++,sum);   //使用%I64d输出    }    return 0;}


        

原创粉丝点击