結果
問題 |
No.462 6日知らずのコンピュータ
|
ユーザー |
![]() |
提出日時 | 2016-12-13 03:56:49 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,667 bytes |
コンパイル時間 | 934 ms |
コンパイル使用メモリ | 109,568 KB |
実行使用メモリ | 177,192 KB |
最終ジャッジ日時 | 2024-11-30 00:32:14 |
合計ジャッジ時間 | 24,179 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 79 TLE * 5 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System; using System.Linq; using System.Collections.Generic; using System.Text; public class Program { public void Proc() { Reader.IsDebug = false; int[] inpt = Reader.ReadLine().Split(' ').Select(a=>int.Parse(a)).ToArray(); this.Length = inpt[0]; if(inpt[1] == 0) { long ans = 1; for(int i=2; i<=this.Length; i++) { ans*=i; ans = ans % MOD; } Console.WriteLine(ans); } else { this.NumList = Reader.ReadLine().Split(' ').Select(a=>long.Parse(a)).OrderBy(a=>a).ToArray(); long ans = this.GetAns(0,0); Console.WriteLine(ans); } } private Dictionary<long, long> dic = new Dictionary<long, long>(); private long GetAns(long selected, long flags) { if(dic.ContainsKey(selected)) { return dic[selected]; } long flags2 = flags; for(int i=0; i<this.NumList.Length; i++) { long key = (long)1<<i; if((flags2&key) == key) { continue; } if(selected == this.NumList[i]) { flags2+=key; continue; } if(selected > this.NumList[i]) { dic[selected] = 0; return 0; } string selStr = Convert.ToString(selected, 2); string numStr = Convert.ToString(this.NumList[i], 2); selStr = selStr.PadLeft(numStr.Length, '0'); bool flag = true; for(int j=numStr.Length -1; j>=0; j--) { if(numStr[j] == '0' && selStr[j] != '0') { flag = false; break; } } if(!flag) { dic[selected] = 0; return 0; } } if(selected == ((long)1<<this.Length)-1) { if(flags2 == ((long)1<<this.NumList.Length)-1) { dic[selected] = 1; } else { dic[selected] = 0; } return dic[selected]; } if(flags2 == ((long)1<<this.NumList.Length)-1) { int cnt = 0; for(int i=0; i<this.Length; i++) { long key = (long)1<<i; if((selected&key)==0) { cnt++; } } long ret = 1; for(int i=1; i<=cnt; i++) { ret*=i; ret = ret % MOD; } dic[selected] = ret; return ret; } long ans = 0; for(int i=0; i<this.Length; i++) { long key = (long)1<<i; if((selected&key)>0) { continue; } ans+=this.GetAns(selected+key, flags2); ans = ans % MOD; } this.dic[selected] = ans; return ans; } private const long MOD = 1000000000 + 7; private int Length = 0; private long[] NumList; public class Reader { public static bool IsDebug = true; private static System.IO.StringReader SReader; private static string InitText = @" 60 1 8 "; public static string ReadLine() { if(IsDebug) { if(SReader == null) { SReader = new System.IO.StringReader(InitText.Trim()); } return SReader.ReadLine(); } else { return Console.ReadLine(); } } } public static void Main(string[] args) { Program prg = new Program(); prg.Proc(); } }