An easy problem HDU5475(暴力)

An easy problem HDU5475

 One day, a useless calculator was being built by Kuros. Let's assume that number X is showed on the screen of calculator. At first, X = 1. This calculator only supports two types of operation.
1. multiply X with a number.
2. divide X with a number which was multiplied before.
After each operation, please output the number X modulo M.

Input
The first line is an integer T(1≤T≤10), indicating the number of test cases.
For each test case, the first line are two integers Q and M. Q is the number of operations and M is described above. (1≤Q≤105,1≤M≤109)
The next Q lines, each line starts with an integer x indicating the type of operation.
if x is 1, an integer y is given, indicating the number to multiply. (0

题意:

题意:X=1,有两种操作:

①X乘上一个整数;

②X除以第n个操作乘过的数。

要求每次操作之后,输出X%M的值

思路:

暴力

乘法取模直接算,除法的时候从头到尾从1再开始乘以遍出去第n个操作的那个数不乘

code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
int a[N];
bool vis[N];
int main(){
    int T,i,j,n,m,x,y,cas = 0;
    ll s;
    scanf("%d",&T);
    while(T--){
        s = 1;
        memset(vis,false,sizeof(vis));
        scanf("%d%d",&n,&m);
        printf("Case #%d:\n",++cas);
        for(i = 1; i <= n; i++){
            scanf("%d%d",&x,&y);
            if(x == 1){
                vis[i] = true;
                a[i] = y;
                s = s * y % m;
                printf("%lld\n",s);
            }
            else{
                for(s = 1,j = 1; j < i; j++){
                    if(j == y)
                        vis[j] = false;
                    else if(vis[j])
                        s = s * a[j] % m;
                }
                printf("%lld\n",s);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值