bbb
code:
#include<bits/stdc++.h>
using namespace std;
long long n,m;
void work(int id)
{
long long sum=0,cnt=0;
while(sum<n*m)
cnt++,sum+=(sum/n)+1;
cout<<cnt<<"\n";
}
int main()
{
int t;
cin>>t;
for(int i=1;i<=t;i++)
{
cin>>n>>m;
work(i);
}
return 0;
}
时间复杂度O(n+log(nm))
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
int t;
cin >> t;
for (int i = 1; i <= t; i ++) {
int n, m;
cin >> n >> m;
int ans = 0, ls = 0;
for (int i = 1; i <= m; i ++) {
int cnti = (i * n - ls + i - 1) / i;
ls += cnti * i, ans += cnti;
} cout << ans << '\n';
}
return 0;
}
更优解,by Gcd_Std时间复杂度 O(m)
ai生成的贪心证明:
最少军舰数 s 的证明
问题重述
现有一个
记 Bob 所选军舰的长度从小到大排序为
题给结论断言:最小的
-
- 对于
i > 1 ,\displaystyle a_i = \left\lfloor \frac{1}{n}\sum_{j=1}^{i-1} a_j \right\rfloor + 1 ; -
下面从必要性与充分性两方面证明该结论。
1. 必要性(下界)
设存在一组可行的军舰长度
引理 1
对任意
特别地,
证明:假设存在某个
考虑 Alice 在所有
首先
此时
另一方面,长度不小于
即单个军舰已超过每行的需求
由引理 1 可知,任何可行解都必须满足
2. 充分性(构造解的上界)
按递推生成序列
引理 2
若一组军舰长度
且总长
证明概要:对
-
基始
r = 1 :条件化为a_i \le S_{i-1} + 1 ,且S_t \ge m 。这是能表示1 到S_t 中任意整数的经典充分条件。对于单独一行 Alice 选择的x \in [1, m] ,贪心地从大到小选取不超过当前余额的最大军舰,总能恰好填满。支付后无需保留军舰,故成立。 -
归纳步:设结论对
r-1 成立。今有满足r 条件的军舰集合,且S_t \ge r \cdot m 。面对第一行 Alice 选择的x (1 \le x \le m ),采用同样的贪心策略:每次选择最大的军舰且其长度\le 当前剩余需求,直到需求降为0 。由条件可证这种选择是可行的,并且被选出的军舰从集合中移除后,剩余军舰仍满足对r-1 的相应不等式,且总长度至少为(r-1)m 。根据归纳假设,剩余军舰可应对后续r-1 行。因此原集合r -可行。
题给构造取
3. 结论
综合必要性与充分性,最小军舰数
的最小正整数