Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

O(log2(10)*10^6)..........0ms

Posted by chiccs at 2011-08-27 20:14:49 on Problem 1543
1.由于b<c<d,故b^3<c^3<d^3,所以 3b^3<b^3+c^3+d^3=a^3,即b<=a/1.44224957
2.设cube[i]=i^3,cube[i]单调,故只需枚举a,b,c并在二分查找cube[a]-cube[b]-cube[c]是否存在
3.b^3<c^3<d^3,所以cube[a]-cube[b]-cube[c]>cube[b]且>cube[c]
4.二分查找容易写错.......

#include <stdio.h>
//Binary Search used,the time complexity will be O(10^6*log2(10))
int cube[101],n,i,j,limit,l,t;
int find(int num)
{
   int left=1,right=l,mid;
   while (left<right)
     {
        mid=(left+right)>>1;
        if (cube[mid]==num) return mid;
        else if (cube[mid-1]==num) return mid-1;
        else if (cube[mid]<num) left=mid+1;
        else right=mid-1; 
     }
   return 0;
}
int main()
{
   scanf("%d",&n);
   cube[1]=1;
   for (i=2;i<=n;i++) cube[i]=cube[i-1]+3*i*i-3*i+1;
   for (l=6;l<=n;l++)
     {
        limit=l/1.44224957+1;
        for (i=2;i<=limit;i++)
        for (j=i;j<=n;j++)
          {
            t=cube[l]-cube[i]-cube[j];
            if (t<cube[i] || t<cube[j]) break;
            t=find(t);
            if (t>0) printf("Cube = %d, Triple = (%d,%d,%d)\n",l,i,j,t);
          }
     }
   scanf("%d",&i);
   return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator