An easy problem
Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1327 Accepted Submission(s): 624
Problem Description
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.
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), 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)
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<y)
if x is 2, an integer n is given. The calculator will divide the number which is multiplied in the nth operation. (the nth operation must be a type 1 operation.)
It's guaranteed that in type 2 operation, there won't be two same n.
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)
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<y)
if x is 2, an integer n is given. The calculator will divide the number which is multiplied in the nth operation. (the nth operation must be a type 1 operation.)
It's guaranteed that in type 2 operation, there won't be two same n.
Output
For each test case, the first line, please output "Case #x:" and x is the id of the test cases starting from 1.
Then Q lines follow, each line please output an answer showed by the calculator.
Then Q lines follow, each line please output an answer showed by the calculator.
Sample Input
1
10 1000000000
1 2
2 1
1 2
1 10
2 3
2 4
1 6
1 7
1 12
2 7
Sample Output
Case #1:
2
1
2
20
10
1
6
42
504
84
Source
题意:
初始化 s = 1
操作1 x,将 s = s*x%mod;
操作2 y,将第y步的x拿出来, s = s/x%mod;
问每一步操作得到的数字是多少。
开始看的时候完全不知道如何下手,有除法操作的取模运算,逆元?不行。然后找题解,然后发现自己的思维太局限了。除法不行的话那我们就将之前的的乘法操作取消就OK了啊!所以弄个标记数组,每一次出现除法的时候再对前前面的数扫一遍就行了,标记我们要除的那位,就等于之前没有乘过了。不过这样做很冒险的,O(n^2) 10^5的数据量。。然后就是强大的线段树的用场了。
4000ms+:
#include <iostream> #include <stdio.h> using namespace std; typedef long long LL; int a[100005],vis[100005]; int main() { int tcase; scanf("%d",&tcase); int t = 1; while(tcase--){ printf("Case #%d: ",t++); int n; LL mod; LL s=1; scanf("%d%lld",&n,&mod); for(int i=1;i<=n;i++){ vis[i] = false; int k,b; scanf("%d%d",&k,&b); if(k==1){ vis[i] = true; a[i] = b; s=s*a[i]%mod; }else{ s = 1; for(int j=1;j<i;j++){ if((LL)j==b){ vis[j] = false; }else if(vis[j]){ s=s*a[j]%mod; } } } printf("%lld ",s); } } return 0; }
线段树版本:
1400ms+
#include <iostream> #include <stdio.h> using namespace std; typedef long long LL; LL mod; struct Tree{ int l,r; LL v; }tree[4*100005]; void pushup(int idx){ tree[idx].v = (tree[idx<<1].v*tree[idx<<1|1].v)%mod; } void build(int l,int r,int idx){ tree[idx].l = l; tree[idx].r = r; if(l==r){ tree[idx].v = 1; return ; } int mid = (l+r)>>1; build(l,mid,idx<<1); build(mid+1,r,idx<<1|1); pushup(idx); } void update(int idx,int v,int id){ if(tree[idx].l==tree[idx].r){ tree[idx].v = v; return ; } int mid = (tree[idx].l+tree[idx].r)>>1; if(mid>=id) update(idx<<1,v,id); else update(idx<<1|1,v,id); pushup(idx); } int main() { int tcase; scanf("%d",&tcase); int t = 1; while(tcase--){ printf("Case #%d: ",t++); int n; LL s=1; scanf("%d%lld",&n,&mod); build(1,n,1); for(int i=1;i<=n;i++){ int k,b; scanf("%d%d",&k,&b); if(k==1){ update(1,b,i); }else{ update(1,1,b); } printf("%lld ",tree[1].v); } } return 0; }