結果
問題 | No.1747 Many Formulae 2 |
ユーザー |
![]() |
提出日時 | 2021-11-19 23:02:33 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 30 ms / 2,000 ms |
コード長 | 1,826 bytes |
コンパイル時間 | 1,425 ms |
コンパイル使用メモリ | 118,420 KB |
実行使用メモリ | 27,720 KB |
最終ジャッジ日時 | 2025-01-01 21:27:29 |
合計ジャッジ時間 | 3,073 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 19 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System.Linq; using static System.Math; using System.Collections.Generic; using System; public class P { public string t { get; set; } public int p { get; set; } } public class Hello { static void Main() { var s = Console.ReadLine().Trim(); getAns(s); } static void getAns(string s) { var sL = s.Length; if (sL == 1) { Console.WriteLine(IsPrime(int.Parse(s)) ? 1 : 0); return; } var aa = new List<long>(); var q = new Queue<P>(); var a = ""; q.Enqueue(new P { t = a + s[0], p = 0 }); q.Enqueue(new P { t = a + s[0] + ",", p = 0 }); while (q.Count() > 0) { var w = q.Dequeue(); if (w.p == sL - 1) { string[] line = w.t.Split(','); var ww = Array.ConvertAll(line, long.Parse).Sum(); aa.Add(ww); continue; } else { if (w.p == sL - 2) { q.Enqueue(new P { t = w.t + s[w.p + 1], p = w.p + 1 }); } else { q.Enqueue(new P { t = w.t + s[w.p + 1], p = w.p + 1 }); q.Enqueue(new P { t = w.t + s[w.p + 1] + ",", p = w.p + 1 }); } } } var count = 0; foreach (var x in aa) if (IsPrime(x)) count++; Console.WriteLine(count); } public static bool IsPrime(long n) { if (n < 2) return false; else if (n == 2) return true; else if (n % 2 == 0) return false; double sqrtNum = Sqrt(n); for (int i = 3; i <= sqrtNum; i += 2) if (n % i == 0) return false; return true; } }