HDU 5475 An easy problem(用大数模板,你就上当了)——2015 ACM/ICPC Asia Regional Shanghai Online
2015-09-26 18:10
429 查看
An easy problem
Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)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
/*********************************************************************/
题意:X=1,有两种操作:
①X乘上一个整数;
②X除以一个之前乘过的数。
要求每次操作之后,输出X%M的值
解题思路:首先,有一点需要提醒一下,每次操作的X是未取模的。
我们不妨举个例子来说明:比如说M=4时,我们进行如下操作
1 3//操作①,将X*3,那么X=3,输出X%M=3
1 2//操作①,将X*2,那么X=6,输出X%M=2
2 1//操作②,除以第一次操作数,即X/3,此时X是6,而不是第二步输出的结果2,故X%M=2
因为如此,X在计算过程中会不断累积,会很大,所以很多人自然而然地就会想到大数模板,那么,恭喜你,可以收获TLE了(我不是很清楚是否有人用大数模板也能过,至少我TLE了)。
这里给我的体会就是,要大胆尝试,其实一开始的时候我就有想到正确方法,然而感觉会TLE,就没有敢去尝试
我们可以记录要乘的数,遇到除法操作时,只需将标记的数抹去再乘一遍就行了,因为取模不影响乘法运算,我们之所以理所当然地觉得这样的方法反而会超时,是因为忽略了大数计算过程的复杂性
欢迎大家交流心得或是提出自己的疑惑
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<stdio.h> #include<string.h> #include<stdlib.h> #include<queue> #include<stack> #include<math.h> #include<vector> #include<map> #include<set> #include<stdlib.h> #include<cmath> #include<string> #include<algorithm> #include<iostream> #define exp 1e-10 using namespace std; const int N = 100005; const int inf = 1000000000; const int mod = 2009; int a ; bool v ; int main() { int t,i,j,k=1,p,n,m,x,y; __int64 s; scanf("%d",&t); while(t--) { p=0,s=1;memset(v,false,sizeof(v)); scanf("%d%d",&n,&m); printf("Case #%d:\n",k++); for(i=1;i<=n;i++) { scanf("%d%d",&x,&y); if(x==1) { v[i]=true; a[i]=y; s=(s*y)%m; printf("%I64d\n",s); } else { for(s=1,j=1;j<i;j++) if(j==y) v[j]=false; else if(v[j]) s=(s*a[j])%m; printf("%I64d\n",s); } } } return 0; }菜鸟成长记
相关文章推荐
- 2015 ACM/ICPC Asia Regional Shanghai Online 1010题
- HDU 5478 Can you find it (卡常数)2015 ACM/ICPC Asia Regional Shanghai Online
- aircrack使用
- Clairewd’s message--hdu4300(Next数组的运用)
- CoCreateInstance of OLE control failed
- 第十二篇:关于main中的两个参数的使用请况
- epoll_wait 时 POLLERR 与 POLLIN 同时返回的现象解析
- ADB not responding. You can wait more,or kill"abd.exe“。。。。。
- composer update 报错 Failed to decode response: zlib_decode()
- 2015 ACM/ICPC Asia Regional Shanghai Online A Sweet Journey(简单题)
- 2015 ACM/ICPC Asia Regional Shanghai Online An easy problem(简单题)
- rails学习笔记:维护商品信息任务C
- 201509——hulu笔试题目——rainfall
- 2015 ACM/ICPC Asia Regional Shanghai Online-1010-A Sweet Journey
- poj 3216 Repairing Company(最短路Floyd + 最小路径覆盖 + 构图)
- Gradle sync failed: peer not authenticated
- signal(SIGCLD,SIG_IGN) http://blog.csdn.net/cffishappy/article/details/7005115
- tomcat 6.0.44 “has failed to stop it. This is very likely to create a memory leak” 问题调查
- Linux下system()函数返错,errno是ECHILD http://blog.csdn.net/taolinke/article/details/8057335
- linux多线程环境下的抢尸行为(system返回-1:No child processes)http://www.aiuxian.com/article/p-129935.html