panda和卡片

版权声明:来自星空计算机团队——申屠志刚 https://blog.csdn.net/weixin_43272781/article/details/84452511

Description

panda喜欢2的幂次,于是他在⾃⼰的书包⾥塞了很多写着2的幂次的卡⽚ 可能多张卡⽚会写着同⼀个数字 panda喜欢那些能⽤⼀张或者多张卡⽚的和所表⽰的数字 ⽐如书包⾥有四张卡⽚2,4,4,64 那么panda就会喜欢10,因为10=2+4+4 也喜欢0 但是不会喜欢12,因为12=4+4+2+2,可怜的panda只有⼀张2 现在panda想知道他⼀共会喜欢多少个数。

Input

 多组测试数据 对于每组数据,第⼀⾏输⼊⼀个整数n (1<=n<=50),表⽰卡⽚的数量 第⼆⾏输⼊n个数,表⽰每张卡⽚的数字 每个数字在[1, 2^50]之间

Output

 对于每组数据输出⼀个整数

Sample Input

2
1 2
4
1 1 1 1
7
1 2 2 2 4 4 16
5
1 32 1 16 32

Sample Output

4
5
32
18

HINT

题解:

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
 
using namespace std;
 
class PowersOfTwo {
public:
  long long count(vector <long long> powers);
};
 
long long PowersOfTwo::count(vector <long long> powers) {
  long long a[51];
  for (int i=0;i<=50;i++)
    a[i]=0;
  long long p[52];
  p[0]=1;
  for (int i=1;i<=50;i++)
    p[i]=p[i-1]*2;
  vector <long long>::iterator cur;
  for (cur=powers.begin();cur<powers.end();cur++)
    for (int j=0;j<=50;j++)
      if (*cur==p[j]) a[j]++;
  long long S=0;
  for (int i=0;i<=50;i++)
  {
    S+=p[i]*a[i];
    for (int j=i+1;j<=50;j++)
      if (S>=p[j])
      {
        a[i]+=p[j-i]*a[j];
        S+=p[j]*a[j];
        a[j]=0;
      }
  }
  S=1;
  for (int i=0;i<=50;i++)
    S*=(a[i]+1);
  return S;
}

int main() {
	PowersOfTwo test;
    int n;
    while(cin >> n) {
        vector <long long> a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        cout << test.count(a) << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43272781/article/details/84452511