Secrets

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

Gerald has been selling state secrets at leisure. All the secrets cost the same: n marks. The state which secrets Gerald is selling, has no paper money, only coins. But there are coins of all positive integer denominations that are powers of three: 1 mark, 3 marks, 9 marks, 27 marks and so on. There are no coins of other denominations. Of course, Gerald likes it when he gets money without the change. And all buyers respect him and try to give the desired sum without change, if possible. But this does not always happen.

One day an unlucky buyer came. He did not have the desired sum without change. Then he took out all his coins and tried to give Gerald a larger than necessary sum with as few coins as possible. What is the maximum number of coins he could get?

The formal explanation of the previous paragraph: we consider all the possible combinations of coins for which the buyer can not give Gerald the sum of n marks without change. For each such combination calculate the minimum number of coins that can bring the buyer at least n marks. Among all combinations choose the maximum of the minimum number of coins. This is the number we want.

Input

The single line contains a single integer n (1 ≤ n ≤ 1017).

Please, do not use the %lld specifier to read or write 64 bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.

Output

In a single line print an integer: the maximum number of coins the unlucky buyer could have paid with.

Sample Input

Input
1
Output
1
Input
4
Output
2

Hint

In the first test case, if a buyer has exactly one coin of at least 3 marks, then, to give Gerald one mark, he will have to give this coin. In this sample, the customer can not have a coin of one mark, as in this case, he will be able to give the money to Gerald without any change.

In the second test case, if the buyer had exactly three coins of 3 marks, then, to give Gerald 4 marks, he will have to give two of these coins. The buyer cannot give three coins as he wants to minimize the number of coins that he gives.

题意

所有的硬币面额都是3的幂,给出价格N,假设N无法由目前所有的硬币组合成,那么求在多花最少钱的情况下最多用多少枚硬币.

思路

想要用的硬币最多,超出N最少,必然优先考虑面额小的硬币,但题目要求N不能被凑整,即若N是3的倍数,那么3元的硬币就不能用,以此类推。因此先用循环找到N不能被整除的面额,答案即为N/a+1。话说这题是数论吗。。。

 //2016.8.13
#include<iostream>
#include<cstdio> using namespace std; int main()
{
long long n, a;
while(cin>>n)
{
a = ;
while(n%a==)a*=;
cout<<n/a+<<endl;
} return ;
}

CodeForces 333A的更多相关文章

  1. Secrets CodeForces - 333A

    Secrets CodeForces - 333A 题意:这个世界上只有这样面值的硬币:1,3,9,27,81,...有一个商人,某一天遇到了一个顾客,他购买了价值n的商品,发现用自己的硬币无法付给商 ...

  2. codeforces 333A - Secrets

    题意:保证不能正好配齐n,要求输出可以用的最大硬币数. 注意如果用到某种硬币,那么这种硬币就有无穷多个.所以11=3+3+3+3,12=9+9,13=3+3+3+3+3 #include<cst ...

  3. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  4. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  5. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  6. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  7. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  8. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  9. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

随机推荐

  1. javascript 闭包理解例子

    function Jquery(){ this.name = 'ysr'; this.sex = 'man'; return { x: this, age : 26 } } var b = new J ...

  2. ds18b20再硬件设计部分的注意事项

    1.DS18B20的供电方式: ps:寄生电源不是实际的电源器件,而是一种供电方式,即通过数据线供电. 如下面的两张图片所示,分别为外部供电模式下单只和多只DS18B20测温系统的典型电路连接图. ( ...

  3. dedecms如何修改共0页/0条记录为英文版?

    近日,在测试一个网站功能的时候,发现在搜索结果的下面为中文的“共0页/0条记录”,但客户的网站为英文版,所以我们需要将搜索的结果信息也要显示为英文,好了,我们开始动手修改dedecms的文件,以达到我 ...

  4. Mysql(集群)业务水平切割 垂直切割(Amoeba)

     Amoeba原理戳这里:Amoeba详细介绍 需要根据企业 数据业务进行切割,垂直切割又称为纵向切割. 垂直切割通说的说就是有多个表,对表进行分离(用户数据.博客文章数据.照片数据.标签数据类型.群 ...

  5. Quick Cocos2dx 与 EnterFrame事件

    利用EnterFrame做出行走的效果,效果图如下: 具体操作: 1 给self多加一个bg1用作与bg无限循环换位 2 在AnotherScene:onEnter方法里面新增onEnterFrame ...

  6. Quick Cocos2dx 与 DragonBones

    照着官方的例子试验了一下DragonBone的使用,代码如下: local AnotherScene = class("AnotherScene", function() retu ...

  7. COCOS2DX遇到的问题

    1 新增类无法在别的类里面include到的问题: 去项目-> 配置属性-> C/C++-> 常规-> 附加包含目录中增加类的文件夹路径 2 新增类出现 extensions\ ...

  8. Java对文件的16进制读取和操作

    大家可以参考一下源代码的相关部分注释,然后写出自己的16进制处理程序.有几个重点地方: 16进制字符串->10进制数 int input = Integer.parseInt("Str ...

  9. 16、手把手教你Extjs5(十六)Grid金额字段单位MVVM方式的选择

    这一节来完成Grid中的金额字段的金额单位的转换.转换旰使用MVVM特性,总体上和控制菜单的几种模式类似.首先在目录app/view/main/menu下建立文件Monetary.js,用于放金额单位 ...

  10. MySQL show status命令参数

    Aborted_clients         由于客户没有正确关闭连接已经死掉,已经放弃的连接数量.   Aborted_connects 尝试已经失败的MySQL服务器的连接的次数.   Binl ...