結果

問題 No.2989 Fibonacci Prize
ユーザー ygussanyygussany
提出日時 2024-12-14 01:35:40
言語 C
(gcc 12.3.0)
結果
RE  
実行時間 -
コード長 3,386 bytes
コンパイル時間 544 ms
コンパイル使用メモリ 32,512 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-12-14 01:35:43
合計ジャッジ時間 2,609 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 1 ms
5,248 KB
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 AC 1 ms
5,248 KB
testcase_14 AC 1 ms
5,248 KB
testcase_15 AC 1 ms
5,248 KB
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
testcase_47 RE -
testcase_48 RE -
testcase_49 RE -
testcase_50 RE -
testcase_51 RE -
testcase_52 RE -
testcase_53 RE -
testcase_54 RE -
testcase_55 RE -
testcase_56 RE -
testcase_57 RE -
testcase_58 RE -
testcase_59 RE -
testcase_60 RE -
testcase_61 RE -
testcase_62 RE -
testcase_63 RE -
testcase_64 WA -
testcase_65 AC 1 ms
5,248 KB
testcase_66 AC 1 ms
5,248 KB
testcase_67 RE -
testcase_68 AC 1 ms
5,248 KB
testcase_69 AC 1 ms
5,248 KB
testcase_70 AC 1 ms
5,248 KB
testcase_71 RE -
testcase_72 AC 1 ms
5,248 KB
testcase_73 AC 1 ms
5,248 KB
testcase_74 AC 1 ms
5,248 KB
testcase_75 RE -
testcase_76 AC 1 ms
5,248 KB
testcase_77 AC 1 ms
5,248 KB
testcase_78 AC 1 ms
5,248 KB
testcase_79 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'solve':
main.c:36:9: warning: implicit declaration of function 'exit' [-Wimplicit-function-declaration]
   36 |         exit(-1);
      |         ^~~~
main.c:2:1: note: include '<stdlib.h>' or provide a declaration of 'exit'
    1 | #include <stdio.h>
  +++ |+#include <stdlib.h>
    2 | 
main.c:36:9: warning: incompatible implicit declaration of built-in function 'exit' [-Wbuiltin-declaration-mismatch]
   36 |         exit(-1);
      |         ^~~~
main.c:36:9: note: include '<stdlib.h>' or provide a declaration of 'exit'

ソースコード

diff #

#include <stdio.h>

int naive(int N, int M)
{
	int i;
	static __int128 F[1001];
	for (i = 3, F[1] = 1, F[2] = 1; i <= M; i++) F[i] = F[i-1] + F[i-2];
	
	int ans = 0, l, r;
	__int128 sum;
	for (l = 1; l <= M; l++) {
		for (r = l; r <= M; r++) {
			for (i = l, sum = 0; i <= r; i++) {
				sum += F[i];
				if (sum > F[M]) break;
			}
			if (i > r && sum % N == 0) ans++;
		}
	}
	return ans;
}

long long solve(int N, int M)
{
	if (M == 1) {
		if (N == 1) return 1;
		else return 0;
	} else if (M == 2) {
		if (N == 1) return 2;
		else return 0;
	} else if (M == 3) {
		if (N == 1) return 4;
		else if (N == 2) return 2;
		else return 0;
	}
	exit(-1);
	if (N == 1) return (long long)(M - 2) * (M - 1) / 2 + 3;
	
	int i, cur, prev, F[2], sum = 2 % N, flag = 0;
	static int num[200001], tmp[200001];
	for (i = 0; i < N; i++) {
		num[i] = 0;
		tmp[i] = 0;
	}
	num[0]++;
	num[1]++;
	num[sum]++;
	for (i = 3, F[0] = 1, F[1] = 1, cur = 1, prev = 0; i <= M - 2; i++, cur ^= 1, prev ^= 1) {
		F[prev] += F[cur];
		if (F[prev] >= N) F[prev] -= N;
		if (sum == 0 && F[prev] == 0 && F[cur] == 1) {
			if (flag == 0) flag = i;
			else {
				flag = i - flag;
				break;
			}
		}
		sum += F[prev];
		if (sum >= N) sum -= N;
		num[sum]++;
		if (flag > 0) tmp[sum]++;
	}
	if (i <= M - 2) {
		int j, k = (M - 2 - i) / flag;
		for (j = 0; j < N; j++) num[j] += k * tmp[j];
		i += k * flag;
		for (num[sum]++, i++, cur ^= 1, prev ^= 1; i <= M - 2; i++, cur ^= 1, prev ^= 1) {
			F[prev] += F[cur];
			if (F[prev] >= N) F[prev] -= N;
			sum += F[prev];
			if (sum >= N) sum -= N;
			num[sum]++;
		}
	}
	
	long long ans = 0;
	for (i = 0; i < N; i++) ans += (long long)num[i] * (num[i] - 1) / 2;
	F[prev] += F[cur];
	if (F[prev] >= N) F[prev] -= N;
	if (F[prev] == 0) ans++;
	if ((F[prev] + F[cur]) % N == 0) ans += 2;
	return ans;
}

#define MT_N 624
#define MT_M 397
#define MT_MATRIX_A 0x9908b0dfUL
#define MT_UPPER_MASK 0x80000000UL
#define MT_LOWER_MASK 0x7fffffffUL

static unsigned int mt[MT_N];
static int mti = MT_N + 1;

void init_genrand(unsigned int s)
{
    mt[0] = s & 0xffffffffUL;
    for (mti = 1; mti < MT_N; mti++) {
        mt[mti] = (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); 
        mt[mti] &= 0xffffffffUL;
    }
}

unsigned int genrand()
{
    unsigned int y;
    static unsigned int mag01[2] = {0x0UL, MT_MATRIX_A};

    if (mti >= MT_N) {
        int kk;
        if (mti == MT_N + 1) init_genrand(5489UL);
		
        for (kk = 0; kk < MT_N - MT_M; kk++) {
            y = (mt[kk] & MT_UPPER_MASK) | (mt[kk+1] & MT_LOWER_MASK);
            mt[kk] = mt[kk+MT_M] ^ (y >> 1) ^ mag01[y&0x1UL];
        }
        for (; kk < MT_N - 1; kk++) {
            y = (mt[kk] & MT_UPPER_MASK) | (mt[kk+1] & MT_LOWER_MASK);
            mt[kk] = mt[kk+(MT_M-MT_N)] ^ (y >> 1) ^ mag01[y&0x1UL];
        }
        y = (mt[MT_N-1] & MT_UPPER_MASK) | (mt[0] & MT_LOWER_MASK);
        mt[MT_N-1] = mt[MT_M-1] ^ (y >> 1) ^ mag01[y&0x1UL];

        mti = 0;
    }
  
    y = mt[mti++];

    y ^= (y >> 11);
    y ^= (y << 7) & 0x9d2c5680UL;
    y ^= (y << 15) & 0xefc60000UL;
    y ^= (y >> 18);

    return y;
}

int main()
{
	int N, M;
	scanf("%d %d", &N, &M);
	printf("%lld\n", solve(N, M));
	
	/*
	for (N = 1; N <= 10; N++) {
		for (M = 1; M <= 160; M++) {
			if (solve(N, M) != naive(N, M)) printf("%d %d %lld %d\n", N, M, solve(N, M), naive(N, M));
		}
	}
	*/
	fflush(stdout);
	return 0;
}
0