1. 程式人生 > >hdu4282 A very hard mathematic problem(二分)

hdu4282 A very hard mathematic problem(二分)

題意:

給你一個K(K<231)找有多少個符合條件的X,Y,Z(X<Y,Z>1),使得XZ+YZ+XYZ=K

題解:

我們去列舉X,Z然後二分Y就可以了,因為X的最大為24000,Z最大為31。

程式碼:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<queue> #include<stack> #include<map> #include<cstdlib> #include<cmath> #define PI 2*asin(1.0) #define LL long long #define pb push_back #define pa pair<int,int> #define clr(a,b) memset(a,b,sizeof(a)) #define lson lr<<1,l,mid #define rson lr<<1|1,mid+1,r
#define bug(x) printf("%d++++++++++++++++++++%d\n",x,x) #define key_value ch[ch[root][1]][0] const int MOD = 1000000007; const int N = 30000 + 100; const int maxn = 100+ 14; const int letter = 130; const int INF = 1e9; const double pi=acos(-1.0); const double eps=1e-8; using namespace std; inline int read() { int
x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } LL n; LL dp[N][32]; int main(){ for(int i=1;i<N;i++) dp[i][0]=1ll; for(int i=1;i<=N;i++){ for(int j=1;j<31;j++) { if(dp[i][j-1]*1ll*i>(1ll<<31)) break; dp[i][j]=dp[i][j-1]*i; } } while(~scanf("%I64d",&n)&&n){ int ans=0; for(LL i=1;i<=30000;i++){ for(LL j=2;j<=31;j++){ if(!dp[i][j]) break; LL l=i+1ll,r=30000; while(l<=r){ LL mid=(l+r)/2ll; if(!dp[mid][j]) {r=mid-1;continue;} LL sum=dp[i][j]+dp[mid][j]+i*j*mid; if(sum==n) {ans++;break;} else if(sum<n) l=mid+1ll; else r=mid-1ll; } } } printf("%d\n",ans); } return 0; }