Problem Description
Rikka is a fervent fan of JoJo’s Bizarre Adventure. As the last episode of Golden Wind has been aired, Rikka, with the help of Yuta, sets up this problem to express the love to Mista.
Mista’s lucky number is 4. Today, Mista wants to test his luck with n magic cards: For each card, there is a non-negative integer on each side. The two numbers on the ith card are wi and 0.
Firstly, Mista puts these n cards to table one by one. For each card, the probability of the wi side to be upward is 12, and these probabilities are independent with each other. As a result, there are n integers on the table. Mista then sums up all these integers and counts the number of 4s in the decimal representation of this sum: He uses this result to measure his luckiness.
Since it’s possible for each side of each card to be upward, there are 2n possible states in total. You are required to calculate the sum of the results for all these situations.
Input
The first line of the input contains a single integer T(4≤T≤4), the number of test cases.
For each test case, the first line contains a single integer n(4≤n≤40), the number of the cards.
The second line contains n integers w1,…,wn(4≤wi≤44444444), the positive numbers on the cards.
Output
For each test case, output a single line with a single integer, the answer.
Hint
There are 44 4s in the sample input. Mista would like this sample input.
In the first test case, there is 1 state with the sum equal to 0; 4 states with the sum equal to 4; 6 states with the sum equal to 8; 4 states with the sum equal to 12 and 1 state with the sum equal to 16.
Therefore, there are only 4 situations with the result equal to 1 while on other cases, the result is 0. So the answer should be 4.
Sample Input
4
4
4 4 4 4
4
4 4 44 44
4
4 44 44 4444
4
444 44444 44444 4444444
Sample Output
4
10
24
38
题目大意
给出N个数,求它们相加出的所有数中位数上含‘4’的总个数
N<=40,ai<=44444444
N=40那么N/2=20,可以状压前20个数的所有可能和后20个数的所有可能
由于20个数加在一起只有10^10,可以考虑按位枚举
问题就转化成对于两个1e6的序列,求各选一个相加后值在区间[L,R]的方案数
可以two pointer O(n)
现在计算复杂度
O(10NlogN)(N=1e6)
然后会T
复杂度来自排序,O(n)可以考虑桶排
考虑按位桶排序
随着位数的添加,新加入的最高位如果相等,按顺序加入桶的数的顺序不用改变
可以省去log
复杂度O(10N)
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#define maxn 1100005
#define INF 0x3f3f3f3f
using namespace std;
inline int getint()
{
int num=0,flag=1;char c;
while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;
while(c>='0'&&c<='9')num=num*10+c-48,c=getchar();
return num*flag;
}
int n;
int p[45];
int n1,n2;
long long a[maxn],b[maxn];
long long aa[maxn],bb[maxn];
long long pw[11];
vector<long long>B[10];
long long ans;
inline void bsort(int k)
{
for(int i=0;i<10;i++)B[i].clear();
for(int i=1;i<=n1;i++)B[(a[i]%pw[k])/pw[k-1]].push_back(a[i]);
for(int i=0,cnt=0;i<10;i++)for(int j=0,sz=B[i].size();j<sz;j++)a[++cnt]=B[i][j];
for(int i=0;i<10;i++)B[i].clear();
for(int i=1;i<=n2;i++)B[(b[i]%pw[k])/pw[k-1]].push_back(b[i]);
for(int i=0,cnt=0;i<10;i++)for(int j=0,sz=B[i].size();j<sz;j++)b[++cnt]=B[i][j];
}
inline void init()
{
int m=n/2;
n1=1<<m,n2=1<<(n-m);
for(int i=0;i<n1;i++)for(int j=0;j<m;j++)
if(i&(1<<j))a[i+1]+=p[j];
for(int i=0;i<n2;i++)for(int j=0;j<(n-m);j++)
if(i&(1<<j))b[i+1]+=p[j+m];
}
int main()
{
int T=getint();
pw[0]=1;for(int i=1;i<=10;i++)pw[i]=pw[i-1]*10;
while(T--)
{
ans=0;
n=getint();
for(int i=0;i<n;i++)p[i]=getint();
memset(a,0,sizeof a),memset(b,0,sizeof b);
init();
for(int k=1;k<=10;k++)
{
bsort(k);
for(int i=1;i<=n1;i++)aa[i]=a[i]%pw[k];
for(int i=1;i<=n2;i++)bb[i]=b[i]%pw[k];
for(int i=1,p1=n2,p2=n2;i<=n1;i++)
{
while(aa[i]+bb[p1]>=4ll*pw[k-1]&&p1)p1--;
while(aa[i]+bb[p2]>=5ll*pw[k-1]&&p2)p2--;
ans+=p2-p1;
}
for(int i=1,p1=n2,p2=n2;i<=n1;i++)
{
while(aa[i]+bb[p1]>=14ll*pw[k-1]&&p1)p1--;
while(aa[i]+bb[p2]>=15ll*pw[k-1]&&p2)p2--;
ans+=p2-p1;
}
}
printf("%lld
",ans);
}
}