FZU 2297 Number theory (线段树维护区间乘积)

本文探讨了在给定的整数操作中,包括乘法和除法,如何有效地进行模运算。通过使用一种特殊的数据结构,我们能够在一系列操作后快速得到结果对特定数值取模后的结果。此方法特别适用于大量运算场景,如算法竞赛和高性能计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given a integers x = 1, you have to apply Q (Q ≤ 100000) operations: Multiply, Divide.

Input

First line of the input file contains an integer T(0 < T ≤ 10) that indicates how many cases of inputs are there.

The description of each case is given below:

The first line contains two integers Q and M. The next Q lines contains the operations in ith line following form:

M yi: x = x * yi.

N di: x = x / ydi.

It’s ensure that di is different. That means you can divide yi only once after yi came up.

0 < yi ≤ 10^9, M ≤ 10^9

Output

For each operation, print an integer (one per line) x % M.

Sample Input

1
10 1000000000
M 2
D 1
M 2
M 10
D 3
D 4
M 6
M 7
M 12
D 7

Sample Output

2
1
2
20
10
1
6
42
504
84

 

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int maxn = 1e5+1;
ll tree1[maxn*4];
ll value;
ll mod;
void update(int l,int r,int root,int k)
{
	if(l==r)
	{
		tree1[root]=value;
		return;
	}
	ll mid=(l+r)/2;
	if(k<=mid)
		update(l,mid,root*2,k);
	else
		update(mid+1,r,root*2+1,k);
	tree1[root]=tree1[root*2]*tree1[root*2+1]%mod;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		int k;
		scanf("%d %lld",&n,&mod);
		char s[100];
		for(ll i=1; i<=maxn*4; i++)
			tree1[i]=1;
		for(int i=1; i<=n; i++)
		{
			scanf("%s",s);
			scanf("%d",&k);
			if(s[0]=='M')
			{
				value=k;
				update(1,maxn,1,i);
			}
			else
			{
				value=1;
				update(1,maxn,1,k);
			}
			printf("%lld\n",tree1[1]);
		}

	}


	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值