結果

問題 No.443 GCD of Permutation
ユーザー bal4ubal4u
提出日時 2019-05-09 13:37:12
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,130 bytes
コンパイル時間 290 ms
コンパイル使用メモリ 31,616 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-22 01:32:46
合計ジャッジ時間 1,309 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 1 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
testcase_30 AC 1 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'ins':
main.c:7:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
    7 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:22:28: note: in expansion of macro 'gc'
   22 |                 *p++ = c = gc();
      |                            ^~
main.c: In function 'out':
main.c:8:15: warning: implicit declaration of function 'putchar_unlocked' [-Wimplicit-function-declaration]
    8 | #define pc(c) putchar_unlocked(c)
      |               ^~~~~~~~~~~~~~~~
main.c:34:17: note: in expansion of macro 'pc'
   34 |         if (!n) pc('0');
      |                 ^~

ソースコード

diff #

// yukicoder: No.443 GCD of Permutation
// 2019.5.8 bal4u

#include <stdio.h>

#if 1
#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)
#else
#define gc() getchar()
#define pc(c) putchar(c)
#endif

int f[10];
char s[10003];

void ins()
{
	int c;
	char *p = s;
	while (1) {
		*p++ = c = gc();
		if (c <= ' ') break;
		f[c & 0xf]++;
	}
	*(p-1) = 0;
}

void out(int n)
{
	int i;
	char ob[20];

	if (!n) pc('0');
	else {
		i = 0; while (n) ob[i++] = n%10 + '0', n/=10;
		while (i--) pc(ob[i]);
	}
}

void outs(char *s) { while (*s) pc(*s++); }

int gcd(int a, int b)
{
	int r;
	while (b != 0) r = a % b, a = b, b = r;
	return a;
}

int bigGCD(char *s, int b)
{
	int a, r;
	
	r = 0;
	while (*s) {
		a = *s++ & 0xf;
		if (*s) a = a * 10 + (*s++ & 0xf);
		r = (r*100 + a) % b;
	}
	return gcd(b, r);
}

int main()
{
	int i, j, g;

	ins();
	j = 0; for (i = 0; i < 10; i++) if (f[i]) j++;
	if (j == 1) outs(s);
	else {
		g = 0;
		for (i = 0; i < 10; i++) if (f[i]) {
			for (j = i+1; j < 10; j++) if (f[j]) {
				if (g == 0) g = 9*(j-i);
				else g = gcd(g, 9*(j-i));
			}
		}
		out(bigGCD(s, g));
	}
	pc('\n');	
	return 0;
}
0