CodeForces 333A Secrets

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.

题意:首先有一堆硬币,面值是3的n次方:1,3,9,27……,然后给你一个数n,让你用硬币去换,条件是:

你用硬币要尽可能的大于n并且最接近n;

你用的硬币数量要尽可能的多;

问要多少个硬币换取。

题解:首先1面值的不能用,然后找到第一个不能整除3的面值,用除数+1就是结果。

#include
#include
#include
#include
#include
using namespace std;
int main()
{
    long long int n,t;
    long long int ans;
    while(~scanf("%lld",&n))
    {
        if(n%3!=0)
        {
            ans=n/3+1;
        }
        else if(n%3==0)
        {
            t=3*3;
            while(t)
            {
                if(t==n)
                {
                    ans=1;
                    break;
                }
                else if(n%t!=0)
                {
                    ans=n/t+1;
                    break;
                }
                else
                t=t*3;
            }
        }
        printf("%lld\n",ans);
    }
}


你可能感兴趣的:(acm_数论)