当前位置:网站首页>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;
}
边栏推荐
- EIP-1559
- [character set 6] wide string and multi byte character conversion
- 43 cas d'analyse du réseau neuronal MATLAB: chapitre 7 régression du réseau RBF - - réalisation de la régression fonctionnelle non linéaire
- Top command meaning
- (十五) TweenRunner
- Get last month, current time and next month
- Set up redis sentinel cluster (instance):
- Analysis of 43 cases of MATLAB neural network: Chapter 7 regression of RBF Network -- Realization of nonlinear function regression
- node示例后台搭建
- 128. Plus longue séquence continue - table de hachage
猜你喜欢

Detailed explanation of iSCSI (V) -- actual operation of iSCSI client configuration

Background attribute compound writing

Notes used by mqtt (combined with source code)

分库分表会带来读扩散问题?怎么解决?

About weights exercise

Background fixing effect

Xshell startup encountered "unable to continue code execution because mfc110.dll cannot be found"
![Offer:[day 8 dynamic planning (simple)] --- > maximum profit of stock](/img/42/000a3e601ba1771a1ee07fcd800307.jpg)
Offer:[day 8 dynamic planning (simple)] --- > maximum profit of stock

Flink CheckPoint : Exceeded checkpoint tolerable failure threshold

清华大学数据挖掘笔记(一)
随机推荐
(15) Tweenrunner
2022 low voltage electrician retraining question bank and online simulation examination
Several ways to restart kubernetes pod
Jenkins Pipeline 语法
Flink CheckPoint : Exceeded checkpoint tolerable failure threshold
(node:22344) [DEP0123] DeprecationWarning: Setting the TLS ServerName to an IP address is not permit
Jupyter notebook sets the default browser to open with an error syntaxerror: (Unicode error) 'UTF-8' codec can't decode byte 0xd4
2022 safety officer-c certificate special operation certificate examination question bank and simulation examination
Problems that cannot be resolved by tar command
Description of string
Composition of box model
IP, DNS, domain name, URL, hosts
Flink传入自定义的参数或配置文件
Top command meaning
MFS详解(四)——MFS管理服务器安装与配置
Machine learning notes - circular neural network memo list
Use NVM to dynamically adjust the nodejs version to solve the problem that the project cannot be run and packaged because the node version is too high or too low
ERROR 1630 (42000): FUNCTION a.avg does not exist. Check the ‘Function Name Parsing and Resolution‘
Handling abnormal data
(十四)InputField逻辑分析