Showing posts with label Sum The Series solution in c. Show all posts
Showing posts with label Sum The Series solution in c. Show all posts

Tuesday, 15 December 2020

Sum The Series Problem Code: SUM1 codechef solution in c

https://www.codechef.com/problems/SUM1 

Explanation:



In the Question, it is mentioned to do %1000000007, to ensure the answer will always a number from 0 to 1000000006 

As n can take a value upto 10^16 in the question and n^2 cannot fit in even long long int, we cannot do n*n and then apply %1000000007

So we cannot do (n*n)%1000000007 so we do like this

((n%1000000007)*(n%1000000007))%1000000007

According to modular arithmetic (a*b)%m = ((a%m)*(b%m))%m

Here is the solution:

#include<stdio.h>

#define MOD 1000000007

int main() 

{

int t;

scanf("%d",&t);

while(t--)

{

    long long int n;

    scanf("%lld",&n);

    printf("%lld\n",(((n%MOD)*(n%MOD))%MOD));

}

return 0;

}