結果

問題 No.376 立方体のN等分 (2)
ユーザー IL_mstaIL_msta
提出日時 2017-02-17 14:38:29
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 253 ms / 5,000 ms
コード長 4,145 bytes
コンパイル時間 2,243 ms
コンパイル使用メモリ 119,740 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-29 06:48:02
合計ジャッジ時間 4,440 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 4 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 5 ms
4,380 KB
testcase_16 AC 84 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 5 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 253 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 94 ms
4,376 KB
testcase_24 AC 29 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 180 ms
4,380 KB
testcase_27 AC 38 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 12 ms
4,380 KB
testcase_32 AC 38 ms
4,380 KB
testcase_33 AC 53 ms
4,380 KB
testcase_34 AC 27 ms
4,380 KB
testcase_35 AC 28 ms
4,376 KB
testcase_36 AC 53 ms
4,380 KB
testcase_37 AC 53 ms
4,384 KB
testcase_38 AC 16 ms
4,376 KB
testcase_39 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifdef __GNUC__
#pragma GCC optimize ("O3")
#pragma GCC target ("avx")
#endif

#define _USE_MATH_DEFINES

#include <iostream>
#include <iomanip>
#include <stdio.h>

#include <sstream>
#include <algorithm>
#include <cmath>

#include <string>
#include <cstring>
#include <vector>
#include <valarray>
//#include <array>//x C++ (G++ 4.6.4)

#include <queue>
#include <complex>
#include <set>
#include <map>
#include <stack>
#include <list>

#include <cassert>//assert();
#include <fstream>
#include <random>
/////////
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)

/////////
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
#define PII pair<int,int>
/////////
using namespace::std;

// 最大公約数
template<class T>
inline T gcd(T a, T b){return b ? gcd(b, a % b) : a;}
//inline T gcd(T a, T b){return b == 0 ? a : gcd(b, a % b);}
/*inline T gcd(T a,T b){
	while(b != 0 ){
		a = a % b;
		swap(a,b);
	}
	return a;
}*/

// 最小公倍数
template<class T>
inline T lcm(T a, T b){return a / gcd(a, b) * b;}
//inline T lcm(T a, T b){return a * b / gcd(a, b);}
////////////////////////////////
vector<LL> SerNum;
vector<LL> SerVal(3,0);
int MaxDepth;
LL MinDfs = (LL)100000000000000;
//pos,
bool dfs(int pos, vector<LL>& vv){
	if( pos >= MaxDepth ){
		if( vv[0] == 1 || vv[1] == 1 || vv[2] == 1 ) return false;
		LL temp = vv[0]+vv[1]+vv[2];
		if( temp < MinDfs ){
			MinDfs = temp;
			SerVal = vv;
		}
		return true;
	}

	if( MinDfs < vv[0]+vv[1]+vv[2] ) return false;

	bool ret = false;
	for(int i=0;i<3;++i){
		vv[i] *= SerNum[pos];
		if( dfs(pos+1,vv) ){
			ret = true;
		}
		vv[i] /= SerNum[pos];
	}
	return ret;
}
inline void solve(){
	LL N;
	cin >> N;
	int CountAll = 0;
	int Count = 0;
	vector<LL> prime;
	vector<int> P;
	LL T = N;
	if( T % 2 == 0 ){
		prime.push_back(2);
		P.push_back(0);
		while(T%2 == 0 ){
			T /= 2;
			P[Count]++;
			++CountAll;
		}
		++Count;
	}
	for(LL i=3;i*i<=T;i+=2){
		if( T % i == 0 ){
			prime.push_back(i);
			P.push_back(0);
			while( T% i == 0 ){
				T /= i;
				P[Count]++;
				++CountAll;
			}
			++Count;
		}
	}
	if( T != 1 ){
		prime.push_back(T);
		P.push_back(1);
		++Count;
		++CountAll;
	}
	T = 0;
	//
	if( CountAll == 1 ){
		cout << N-1 << ' ' << N-1 << endl;
		return;
	}
	if( CountAll == 2){
		vector<LL> ans(3);
		for(int i=0;i<Count;++i){
			if(P[i]){
				ans[0] = prime[i]-1;
				P[i]--;
				break;
			}
		}
		for(int i=0;i<Count;++i){
			if( P[i] ){
				ans[1] = prime[i]-1;
				P[i]--;
				break;
			}
		}
		cout << ans[0]+ans[1] << ' ' << N-1;
		return;
	}
	vector<LL> ans(3,1);
	int SerCount = 0;
	
	/*if(Count == 1){//1種類なら
		while(P[0] >= 3 ){//!?
			ans[0] *= prime[0];
			ans[1] *= prime[0];
			ans[2] *= prime[0];
			P[0] -= 3;
		}
	}*/
	for(int i=0;i<Count;++i){
		if( P[i] ){
			SerCount++;
			int temp = P[i];
			while( temp-- ){
				SerNum.push_back(prime[i]);
			}
		}
	}
	
	MaxDepth = SerNum.size();
	if( MaxDepth == 0 ){
		LL temp = 0;
		for(int i=0;i<3;++i){
			temp += ans[i]-1;
		}
		cout << temp << ' ' << N-1 << endl;
		return;
	}
	vector<LL> val(3,1);
	int P0 = P[0];
	for(int a=0;2*a<=P0;++a){
		if( a ) ans[0] *= SerNum[0];
		ans[1] = 1;
		for(int b=a;a+b<=P0;++b){
			int c = P0-a-b;
			if( c < b )break;
			//if(b) ans[1] *= SerNum[0];
			//(SerNum[0])^c
			ans[1] = 1;
			LL mul = SerNum[0];
			int temp = b;
			while(temp){
				if( temp & 1 ) ans[1] *= mul;
				mul *= mul;
				temp >>=1;
			}//for(int i=0;i<b;++i){ans[1] *= SerNum[0];}
			///
			//(SerNum[0])^c
			ans[2] = 1;
			mul = SerNum[0];
			temp = c;
			while(temp){
				if( temp & 1) ans[2] *= mul;
				mul *= mul;
				temp >>=1;
			}//for(int i=0;i<c;++i){ans[2] *= SerNum[0];}
			////
			bool bRes = dfs(P0,ans);
			LL aaa = SerVal[0]*SerVal[1]*SerVal[2];
			if( bRes && aaa != N ){
				cout << " ";
			}
		}
	}

	LL temp = 0;
	for(int i=0;i<3;++i){
		temp += SerVal[i]-1;
	}
	cout << temp << ' ' << N-1 << endl;
}

int main(void){
	std::cin.tie(0);
	std::ios::sync_with_stdio(false);
	//std::cout << std::fixed;//小数を10進数表示
	//cout << setprecision(16);//小数をいっぱい表示する。16?
	
	solve();
	return 0;
}
0