题目描述
Archmage(大法师)
Archmage (AM for short) is a hero unit in WatercraftⅢ.
He has n mana at most and two powerful skills which enable him to show great strength in battle:
Use x mana to summon a Water Element.
Brilliance Aura restores y mana for AM in a second, and it is the only way to restore mana. Since his mana cannot exceed the upper limit, the superfluous mana will dissipate. i.e., suppose you have p mana before the restoration, your mana will become min(p+y,n).
Since the power of an Archmage is tremendous, the upper limit of mana n is always greater than or equal to x+y.
Every second, Archmage will summon exactly one Water Element (if the mana is enough, i.e., his mana won’t be less than 0 after that) or do nothing. Then no matter what he did before, he will restore y mana.
Archmage has n mana at the end of the second 0, and the game starts at the beginning of the second 1.
He wants to know how many Water Elements he can summon at most before the end of the second m.
输入描述
The input consists of multiple test cases.
The first line contains a single integer t(1≤t≤105), indicating the number of test cases.
Each of the next t lines contains 4 integers n,m,x,y(1≤m,x,y≤109,x+y≤n≤2×109).
输出描述
For each test case, output the answer in a line.
输入输出样例
Input
3
2 2 1 1
4 4 2 1
6 10 4 2
Output
2
3
6
线索提示
In test case 1, Archmage can cast spells every second, so the answer is 2.
In test case 2, here’s a way for Archmage to cast spells 3 times.
Second 1: Archmage cast spells, and there is 4−x+y=3 mana left.
Second 2: Archmage cast spells, and there is 3−x+y=2 mana left.
Second 3: Archmage cast spells, and there is 2−x+y=1 mana left.
Second 4: Archmage doesn’t have enough mana so he can do nothing, and there is 1+y=2 mana left.
一、解题思路
上面说了一大堆,翻译过来的人话就是:
第一个输入的数字t代表案例的个数
在每个案例中输入n,m,x,y四个数字,其中
- n代表大法师蓝量单位的上限(在游戏一开始也是n)
- x代表大法师在每秒花费 x 个单位的蓝量释放一次技能
- y代表法师每次释放完以后,可以回复y点的蓝
- 问 m 秒内能最多放几次技能
观察易得
- x + y ≤ n,n是蓝量上限,单次的加蓝一定不能超过这个数,同理,对于第一秒钟也是这样
- 对于输入x ≤ y, 则法师每秒都能释放一次技能,因为每一次给他补的蓝足够他下一次用,最终施法次数 = 输入的秒数
- x > y, 则前 m − 1 秒内恢复的魔法值都可以被利用上,则可以通过 n + ( m − 1 ) ∗ y x \frac{n+\left( m-1 \right) *y}{x} xn+(m−1)∗y计算施法次数
- 由于通过 n + ( m − 1 ) ∗ y x \frac{n+\left( m-1 \right) *y}{x} xn+(m−1)∗y计算的次数可能大于m,所以用头文件中的min函数取最小值
注意点
使用模拟?
//作者使用模拟方法TLE的代码
int main()
{
LL t;
LL n, m, x, y;
LL cnt = 0; //统计施法次数
cin >> t;
while (t--)
{
cnt = 0;
cin >> n >> m >> x >> y;
for (int i = 0; i < m; i++)
{
if (n - x >= 0) //剩余有法力值
{
n = n - x + y; //第一种情况,产生水元素
cnt++;
}
else
{
n += y; //第二种情况,自己回复法力值
}
}
cout << cnt << endl;
}
return 0;
}
如果我们按照上述的题意,使用模拟的方式,则容易造成超时
数据类型
在VJ上面给的数据测试集中,有一些比较大的数字,应使用long long 或者unsigned long long 类型的变量,要不然会WA
二、题解
源代码
代码如下:
/*
* Author: FeverTwice
* Date: 2021-05-01
* Func: Solution for Archmage
*/
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
#define lyh(i,a,b) for(int i=a;i<=b;i++)
#define hyl(i,a,b) for(int i=a;i>=b;i--)
#define LL long long
#define mm memset
#define EPS 1e-8
#define INF 0x7fffffff
using namespace std;
int main()
{
LL t;
LL n, m, x, y;
LL cnt = 0; //统计施法次数
cin >> t;
while (t--)
{
cin >> n >> m >> x >> y;
if (x <= y)
{
cnt = m;
}
else
{
cnt = min((n + (m - 1) * y) / x, m);
}
cout << cnt << endl;
}
return 0;
}
VJudge评判结果
参考文献
[1]A. Archmage, author:sdutlyq
写在最后
各位看官,都看到这里了,麻烦动动手指头给博主来个点赞8,您的支持作者最大的创作动力哟! <(^-^)>
才疏学浅,若有纰漏,恳请斧正
本文章仅用于各位同志作为学习交流之用,不作任何商业用途,若涉及版权问题请速与作者联系,望悉知