Codeforces 755A. A. PolandBall and Hypothesis Solution


Solve in C++:

//AH Tonmoy
//Department of CSE ,Islamic University

#include<bits/stdc++.h>
#define M 1000009
using namespace std;
bool marked[M];
vector<int>vec;
void sieve(int n)
{
    for(int i=4; i<=n; i+=2)
        marked[i]=true;
    for(int i=3; i*i<=n; i+=2)
    {
        if(marked[i]==false)
        {
            for(int j=i*i; j<=n; j+=i)
            {
                marked[j]=true;
            }
        }
    }
    vec.push_back(2);
    for(int i=3; i<=n; i+=2)
    {
        if(marked[i]==false)
        {
            vec.push_back(i);

        }
    }
}
main()
{
    int m,r,f,i;
    sieve(1000009);
    while(scanf("%d",&m)!=EOF)
    {
        int i,j,f=0;
        if(m==0)
            break;
        else if(m==1)
        cout<<"3"<<endl;
        else if(m==2)
        cout<<"4"<<endl;
        for(i=1; i<=m; i++)
        {
            r=m*i+1;

            if(marked[r]==true)
            {
                  f=1;
                  break;
            }
            if( f==1)
                break;

        }
        if( f==1)
            printf("%d\n",i);
    }
}

No comments

Most View Post

Recent post

Codeforces Round 925 (Div. 3) 1931D. Divisible Pairs Solution

    Problem Link  :   https://codeforces.com/contest/1931/problem/D S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. ...

Powered by Blogger.