URAL 1209. 1, 10, 100, 1000... (规律 + 二分)

33 篇文章 0 订阅
23 篇文章 0 订阅

1209. 1, 10, 100, 1000...

Time limit: 1.0 second
Memory limit: 64 MB
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 integer  N in the first line (1 ≤  N ≤ 65535). The  i-th of  N left lines contains the integer  Ki — the number of position in the sequence  (1 ≤  Ki ≤ 2 31 − 1) .

Output

You are to output  N digits 0 or 1 separated with a space. More precisely, the  i-th digit of output is to be equal to the  Ki-th digit of described above sequence.

Sample

input output
4
3
14
7
6
0 0 1 0




题意:给定n个数ki,问1101001000......的第ki个位置的数字是0还是1.

解析:利用规律可推出第i个‘1’的位置编号为 i * (i - 1) / 2 + 1,利用二分判断询问的位置是否为1即可。



AC代码:

#include <cstdio>

int check(long long x){
    long long r = 1LL<<31, l = 0, mid, y;
    while(l <= r){                      //二分判断是否为1
        mid = l + (r - l)/2;
        y = mid*(mid-1)/2 + 1;
        if(y == x) return 1;
        else if(y < x) l = mid + 1;
        else r = mid - 1;
    }
    return l <= r;
}

int main(){
    #ifdef sxk
        freopen("in.txt", "r", stdin);
    #endif //sxk

    int n;
    long long c;
    scanf("%d", &n);
    for(int i=0; i<n; i++){
        scanf("%lld", &c);
        printf("%d%c", check(c), i < n ? ' ' : '\n');
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值