結果

問題 No.442 和と積
ユーザー IL_mstaIL_msta
提出日時 2017-01-21 00:45:03
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 6,143 bytes
コンパイル時間 1,374 ms
コンパイル使用メモリ 105,632 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-04 22:56:22
合計ジャッジ時間 1,878 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 1 ms
6,944 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:126:15: warning: integer constant is so large that it is unsigned
  126 | const ULL MAX=10000000000000000000;     //10**19
      |               ^~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

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

#define _USE_MATH_DEFINES

#include <iostream>
#include <iomanip>

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

#include <string>
#include <cstring>
#include <vector>
#include <valarray>
#include <array>

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

#include<cassert>//assert();
#include <fstream>
/////////
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define P(p) cout<<(p)<<endl;
 
#define PII pair<int,int>
#define MAIN_INIT std::cin.tie(0); \
    std::ios::sync_with_stdio(false);\
	std::cout << std::fixed;//小数を10進数表示
/////////
#ifdef getchar_unlocked
#define mygc(c) (c)=getchar_unlocked()
#else
#define mygc(c) (c)=getchar()
#endif
#ifdef putchar_unlocked
#define mypc(c) putchar_unlocked(c)
#else
#define mypc(c) putchar(c)
#endif
/////////
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
/////////
using namespace::std;
/////////
#ifdef _DEBUG
#define DEBUG_BOOL(b) assert(b)
#else
#define DEBUG_BOOL(b) 
#endif
/////数値読み込み
#define ENABLE_READER_ON(T)	\
inline void reader(T &x){int k;x = 0;bool flag = true;\
while(true){mygc(k);\
if( k == '-'){flag = false;break;}if('0' <= k && k <= '9'){x = k - '0';break;}\
}\
if( flag ){while(true){mygc(k);if( k<'0' || '9'<k)break;x = x * 10 + (k - '0');}}\
else{while(true){mygc(k);if( k<'0' || '9'<k)break;x = x * 10 - (k-'0');}}\
}
//整数
ENABLE_READER_ON(int)
ENABLE_READER_ON(long)
ENABLE_READER_ON(long long)
ENABLE_READER_ON(unsigned int)
ENABLE_READER_ON(unsigned long)
ENABLE_READER_ON(unsigned long long)

//////
//文字読み込み
inline int reader(char& c){int i;
for(;;){mygc(i);if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF) break;}
c = i;
return i;}

inline int reader(char c[]){int i,s=0;
for(;;){mygc(i);if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF) break;}
c[s++]=i;
for(;;){mygc(i);if(i==' '||i=='\n'||i=='\r'||i=='\t'||i==EOF) break;c[s++]=i;}
c[s]='\0';return s;
}

inline int reader(string& c,int size=100){int i;c.reserve(size);
for(;;){mygc(i);if(i != ' '&&i != '\n'&&i != '\r'&&i != '\t'&&i != EOF)break;}
c.push_back(i);
for(;;){mygc(i);if(i == ' ' || i == '\n' || i == '\r' || i == '\t' || i == EOF)break;c.push_back(i);}
return c.size();}

/////////
//数値出力
#define ENABLE_WRITER_ON(T)	\
inline void writer(T x){char f[20];int s = 0;\
if (x<0){mypc('-');while(x){f[s++] = ~(x%10)+1,x /= 10;}}\
else{while(x){f[s++] = (x % 10), x /= 10;}}\
if (!s)f[s++] = 0;while (s--)mypc(f[s] + '0');}

ENABLE_WRITER_ON(int)
ENABLE_WRITER_ON(long)
ENABLE_WRITER_ON(long long)
ENABLE_WRITER_ON(unsigned int)
ENABLE_WRITER_ON(unsigned long)
ENABLE_WRITER_ON(unsigned long long)
/////////
inline void writer(const char c){mypc(c);}
inline void writer(const char c[]){for (int i = 0; c[i] != '\0'; i++)mypc(c[i]); }
inline void writer(const string str){writer( str.c_str() );}
///////////////////////////////////////////////////////////
// 最大公約数
template<class T>
inline T gcd(T a, T b){return b == 0 ? a : gcd(b, a % b);}
// 最小公倍数
template<class T>
inline T lcm(T a, T b){return a * b / gcd(a, b);}
////////////////////////////////
typedef pair<ULL,ULL> Pnum;
const ULL MAX=10000000000000000000;	//10**19
Pnum mul(ULL num1,ULL num2){
	Pnum ret;
	const ULL MH = 10000000000;//10**10
	const ULL ML = 1000000000;//10**9
	ULL CARR;
	ULL CAL,TEMP1,TEMP2;
	ULL AH,AL;
	ULL BH,BL;
	AH = num1/ML;
	AL = num1%ML;
	BH = num2/ML;
	BL = num2%ML;

	ret.second = AL*BL;//10**18

	CAL = AH*BL+AL*BH;//10**18
	ret.first  = CAL/MH;
	
	TEMP1 = MAX - ret.second - 1;
	TEMP2 = (CAL%MH)*ML;
	CARR = 0;
	if( TEMP1 >= TEMP2 ){
		ret.second += TEMP2;
	}else{
		TEMP2 = TEMP2 - TEMP1 - 1;
		CARR = 1;
		ret.second = TEMP2;
	}
	//////
	CAL = AH*BH ;
	ret.first += CAL/10;
	TEMP1 = MAX - ret.second -1;
	TEMP2 = (CAL%10) * (MAX/10);
	CARR = 0;
	if( TEMP1 >= TEMP2 ){
		ret.second += TEMP2;
	}else{
		TEMP2 = TEMP2 - TEMP1 - 1;
		CARR = 1;
		ret.second = TEMP2;
	}
	ret.first += CARR;
	return ret;
}

Pnum add(Pnum A,Pnum B){
	ULL TEMP1,TEMP2,CARR;
	Pnum ret;
	ret = A;

	TEMP1 = MAX - A.second - 1;
	TEMP2 = B.second;
	if( TEMP1 >= TEMP2 ){
		ret.second += TEMP2;
	}else{
		TEMP2 = TEMP2 - TEMP1 - 1;
		CARR = 1;
		ret.second = TEMP2;
	}
	ret.first += CARR + B.first;//over
	return ret;
}
void show(Pnum num){
	bool flag = false;
	if( num.first != 0){
		cout << num.first;
		flag = true;
	}
	if( flag ){
		ULL tempMax = MAX;
		while(tempMax != 0 && num.second / tempMax == 0){
			cout << "0";
			tempMax /= 10;
		}
	}
	if( num.second ){
		cout << num.second;
	}
}
bool operator>(Pnum A,Pnum B){
	if( A.first > B.first )return true;
	if( A.first < B.first )return false;
	if( A.second > B.second )return true;
	if( A.second < B.second )return false;
	return false;
}
bool operator<(Pnum A,Pnum B){
	return B>A;
}
bool operator==(Pnum A,Pnum B){
	if(A.first == B.first && A.second == B.second){
		return true;
	}
	return false;
}
bool operator>=(Pnum A,Pnum B){
	return A>B || A==B;
}
bool operator<=(Pnum A,Pnum B){
	return A<B || A==B;
}
Pnum operator-(Pnum A,Pnum B){
	assert(A>B);
	Pnum ret;
	ret = A;
	if( ret.first >= B.first ){
		ret.first -= B.first;
	}
	if( ret.second < B.second ){
		assert(ret.first >= 1);
		ret.first -=1;
		ret.second += MAX - B.second;
	}else{
		ret.second -= B.second;
	}
	return ret;
}

ULL operator%(Pnum A,ULL C){
	Pnum pnum1,pnum2;
	pnum1 = A;
	ULL tempMAX = MAX;
	while(tempMAX ){
		pnum2 = mul(C,tempMAX);
		while( pnum1 > pnum2 ){
			pnum1 = pnum1 - pnum2;
		}
		tempMAX /= 10;
	}
	return pnum1.second;
}

inline void solve(){
	ULL A,B,C;
	reader(A);
	reader(B);
	C = A+B;
	//A = 876543210987654321;
	//B = 876543210987654321;
	//A = 1000000000000000000;
	//B = 1000000000000000000;
	//1000000000000000000
	//cout << A << "\n" << B << endl;
	Pnum AB;
	AB = mul(A,B);
//show(AB);
//cout << endl;
	//AB%C
	ULL D = AB % C;
	//cout << D << endl;
	writer( gcd(C,D) );
	writer("\n");
}


int main(void){
    MAIN_INIT;
    cout << setprecision(16);//小数をいっぱい表示する。16?

	solve();

	return 0;
}
0