Codeforces 472A. Design Tutorial: Learn from Math 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;
            }
        }
    }
    for(int i=4; i<=n; i+=2)
    {
        if(marked[i]==true)
        {
            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;
        for(i=0; i<(vec.size()); i++)
        {
            r=(m-vec[i]);

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

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

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.