結果

問題 No.58 イカサマなサイコロ
ユーザー 14番14番
提出日時 2016-04-06 22:09:30
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 125 ms / 5,000 ms
コード長 4,696 bytes
コンパイル時間 887 ms
コンパイル使用メモリ 112,216 KB
実行使用メモリ 27,220 KB
最終ジャッジ日時 2024-04-14 22:19:54
合計ジャッジ時間 1,967 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
23,348 KB
testcase_01 AC 125 ms
27,220 KB
testcase_02 AC 35 ms
25,212 KB
testcase_03 AC 33 ms
25,040 KB
testcase_04 AC 35 ms
25,012 KB
testcase_05 AC 35 ms
25,260 KB
testcase_06 AC 45 ms
25,260 KB
testcase_07 AC 34 ms
25,204 KB
testcase_08 AC 35 ms
24,824 KB
testcase_09 AC 40 ms
24,948 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.Collections.Generic;
using System.Text;

public class Program
{
    public void Proc(){
        Reader.IsDebug = false;
        this.DiceCount = int.Parse(Reader.ReadLine());
        Dictionary<int, long> jiroDic = this.GetKumiawase(this.DiceCount, 0);
        int ikasamaCount = int.Parse(Reader.ReadLine());
        Dictionary<int,long> taroDic = this.GetKumiawase(this.DiceCount, ikasamaCount);

        List<int> jiroKeys = new List<int>();
        jiroKeys.AddRange(jiroDic.Keys);
        jiroKeys.Sort();
        
        List<int> taroKeys = new List<int>();
        taroKeys.AddRange(taroDic.Keys);
        taroKeys.Sort();
        
        long winCount = 0;
        long total = 0;
        for(int i=0; i<jiroKeys.Count; i++) {
            total += jiroDic[jiroKeys[i]];
            for(int j=0; j<taroKeys.Count; j++) {
                if(jiroKeys[i] < taroKeys[j]) {
                    winCount += (jiroDic[jiroKeys[i]] * taroDic[taroKeys[j]]); 
                } 
            }
        }
        total = total * total;
        bool mustExit = true;
        do
        {
            mustExit = true;
            for(long i=2; i<=Math.Sqrt(winCount); i++) {
                if(winCount % i == 0 && total % i ==0) {
                    winCount = winCount / i;
                    total = total / i;
                    mustExit = false;
                    break;
                }
            }
            
        } while (!mustExit);
        
        Console.WriteLine((winCount / (double)total).ToString("########################0.#######"));
    }
    
    private int DiceCount;
    
    private Dictionary<int, Dictionary<int, long>> dic = new Dictionary<int,Dictionary<int,long>>();
    
    private Dictionary<int, Dictionary<int, long>> ikasamaDic = new Dictionary<int, Dictionary<int, long>>();
    
    private Dictionary<int, long> GetKumiawase(int cnt, int ikasama) {
        if(ikasama > 0) {
            if(ikasamaDic.ContainsKey(cnt)) {
                return ikasamaDic[cnt];
            }            
        } else
        {
            if(dic.ContainsKey(cnt)) {
                return dic[cnt];
            }
        }
        
        if(cnt == 1) {
            Dictionary<int, long> ret = new Dictionary<int, long>();
            if(ikasama > 0) {
                for(int i=4; i<=6; i++) {
                    ret.Add(i, 2);
                }
                ikasamaDic.Add(cnt, ret);
            } else
            {
                for(int i=1; i<=6; i++) {
                    ret.Add(i, 1);
                }
                dic.Add(cnt, ret);
            }
            return ret;
        }
        
        Dictionary<int, long> ans = new Dictionary<int, long>();
        if(ikasama > 0) {
            for(int i=4; i<= 6; i++) {
                Dictionary<int, long> ret = this.GetKumiawase(cnt - 1, ikasama - 1);
                foreach (int key in ret.Keys)
                {
                    int newKey = key + i;
                    if(ans.ContainsKey(newKey)) {
                        ans[newKey] += ret[key] * 2;
                    } else
                    {
                        ans.Add(newKey, ret[key] * 2);
                    }
                }
            }
            ikasamaDic.Add(cnt, ans);
        } else
        {
            for(int i=1; i<= 6; i++) {
                Dictionary<int, long> ret = this.GetKumiawase(cnt - 1, ikasama);
                foreach (int key in ret.Keys)
                {
                    int newKey = key + i;
                    if(ans.ContainsKey(newKey)) {
                        ans[newKey] += ret[key];
                    } else
                    {
                        ans.Add(newKey, ret[key]);
                    }
                }
            }
            dic.Add(cnt, ans);
        }
        return ans;
    }
    
    public static void Main(string[] args)
    {
        Program prg = new Program();
        prg.Proc();
    }
}

class Reader
{
    public static bool IsDebug = true;
    
    private static System.IO.StringReader sr;
    
    public static string ReadLine() {
        if(IsDebug) {
            if(sr == null) {
                sr = new System.IO.StringReader(initStr.Trim());
            }
            return sr.ReadLine();
        } else {
            return Console.ReadLine();
        }
    }
    
    public static int[] GetInt(char delimiter = ' ') {
        string[] inpt = ReadLine().Split(delimiter);
        int[] ret = new int[inpt.Length];
        for(int i=0; i<inpt.Length; i++) {
            ret[i] = int.Parse(inpt[i]);
        }
        return ret;
    }
    
    private static string initStr = @"


2
2

";
}

0