Ural1209(數學推導)
來源:程序員人生 發布時間:2015-04-01 08:17:08 閱讀次數:2530次
題目鏈接:點擊打開鏈接
解題思路:
此題甚好。推導公式,首先視察序列110100100010000?????,我們把為1的下標單獨拿出來看。順次為1、2、4 、7、 11?????,可以分解為1+(0) 、1+(0+1)、1+(0+1+2)、1+(0+1+2+3)、1+(0+1+2+3+4),可以推導出規律1 + x * (x - 1) / 2。
那末對每一個n,我們只要判斷是不是存在x使n == 1 + x * (x - 1) / 2便可。對最后判斷開根號是不是為整數的判斷方法,我們可以用temp - (int)temp == 0來判斷,此法甚妙!
完全代碼:
#include <algorithm>
#include <iostream>
#include <cstring>
#include <climits>
#include <cstdio>
#include <string>
#include <cmath>
#include <map>
#include <queue>
using namespace std;
typedef long long LL;
const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const double EPS = 1e⑼;
const double PI = acos(⑴.0); //M_PI;
const int maxn = 700001;
int ans[maxn];
int main()
{
#ifdef DoubleQ
freopen("in.txt","r",stdin);
#endif
int T;
cin >> T;
LL n;
int cnt = 0;
for(int i = 0 ; i < T ; i ++)
{
cin >> n;
double temp = sqrt(1 + 8 * (n - 1));
if(temp - (int)temp == 0)
ans[cnt++] = 1;
else
ans[cnt++] = 0;
}
for(int i = 0 ; i < cnt ; i ++)
printf("%d%s" , ans[i] , i == cnt - 1 ? "
" : " ");
}
更多精彩請訪問:點擊打開鏈接
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈