結果

問題 No.757 チャンパーノウン定数 (2)
ユーザー kotatsugame
提出日時 2018-12-05 02:16:07
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 452 ms / 2,000 ms
コード長 824 bytes
コンパイル時間 692 ms
コンパイル使用メモリ 66,680 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-07-16 02:54:43
合計ジャッジ時間 8,249 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 51
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:6:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
    6 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
#define int long
using namespace std;
int b;
string d;
main()
{
	cin>>b>>d;
	int x;
	for(x=1;x<d.size();x++)
	{
		string tmp=d;
		int sub=x*(b-1);
		int id=d.size()-x;
		while(sub>0&&id>=0)
		{
			if(d[id]-48>=sub%b)
			{
				d[id]-=sub%b;
			}
			else
			{
				d[id]=d[id]-sub%b+b;
				sub+=b;
			}
			sub/=b;
			id--;
		}
		if(sub>0)
		{
			d=tmp;
			break;
		}
	}
	string ans="";
	int carry=0;
	for(int i=0;i<d.size()-x;i++)carry=carry*b+d[i]-48;
	for(int i=x;i>0;i--)
	{
		carry=carry*b+d[d.size()-i]-48;
		ans+=48+carry/x+(i==x);
		carry%=x;
	}
	if(carry>0)
	{
		int id=ans.size()-1;
		while(id>=0&&ans[id]-48==b-1)ans[id++]=48;
		if(id<0)return 1;
		ans[id]++;
	}
	else
	{
		int id=ans.size()-1;
		while(ans[id]-48==0)ans[id++]=48+b-1;
		ans[id]--;
	}
	cout<<ans[carry?carry-1:ans.size()-1]<<endl;
}
0