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