結果

問題 No.713 素数の和
ユーザー Konton7Konton7
提出日時 2020-01-13 18:29:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 808 bytes
コンパイル時間 2,210 ms
コンパイル使用メモリ 202,808 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-23 09:11:45
合計ジャッジ時間 2,770 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:41:13: 警告: ‘ans’ may be used uninitialized [-Wmaybe-uninitialized]
   41 |     cout << ans << endl;
      |             ^~~
main.cpp:35:12: 備考: ‘ans’ はここで定義されています
   35 |     int n, ans;
      |            ^~~

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

void shave(vector<int> &v, int n)
{
    vector<int> x, y;
    int n_sq = sqrt(n) + 1;
    for (int i = 3; i < n + 1; i += 2)
    {
        x.push_back(i);
    }
    for (int i = 3; i < n_sq; i += 2)
    {
        for (auto itr = x.begin(); itr != x.end(); itr++)
        {
            if (*itr == i || *itr % i != 0)
                y.push_back(*itr);
        }
        x.clear();
        x = y;
        y.clear();
    }
    if (n >= 2)
        x.insert(x.begin(), 2);

    for (auto itr = x.begin(); itr != x.end(); itr++)
    {
        v.push_back(*itr);
    }
}

int main(){
    int n, ans;
    cin >> n;
    vector<int> primes;
    shave(primes, n);
    for (int x : primes)
        ans += x;
    cout << ans << endl;

    return 0;
}
0