結果
問題 | No.1632 Sorting Integers (GCD of M) |
ユーザー |
👑 |
提出日時 | 2021-07-30 22:23:34 |
言語 | C (gcc 13.3.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,622 bytes |
コンパイル時間 | 339 ms |
コンパイル使用メモリ | 31,232 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-16 00:53:14 |
合計ジャッジ時間 | 1,774 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 46 WA * 13 |
コンパイルメッセージ
main.c: In function 'next_integer': main.c:20:16: warning: type defaults to 'int' in declaration of 'c' [-Wimplicit-int] 20 | static c[10] = {}; | ^
ソースコード
#include <stdio.h> const int Mod = 1000000007; long long div_mod(long long x, long long y, long long z) { if (x % y == 0) return x / y; else return (div_mod((1 + x / y) * y - x, (z % y), y) * z + x) / y; } long long pow_mod(int n, int k) { long long N, ans = 1; for (N = n; k > 0; k >>= 1, N = N * N % Mod) if (k & 1) ans = ans * N % Mod; return ans; } int next_integer(int X) { static c[10] = {}; int i = X % 10, j = X / 10 % 10; c[i]++; while (i <= j) { X /= 10; c[j]++; i = j; j = X / 10 % 10; } if (j == 0) return 0; X /= 100; c[j]++; for (i = j + 1; c[i] == 0; i++); X = X * 10 + i; c[i]--; for (i = 1; i <= 9; i++) { for (j = 1; j <= c[i]; j++) X = X * 10 + i; c[i] = 0; } return X; } int gcd(int a, int b) { if (a == 0) return b; else return gcd(b % a, a); } int main() { int i, j, N, c[10]; scanf("%d", &N); for (i = 1; i <= 9; i++) scanf("%d", &(c[i])); for (i = 1; i <= 9; i++) if (c[i] == N) break; if (i <= 9) { printf("%lld\n", div_mod(pow_mod(10, N) - 1, 9, Mod) * i % Mod); fflush(stdout); return 0; } int X, ans = 0; if (N <= 9) { for (i = 1, X = 0; i <= 9; i++) { for (j = 1; j <= c[i]; j++) X = X * 10 + i; c[i] = 0; } do { ans = gcd(ans, X); X = next_integer(X); } while (X > 0); printf("%d\n", ans); fflush(stdout); return 0; } long long sum = 0; if (c[4] + c[8] == N) ans = 4; else if (c[2] + c[4] + c[6] + c[8] == N) ans = 2; else ans = 1; for (i = 1; i <= 9; i++) sum += (long long)i * c[i]; if (sum % 9 == 0) ans *= 9; else if (sum % 3 == 0) ans *= 3; printf("%d\n", ans); fflush(stdout); return 0; }