Spoj PTIME - Prime Time Solution


 

Problem  Link :  https://www.spoj.com/problems/PTIME/

Solution in C++:

///La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
///Abul Hasnat  Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
///**********ALLAH IS ALMIGHTY************///
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
const ll N=1e6;
vector<ll>prime;
ll i,j;
bool vis[N];
void sieve()
{
    for(i=3; i*i<=N; i+=2)
    {
        if(vis[i]==0)
        {
            for( j=i*i; j<=N; j+=2*i)
            {
                vis[j]=1;
            }
        }
    }
    prime.push_back(2);
    for(i=3; i<=N; i+=2)
    {
        if(vis[i]==0)
        {
            prime.push_back(i);
        }
    }
}
int main()
{
    ll t,n,i,mx,cnt,cn,cp;
    sieve();
    cin>>n;
    bool flag=true;
    for(i=0; prime[i]<=n; i++)
    {
        int sum=0, num=n;
        while(num>0)
        {
            sum+=num/prime[i];
            num/=prime[i];
        }
        if(flag)
        {
            flag=false;
            printf("%d^%d",prime[i],sum);
        }
        else printf(" * %d^%d",prime[i],sum);
    }
}



No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

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

Powered by Blogger.