panda和卡片
Description
panda喜歡2的冪次,于是他在??的書包?塞了很多寫著2的冪次的卡? 可能多張卡?會(huì)寫著同?個(gè)數(shù)字 panda喜歡那些能??張或者多張卡?的和所表?的數(shù)字 ?如書包?有四張卡?2,4,4,64 那么panda就會(huì)喜歡10,因?yàn)?0=2+4+4 也喜歡0 但是不會(huì)喜歡12,因?yàn)?2=4+4+2+2,可憐的panda只有?張2 現(xiàn)在panda想知道他?共會(huì)喜歡多少個(gè)數(shù)。
Input
?多組測試數(shù)據(jù) 對于每組數(shù)據(jù),第??輸??個(gè)整數(shù)n (1<=n<=50),表?卡?的數(shù)量 第??輸?n個(gè)數(shù),表?每張卡?的數(shù)字 每個(gè)數(shù)字在[1, 2^50]之間
Output
?對于每組數(shù)據(jù)輸出?個(gè)整數(shù)
Sample Input
2 1 2 4 1 1 1 1 7 1 2 2 2 4 4 16 5 1 32 1 16 32Sample Output
4 5 32 18HINT
題解:
#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; }?
總結(jié)
- 上一篇: LLL和小猫
- 下一篇: 粗题⼈不考你没学过的算法