当前位置:网站首页>Subtractive integer (number theory)
Subtractive integer (number theory)
2022-06-12 09:04:00 【Little wish】
link :https://ac.nowcoder.com/acm/contest/10746/F
source : Cattle from
Title Description
Here's a number N Subtract sequentially 1,2,3,… Until it's not enough . If it happens to be reduced to 0 ends , Otherwise, add N Continue from 1 Begin to reduce , Until it's reduced to 0 until .
Please give me a number , The calculation repeats the process several times for this number .
Input description
The first line of input has a positive integer t t t, 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1≤t≤104, Represents the number of groups of test data
Next t One number per line N N N, 1 ≤ N ≤ 1 0 8 1 \le N \le 10^8 1≤N≤108
Output description
For each group of input , If it can be reduced to 0, Output the number of times the process is repeated in one line , Otherwise output “Impossible”.
The sample input
3
1
2
3
Sample output
1
2
1
Their thinking
BF practice , Simulate subtracting each time 1,2,3,… Until it's not enough , Record times , The optimization method is to record the prefix , Use binary search to complete a process directly , But it will still time out .
There's a trick , Store the values that have been answered , Sure AC, The test data has many duplicate values .
Be careful : You cannot use a stored value in a loop , Because the number added each time x x x The value is different. .
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;
int T, n;
const int N = 1e5;
int a[N], cnt;
void init() {
int i = 1, sum = 0;
while (sum < 1e8) {
sum += i;
a[i] = sum;
i++;
}
cnt = i;
}
map<int, int> mp;
void solve(int x) {
if (mp.count(x)) {
printf("%d\n", mp[x]);
return;
}
int t = x, res = 0;
while (1) {
res++;
int i = lower_bound(a+1, a+cnt, t) - a;
if (t == a[i]) {
mp[x] = res;
printf("%d\n", res);
break;
}
t -= a[i-1];
t += x;
}
}
int main() {
//freopen(" Subtract an integer .in", "r", stdin);
scanf("%d", &T);
init();
while (T--) {
scanf("%d", &n);
solve(n);
}
return 0;
}
Positive solution , hypothesis N N N Not enough minus the remainder is t t t, The next number to subtract is i i i, t < i t<i t<i Keep adding N N N Repeat after , Remainder is 2 t 2t 2t, 3 t 3t 3t,… a t at at, When a t > i at>i at>i when , subtract i i i Remainder after a t − i at-i at−i Less than... Again i i i Of , The remainder of the repetition is ( a + 1 ) t − i (a+1)t-i (a+1)t−i, until b t − i > i bt-i>i bt−i>i, The remainder becomes b t − 2 i bt-2i bt−2i, Repeat , until c t − d i = i ct-di=i ct−di=i, namely c t = l c m ( t , i ) ct=lcm(t,i) ct=lcm(t,i), At this time c c c Is the number of times the process is repeated .
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int T, n;
const int N = 1e5;
int a[N], cnt;
void init() {
int i = 1, sum = 0;
while (sum < 1e8) {
sum += i;
a[i] = sum;
i++;
}
cnt = i;
}
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a%b);
}
void solve(int x) {
int i = lower_bound(a+1, a+cnt, x) - a;
int t = x - a[i-1];
int res = i * t / gcd(i, t);
printf("%d\n", res/t);
}
int main() {
//freopen(" Subtract an integer .in", "r", stdin);
scanf("%d", &T);
init();
while (T--) {
scanf("%d", &n);
solve(n);
}
return 0;
}
边栏推荐
- 分库分表会带来读扩散问题?怎么解决?
- Several ways to restart kubernetes pod
- [character set 8] char8_ t、char16_ t、char32_ t、wchar、char
- Cookies and sessions
- [sklearn] lightgbm
- (十四)InputField逻辑分析
- Analysis of 43 cases of MATLAB neural network: Chapter 7 regression of RBF Network -- Realization of nonlinear function regression
- Background color translucent
- EIP-1559
- 利用nvm动态调整nodejs版本,解决因为node版本过高或过低导致项目无法运行和打包
猜你喜欢
随机推荐
长安链节点证书、角色、权限管理介绍
第六章-包含多个段的程序
128. Plus longue séquence continue - table de hachage
Exists usage in SQL
Convert spaces to < br / > newline labels
Display the remaining valid days according to the valid period
Domain name mapping to specified IP
Background attribute compound writing
128. 最长连续序列-哈希表
剑指 Offer II 016. 不含重复字符的最长子字符串-滑动窗口
Sword finger offer II 016 Longest substring without repeating characters - sliding window
《MATLAB 神经网络43个案例分析》:第7章 RBF网络的回归--非线性函数回归的实现
When the uniapp page jumps with complex data parameters.
Technology cloud report: how will the industrial Internet rebuild the security boundary in 2022?
Construction of memcached cache service under Linux:
Flink CheckPoint : Exceeded checkpoint tolerable failure threshold
Difference between binary GB and gib
Binlog in mysql:
【sklearn学习】LightGBM
ERROR 1630 (42000): FUNCTION a.avg does not exist. Check the ‘Function Name Parsing and Resolution‘



![[data storage] storage of floating point data in memory](/img/c4/a67735858ce5d58bd504b087a2d123.png)





