結果

問題 No.2069 み世界数式
ユーザー tailstails
提出日時 2022-09-11 00:03:50
言語 C90
(gcc 12.3.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 2,139 bytes
コンパイル時間 957 ms
コンパイル使用メモリ 27,904 KB
実行使用メモリ 11,628 KB
最終ジャッジ日時 2024-11-27 07:29:14
合計ジャッジ時間 2,933 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 41
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function ‘main’:
main.c:123:9: warning: implicit declaration of function ‘read’; did you mean ‘rd’? [-Wimplicit-function-declaration]
  123 |         read(0,buf,sizeof buf);
      |         ^~~~
      |         rd
main.c:130:17: warning: implicit declaration of function ‘write’ [-Wimplicit-function-declaration]
  130 |                 write(1,"-1\n",3);
      |                 ^~~~~
main.c:135:9: warning: implicit declaration of function ‘_exit’ [-Wimplicit-function-declaration]
  135 |         _exit(0);
      |         ^~~~~
main.c:135:9: warning: incompatible implicit declaration of built-in function ‘_exit’ [-Wbuiltin-declaration-mismatch]

ソースコード

diff #

#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")

#define rd() ({long _v=0,_c;while(_c=*rp++-48,_c>=0)_v=_v*10+_c;_v;})

typedef unsigned long ulong;
char buf[1024];

struct A {
	int op;
	char* ep;
	struct A* c1;
	struct A* c2;
};

struct B {
	struct A a[301];
};

struct B pool[8192];
struct B* pp=pool;

struct B* eval(char**epp,long m){
	char*e=*epp;
	struct B*mul=0;
	struct B*add=0;
	char*mulp=0;
	char*addp=0;
	while(1){
		struct B*cur;
		if(*e=='('){
			++e;
			cur=eval(&e,m);
		}else{
			long v=0;
			for(long c;c=*e-'0',c>=0&&c<=9;e++){
				v=v*10+c;
			}
			cur=pp++;
			cur->a[v].op='0';
		}
		{
			char*curp=e++;
			if(mul){
				struct B*r=pp++;
				for(int x=0;x<=m;++x){
					if(mul->a[x].op){
						for(int y=0;y<=m;++y){
							if(cur->a[y].op){
								if(y){
									struct A*a=&r->a[x/y];
									a->op='/';
									a->c1=&mul->a[x];
									a->c2=&cur->a[y];
									a->ep=mulp;
								}
								if(x*y<=m){
									struct A*a=&r->a[x*y];
									a->op='*';
									a->c1=&mul->a[x];
									a->c2=&cur->a[y];
									a->ep=mulp;
								}
							}
						}
					}
				}
				mul=r;
			}else{
				mul=cur;
			}
			mulp=curp;
		}
		if(*mulp!='&'){
			if(add){
				struct B*r=pp++;
				for(int x=0;x<=m;++x){
					if(add->a[x].op){
						for(int y=0;y<=m;++y){
							if(mul->a[y].op){
								if(x-y>=0){
									struct A*a=&r->a[x-y];
									a->op='-';
									a->c1=&add->a[x];
									a->c2=&mul->a[y];
									a->ep=addp;
								}
								if(x+y<=m){
									struct A*a=&r->a[x+y];
									a->op='+';
									a->c1=&add->a[x];
									a->c2=&mul->a[y];
									a->ep=addp;
								}
							}
						}
					}
				}
				add=r;
			}else{
				add=mul;
			}
			mul=0;
			addp=mulp;
			if(*addp=='\n'||*addp==')'){
				break;
			}
		}
	}
	*epp=e;
	return add;
}

void apply(struct A*a){
	if(a){
		if(a->ep) *a->ep=a->op;
		apply(a->c1);
		apply(a->c2);
	}
}

int main(){
	read(0,buf,sizeof buf);
	char*rp=buf;
	long m=rd();
	long ans=rd();
	char*expr=rp;
	struct B*b=eval(&expr,m);
	if(b->a[ans].op==0){
		write(1,"-1\n",3);
	}else{
		apply(&b->a[ans]);
		write(1,rp,expr-rp);
	}
	_exit(0);
}
0