結果

問題 No.6 使いものにならないハッシュ
ユーザー mudbdbmudbdb
提出日時 2015-06-13 15:30:30
言語 C90
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,887 bytes
コンパイル時間 751 ms
コンパイル使用メモリ 26,672 KB
実行使用メモリ 4,484 KB
最終ジャッジ日時 2023-10-14 22:52:25
合計ジャッジ時間 1,908 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 3 ms
4,352 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 1 ms
4,352 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,484 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,352 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 3 ms
4,348 KB
testcase_18 AC 3 ms
4,348 KB
testcase_19 AC 3 ms
4,352 KB
testcase_20 AC 2 ms
4,352 KB
testcase_21 AC 1 ms
4,348 KB
testcase_22 AC 3 ms
4,356 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,352 KB
testcase_26 AC 2 ms
4,352 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 3 ms
4,352 KB
testcase_31 AC 2 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>
int hash(int N)
{
  int work = N;
  int hash = 0;
  int base = 10;
  while (1) {
    while (work != 0) {
      hash += (work % base);
      work /= base;
    }
    if (hash >= base) {
      work = hash;
      hash = 0;
    } else {
      break;
    }
  }
  return hash;
}
int Pr_begin = 0;
int Pr_end = 0;
int *Pr = 0;
int pr_gen(int K, int N)
{
  int i, j;
  char *chk = (char*)malloc(sizeof(char)*(N+1));
  for (i=0; i<=N; i++) chk[i] = 1;
  chk[0] = 0;
  chk[1] = 0;
  for (i=2; i<=N; i++) {
    if (chk[i] == 1) {
      for (j=i*2; j<=N; j+=i) chk[j] = 0;
      Pr_end++;
    }
  }
  Pr = (int*)malloc(sizeof(int)*Pr_end);
  Pr_end = 0;
  for (i=0; i<=N; i++) {
    if (chk[i] == 1) {
      Pr[Pr_end] = i;
      Pr_end++;
    }
  }
  free(chk);
  chk = 0;

  if (K <= Pr[0]) {
    Pr_begin = 0;
  } else {
    for (i=0; i<Pr_end-1; i++) {
      if ((Pr[i] < K)&&(K <= Pr[i+1])) break;
    }
    Pr_begin = i+1;
  }
  return 0;
}
int main()
{
  int K;
  int N;
  scanf("%d", &K);
  scanf("%d", &N);

  pr_gen(K, N);

  int i;
  int pr_n = Pr_end - Pr_begin;
  int *key = (int*)malloc(sizeof(int)*pr_n);
  for (i=0; i<pr_n; i++) key[i] = hash(Pr[Pr_begin + i]);

  int *count = (int*)malloc(sizeof(int)*pr_n);
  for (i=0; i<pr_n; i++) count[i] = 0;
  int begin;
  int end;
  int match = 0;
  for (begin = 0; begin < pr_n-1; ) {
    for (end = begin+1; end < pr_n; end++) {
      for (i=begin; i<end; i++) {
        if (key[i] == key[end]) {
          match = 1;
          goto BRK;
        }
      }
    }
    BRK:
    count[begin] = end - begin;
    if (match == 1) {
      begin = i+1;
      match = 0;
    } else {
      break;
    }
  }
  count[pr_n-1] = 1;

  int max = 0;
  int pr_i;
  for (i=0; i<pr_n; i++) {
    if (max <= count[i]) {
      max = count[i];
      pr_i = i;
    }
  }
  printf("%d\n", Pr[Pr_begin + pr_i]);
  return 0;
}
0