码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 4282 A very hard mathematic problem

时间:2015-08-12 01:24:00      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:hdu 4282 a very hard

A very hard mathematic problem

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


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
 

题解:把Z=2的情况特判一下,其他暴力枚举。

#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
#define N 5005
#define ll long long

using namespace std;
ll k;

ll P(ll x,int n) {
    ll res=1;
    while(n) {
        if(n&1)res*=x;
        x*=x;
        n>>=1;
    }
    return res;
}

int main() {
   // freopen("test.in","r",stdin);
    while(~scanf("%I64d",&k)&&k) {
        int ans=0;
        ll kk=sqrt(k);
        if(kk*kk==k) {///z==2的情况
            ans+=(kk+1)/2-1;
        }
        for(int z=3; z<=31; z++) {
            for(int x=1;; x++) {
                ll xx=P((ll)x,z);
                if(xx>=k/2)break;
                for(int y=x+1;; y++) {
                    ll yy=P((ll)y,z);
                    if(yy+xx+(ll)x*y*z>k)break;
                    else if(yy+xx+(ll)x*y*z==k) {
                        ans++;
                        break;
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu 4282 A very hard mathematic problem

标签:hdu 4282 a very hard

原文地址:http://blog.csdn.net/acm_baihuzi/article/details/47431331

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!