結果

問題 No.1747 Many Formulae 2
ユーザー bluemeganebluemegane
提出日時 2021-11-19 23:02:33
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 67 ms / 2,000 ms
コード長 1,826 bytes
コンパイル時間 2,839 ms
コンパイル使用メモリ 67,924 KB
実行使用メモリ 23,588 KB
最終ジャッジ日時 2023-08-30 10:05:47
合計ジャッジ時間 5,049 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
21,496 KB
testcase_01 AC 67 ms
19,500 KB
testcase_02 AC 64 ms
21,460 KB
testcase_03 AC 63 ms
21,532 KB
testcase_04 AC 64 ms
21,548 KB
testcase_05 AC 64 ms
21,432 KB
testcase_06 AC 63 ms
21,588 KB
testcase_07 AC 65 ms
21,448 KB
testcase_08 AC 64 ms
23,508 KB
testcase_09 AC 65 ms
21,544 KB
testcase_10 AC 66 ms
23,588 KB
testcase_11 AC 65 ms
21,412 KB
testcase_12 AC 64 ms
21,456 KB
testcase_13 AC 65 ms
21,564 KB
testcase_14 AC 64 ms
19,452 KB
testcase_15 AC 66 ms
23,476 KB
testcase_16 AC 65 ms
21,536 KB
testcase_17 AC 58 ms
21,360 KB
testcase_18 AC 60 ms
21,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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;
    }

}
0