Ural1209(數(shù)學(xué)推導(dǎo))
來(lái)源:程序員人生 發(fā)布時(shí)間:2015-04-01 08:17:08 閱讀次數(shù):2525次
題目鏈接:點(diǎn)擊打開(kāi)鏈接
解題思路:
此題甚好。推導(dǎo)公式,首先視察序列110100100010000?????,我們把為1的下標(biāo)單獨(dú)拿出來(lái)看。順次為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),可以推導(dǎo)出規(guī)律1 + x * (x - 1) / 2。
那末對(duì)每一個(gè)n,我們只要判斷是不是存在x使n == 1 + x * (x - 1) / 2便可。對(duì)最后判斷開(kāi)根號(hào)是不是為整數(shù)的判斷方法,我們可以用temp - (int)temp == 0來(lái)判斷,此法甚妙!
完全代碼:
#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 ? "
" : " ");
}
更多精彩請(qǐng)?jiān)L問(wèn):點(diǎn)擊打開(kāi)鏈接
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)