列表

详情


NC15727. G. Coins

描述

In the latest activity in the game, you want to find the most efficient way to collect at least Z coins. There is a battle in this game. Everytime you finish the battle, you can get N coins. To make the activity more interesting, you can spend X coins to exchange a special card. If you carry K cards in the battle, you can get coins when you finish the battle rather than only N coins. So how many times will you finish the battle at least?

输入描述

\emph{Input contains multiple but no more than 10 test cases}, the first line of input is an integer T, the number of test cases.

In the following T lines each line has for integers (), and the meaning of them have been mentioned.

输出描述

For each case, please output the minimum times you should finish the battle.

示例1

输入:

1
1 10 1 100

输出:

39

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 4ms, 内存消耗: 476K, 提交时间: 2019-10-22 20:54:29

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
    int T;
    ll n,x,y,z;
    scanf("%d",&T);
    while(T--) {
        scanf("%lld%lld%lld%lld",&n,&x,&y,&z);
        ll ans=(z+n-1)/n,now=0;
        for(int i=0;i<ans;) {
            ll k=now/x;
            n+=k*y,now%=x;
            ans=min(ans,(z-now+n-1)/n+i);
            k=(x-now+n-1)/n;
            now+=k*n;
            i+=k;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

C++11(clang++ 3.9) 解法, 执行用时: 4ms, 内存消耗: 348K, 提交时间: 2020-03-26 21:46:34

#include<iostream>
#include<cstdio>
int T;
long long n,x,y,z;
int main()
{
	std::cin>>T;
	while(T--)
	{
		std::cin>>n>>x>>y>>z;
		long long ans=(z+n-1)/n;
		long long now=0;
		for(int i=0;i<ans;)
		{
			long long k=now/x;
			n+=k*y;
			now-=k*x;
			ans=std::min(ans,(z-now+n-1)/n+i);
			k=(x-now+n-1)/n;
			now+=k*n;
			i+=k;
		}
		std::cout<<ans<<std::endl;
	}
	return 0;
}

上一题