Timus 1209. 1, 10, 100, 1000... 根据数列推导公式

本文介绍了一种快速求解特定序列中任意位置数字的方法。针对由递增的10的幂次组成的序列,通过数学公式推导而非循环迭代的方式确定序列任一位置上的数字。示例程序展示了如何高效计算并输出指定位置的数字。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Let's consider an infinite sequence of digits constructed of ascending powers of 10 written one after another. Here is the beginning of the sequence: 110100100010000… You are to find out what digit is located at the definite position of the sequence.

Input

There is the only integerNin the first line (1 ≤N≤ 65535). Thei-th ofNleft lines contains the integerKi— the number of position in the sequence<nobr>(1 ≤<em>K<span style="bottom:-0.4em; position:relative; vertical-align:baseline">i</span></em>≤ 2<span style="position:relative; top:-0.4em; vertical-align:baseline">31</span>− 1)</nobr>.

Output

You are to outputNdigits 0 or 1 separated with a space. More precisely, thei-th digit of output is to be equal to theKi-th digit of described above sequence.

Sample

input output
4
3
14
7
6
0 0 1 0


这道题的关键就是要推导出公式了,如果使用循环,那么肯定是超时的。

根据数列的特征,知道循环的周期是1, 2, 3, 4, 5……

那么就可以知道需要计算k的位置是到了那个周期了,提示到这,看程序吧

void sequence1101001000()
{
	int T = 0;
	long long k = 0, n = 0;
	cin>>T;
	while (T--)
	{
		cin>>k;
		if (k < 3) 
		{
			cout<<1<<' ';
			continue;
		}
		n = sqrt((double)(k<<1));
		n = ((1+n)*n)>>1;
		if (n+1 == k) cout<<1<<' ';
		else cout<<0<<' ';
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值