7 30 个人赛

C

A sequence a1,a2,…,an is called good if, for each element ai, there exists an element aj (i≠j) such that ai+aj is a power of two (that is, 2d for some non-negative integer d).

For example, the following sequences are good:

  • [5,3,11]

(for example, for a1=5 we can choose a2=3. Note that their sum is a power of two. Similarly, such an element can be found for a2 and a3),

  • [1,1,1,1023],
  • [7,39,89,25,89],
  • [].

Note that, by definition, an empty sequence (with a length of 0

) is good.

For example, the following sequences are not good:

  • [16]

(for a1=16, it is impossible to find another element aj such that their sum is a power of two),

  • [4,16]
  • (for a1=4, it is impossible to find another element aj such that their sum is a power of two),
  • [1,3,2,8,8,8]
  • (for a3=2, it is impossible to find another element aj

    You are given a sequence a1,a2,…,an. What is the minimum number of elements you need to remove to make it good? You can delete an arbitrary set of elements.

    Input

    The first line contains the integer n(1≤n≤120000) — the length of the given sequence.

    The second line contains the sequence of integers a1,a2,…,an(1≤ai≤109).

    Output

    Print the minimum number of elements needed to be removed from the given sequence in order to make it good. It is possible that you need to delete all n

    elements, make it empty, and thus get a good sequence.

    • such that their sum is a power of two).

Examples

Input

6
4 7 1 5 4 9

Output

1

Input

5
1 2 3 4 5

Output

2

Input

1
16

Output

1

Input

4
1 1 1 1023

Output

0

Note

In the first example, it is enough to delete one element a4=5. The remaining elements form the sequence [4,7,1,4,9], which is good.

 题意:给你一个数列,然后让你删掉最少的数,让每一个数都可以与另外一个数相加,之和等于2的^d次方,问最少删掉的个数

 AC代码:

#include<iostream>
#include <algorithm>
#include <cstdlib>
#include<cstdio>
#include<cstring>
#include <cmath>
#include <map>
using namespace std;
const int M=200000+10;
const int MAX=0x3f3f3f3f;
typedef long long ll;
map<ll,ll> m;
char s[2010000];
int main()
{
    ll n,a[201000],i,j,ans=0;
    scanf("%lld",&n);
    for(i=0;i<n;i++)
    {
        scanf("%lld",&a[i]);
        m[a[i]]++;
    }
    for(i=0;i<n;i++)
    {
        ll t=1,g=0;
        for(j=0;j<31;j++)//2的30次方超过了10的9次方
        {
            if(m[t-a[i]]!=0)
            {
                if(t-a[i]==a[i])//如果两个数相等  例如2 必须满足至少两个2才可以
                {
                   if(m[a[i]]>1)
                    g++;
                }
                else
                    g++;
            }
            t*=2;
        }
        if(g==0)
            ans++;
    }
    printf("%lld\n",ans);
    return 0;

}

D

Polycarp likes numbers that are divisible by 3.He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3.

For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3.

Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid.

What is the maximum number of numbers divisible by 3  that Polycarp can obtain?

Input

The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2⋅105, inclusive. The first (leftmost) digit is not equal to 0.

Output

Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s.

Examples

Input

3121

Output

2

Input

6

Output

1

Input

1000000000000000000000000000000000

Output

33

Input

201920181

Output

4

Note

In the first example, an example set of optimal cuts on the number is 3|1|21.

In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3.

In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33

digits 0. Each of the 33 digits 0 forms a number that is divisible by 3.

In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3.

题意:给一个仅包含数字的字符串,将字符串分割成多个片段(无前导0),求这些片段里最多有多少是3的倍数 

//此代码没有AC 欢迎大佬提出错误
#include<iostream>
#include <algorithm>
#include <cstdlib>
#include<cstdio>
#include<cstring>
#include <cmath>
using namespace std;
const int M=200000+10;
const int MAX=0x3f3f3f3f;
typedef long long ll;
char s[2010000];
int main()
{
    ll i,a,n,b,ans=0;
    memset(s,0,sizeof(s));
    gets(s);
    n=strlen(s);
    if(n==1)
    {
        if((s[0]-48)%3==0)
            printf("1\n");
        else
            printf("0\n");
    }
    else
    {
        ll k=0;
        for(i=0;i<n;i++)
        {
            if(s[i]!='0')
            {
                k=i;
                break;
            }
        }
        for(i=k; i<n; i++)
        {
            if((s[i]-48)%3==0)
                ans++;
            else
                if(((s[i]-48)*10+(s[i+1]-48))%3==0)
                {
                    ans++;
                    i+=1;
                }

            else
            if(((s[i]-48)*100+(s[i+1]-48)*10+(s[i+2]-48))%3==0)
            {
                ans++;
                i+=2;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

#include<iostream>
#include <algorithm>
#include <cstdlib>
#include<cstdio>
#include<cstring>
#include <cmath>
using namespace std;
const int M=200000+10;
const int MAX=0x3f3f3f3f;
typedef long long ll;
char s[2010000];
int main()
{
    ll n,sum=0,ans=0,e=0,i;
    memset(s,0,sizeof(s));
    gets(s);
    n=strlen(s);
    for(i=0;i<n;i++)
    {
        ll t=(s[i]-'0')%3;
        sum+=t;
        e++;
        if(e==3||sum%3==0||t%3==0)
        {
            sum=0;
            e=0;
            ans++;
        }
    }
    printf("%lld\n",ans);
    return 0;
}

另一大佬代码


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include <cstring>
#include <map>
using namespace std;
typedef long long LL;
int main()
{
    char a[200020];
    int b[200020];
    int i,j,k,ans=0;
    gets(a);
    int len=strlen(a);
    for(i=0;i<len;i++)
        b[i]=a[i]-48;
    b[len]=-1;
    b[len+1]=-1;
    for(i=0;i<len;)
    {
        if(b[i]%3==0)
        {i++;ans++;}
        else
        {
            int x,y;
            x=b[i]%3;
            y=b[i+1]%3;
            if(x==1&&y==2)
            {i+=2;ans++;}
            else if(x==2&&y==1)
            {i+=2;ans++;}
            else if(y==0)
            {i+=2;ans++;}
            else if(x==-1||y==-1)//判断是否是后面的
                break;
            else
            {
                if(len-i<=2)//判断是否是后面的
                 i+=3;
                else
                {ans++;i+=3;}
            }
        }

    }
    cout<<ans<<endl;
    return 0;
}

 

posted @ 2018-07-31 13:24  ~~zcy  阅读(149)  评论(0编辑  收藏  举报