题意
定义奇妙数x 为,存在两个正整数b>a,x = b * b - a * a
求第n个奇妙数,1<=n<=1e9
思路
b * b - a * a = (b+a) * (b-a)
- 当b,a奇偶性不同时,b+a,b-a都是奇数,所以(b+a) * (b-a)是奇数。我们发现,所有奇数,除了1,都是奇妙数。
- 当b,a奇偶性相同时,b+a,b-a都是偶数,所以(b+a) * (b-a)是4的倍数,我们发现,所有4的倍数,除了4,都是奇妙数。
代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 200010;
const int mod = 1e9 + 7;
/*
b*b - a*a = (b + a) * (b - a), b > a
case 1: b and a have different parity.
---> all odd numbers are avaiable.(expect for 1)
case 2: b and a have the same parity.
---> all numbers, which modulos 4 is 0, is available(expect for 4)
x = 2 * k + 1, k >= 1
x = 4 * k, k >= 1
3, 5, 7, 8, 9, 11, 12
2 * 2 - 1 * 1 = 3,
3 * 3 - 2 * 2 = 5,
4 * 4 - 3 * 3 = 7,
3 * 3 - 1 * 1 = 8
*/
ll n, m, res;
int mp[] = {0, 1, 3};
void solve() {
scanf("%lld", &n);
if (n == 1LL) {
res = 3;
} else {
n -= 1;
res = 4LL + 4 * (n / 3);
n %= 3;
res += mp[n];
}
printf("%lld\n", res);
}
int main() {
int t;
// scanf("%d", &t);
t = 1;
while (t--) {
solve();
}
}