0% found this document useful (0 votes)
5 views2 pages

Math Day Contest Problem

The document describes a programming problem called 'Math Day' where participants must compute A raised to the factorial of N modulo P for multiple test cases. It includes constraints for the input values and provides a sample input and output. The solution involves implementing a modular exponentiation function to handle large numbers efficiently.

Uploaded by

ravi sanjeevan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Math Day Contest Problem

The document describes a programming problem called 'Math Day' where participants must compute A raised to the factorial of N modulo P for multiple test cases. It includes constraints for the input values and provides a sample input and output. The solution involves implementing a modular exponentiation function to handle large numbers efficiently.

Uploaded by

ravi sanjeevan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

@csalgo

Problem Name: Math Day


Problem Difficulty: None
Problem Constraints: 1 ≤ T ≤ 100
1 ≤ A ≤ 10^5.
1 ≤ N ≤ 10^5.
1 ≤ P ≤ 10^5.
Problem Description:
Math Day is being celebrated at Coding Blocks. So Prateek Bhaiya rolled out a
contest on Maths Problems. Here goes one.<br/> Given three positive integers
<b>A,N,P</b>. Compute <b>A<sup>N!</sup> %P</b>.<br/><br/>

Input Format: The first line of the input gives the number of test cases, <b>T</b>.
<b>T</b> lines follow.
<br/> Each line contains three integers <b>A, N</b> and <b>P</b>, as described
above.
Sample Input: 2
2 1 2
3 3 2
Output Format: For every test case, print the answer in a new line.
Sample Output: 0
1

=====Solution=====
#include<bits/stdc++.h>
#include<unordered_set>
using namespace std;
#define fio ios_base::sync_with_stdio(false)

#define ll long long int

#define s(x) scanf("%lld",&x)


#define s2(x,y) s(x)+s(y)
#define s3(x,y,z) s(x)+s(y)+s(z)

#define p(x) printf("%lld\n",x)


#define p2(x,y) p(x)+p(y)
#define p3(x,y,z) p(x)+p(y)+p(z)
#define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++)
#define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--)

#define ff first
#define ss second
#define mp(x,y) make_pair(x,y)
#define pll pair<ll,ll>
#define pb push_back

ll inf = 1e18;
ll mod = 1e9 + 7 ;
ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);}

/****************************************************************************/

ll modExp(ll a,ll e,ll mod){


if(e==0)return 1ll;
if(a==0) return 0ll;

ll pro=1;
while(e>0){
if(e&1)pro=(pro*a)%mod;
a=(a*a)%mod;
e=e/2;
}
return pro;
}

int main()
{
// freopen("[Link]","r",stdin);
// freopen("[Link]","w",stdout);
ll t=1;
s(t);
while(t--){
ll a,n,pp;
s3(a,n,pp);
ll ans = a;
F(i,1,n)ans=modExp(ans,i,pp);
cout<<ans<<endl;
}
return 0;
}

@csalgo Telegram

You might also like