2012 ACM/ICPC Asia Regional Tianjin Online

A very hard mathematic problem

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1455 Accepted Submission(s): 440


Problem Description
  Haoren is very good at solving mathematic problems. Today he is working a problem like this:
  Find three positive integers X, Y and Z (X < Y, Z > 1) that holds
   X^Z + Y^Z + XYZ = K
  where K is another given integer.
  Here the operator “^” means power, e.g., 2^3 = 2 * 2 * 2.
  Finding a solution is quite easy to Haoren. Now he wants to challenge more: What’s the total number of different solutions?
  Surprisingly, he is unable to solve this one. It seems that it’s really a very hard mathematic problem.
  Now, it’s your turn.

Input
  There are multiple test cases.
  For each case, there is only one integer K (0 < K < 2^31) in a line.
  K = 0 implies the end of input.
  

Output
  Output the total number of solutions in a line for each test case.

Sample Input
9 53 6 0

Sample Output
1 1 0   
Hint
9 = 1^2 + 2^2 + 1 * 2 * 2 53 = 2^3 + 3^3 + 2 * 3 * 3

Source
貌似昨天rp都用光了,今天上來A題秒過n多人,壓力山大,然後看到上面那倒,一看就是二分查找的感覺,但是輸入2^31,電腦就卡了不給輸出,開始還以爲難道超時了,難道還有什麼高深莫測的優化,無奈放下,剛看到最後一題簡單想做來着,不知道我們那個賬號有的人這麼多盡然a了,簡單的都被A完了我幹嘛Y-Y......然後看到這道題過的人越來越多,果斷再來debug,一個一個數據輸到104800,還有輸出,到104900,瞬間卡屏,不至於瞬間超時,然後終於想通了,數據溢出了(Y-Y),雖然k《=2^31;二分計算函數值的時候會超過的啊Y-Y,終於悶騷的打了一瓶醬油過了,昨天過一道,今天還是一道Y——Y,下次來個突破吧,阿彌陀佛善哉善哉
#include<string.h>
#include<stdio.h>
#include<math.h>
long long  y,z,k,sum,f,ok[1290];
long long fx(long  long a,long long b,long long c)
{
 long long i,aa=1,bb=1;
 for (i=1;i<=c;i++)
 {
    aa=aa*a;//本來用pow,記得好像是計算實數會不會有精度誤差不用算了-
    bb=bb*b;
 }
 return aa+bb+a*b*c;
}
void find(long long  l,long long r)
{
 long long m=(l+r)/2,key;
 key=fx(m,y,z);
 if (key==k) {if (m>0&&m<y) f=1; return ;}
 if (l>=r) return;
 if (key<k) find(m+1,r);
       else find(l,m-1);
}
int main()
{
 long long t,exp=0;
 while (scanf("%lld",&k),k)

 {
  sum=0;
  t=sqrt(k);
  if (t*t==k)//z=2,直接開方,
  {
   if (t%2==1) sum=t/2;
       else sum=t/2-1;
  }
 for (z=3;fx(1,2,z)-exp<=k;z++)
 {
  y=2;
  while (fx(1,y,z)-exp<=k)//說起exp,是爲了調試程序用的看邊界條件,其實邊界開始就對的最後exp=0,可有可無
  {
   f=0;
   find(1,y-1);
   sum=sum+f;
   ++y;
  }
 }
 printf("%lld\n",sum);

 }
 return 0;
}最後再去hdu用gcc跑了下15ms,再用c跑了下0ms,衝到第一了吼吼。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章