微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

HDU 5475 An easy problem(用大数模板,你就上当了)——2015 ACM/ICPC Asia Regional Shanghai Online

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( 1T10   ),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. ( 1Q105,1M109  )
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<y109 )
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
 
/*********************************************************************/

题意: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[N];
bool v[N];
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;
}
菜鸟成长记

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐