B - Ternary Logic
Little Petya very much likes computers. Recently he has received a new “Ternatron IV” as a gift from his mother. Unlike other modern computers, “Ternatron IV” operates with ternary and not binary logic. Petya immediately wondered how the xor operation is performed on this computer (and whether there is anything like it).

It turned out that the operation does exist (however, it is called tor) and it works like this. Suppose that we need to calculate the value of the expression a tor b. Both numbers a and b are written in the ternary notation one under the other one ( b under a). If they have a different number of digits, then leading zeroes are added to the shorter number until the lengths are the same. Then the numbers are summed together digit by digit. The result of summing each two digits is calculated modulo 3. Note that there is no carry between digits (i. e. during this operation the digits aren’t transferred). For example: 1410 tor 5010 = 01123 tor 12123 = 10213 = 3410.

Petya wrote numbers a and c on a piece of paper. Help him find such number b, that a tor b = c. If there are several such numbers, print the smallest one.

Input
The first line contains two integers a and c (0 ≤ a, c ≤ 109). Both numbers are written in decimal notation.

Output
Print the single integer b, such that a tor b = c. If there are several possible numbers b, print the smallest one. You should print the number in decimal notation.

Examples

Input
14 34
Output
50

Input
50 34
Output
14

Input
387420489 225159023
Output
1000000001

Input
5 5
Output
0

题意 :对于c的三进制的每一位由a与b的三进制对应位加和%3得出,知道a,c求最小的b
直接求出a,c的三进制,根据题目规则求出b的三进制,最后把b转化成10进制就是结果

#include<iostream>
#include<cmath>
using namespace std;
int a, b, c, i, j, k, sa[100], sb[100], sc[100], l1, l2, l;
int main()
{l1 = l2  = 0;cin >> a >> c;while (a) {sa[l1] = a % 3;l1++;a /= 3;}if (l1 > 0) l1--;//sa[l1]==0,我们要找到不为0的那一位,a==0的话不处理while (c) {sc[l2] = c % 3;l2++;c /= 3;}if (l2 > 0) l2--;l = (l1 > l2) ? l1 : l2;for (i = 0; i <= l; i++){while ((sa[i] + sb[i]) % 3 != sc[i]) sb[i]++;//因为每一位只有0,1,2,所以直接暴力了,并且确定的值一定是最小的}for (i = 0; i <= l; i++){b +=((long long) pow(3, i) )* sb[i];//pow()返回值为double型,做一下类型转换}cout << b;
}

B - Ternary Logic相关推荐

  1. logback节点配置详解

    logback节点配置详解 一:根节点 <configuration></configuration> 属性 : debug : 默认为false ,设置为true时,将打印出 ...

  2. Logback 专题

    logback-spring.xml <?xml version="1.0" encoding="UTF-8"?> <configuratio ...

  3. logback配置文件详解

    logback配置文件详解 <?xml version="1.0" encoding="UTF-8"?> <!-- 根节点 , logback ...

  4. x86和amd64指令参考

    https://www.felixcloutier.com/x86/index.html 目录 x86 and amd64 instruction reference Core Instruction ...

  5. 一篇文章学会日志logback的使用

    1.Logback为取代log4j而生 java日志有:slf4j,log4j,logback,common-logging slf4j:是规范/接口 日志实现:log4j,logback,commo ...

  6. SQL 中的 NULL 你真的懂了吗?【数据库|SQL】

    目录 SQL 中的 NULL 查询某列值为 NULL 的数据 NULL 是「未知」 三元逻辑(原文为 Ternary Logic) NOT IN 和 NULL 小结 SQL 中的 NULL (译自 N ...

  7. KernelPanic·并发并行专题

    Kernel Panic © Rio & Tao Wu 0x0d //Canada:big villeage 因纽特人 //木遥带来的严重涨粉,美女级粉丝 4 < 5 ========= ...

  8. P4在table中使用ternary匹配

    P4支持ternary匹配,也就是对于一个匹配字段,你可以用mask指定这个字段中的哪些bits是你关心的(1表示关心,0表示忽略),从而只匹配你关心的bits的值而忽略其他的值. 那在代码中怎么使用 ...

  9. 高完整性系统工程(三): Logic Intro Formal Specification

    目录 1. Propositions 命题 2.1 Propositional Connectives 命题连接词 2.2 Variables 变量 2.3 Sets 2.3.1 Set Operat ...

最新文章

  1. 从面试角度分析ArrayList源码
  2. 千里之行,始于OKR——轻雀协作团队管理分享
  3. python比前端好学吗_前端学习到底难不难?
  4. Windows socket c++ TCP UDP 简单客户端 vs2013
  5. Linux服务器开发之:stat(),fstat(),lstat()详细介绍+案例演示
  6. Zookeeper的概述
  7. 如何用最短的时间学会C语言,并掌握C语言的精髓所在?
  8. 分享WordPress博客搜索引擎优化的六点经验
  9. MySQL中EXPLAIN详解
  10. 设计模式---解释器模式(C++实现)
  11. 拓端tecdat|R语言空气污染数据的地理空间可视化和分析:颗粒物2.5(PM2.5)和空气质量指数(AQI)
  12. 管理账目的计算机软件有哪些,会计核算软件有哪些功能模块
  13. C++宠物商店管理系统
  14. clion配置opencv
  15. 算法-“许三多”方法
  16. “机器学习实战”刻意练习——分类问题:决策树
  17. 抖音小程序基础之 TTSS 样式是什么
  18. DHTML乌托邦:使用JavaScript和DOM的现代Web设计
  19. 【大数进制转换】清华大学考研复试上机——进制转换(10—2)
  20. 网络安全将是未来10年里面的黄金产业

热门文章

  1. 选文字游戏 一个根据颜色来选择对应文字的小游戏
  2. Python 图像文本识别 EasyOCR
  3. 撇开PUE,评估数据中心增长的真正环境影响
  4. 关于消防系统安装的一些规范
  5. 万年历单片机C语言报告,C语言单片机万年历系统设报告优秀文档.doc
  6. 微信开通公众号 每日发一篇编程精文 自己搞着玩,不盈利
  7. 实时计算 Flink SQL 核心功能解密
  8. GD32E230开发笔记-GD32选型
  9. windows下如何用python控制打印机打印_巧用win32print来控制windows系统打印机并推送打印任务...
  10. 求解多变量非线性全局最优解_约束条件下多变量非线性函数的区间算法.doc