hdu 5475 An easy problem 2015上海网络赛 模拟 剪枝

题目

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5475

题目来源:上海网络赛的第二题

简要题意:对一个数操作可以乘 y 或者取消之前某次乘的y求结果模 M

数据范围:1Q105;1M109;0<y109

题解

其实理论上这道题应该是用线段树做的。

但是正所谓暴力出奇迹,随便写了个暴力剪枝就过了。

实现

剪枝的话我的方法就是分情况讨论,对于形成区间的直接循环乘上。

对于不会被消除的可以再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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值