链接:https://ac.nowcoder.com/acm/contest/10746/F
来源:牛客网
题目描述
给你一个数字N依序减掉1,2,3,…直到不够减。如果刚好减到0就结束,否则就加上N继续从1开始减,直到减到0为止。
请给出一个数字,计算对于该数字一共重复了几次这个过程。
输入描述:
输入的第一行有一个正整数 t, 1 \le t \le 10^4t,1≤t≤10
4
,代表测试数据的组数
接下来t行每行一个数 N, 1 \le N \le 10^8N,1≤N≤10
8
输出描述:
对于每组输入,若能够在有限次内削减为0,在一行中输出重复的过程次数,否则输出 “Impossible”。
示例1
输入
复制
3
1
2
3
输出
复制
1
2
1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main(){
int T;
scanf("%d", &T);
while(T --){
int n;
scanf("%d", &n);
int x = 1;
int temp = n;
while(temp >= x){
temp -= x;
x ++;
}
for (int i = 1; ; i ++){
if (i * temp % x == 0){
cout << i << endl;
break;
}
}
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M = (int)1e3;
const int N = (int)1e5;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const ll mod = (ll)1e9 + 7;
unordered_map<int, int> mp;
int cal(int n)
{
int l = 1, r = (int)1e5, mid;
while(l < r)
{
mid = (l + r + 1) >> 1;
if(1ll * mid * (mid + 1) / 2 <= n) l = mid;
else r = mid - 1;
}
return 1ll * r * (r + 1) / 2;
}
void work()
{
int n; scanf("%d", &n);
if(mp.count(n))
{
printf("%d\n", mp[n]);
return;
}
int m = n;
int p = 0;
while(1)
{
++p;
n -= cal(n);
if(n == 0)
{
mp[m] = p;
printf("%d\n", p);
return;
}
n += m;
}
}
int main()
{
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int T; scanf("%d", &T);
while(T--) work();
// work();
return 0;
}