二分查找法

 

                                            3286: 1 10 100 1000

                                                              时间限制: 1 Sec  内存限制: 128 MB
                                                                         提交: 126  解决: 39
                                                            [提交][状态][讨论版][命题人:acm4302]

题目描述

1,10,100,1000...组成序列1101001000...,求这个序列的第N位是0还是1(自左向右)。

输入

第一行一个整数T,表示数组组数
后面T行,每行一个整数N  (N<=10^9)

输出

共T行,如果该位是0,输出0;如果该位是1,输出1

样例输入

3
1
2
3

样例输出

1
1
0

此题没有告诉你要查找哪个数,而是用二分法渐渐缩小范围去试着找出符合条件的那个数。

 


#include <iostream>

using namespace std;
typedef long long ll;
ll Binary_search(int m)  //二分
{
    ll first=1;
    ll last=m;
    while (first<=last)
    {
        ll mid=(first+last)/2;
        ll k=mid*(mid+1)/2;
        if(k==m)
        {
            return 1;
        }
        else if(k<m)
        {
            first=mid+1;
        }
        else if(k>m)
        {
            last=mid-1;
        }
    }
    return 0;
}

int main()
{
    int T;

    cin >>T;
    while (T--)
    {
        int n;
        cin >>n;
        if(n==1)
        {
            cout <<"1"<<endl;
            continue;
        }
        if(Binary_search(n-1))
        {
            cout <<"1"<<endl;
        }
        else
            cout << 0<< endl;

    }


    return 0;
}

 

2  code force----Really Big Numbers

Ivan likes to learn different things about numbers, but he is especially interested in really big numbers. Ivan thinks that a positive integer number x is really big if the difference between x and the sum of its digits (in decimal representation) is not less than s. To prove that these numbers may have different special properties, he wants to know how rare (or not rare) they are — in fact, he needs to calculate the quantity of really big numbers that are not greater than n.

Ivan tried to do the calculations himself, but soon realized that it's too difficult for him. So he asked you to help him in calculations.

Input

The first (and the only) line contains two integers n and s (1 ≤ n, s ≤ 1018).

Output

Print one integer — the quantity of really big numbers that are not greater than n.

Examples

input

12 1

output

3

input

25 20

output

0

input

10 9

output

1

Note

In the first example numbers 10, 11 and 12 are really big.

In the second example there are no really big numbers that are not greater than 25 (in fact, the first really big number is 30: 30 - 3 ≥ 20).

In the third example 10 is the only really big number (10 - 1 ≥ 9).

题意概述:

定义really big数就是那种本身与各项数字之和的差大于等于s的数,给你一个n和s,问不大于n的really big数个数有多少个?

不断二分,找到想要的最小的 really big 值(有序的序列可以用二分找的想要的值

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
using namespace std;

#define  inf 0x3f3f3f3f
const long long mod=998244353;
long long  dp[1010][2010][5];

int check(long long a,long long s)
{
	int sum=0;
	long long n=a;
	while(n)
	{
		sum+=n%10;
		n=n/10;
	}
	if(a-sum>=s) return 1;
	else return 0;
}
int main(int argc, char** argv) 
{   
   	long long  n,s;
   	cin>>n>>s;
   	long long l=0,r=n;
   	long long ans=0;
   	while(l<=r)
   	{ 
   	  long long mid=(l+r)/2;
   	  if(check(mid,s))
   	  {
   	     ans=n-mid+1;
		 r=mid-1;	
	  }
	  else l=mid+1;
   	  
	}
	
	cout <<ans<<endl;
    
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值