! HDU 4282 A very hard mathematic problem-card time-(two-minute enumeration)

Source: Internet
Author: User

Test instructions: x^z+y^z+xyz=k, given K, the number of x, y that satisfies the formula is obtained. (x<y,z>1,k<2^31)

Analysis:

A look is the card time problem, the general method of this problem is enumeration, but first analysis of the relationship between the data, the enumeration of variables to reduce the scope of the data to an acceptable degree; the other two points in this problem is a common method.

This is a bit more complicated, and there are three variables to enumerate. First Z as a power, and k<2^31, so z can not exceed 31, and then consider the case of z take the minimum 2, at this time there is (X+y) ^2=k, the estimated x+y<=10^4.5, that is x+y not more than 40000, this will be the X and Y range estimates.

Then there is the practice: because there are three variables, if enumerated by the above estimate, the triple loop will time out, which requires us to use a more efficient method: Double-loop enumeration of z and X, and then on the basis of two Y.

Another trick here is to pre-preprocess all the integers to the power, so the enumeration is no longer calculated.

Code:

#include <cstdio> #include <cstring> #include <iostream> #include <cstring> #define MIN (b) A <b?a:b#define INF 1000000007using namespace Std;long long N;long long X,y,z;long long A[50005][32];long long cnt,t  Mp;void init () {memset (a,0,sizeof (a)); for (long long i=1;i<=50000;i++) {a[i][1]=i;for (Long long j=2;j<=31;j++) {A I [J]=a[i][j-1]*i;if (A[i][j]>inf) Break;}}} BOOL Binary_search (long long X,long long Z) {Long long l=x+1,r=50000,mid;while (l<=r) {mid= (l+r) >>1;if (a[mid][z ] {if (tmp+a[mid][z]+x*mid*z==n) return True;else if (tmp+a[mid][z]+x*mid*z<n) l=mid+1; else r=mid-1;} else r=mid-1;} return false;} int main () {init ();    while (scanf ("%i64d", &n)!=eof) {    if (!n) break;    cnt=0;    for (x=1;x<=50000&&x<n;x++) {for    (z=2;z<=31;z++) {    if (a[x][z]&& (N-a[x][z]) >0) {     tmp=a[x][z];    if (Binary_search (x,z)) cnt++;}}    }    printf ("%i64d\n", CNT);    }    return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

! HDU 4282 A very hard mathematic problem-card time-(two-minute enumeration)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.