D - Number theory
给定整数x = 1,您必须应用Q(Q≤100000)运算:乘,除。
输入
输入文件的第一行包含一个整数T(0 <T≤10),表示有多少输入情况。
每种情况的描述如下:
第一行包含两个整数Q和M.下一个Q行包含第i行后面的操作:
我这样做:x = x * do。
N di:x = x / ydi。
确保di不同。这意味着你可以在yi出现之后只划一次yi。
0 <yi≤10^ 9,M≤10^ 9
产量
对于每个操作,打印一个整数(每行一个)x%M。
样本输入
1 10 1000000000 M 2 D 1 M 2 M 10 D 3 D 4 M 6 M 7 M 12 D 7
样本输出
2 1 2 20 10 1 6 42 504 84
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<string.h>
#include<stdlib.h>
const double PI=acos(-1);
const int INF=1e9+7;
const int xmax=1e5;
const int xmin=-1e6+7;
typedef long long ll;
using namespace std;
ll tree[xmax*4];
ll v,mod;
void update(int l,int r,int root,int k)
{
if(l==r)
{
tree[root]=v;
return ;
}
int m=(l+r)/2;
if(k<=m)
update(l,m,root*2,k);
else
update(m+1,r,root*2+1,k);
tree[root]=tree[root*2]*tree[root*2+1]%mod;
}
int main()
{
int q,x;
int t;
char s[10];
scanf("%d",&t);
while(t--)
{
for(int i=1; i<=xmax*4; i++)
tree[i]=1;
scanf("%d%lld",&q,&mod);
for(int i=1; i<=q; i++)
{
scanf("%s%d",s,&x);
if(s[0]=='M')
{
v=x;
update(1,xmax,1,i);
printf("%lld\n",tree[1]);
}
else if(s[0]=='D')
{
v=1;
update(1,xmax,1,x);
printf("%lld\n",tree[1]);
}
}
}
return 0;
}