【线段树】Number theory

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值