三角形数:an=n*(n+1)/2;
完全平方数:bn=c^2;
既是三角形数又是完全平方数:An=6*A(n-1)-A(n-2)+2;
A[23]={ 0, 1, 8, 49, 288, 1681, 9800, 57121, 332928, 1940449, 11309768, 65918161, 384199200, 2239277041, 13051463048, 76069501249, 443365544448, 2584123765441, 15061377048200, 87784138523761,511643454094368, 2982076586042449, 17380816062160328};
所以这道题只需要打表就好了。。
注意是让r>=N,a存的是下标。
#include<iostream> #include<cstdio> #include<cstring> using namespace std; typedef long long ll; ll a[23] = { 0, 1, 8, 49, 288, 1681, 9800, 57121, 332928,1940449, 11309768, 65918161, 384199200, 2239277041, 13051463048, 76069501249, 443365544448, 2584123765441, 15061377048200, 87784138523761,511643454094368, 2982076586042449, 17380816062160328 }; int main() { int T,n; cin >> T; for (int cas = 1; cas <= T; cas++) { cin >> n; for (int i = 0; i < 23; i++) { if (a[i] >= n) { cout << "Case #" << cas << ": " << a[i] << endl; break; } } } return 0; }
还有个脑洞特大的做法:
http://blog.csdn.net/hyesuixin/article/details/77916761
其他:
http://blog.csdn.net/a15110103117/article/details/77916895
(感谢博主)