結果

問題 No.843 Triple Primes
ユーザー tarattata1
提出日時 2019-06-29 12:20:37
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 1,299 bytes
コンパイル時間 914 ms
コンパイル使用メモリ 74,432 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-19 14:10:44
合計ジャッジ時間 2,170 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 42
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main(int, char**)’:
main.cpp:48:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   48 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~

ソースコード

diff #

#include <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <list>
#include <iterator>
#include <assert.h>
#pragma warning(disable:4996) 

typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
using namespace std;

vector<int> pp;
vector<int> a;

void preparePrime( int maxp )
{
    int i;
    a.resize(maxp+1);
    for(i=2; i<=maxp; i++) {
        int k=i+i;
        while(k<=maxp) {
            a[k]=1;
            k+=i;
        }
    }
    for(i=2; i<=maxp; i++) {
        if(a[i]==0) pp.push_back( i );
    }
    return;
}


int main(int argc, char* argv[])
{
    int n;
    scanf("%d", &n);
    preparePrime( n );

    int cnt=0;
    int i, j;

    double lim=float(sqrt((double)n*2))+0.1;
    for(i=2; i<=lim; i++) {
        if(a[i]) continue;
        int k = i*i;
        for(j=0; j<(int)pp.size(); j++) {
            int val=k-pp[j];
            if(val>n) continue;
            if(val<2) break;
            if (a[val]==0) {
                cnt++;
            }
        }        
    }
    printf("%d\n", cnt);


    return 0;
}
0