calculator(快速幂+map)

In the distant space, there is a technologically advanced planet.

One day they provided the Earth with a code that could achieve the ultimate meaning of the universe. People were very happy, but found that this code can only run on computers with a word length of 47 bits. As a good computer scientist, you need to implement a tool to simulate running this code on our computer.

This tool needs to simulate the following instructions:

“def x n” : define a unsigned 47 bits integer variable named x, with initial value n, n is an integer in [0, 2^47-1]

“add x y” : means x = x + y

“sub x y” : means x = x - y

“mul x y” : means x = x * y

“div x y” : means x = x / y, we guarantee y is not zero

“mod x y” : means x = x % y, we guarantee y is not zero

When the result of addition and multiplication cannot be represented by 47 bits, the part above 47 bits is truncated.

When the result of subtraction is less than zero, the result should add 2^47.

The name of each variable only contains letters and the length does not greater than 20.

Input
Contains multiple lines of input, one instruction per line.

Output
For each instruction, output the value of the first argument after calculation. For example, “def abc 100”, then your output will be “abc = 100” in a line with no quotation marks.

See Sample Output for more information.

Sample Input
def six 6
def abc 1
def bcd 0
sub bcd abc
add abc six
def universe 0
mul abc six
add universe abc
div bcd six
mod bcd abc
Sample Output
six = 6
abc = 1
bcd = 0
bcd = 140737488355327
abc = 7
universe = 0
abc = 42
universe = 42
bcd = 23456248059221
bcd = 5
思路:快速幂应用(幂运算转换为乘积运算):要转换一下思路,模拟小学加法变乘法(乘积运算转变为加法运算)
2^47: 1LL<<47

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
typedef long long ll;
using namespace std;
const ll mod=1LL<<47;
map<string,ll> mp;
ll mul_mod(ll x,ll y)//快速乘取模
{
    ll ans=0; 
    while(y)
    {
        if(y&1) ans=(ans+x)%mod; //y是奇数
        y>>=1;//y是偶数
        x=(x+x)%mod;
    }
    return ans;
}
int main()
{
    ios::sync_with_stdio(false);
    string a,b,c;
    while(cin>>a>>b)
    {
        if(a=="def")
        {
            ll x;
            cin>>x;
            mp[b]=x;
            cout<<b<<" = "<<x<<endl;
            continue;
        }
        cin>>c;
        if(a[0]=='a')
        {
            mp[b]=(mp[c]+mp[b])%mod;
            cout<<b<<" = "<<mp[b]<<endl;
        }
        else if(a[0]=='s')
        {
            mp[b]=(mp[b]-mp[c]+mod)%mod;
            cout<<b<<" = "<<mp[b]<<endl;
        }
        else if(a[0]=='d')
        {
            mp[b]/=mp[c];
            mp[b]=(mp[b]+mod)%mod;
            cout<<b<<" = "<<mp[b]<<endl;
        }
        else if(a=="mod")
        {
            mp[b]%=mp[c];
            cout<<b<<" = "<<mp[b]<<endl;
        }
        else
        {
            mp[b]=mul_mod(mp[b],mp[c]);
            cout<<b<<" = "<<mp[b]<<endl;
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值