Codeforces 988 D. Points and Powers of Two(数学+结论)

codeforces每日一题。
题意:
给出一个数组,让你挑选出能组成任意pair<u,v>差值为2的幂的序列,并且使这个序列长度尽可能大。

思路:

  1. 针对于挑选出来的序列,任意pair对<u,v>的差值为2的幂数。
  2. 假设有多个pair对,设dis(a,b)=2的x次幂,dis(a,c)等于2的y次幂,即有dis(a,c)=dis(a,b)+dis(b,c)=2的x次幂+2的y次幂,很明显若是dis(a,c)为2的幂数,那么x==y,可得每对pair的dis是相等的。
  3. 那么对于第四对pair,dis(a,d)=3*dis(a,b),很明显不符合条件。
  4. 所以这道题的目的就是在数组里寻找是否有长度为3的序列满足题意,如果没有再考虑2,再考虑1 。

代码如下:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define mod 1000000007
#define PI acos(-1)
#define fi first
#define se second
#define lowbit(x) (x&(-x))
#define mp make_pair
#define pb push_back
#define ins insert
#define si size()
#define E exp(1.0)
#define fixed cout.setf(ios::fixed)
#define fixeds(x) setprecision(x)
#pragma GCC optimize(2)
using namespace std;
inline ll read(){ll s=0,w=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();return s*w;}
void put1(){ puts("YES") ;}void put2(){ puts("NO") ;}void put3(){ puts("-1"); }
ll qp(ll a,ll b, ll p){ll ans = 1;while(b){if(b&1){ans = (ans*a)%p;--b;}a =
(a*a)%p;b >>= 1;}return ans%p;}ll Inv(ll x,ll p){return qp(x,p-2,p);}
ll Cal(ll n,ll m,ll p){if (m>n) return 0;ll ans = 1;for(int i = 1; i <= m; ++i)
ans=ans*Inv(i,p)%p*(n-i+1)%p;return ans%p;}

const int manx=2e5+5;

ll lg[manx],a[manx];;
set<ll>s;
vector<ll>ans;

int main(){
    ll n=read(),flag=0;
    for(int i=1;i<=n;i++){
        ll x=a[i]=read();
        s.ins(x);
    }
    lg[0]=1;
    for(int i=1;i<=32;i++) lg[i]=lg[i-1]*2;
    for(int i=1;i<=n;i++){
        for(int j=0;j<=31;j++){
            ll l=a[i]-lg[j],r=a[i]+lg[j];
            if(s.count(l)&&s.count(r)){
                ans.clear();
                ans.pb(l),ans.pb(r),ans.pb(a[i]);
                flag=1;
                break;
            }
            else if(ans.si==0){
                if(s.count(l)) ans.pb(l),ans.pb(a[i]);
                else if(s.count(r)) ans.pb(r),ans.pb(a[i]);
            }
        }
        if(flag) break;
    }
    if(ans.si>0){
        cout<<ans.si<<endl;
        for(auto v: ans)
            cout<<v<<" ";
    }
    else cout<<1<<endl<<a[1];
    return 0;
}

发布了81 篇原创文章 · 获赞 47 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/JiangHxin/article/details/104884971