1. 程式人生 > >Barbells(三進位制)

Barbells(三進位制)

題意:給你一串b數字。一串p。左右槓鈴需要相等 然後記錄加過重量的槓鈴的重量。如題中所示。

然後對於一個p 可以加左邊 可以加右邊 或者不加三種情況。所以可以用三進位制來表示。

#include<bits/stdc++.h>
using namespace std;

#define pi acos(-1)
#define endl '\n'
#define rand() srand(time(0));
#define me(x) memset(x,0,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0);

typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
//const int dx[]={-1,0,1,0,-1,-1,1,1};
//const int dy[]={0,1,0,-1,1,-1,1,-1};
const int maxn=1e4+5;
const int maxx=1e6+100;
const double EPS=1e-7;
const int MOD=10000007;
typedef pair<int, int>P;
#define mod(x) ((x)%MOD);
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
long long gcd(long long a , long long b){if(b==0) return a;a%=b;return gcd(b,a);}
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define W while


int a[20],b[20];
LL qpow(LL x,LL y)
{
    LL res=1;
    while(y)
    {
        if(y&1) res=res*x;
        y>>=1;
        x=x*x;
    }
    return res;
}
set<LL>s;
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    for(int i=1;i<=m;i++)
    {
        cin>>b[i];
    }
    int temp=qpow(3,m);//同二進位制
    for(int i=0;i<temp;i++)
    {
        int t=i,cont=1;
        LL sum1=0,sum2=0;
        while(t)
        {
            if(t%3==1) sum1+=b[cont];
            if(t%3==2) sum2+=b[cont];
            cont++,t/=3;
        }
        if(sum1==sum2)
        {
            FOR(1,n,j)
                s.insert(a[j]+sum1+sum2);
        }
    }
    foreach(it,s)
        cout<<*it<<endl;
}