Uint47 calculator【map数组+快速积+各种取余公式】
Uint47 calculator
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
题意:
给出五中操作方式 分别为def、add、mul等 对def 后面跟字符串a 和数字x 表示给字符串权值为x 其余操作均是 后跟两字符串x、y 表两者间的各种运算 且所有字符串 前民已经用def定义了
思路:
map数组:
学过map的话 应该可以想到用它 但我之前学过 可没好好的用过 借这个机会好好学一下
开始写了结构体 先查然后计算 最后更新 总是WA 就听学长的用了map
map有个很好的特点 就是作用相当于数组
例如:a[3] 现在 定义了个map<string,int>mp 就可以用字符串或其他类型(其他类型要重新定义)来代替原来的3 并且数组大小无穷 不受限制
快速积:
第一次听说 因为在计算中出现2^47 * 2^47 很有可能会爆LL 这时候快速乘就有用处了 它可以实现边乘边取余
代码上和快速幂基本相同 差别是初始定义ans=0 还有把乘法改成加法就可以了 原理还没有好好的看
各种取余公式:
对于加法取余:(a+b)%mod=(a%mod+b%mod)%mod
乘法:(a*b)%mod=(a%mod*b%mod) %mod
减法:(a-b)%mod=(a-b+mod)%mod 这个地方一定要仔细 好多题都是卡这个
除法:估计要用到逆元
还有个小知识点就是:
求2的n次幂的时候 可以直接用 1LL<<n 表示 没必要写快速幂
AC代码:
#include<iostream>
#include<stdio.h>
#include<map>
#include<string>
using namespace std;
typedef long long LL;
LL mod=1LL<<47;
map<string,LL>mp;
LL qmul(LL a,LL b)
{
LL ans=0;
while(b){
if(b%2){
ans=(ans+a)%mod;
}
a=(a+a)%mod;
b/=2;
}
return ans;
}
int main()
{
string a,b,c;
while(cin>>a>>b){
LL num;
if(a=="def"){
cin>>num;
}
else{
cin>>c;
if(a=="add"){
num=(mp[b]+mp[c])%mod;
}
else if(a=="sub"){
num=(mp[b]-mp[c]+mod)%mod;
}
else if(a=="mul"){
num=qmul(mp[b],mp[c]);
}
else if(a=="div"){
num=mp[b]/mp[c];
}
else if(a=="mod"){
num=mp[b]%mp[c];
}
}
if(num<0){
num+=mod;
}
mp[b]=num;
cout<<b<<" = "<<num<<"\n";
}
return 0;
}
Uint47 calculator【map数组+快速积+各种取余公式】的更多相关文章
- NYOJ--102--次方求模(快速求幂取模)
次方求模 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 求a的b次方对c取余的值 输入 第一行输入一个整数n表示测试数据的组数(n<100)每组测试只有一 ...
- LuoguP1226 【模板】快速幂||取余运算
题目链接:https://www.luogu.org/problemnew/show/P1226 第一次学快速幂,将别人对快速幂原理的解释简要概括一下: 计算a^b时,直接乘的话计算次数为b,而快速幂 ...
- 洛谷 P1226 【模板】快速幂||取余运算
题目链接 https://www.luogu.org/problemnew/show/P1226 题目描述 输入b,p,k的值,求b^p mod k的值.其中b,p,k*k为长整型数. 输入输出格式 ...
- FZU-Problem 2294 Uint47 calculator
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2294 题意:按照所给负号进行赋值.加.减乘.除和取余的操作. 解题思路:用map来存储字符串与值之间的对应关系 ...
- HDOJ 5666//快速积,推公式
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5666 题意:给一条直线x+y=q,在(0,0)往x+y=q上面的整数点连线,x+y=q与x,y轴截成的三角 ...
- LightOJ - 1282 - Leading and Trailing(数学技巧,快速幂取余)
链接: https://vjudge.net/problem/LightOJ-1282 题意: You are given two integers: n and k, your task is to ...
- ZZNU-oj-2141:2333--【O(N)求一个数字串能整除3的连续子串的个数,前缀和数组+对3取余组合数找规律】
2141: 2333 题目描述 “别人总说我瓜,其实我一点也不瓜,大多数时候我都机智的一批“ 宝儿姐考察你一道很简单的题目.给你一个数字串,你能判断有多少个连续子串能整除3吗? 输入 多实例输入,以E ...
- 大数计算_BigNum优化_加减乘除乘方取余_带注释_数组
#include <iostream> #include <algorithm> #include <cstring> #include <cstdlib&g ...
- jquery ajax post 传递数组 ,多checkbox 取值
jquery ajax post 传递数组 ,多checkbox 取值 http://w8700569.iteye.com/blog/1954396 使用$.each(function(){});可以 ...
随机推荐
- Vue刷新页面的三种方式
我们在写项目的时候,经常会遇到,用户执行完某个动作,改变了某些状态,需要重新刷新页面,以此来重新渲染页面 1.原始方法: location.reload(); 2.vue自带的路由跳转: this.$ ...
- 201771010120 苏浪浪 《面向对象程序设计(java)》第11周学习总结
实验十一 集合 1.实验目的与要求 (1) 掌握Vetor.Stack.Hashtable三个类的用途及常用API: (2) 了解java集合框架体系组成: (3) 掌握ArrayList.Lin ...
- Java——多线程基础知识
多线程进程和线程的区别:每一个进程拥有自己的一整套变量,而线程则共享数据.java.lang.Thread static void sleep(long millis) 线程休眠给定的毫秒数,用 ...
- Linux以指定用户非root用户运行程序、进程
方式一: 使用su命令切换用户运行 su 用户名 方式二: useradd -s /sbin/nologin -M test -s /sbin/nologin表示创建一个禁止登陆的用户(比如www ...
- 快速复习C语言 - 1变量与运算符
变量与运算符 本篇以读者知道 int.char.float.double 等数据类型为前提条件. float 类型注意事项 float 类型数没有办法跟一个数真正比较是否相等,可以定义借助绝对值在一定 ...
- Linux内存管理—详细讲解
摘要:本章首先以应用程序开发者的角度审视Linux的进程内存管理,在此基础上逐步深入到内核中讨论系统物理内存管理和内核内存的使用方法.力求从外到内.水到渠成地引导网友分析Linux的内存管理与使用.在 ...
- Johnson-Trotter(JT)算法生成排列
对于生成{1,……,n}的所有n!个排列的问题,我们可以利用减治法,该问题的规模减一就是要生成所有(n-1)!个排列.假设这个小问题已经解决了,我们可以把n插入到n-1个元素的每一种排列中的n ...
- Ftrace的部分使用方法
ftrace主要是用于调试linux kernel调度相关的一个工具,也可用于分析部分kernel性能问题. 相关ftrace的介绍可以参考:kernel/msm-4.9/Documentation/ ...
- Java实现蓝桥杯 算法提高 线段和点
算法提高 线段和点 时间限制:1.0s 内存限制:256.0MB 提交此题 问题描述 有n个点和m个区间,点和区间的端点全部是整数,对于点a和区间[b,c],若a>=b且a<=c,称点a满 ...
- Java实现 LeetCode 547 朋友圈(并查集?)
547. 朋友圈 班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递性.如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友.所谓的朋友圈,是指 ...