http://acm.hdu.edu.cn/showproblem.php?pid=3546

题意:10个寄存器初值为1,有加乘赋值运算,最多30w次运算,大数最多5000位,问最后10个寄存器的结果

题解:想直接暴力,但是会超时,30w*5000略多,关键优化是注意到如果出现赋值运算,则被赋值的寄存器之前的值都不用计算,反之赋值的寄存器之前的值必须计算,这样先倒着扫一遍,给每个运算上个标记(判断是不是必须计算),接着从头扫,跳过不必要的式子。

顺便测了一下新找的大数模板,还可以接受,功能已经非常全面了

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std; #define MAXN 9999
#define MAXSIZE 10
#define DLEN 4 class BigInt
{
private:
int a[]; //可以控制大数的位数
int len; //大数长度
public:
BigInt(){ len = ;memset(a,,sizeof(a)); } //构造函数
BigInt(const int); //将一个int类型的变量转化为大数
BigInt(const char*); //将一个字符串类型的变量转化为大数
BigInt(const BigInt &); //拷贝构造函数
BigInt &operator=(const BigInt &); //重载赋值运算符,大数之间进行赋值运算 friend istream& operator>>(istream&, BigInt&); //重载输入运算符
friend ostream& operator<<(ostream&, BigInt&); //重载输出运算符 BigInt operator+(const BigInt &) const; //重载加法运算符,两个大数之间的相加运算
BigInt operator-(const BigInt &) const; //重载减法运算符,两个大数之间的相减运算
BigInt operator*(const BigInt &) const; //重载乘法运算符,两个大数之间的相乘运算
BigInt operator/(const int &) const; //重载除法运算符,大数对一个整数进行相除运算 BigInt operator^(const int &) const; //大数的n次方运算
int operator%(const int &) const; //大数对一个int类型的变量进行取模运算
bool operator>(const BigInt & T)const; //大数和另一个大数的大小比较
bool operator<(const BigInt & T) const;
bool operator==(const BigInt & T) const;
bool operator>(const int & t)const; //大数和一个int类型的变量的大小比较
bool operator<(const int &t) const;
bool operator==(const int &t) const; void print(); //输出大数
}; bool BigInt::operator==(const BigInt & T) const {
return !(*this > T) && !(T > *this);
}
bool BigInt::operator==(const int &t) const {
BigInt T = BigInt(t);
return *this == T;
}
bool BigInt::operator<(const BigInt & T) const {
return T > *this;
}
bool BigInt::operator<(const int &t) const {
return BigInt(t) > *this;
}
BigInt::BigInt(const int b) //将一个int类型的变量转化为大数
{
int c,d = b;
len = ;
memset(a,,sizeof(a));
while(d > MAXN)
{
c = d - (d / (MAXN + )) * (MAXN + );
d = d / (MAXN + );
a[len++] = c;
}
a[len++] = d;
}
BigInt::BigInt(const char*s) //将一个字符串类型的变量转化为大数
{
int t,k,index,l,i;
memset(a,,sizeof(a));
l=strlen(s);
len=l/DLEN;
if(l%DLEN)
len++;
index=;
for(i=l-;i>=;i-=DLEN)
{
t=;
k=i-DLEN+;
if(k<)
k=;
for(int j=k;j<=i;j++)
t=t*+s[j]-'';
a[index++]=t;
}
}
BigInt::BigInt(const BigInt & T) : len(T.len) //拷贝构造函数
{
int i;
memset(a,,sizeof(a));
for(i = ; i < len ; i++)
a[i] = T.a[i];
}
BigInt & BigInt::operator=(const BigInt & n) //重载赋值运算符,大数之间进行赋值运算
{
int i;
len = n.len;
memset(a,,sizeof(a));
for(i = ; i < len ; i++)
a[i] = n.a[i];
return *this;
}
istream& operator>>(istream & in, BigInt & b) //重载输入运算符
{
char ch[MAXSIZE*];
int i = -;
in>>ch;
int l=strlen(ch);
int count=,sum=;
for(i=l-;i>=;)
{
sum = ;
int t=;
for(int j=;j<&&i>=;j++,i--,t*=)
{
sum+=(ch[i]-'')*t;
}
b.a[count]=sum;
count++;
}
b.len =count++;
return in; }
ostream& operator<<(ostream& out, BigInt& b) //重载输出运算符
{
int i;
cout << b.a[b.len - ];
for(i = b.len - ; i >= ; i--)
{
cout.width(DLEN);
cout.fill('');
cout << b.a[i];
}
return out;
} BigInt BigInt::operator+(const BigInt & T) const //两个大数之间的相加运算
{
BigInt t(*this);
int i,big; //位数
big = T.len > len ? T.len : len;
for(i = ; i < big ; i++)
{
t.a[i] +=T.a[i];
if(t.a[i] > MAXN)
{
t.a[i + ]++;
t.a[i] -=MAXN+;
}
}
if(t.a[big] != )
t.len = big + ;
else
t.len = big;
return t;
}
BigInt BigInt::operator-(const BigInt & T) const //两个大数之间的相减运算
{
int i,j,big;
bool flag;
BigInt t1,t2;
if(*this>T)
{
t1=*this;
t2=T;
flag=;
}
else
{
t1=T;
t2=*this;
flag=;
}
big=t1.len;
for(i = ; i < big ; i++)
{
if(t1.a[i] < t2.a[i])
{
j = i + ;
while(t1.a[j] == )
j++;
t1.a[j--]--;
while(j > i)
t1.a[j--] += MAXN;
t1.a[i] += MAXN + - t2.a[i];
}
else
t1.a[i] -= t2.a[i];
}
t1.len = big;
while(t1.a[t1.len - ] == && t1.len > )
{
t1.len--;
big--;
}
if(flag)
t1.a[big-]=-t1.a[big-];
return t1;
} BigInt BigInt::operator*(const BigInt & T) const //两个大数之间的相乘运算
{
BigInt ret;
int i,j,up;
int temp,temp1;
for(i = ; i < len ; i++)
{
up = ;
for(j = ; j < T.len ; j++)
{
temp = a[i] * T.a[j] + ret.a[i + j] + up;
if(temp > MAXN)
{
temp1 = temp - temp / (MAXN + ) * (MAXN + );
up = temp / (MAXN + );
ret.a[i + j] = temp1;
}
else
{
up = ;
ret.a[i + j] = temp;
}
}
if(up != )
ret.a[i + j] = up;
}
ret.len = i + j;
while(ret.a[ret.len - ] == && ret.len > )
ret.len--;
return ret;
}
BigInt BigInt::operator/(const int & b) const //大数对一个整数进行相除运算
{
BigInt ret;
int i,down = ;
for(i = len - ; i >= ; i--)
{
ret.a[i] = (a[i] + down * (MAXN + )) / b;
down = a[i] + down * (MAXN + ) - ret.a[i] * b;
}
ret.len = len;
while(ret.a[ret.len - ] == && ret.len > )
ret.len--;
return ret;
}
int BigInt::operator %(const int & b) const //大数对一个int类型的变量进行取模运算
{
int i,d=;
for (i = len-; i>=; i--)
{
d = ((d * (MAXN+))% b + a[i])% b;
}
return d;
}
BigInt BigInt::operator^(const int & n) const //大数的n次方运算
{
BigInt t,ret();
int i;
if(n<)
exit(-);
if(n==)
return ;
if(n==)
return *this;
int m=n;
while(m>)
{
t=*this;
for( i=;i<<<=m;i<<=)
{
t=t*t;
}
m-=i;
ret=ret*t;
if(m==)
ret=ret*(*this);
}
return ret;
}
bool BigInt::operator>(const BigInt & T) const //大数和另一个大数的大小比较
{
int ln;
if(len > T.len)
return true;
else if(len == T.len)
{
ln = len - ;
while(a[ln] == T.a[ln] && ln >= )
ln--;
if(ln >= && a[ln] > T.a[ln])
return true;
else
return false;
}
else
return false;
}
bool BigInt::operator >(const int & t) const //大数和一个int类型的变量的大小比较
{
BigInt b(t);
return *this>b;
}
void BigInt::print() //输出大数
{
int i;
cout << a[len - ];
for(i = len - ; i >= ; i--)
{
cout.width(DLEN);
cout.fill('');
cout << a[i];
}
cout << endl;
}
char op[][] ;
int vis[],iscal[] ;
int main()
{
int n= ;
while(cin >> op[n++]) ;
for(int i= ;i< ;i++)
vis[i]= ;
for(int i=n- ;i>= ;i--)
{
iscal[i]=vis[op[i][]-'A'] ;
if(op[i][]=='=')
{
vis[op[i][]-'A']= ;
vis[op[i][]-'A']= ;
}
}
BigInt a[] ;
for(int i= ;i< ;i++)
a[i]= ;
for(int i= ;i<n ;i++)
{
if(iscal[i])
{
if(op[i][]=='=')
{
a[op[i][]-'A']=a[op[i][]-'A'] ;
}
else if(op[i][]=='+')
{
a[op[i][]-'A']=a[op[i][]-'A']+a[op[i][]-'A'] ;
}
else if(op[i][]=='*')
{
a[op[i][]-'A']=a[op[i][]-'A']*a[op[i][]-'A'] ;
}
}
}
for(int i= ;i< ;i++)
cout << a[i] << endl ;
return ;
}

HDU 3546的更多相关文章

  1. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  3. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  4. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  5. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  6. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  7. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

  8. HDU 3791二叉搜索树解题(解题报告)

    1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...

  9. hdu 4329

    problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟  a.     p(r)=   R'/i   rel(r)=(1||0)  R ...

随机推荐

  1. hdu 5587 Array 二分

    Array Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem ...

  2. 广州工业大学2016校赛 F 我是好人4 dfs+容斥

    Problem F: 我是好人4 Description 众所周知,我是好人!所以不会出太难的题,题意很简单 给你n个数,问你1000000000(含1e9)以内有多少个正整数不是这n个数任意一个的倍 ...

  3. django模型和字段

    一个模型(model)就是一个单独的.确定的数据的信息源,包含了数据的字段和操作方法.通常,每个模型映射为一张数据库中的表. 基本的原则如下: 每个模型在Django中的存在形式为一个Python类 ...

  4. Socket入门之前的知识点

    Socket难点 数据粘包 心跳维持 数据丢包 性能问题 7层网络模型-OSI 基础层:物理层(Physical).数据链路层(Datalink).网络层(Network) 传输层(Transport ...

  5. 时间常用api

    1.常用api 创建 Date 对象  -  年  -  月  -  日   -  小时  -  分  -  秒 -  星期 var now=new Date() var year = now.get ...

  6. 批量删除Redis数据库中的Key

    批量删除KeyRedis 中有删除单个 Key 的指令 DEL,但好像没有批量删除 Key 的指令,不过我们可以借助 Linux 的 xargs 指令来完成这个动作 redis-cli keys &q ...

  7. c# 获取方法所在的命名空间 类名 方法名

    平时我们在记录日志的时候难免会需要直接记录当前方法的路径,以便查找,但是每次都输入方法名称非常的繁琐,同时如果修改了方法名称也要去手动修改日志内容,真的是劳命伤财啊,所以有了如下方法则可解决我们的大难 ...

  8. java中的static关键字 学习总结

    使用static关键字修饰的变量和方法为静态变量.静态方法. 非静态方法可以访问静态变量/方法和非静态变量/方法,但静态方法只能访问静态变量/方法. 可以看到在静态方法中调用非静态变量和非静态方法时, ...

  9. SGE:qsub/qstat/qdel/qhost 任务投递和监控

    参考: Oracle Grid Engine qsub命令 SGE - qsub使用范例 SGE作业基本用法 qsub是最为稳定的底层任务投递系统,就是把一个脚本投递到集群的计算节点上运行. 注意,只 ...

  10. arcgis for silverlight 地图放大到某个点或者几何对象

    http://blog.csdn.net/xuan444150/article/details/7727866   分类: silverlight王国 GIS王国 2012-07-09 08:50 1 ...