G. Coins 模拟 + 数学 + 剪枝

G. Coins
G. Coins
比赛主页

我的提交

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
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 N + K \times YN+K×Y 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 N, X, Y, ZN,X,Y,Z (1 \le N, X, Y, Z \le 10^91≤N,X,Y,Z≤10
9
), 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

题解

image

1,在最开始买卡片。
2,买的话跳跃着买,
当前值如果能买多个卡片,一定要么买0个,要么手里剩余的金币全买卡片,可以设 买a,b 个卡片,然后写出不等式,发现不等式的成立和a b 的大小无关。(蒋哥太强了!) 故两边取极限即可。
如果不够买一个的,则直接算出要买几天才能买一个的,直接O(1)的买。

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define int long long
#define fi first
#define se second
#define pb push_back

#define foa(x, y, z) for(int x = (y), ooo = (z); x <= z; ++x)
#define fos(x, y, z) for(int x = (y), ooo = (z); x >= z; --x)
#define ckmax(x, y) ((x) < (y) ? (x) = (y), 1 : 0)
#define ckmin(x, y) ((x) > (y) ? (x) = (y), 1 : 0)

typedef pair<int, int> pii;
typedef long long ll;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f3f3f3f3f;
const int N = 1e6 + 10;
int n, m;
int x, y, z;
int res = inf;
void dfs(int has, int day, int d)
{
    if(day >= res) return;
    int cnt = day + max((z - has + d - 1) / d, 0ll);
    ckmin(res, cnt);
    if(has >= x) dfs(has % x, day, d + y * (has / x));
    else {
        int t = (x - has + d - 1) / d;
        dfs(has + d * t, day + t, d);
    }
}
void solve()
{
    res = inf;
    cin >> n >> x >> y >> z;
    dfs(0, 0, n);
    cout << res << endl;
}
signed main()
{
	// ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while(t--)
	solve();
    return 0;
}
posted @   1564269628  阅读(22)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示