题目
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5475
题目来源:上海网络赛的第二题
简要题意:对一个数操作可以乘 y 或者取消之前某次乘的
y 求结果模 M数据范围:
1⩽Q⩽105;1⩽M⩽109;0<y⩽109
题解
其实理论上这道题应该是用线段树做的。
但是正所谓暴力出奇迹,随便写了个暴力剪枝就过了。
实现
剪枝的话我的方法就是分情况讨论,对于形成区间的直接循环乘上。
对于不会被消除的可以再for一遍直接线性处理掉。
消除的操作就不必管了,并到区间处理里了。
代码
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
LL MOD;
const int N = 1E5+5;
struct Node {
LL v;
int be, en;
Node(LL v, int be, int en) : v(v), be(be), en(en) {}
Node() {}
};
Node a[N];
LL res[N];
int main()
{
int t, u, v, cas = 1, n;
scanf("%d", &t);
while (t--) {
scanf("%d%I64d", &n, &MOD);
int cnt = 0;
for (int i = 1; i <= n; i++) {
scanf("%d%d", &u, &v);
if (u == 1) {
a[++cnt] = Node(v, i, -1);
} else {
a[++cnt] = Node(-1, -1, -1);
a[v].en = i;
}
res[i] = 1;
}
for (int i = 1; i <= cnt; i++) {
if (a[i].en != -1) {
for (int j = a[i].be; j < a[i].en; j++) {
res[j] = res[j]*a[i].v%MOD;
}
}
}
int x = 1;
LL mul = 1;
for (int i = 1; i <= n; i++) {
while (x <= cnt && (a[x].en != -1 || a[x].v == -1)) {
x++;
}
if (x <= cnt && a[x].be == i) {
mul = mul*a[x++].v%MOD;
}
res[i] = res[i]*mul%MOD;
}
printf("Case #%d:\n", cas++);
for (int i = 1; i <= n; i++) {
printf("%I64d\n", res[i]);
}
}
return 0;
}

本文探讨了一种通过暴力剪枝方法解决特定数论问题的技术,具体涉及对数值进行乘法操作并考虑取消操作的情况。文章详细介绍了算法实现步骤及优化策略,旨在提供一种高效解决方案。

1191

被折叠的 条评论
为什么被折叠?



