HDU5475

An easy problem

Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1697    Accepted Submission(s): 760

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.

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<y≤109)
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.

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

2015 ACM/ICPC Asia Regional Shanghai Online

既然说是简单题,那就不用想的太复杂,暴力的做法也能过

 1 //2016.9.12
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #define N 100005
 6
 7 using namespace std;
 8
 9 int nu[N], book[N];
10
11 int main()
12 {
13     long long ans;
14     int T, kase = 0, q, mod, op;
15     scanf("%d", &T);
16     while(T--)
17     {
18         ans = 1;
19         memset(book, true, sizeof(book));
20         printf("Case #%d:\n", ++kase);
21         scanf("%d%d", &q, &mod);
22         for(int i = 1; i <= q; i++)
23         {
24             scanf("%d%d", &op, &nu[i]);
25             if(op == 1)
26             {
27                 ans *= nu[i];
28                 ans %= mod;
29             }
30             else
31             {
32                 book[nu[i]] = false;
33                 book[i] = false;
34                 ans = 1;
35                 for(int  j = 1; j < i; j++)
36                 {
37                     if(book[j])ans = (ans*nu[j])%mod;
38                 }
39             }
40             printf("%lld\n", ans);
41         }
42     }
43
44     return 0;
45 }
时间: 2024-10-13 21:05:53

HDU5475的相关文章

HDU5475(线段树)

An easy problem Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1553    Accepted Submission(s): 697 Problem Description One day, a useless calculator was being built by Kuros. Let's assume that

ACM学习历程—HDU5475 An easy problem(线段树)(2015上海网赛08题)

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. divid

hdu-5475 An easy problem---线段树+取模

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5475 题目大意: 给X赋初值1,然后给Q个操作,每个操作对应一个整数M: 如果操作是1则将X乘以对应的M, 如果是2则除以第M次操作对应的M',求每次操作后X的值对给定值取摸的结果. 解题思路: 第一眼看这道题,以为就是水题,直接模拟暴力呀,但是发现这样是错误的,因为这里有除法,对除法取模,就应该是逆元,但是逆元不一定存在 想了之后发现可以用线段树保存每一个要乘以的数字,对于操作一就加入数字即可,

数论学习(题库有很多啦。)

逆元模板题 hdu1576 Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1). Input 数据的第一行是一个T,表示有T组数据.每组数据有两个数n(0 <= n < 9973)和B(1 <= B <= 10^9). Output 对应每组数据输出(A/B)%9973 Sample Input 2 1000 53 87 123456789 Sample Ou