CF1195B

CF1195B

题意:

有一个盒子,每次可以做两个操作:

1.每次吃掉一块蛋糕
2.每次放入比上一次放入数多1的蛋糕
当盒子为空时,只能执行第 $ 2 $ 个操作。第 $ 1 $ 次操作永远是放入一个蛋糕。 现在给出操作数 $ n $ 和操作后蛋糕数量 $ k $ ,求出吃了多少块蛋糕。

解法:

解方程 $ frac{x(x+1)}{2} - (n - x) = k $
化简得 $ x = frac{sqrt{9+8(n+k)}-3}{2} $

CODE:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
 
using namespace std;
 
#define LL long long
#define N 100100
 
LL n,k,ans;
 
int main() {
    scanf("%lld%lld",&n,&k);
    int i = 1;
    for(LL i = 1 ; ; i++) {
		if (i * (i + 1) / 2 - (n - i) == k) {
			printf("%lld
", n - i);
            //system("pause");
			return 0;
		}
	}
    return 0;
}
原文地址:https://www.cnblogs.com/Repulser/p/11421225.html