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

大水啊,16ms

Posted by KatrineYang at 2016-11-11 04:59:56 on Problem 1543
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
using namespace std;

int cubes[101];

int isCube(int n){
	int cr = (int)pow(n+0.0, 1/3.0);
	if(cr*cr*cr==n) return cr;
	if((cr+1)*(cr+1)*(cr+1)==n) return cr+1;
	else return -1;
}

void init(){
	for(int i = 0; i <= 100; i++) cubes[i] = i*i*i;
}

struct cube{
	int a,b,c,d;
}cub[2000];

bool cmp(const cube &c1, const cube &c2){
	return c1.a < c2.a || (c1.a==c2.a && c1.d < c2.d);
}

int gs = 0;

void preprocess(){
	init();
	for(int a=6; a<=100; a++){
		//cout << a << endl;
		int ca = cubes[a];
		int lb = (int)pow((ca-1)/3.0, 1/3.0)+1;
		//cout << lb << endl;
		for(int b = lb; b < a; b++){
			for(int c = 2; c <= b; c++){
				int d;
				if((d = isCube(ca-cubes[b]-cubes[c])) != -1){
					if(d<=c && d>1){
						//cout << a << " " << b <<  " " << c << " " << d << endl;
						cub[gs].a = a;
						cub[gs].b = b;
						cub[gs].c = c;
						cub[gs].d = d;
						gs++;
					}
				}
			}
		}
	}
	sort(cub, cub+gs, cmp);
}

int main() {
	//cout << isCube(216) << endl;
	//cout << 1 << endl;
	preprocess();
	//cout << 1 << endl;
	int n;
	scanf("%d", &n);
	for(int i = 0; i < gs; i++){
		if(cub[i].a > n) break;
		printf("Cube = %d, Triple = (%d,%d,%d)\n", cub[i].a, cub[i].d, cub[i].c, cub[i].b);
	}
	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