結果

問題 No.843 Triple Primes
ユーザー zekezeke
提出日時 2019-09-24 16:01:47
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 2,615 bytes
コンパイル時間 835 ms
コンパイル使用メモリ 93,752 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 08:54:42
合計ジャッジ時間 2,357 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 9 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 8 ms
4,348 KB
testcase_08 AC 8 ms
4,348 KB
testcase_09 AC 9 ms
4,348 KB
testcase_10 AC 7 ms
4,348 KB
testcase_11 AC 8 ms
4,348 KB
testcase_12 AC 9 ms
4,348 KB
testcase_13 AC 8 ms
4,348 KB
testcase_14 AC 8 ms
4,348 KB
testcase_15 AC 8 ms
4,348 KB
testcase_16 AC 8 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 3 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 6 ms
4,348 KB
testcase_23 AC 6 ms
4,348 KB
testcase_24 AC 4 ms
4,348 KB
testcase_25 AC 4 ms
4,348 KB
testcase_26 AC 9 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 8 ms
4,348 KB
testcase_29 AC 4 ms
4,348 KB
testcase_30 AC 8 ms
4,348 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 4 ms
4,348 KB
testcase_34 AC 5 ms
4,348 KB
testcase_35 AC 9 ms
4,348 KB
testcase_36 AC 3 ms
4,348 KB
testcase_37 AC 7 ms
4,348 KB
testcase_38 AC 6 ms
4,348 KB
testcase_39 AC 8 ms
4,348 KB
testcase_40 AC 1 ms
4,348 KB
testcase_41 AC 2 ms
4,348 KB
testcase_42 AC 8 ms
4,348 KB
testcase_43 AC 8 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cassert>
#include <algorithm>
#include <functional>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <map>
#include <iomanip>
#include <limits>
using ll = long long;
using ld = long double;
int MOD = 1e9 + 7;
using namespace std;
struct UnionFind
{
    vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2

    UnionFind(int N) : par(N)
    { //最初は全てが根であるとして初期化
        for (int i = 0; i < N; i++)
            par[i] = i;
    }

    int root(int x)
    { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}
        if (par[x] == x)
            return x;
        return par[x] = root(par[x]);
    }

    void unite(int x, int y)
    {                     // xとyの木を併合
        int rx = root(x); //xの根をrx
        int ry = root(y); //yの根をry
        if (rx == ry)
            return;   //xとyの根が同じ(=同じ木にある)時はそのまま
        par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける
    }

    bool same(int x, int y)
    { // 2つのデータx, yが属する木が同じならtrueを返す
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
};
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
vector<vector<char>> vec;
vector<vector<int>> reg;
int h, w;
void dfs(int y, int x, int temp, char color)
{
    reg[y][x] = temp;
    color = vec[y][x];
    for (int i = 0; i < 4; i++)
    {
        if (y + dy[i] >= 0 && y + dy[i] < h && x + dx[i] >= 0 && x + dx[i] < w && reg[y + dy[i]][x + dx[i]] == -1 && vec[y + dy[i]][x + dx[i]] != color)
        {
            dfs(y + dy[i], x + dx[i], temp, color);
        }
    }
    return;
}
int main()
{
    int n;
    cin >> n;
    vector<int> prime;
    int m = n;
    vector<bool> reg(n + 1, true);
    for (int i = 2; i <= n; i++)
    {
        if (reg[i])
        {
            prime.push_back(i);
            int k = 1;
            while (1)
            {
                reg[k * i] = false;
                k++;
                if (k * i > n)
                {
                    break;
                }
            }
        }
    }
    int c = 0;
    for (int i = 0; i < prime.size(); i++)
    {
        if (binary_search(prime.begin(), prime.end(), pow(prime[i], 2) - 2))
        {
         //   cout << prime[i] << endl;
            c++;
            if (pow(prime[i], 2) - 2!=2){
                c++;
            }
        }
    }
    cout << c << endl;
}
0