Problem Description
One day, a useless calculator was being built by Kuros. Let’s assume that number X is showed on the screen of calculator. At first, X = 1. This calculator only supports two types of operation.
1. multiply X with a number.
2. divide X with a number which was multiplied before.
After each operation, please output the number X modulo M.
Input
The first line is an integer T(1≤T≤10), indicating the number of test cases.
For each test case, the first line are two integers Q and M. Q is the number of operations and M is described above. (1≤Q≤105,1≤M≤109)
The next Q lines, each line starts with an integer x indicating the type of operation.
if x is 1, an integer y is given, indicating the number to multiply. (0
#include<stdio.h>
#include<string.h>
#define ll long long
ll a[555555];
int main()
{
int t;
scanf("%d",&t);
int c=1;
while(t--)
{
int n;
ll m;
scanf("%lld%lld",&n,&m);
ll ans=1;
printf("Case #%d:\n",c++);
for(int i=1;i<=n;i++)
{
int z;
scanf("%d %lld",&z,&a[i]);
if(z==1)
{
ans=ans*a[i]%m;
}
else
{
a[a[i]]=1;
a[i]=1;
ans=1;
for(int j=1;j<i;j++)
ans=ans*a[j]%m;
}
printf("%lld\n",ans);
}
}
}