Lightoj 1007 – Mathematically Hard Solution

Solution in C++:

///**********ALLAH IS ALMIGHTY************///

///AH Tonmoy

///Department of CSE,23rd batch

///Islamic University,Bangladesh  

#include<bits/stdc++.h>

#define m 5000000

using namespace std;

int p[m+1];

unsigned long long  s[m+1];

int  i,j;

void cphi()

{

    for(i=2; i<=m; i++)

    {

        p[i]=i;

    }

    for(i=2; i<=m; i++)

    {

        if(p[i]==i)

        {

            for(j=i; j<=m; j+=i)

            {

                p[j]-=p[j]/i;

            }

        }

    }

}

void sum()

{

    s[1]=0;

    for(i=2; i<=m; i++)

    {

        s[i]=p[i];

        s[i]*=p[i];

        s[i]+=s[i-1];

    }

}

int main()

{

    cphi();

    sum();

   int  t,a,b;

    cin>>t;

    for(i=1; i<=t; i++)

    {

        cin>>a>>b;

        printf("Case %d: %llu\n",i,s[b]-s[a-1]);

    }


}


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.