A. Consecutive Sum Riddle( Codeforces Round #747 (Div. 2))
Posted ~晚风微凉~
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了A. Consecutive Sum Riddle( Codeforces Round #747 (Div. 2))相关的知识,希望对你有一定的参考价值。
原题:
Theofanis has a riddle for you and if you manage to solve it, he will give you a Cypriot snack halloumi for free (Cypriot cheese).
You are given an integer nn. You need to find two integers ll and rr such that −1018≤l<r≤1018−1018≤l<r≤1018 and l+(l+1)+…+(r−1)+r=nl+(l+1)+…+(r−1)+r=n.
Input
The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases.
The first and only line of each test case contains a single integer nn (1≤n≤10181≤n≤1018).
Output
For each test case, print the two integers ll and rr such that −1018≤l<r≤1018−1018≤l<r≤1018 and l+(l+1)+…+(r−1)+r=nl+(l+1)+…+(r−1)+r=n.
It can be proven that an answer always exists. If there are multiple answers, print any.
Example
input
Copy
7 1 2 3 6 100 25 3000000000000
output
Copy
0 1 -1 2 1 2 1 3 18 22 -2 7 999999999999 1000000000001
Note
In the first test case, 0+1=10+1=1.
In the second test case, (−1)+0+1+2=2(−1)+0+1+2=2.
In the fourth test case, 1+2+3=61+2+3=6.
In the fifth test case, 18+19+20+21+22=10018+19+20+21+22=100.
In the sixth test case, (−2)+(−1)+0+1+2+3+4+5+6+7=25(−2)+(−1)+0+1+2+3+4+5+6+7=25.
卡点:
1.最小的负数边界点怎么确定
2.想用二分
标签:
1.找规律、数学
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll l,r,n;
int t;
int main()
scanf("%d",&t);
while(t--)
scanf("%lld",&n);
printf("%lld %lld\\n",-n+1,n);
return 0;
总结:其实很简单,不要被样例给误导了,答案有多个只需要输出一个。
因为是加法,连续加,递增数列,那么其实每个数都可以出现在加的数里,只需要把前面多的一连串用相应的复数形式抵消掉就好了。
以上是关于A. Consecutive Sum Riddle( Codeforces Round #747 (Div. 2))的主要内容,如果未能解决你的问题,请参考以下文章
CodeForces - 1594A Consecutive Sum Riddle数学计算
CodeForces - 1594A Consecutive Sum Riddle数学计算
Sum of Consecutive Prime Numbers