题目大意:
共有T组测试数据,每组数据给定一个n。
给定一个 n, 找到一个 使得 l + ( l + 1 ) + ... + ( r - 1 ) + r = n。
其中
Example
Input
7
1
2
3
6
100
25
3000000000000
Output0 1 -1 2 1 2 1 3 18 22 -2 7 999999999999 1000000000001
思路:
观察 n == 2 时的输出
可发现 令 l = - ( n + 1 ), r = n 即可。
AC代码:
#include <iostream>
using namespace std;
int main()
{
int T;
cin >> T;
long long n;
while(T--)
{
cin >> n;
cout << -(n - 1) << " " << n << endl;
}
return 0;
}
题目大意:
共T组数据,每组数据给定一个 n, k。
其中,每个 n 决定一个数列,为 n 的各次幂的和组成的升序序列。
如 n = 3 时 这个数列为 {1,3,4,9...},输出这个数列的第 k 个数。
思路:
利用二进制特性,将 k 转化为二进制,从第0位开始以此检验 k 第 i 位是否为1,若第 i 位为1,则res += 所得 res 即为答案。
Example
Input
3 3 4 2 12 105 564Output
9 12 3595374
AC代码:
#include <iostream>
using namespace std;
const int mod = 1e9 + 7;
int main()
{
int t;
cin >> t;
while(t--)
{
int n, k;
cin >> n >> k;
long long m = 1;
long long res = 0;
for(int i = 0; i < 30; i++)
{
if((k >> i) & 1)
res += m;
m *= n;
m %= mod;
}
res %= mod;
cout << res << endl;
}
return 0;
}