X is fighting beasts in the forest, in order to have a better chance to survive he’s gonna buy upgrades for his weapon. Weapon upgrade shops are available along the forest, there are n shops, where the ith of them provides an upgrade with energy ai. Unfortunately X can make use only of the maximum power of two that divides the upgrade energy, lets call it the powerincrement. For example, an upgrade with energy of 6 has a power increment of 1 because 6 is divisible by 21 but not divisible by 22 or more, while for upgrade with energy of 5 power increment is 0, also after buying a weapon with energy v, X can only buy upgrades with energies greater than v, in other words, the upgrades energies that X is gonna buy should be in strictly increasing order. X is wondering what is the maximum power he can achieve at the end of his journey. note that only the energy of upgrades should be in strictly increasing order not the power increment of the upgrade. 1 < n < 100, 1 ≤ ai ≤ 106
Input
The first line of input contains one integer, T, which denotes number of test cases. Each test case contains two lines, the first one contains one integer n which denotes number of shops, and the next line contains n integers the ith of them denotes the energy of the upgrade provided by the ith shop.
Output
Print one integer denotes maximum power X can achieve at the end of his journey
Example
Input
2
3
1 10 16
2
8 12
Output
5
5
题意:每个商店升级能力是ai,升级功能是ai以2的k次幂为因数的k的最大值(例如,ai=14,k=1;ai=8,k=3)。求最后升级后能力最大值。(购买的能力需要是递增的,也就是a[i]>a[i-1])
思路:dp动归
dp[i]表示升级到i商店结止的能力。
dp[0]=0;
dp[i]=max(dp[i],dp[j]+k[i])
/**/
#include<bits/stdc++.h>
using namespace std;
int ppow[110];
void findx()//2的17次方在1e5左右,2的20次方在1e6左右
{
int i;
int k=1;
for(i=1;i<=17;i++)
{
k*=2;
ppow[i]=k;
}
}
int main()
{
int t,i,j,n;
int a[110];
int dp[110];
int k[110];
cin>>t;
findx();
while(t--)
{
cin>>n;
for(i=1;i<=n;i++)
{
cin>>a[i];
for(j=1;j<=17;j++)
{
if(a[i]%ppow[j]!=0)
break;
}
dp[i]=j-1;
k[i]=j-1;
}
dp[0]=0;
for(i=2;i<=n;i++)
{
for(j=1;j<i;j++)
{
if(a[i]>a[j])
dp[i]=max(dp[i],dp[j]+k[i]);
}
}
int ans=0;
for(i=1;i<=n;i++)
{
ans=max(ans,dp[i]);
}
cout<<ans<<endl;
}
return 0;
}