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. Golang学习笔记:包制作

    golang的包跟java as js之类的大不一样,一定要存在GOPATH里面,GOPATH是专门用于存放golang第三方的库,里面有两个文件夹, src:源代码目录 pkg:编译后的第三方包,这 ...

  2. BNU OJ 51003 BQG's Confusing Sequence

    二进制++高精度取模 #include<cstdio> #include<cstring> #include<algorithm> using namespace ...

  3. swift之向ftp服务器传文件

    在 mac 上如何使用 xcode, swift 语言开发一个向 ftp 服务器上传文件的工具? 使用的是第三方库 Rebekka,下载地址为:https://github.com/Constanti ...

  4. SpringMVC 接收表单数据的方式 - Samuel - 博客频道 - CSDN.NET

    1.@RequestParam @RequestMapping(value = "/xxxx.do") public void create(@RequestParam(value ...

  5. losbyday Linux下的强大工具之一akw(转),Shell必备

    简单使用:awk :对于文件中一行行的独处来执行操作 .awk -F :'{print $1,$4}'   :使用‘:’来分割这一行,把这一行的第一第四个域打印出来 .  详细介绍:AWK命令介绍 a ...

  6. (简单) POJ 3667 Hotel,线段树+区间合并。

    Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and e ...

  7. libusb 开发者指南-牛胜超(转)

    源:libusb 开发者指南 libusb Developers Guidelibusb 开发者指南 原作者:Johannes Erdfelt翻译者:牛胜超 Table of Contents目录 P ...

  8. imagebutton、imageview的属性

    [转]http://blog.csdn.net/victoryckl/article/details/14162131 http://blog.sina.com.cn/s/blog_68b3fdc30 ...

  9. onethink的插件扩展

    2014年7月25日 07:22:35 编写一个文章可以同步到微博的插件 思路:首先要进行微博的绑定,获得令牌.然后调用微博的api即可. 2014年7月25日 08:40:27 服务器与本地有区别. ...

  10. EntityFrameWork分页

    EF分页代码 using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sy ...