結果

問題 No.6 使いものにならないハッシュ
ユーザー バカらっく
提出日時 2017-06-25 16:47:31
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 62 ms / 5,000 ms
コード長 2,720 bytes
コンパイル時間 2,130 ms
コンパイル使用メモリ 109,312 KB
実行使用メモリ 23,100 KB
最終ジャッジ日時 2024-09-16 16:39:38
合計ジャッジ時間 4,383 ms
ジャッジサーバーID
(参考情報)
judge1 / judge6
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Text;

public class Program
{

    public void Proc()
    {
        int fromNum = int.Parse(Reader.ReadLine());
        int toNum = int.Parse(Reader.ReadLine());
        SetPrimeList(toNum);
        int[] flags = new int[10].Select(a=>-1).ToArray();
        int maxlen = 0;
        int firstPrime = 0;
        for (int i = 0; i < PrimeList.Length; i++) {
            if (PrimeList[i] < fromNum) {
                continue;
            }
            if(PrimeList[i]>toNum) {
                if (i - flags.Where(a=>a>=0).Min()>=maxlen) {
                    firstPrime = PrimeList[flags.Where(a => a >= 0).Min()];
                }
                break;
            }

            int prime = PrimeList[i];
            int hash = GetHash(prime);
            if(flags[hash]>=0) {
                int idx = flags.Where(a => a >= 0).Min();
                int len = flags.Where(a => a >= 0).Max() - idx + 1;
				if (maxlen <= len)
				{
					maxlen = len;
					firstPrime = PrimeList[idx];
				}
				int target = flags[hash];
				for (int j = 0; j < flags.Length; j++)
				{
					if (flags[j] <= target)
					{
						flags[j] = -1;
					}
				}
			}
            flags[hash] = i;
        }
        int lastIdx = flags.Where(a => a >= 0).Min();
        int lastLen = flags.Where(a => a >= 0).Max() - lastIdx + 1;
        if(maxlen <= lastLen) {
            firstPrime = PrimeList[lastIdx];
        }
        Console.WriteLine(firstPrime);
    }

    private int GetHash(int num) {
        int tmp = num;
        while(tmp >= 10) {
            tmp = tmp.ToString().Select(a => int.Parse(a.ToString())).Sum();
        }
        return tmp;
    }

    private void SetPrimeList(int num) {
        bool[] flags = new bool[num + 1];
        List<int> tmp = new List<int>();
        for (int i = 2; i <= num; i++) {
            if (flags[i]) {
                continue;
            }
            tmp.Add(i);
            int max = num / i;
            for (int j = 1; j <= max; j++) {
                flags[i * j] = true;
            }
        }
        this.PrimeList = tmp.ToArray();
    }
    private int[] PrimeList;

    public class Reader
	{
		private static StringReader sr;
		public static bool IsDebug = false;
		public static string ReadLine()
		{
			if (IsDebug)
			{
				if (sr == null)
				{
					sr = new StringReader(InputText.Trim());
				}
				return sr.ReadLine();
			}
			else
			{
				return Console.ReadLine();
			}
		}
		private static string InputText = @"




1
11




";
	}

	public static void Main(string[] args)
	{
#if DEBUG
		Reader.IsDebug = true;
#endif
		Program prg = new Program();
		prg.Proc();
	}
}
0