HDU 7105 Power Sum
题目链接:HDU 7105 Power Sum
题目大意:
题解:
易知\((x+1)^2-(x+2)^2-(x+3)^2+(x+4)^2=4\),所以可以令\(n\)对\(4\)取模,根据余数\(0,1,2,3\)取不同的\(01\)序列,后面加上\(\frac{n}{4}\)个“\(1001\)”即可。
#include <iostream>
#include <string>
using namespace std;
int t, n;
const int ki[] = {0, 1, 4, 2};
const string ti[] = {"", "1", "0001", "01"};
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> t;
while (t--) {
cin >> n;
string ans = ti[n % 4];
int k = ki[n % 4];
while (n >= 4) {
ans += "1001";
k += 4;
n -= 4;
}
cout << k << endl << ans << endl;
}
return 0;
}