【模拟】Uint47 calculator

楚骞尧
2023-12-01

Uint47 calculator

 FZU - 2294 

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

解题思路:

emmmm,模拟一下过程即可,可以使用map数组,更简单,就是需要注意一下,乘法不能直接讲两个数相乘,可能会超long long,所以需要模拟一下乘法的过程,其他没什么坑。。。

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = (1LL<<47);
map<string,ll> m;
ll getnum(string s) {
	int len=s.size();
	ll ans=0;
	int i=0;
	while(i<len) {
		ans=ans*10+(ll)(s[i]-'0');
		i++;
	}
    return ans;
}
ll cheng(ll a,ll b) {
    b=b%mod;
    ll ans=0;
    while(a) {
        if(b&1) ans=(ans+a)%mod;
        a=(a<<1)%mod;
        b>>=1;
    }
    return ans%mod;
}
int main() 
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif
    //freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(0),cin.tie(0);
    string s1,s2,s3;
    while(cin>>s1>>s2>>s3) {
    	if(s1=="def") {
    		m[s2]=getnum(s3);
    		cout<<s2<<" = "<<m[s2]<<endl;
    	}
        else if(s1=="sub") {
            ll nape=m[s2]-m[s3];
            if(nape<0) nape=(nape+mod)%mod;
            m[s2]=nape;
            cout<<s2<<" = "<<m[s2]<<endl;
        }
        else if(s1=="add") {
            ll nape=(m[s2]+m[s3])%mod;
            m[s2]=nape;
            cout<<s2<<" = "<<m[s2]<<endl;
        }
        else if(s1=="div") {
            ll nape=m[s2]/m[s3];
            m[s2]=nape;
            cout<<s2<<" = "<<m[s2]<<endl;
        }
        else if(s1=="mod") {
            ll nape=m[s2]%m[s3];
            m[s2]=nape;
            cout<<s2<<" = "<<m[s2]<<endl;
        }
        else if(s1=="mul") {
            ll nape=cheng(m[s2],m[s3]);
            m[s2]=nape;
            cout<<s2<<" = "<<m[s2]<<endl;
        }
    }
    return 0;
}

 

 类似资料: