I tried to figure out every test case covered …but still getting wrong answer …please point out my mistake …
this is the problem statement
my solution
I tried to figure out every test case covered …but still getting wrong answer …please point out my mistake …
this is the problem statement
my solution
Try this test case:
3
2 2 2
2
1 3 5
1 3 1000000
It give 1 6 whereas the actual output is 3 8. Hope it helps.
I am not getting the missed test case …
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
long long int fast_exp(long long int ,long long int );
int a[100001][101];
long long int mod;
int main()
{
int n;
scanf("%d",&n);
int temp;
for(int i=0;i<=100000;i++)
{
for(int j=0;j<=100;j++)
{
a[i][j]=0;
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=100;j++)
a[i][j]=a[i][j]+a[i-1][j];
scanf("%d",&temp);
a[i][temp]++;
}
int left,right;
int t;
scanf("%d",&t);
long long int ans;
long long int x;
while(t--)
{
ans=1;
scanf("%d",&left);
scanf("%d",&right);
scanf("%d",&mod);
if(left==right)
{
for(int i=1;i<=100;i++)
{
ans=(ans*fast_exp(i,a[i]))%mod;
//ans=ans%mod;
}
}
else{
for(int i=1;i<=100;i++)
{
x=a[right][i]-a[left-1][i];
if(x>0)
{
ans=(ans*fast_exp(i,x))%mod;
//ans=ans%mod;
//cout<<"jhfdvbjfd"<<ans<<"\n";
}
}
}
printf("%lld\n",ans);
}
return 0;
}
long long int fast_exp(long long int base, long long int exp) {
long long int res=1;
while(exp>0) {
if(exp%2==1) res=(res*base)%mod;
base=(base*base)%mod;
exp/=2;
}
return res%mod;
}
/*
5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
4
1 2 3
2 3 4
1 1 1
1 5 1000000000
*/
Try using scanf("%lld",&mod);
instead of scanf("%d",&mod);
The variable mod
is declared as a long long int
.
vivek07671…in your code this statement is wrong…
if(left==right)
{
for(int i=1;i<=100;i++)
{
ans=(ans*fast_exp(i,a[i]))%mod; //Wrong statement
}
}
Just try this input on your code
5
5 5 5 5 5
1
3 3 100
The answer should be 5 but your code will give 125…
The correct statement is
ans=(ans*fast_exp(i,a[i]-a[left-1)[i])%mod;