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\n",ans);
}
}

2019杭电多校赛第九场 Rikka with Mista的更多相关文章

  1. 可持久化线段树的学习(区间第k大和查询历史版本的数据)(杭电多校赛第二场1011)

    以前我们学习了线段树可以知道,线段树的每一个节点都储存的是一段区间,所以线段树可以做简单的区间查询,更改等简单的操作. 而后面再做有些题目,就可能会碰到一种回退的操作.这里的回退是指回到未做各种操作之 ...

  2. 【杂题总汇】HDU2018多校赛第九场 Rikka with Nash Equilibrium

    [HDU2018多校赛第九场]Rikka with Nash Equilibrium 又是靠这样一道题擦边恰好和第两百名分数一样~愉快

  3. 2019杭电多校第七场 HDU - 6656 Kejin Player——概率&&期望

    题意 总共有 $n$ 层楼,在第 $i$ 层花费 $a_i$ 的代价,有 $pi$ 的概率到 $i+1$ 层,否则到 $x_i$($x_i \leq 1$) 层.接下来有 $q$ 次询问,每次询问 $ ...

  4. 2019 杭电多校第八场 HDU - 6665 Calabash and Landlord 两矩形分平面

    题意 给出两个矩形,问这两个矩形把平面分成了几部分. 分析 不需要什么高级技能,只需 “简单” 的分类讨论. (实在太难写了,对拍找出错误都不想改 推荐博客,其中有个很好的思路,即只讨论答案为2,3, ...

  5. [2019杭电多校第十场][hdu6701]Make Rounddog Happy

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6701 题目大意为求满足 $max(a_{l},a_{l+1}\cdot \cdot \cdot a_{ ...

  6. [2019杭电多校第八场][hdu6667]Roundgod and Milk Tea

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6667 题目大意是说n个班级,每个班级有ai人和bi杯茶,每个人只能喝其他班的茶并且只能喝一杯.问最多有 ...

  7. [2019杭电多校第七场][hdu6656]Kejin Player

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6656 题意为从i级花费a元有p的概率升到i+1级,有1-p的概率降到x级(x<i),查询从L级升 ...

  8. [2019杭电多校第七场][hdu6655]Just Repeat

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6655 题意是说两个人都有一些带有颜色的牌,两人轮流出牌,但是不能出对面出过的颜色的牌,最后谁不能出牌谁 ...

  9. [2019杭电多校第七场][hdu6651]Final Exam

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6651 题意:n个科目,总共m分,通过一科需要复习花费科目分数+1分钟,在不知道科目分数的情况下,问最少 ...

随机推荐

  1. spring boot 实践总结(转)

    pring Boot是最流行的用于开发微服务的Java框架.在本文主要分享的是在专业开发中使用Spring Boot所采用的最佳实践.这些内容是基于个人经验和一些熟知的Spring Boot专家的文章 ...

  2. echarts拓扑图(graph,力导向布局图)

    echarts连接:https://gallery.echartsjs.com/editor.html?c=xCLEj67T3H 讲解:https://www.cnblogs.com/koala201 ...

  3. SofaBoot使用Nacos进行服务注册发现

    前提 最近创业公司的项目组基于业务需要,开发一套新的微服务,考虑到选用的组件必须是主流.社区活跃.生态完善以及方便迁移到云上等因素,引入了SOFAStack全家桶.微服务开发里面,一个很重要的功能就是 ...

  4. SpringBoot系列之自定义starter实践教程

    SpringBoot系列之自定义starter实践教程 Springboot是有提供了很多starter的,starter翻译过来可以理解为场景启动器,所谓场景启动器配置了自动配置等等对应业务模块的一 ...

  5. 002使用eop来烧写程序

  6. 洛谷$P4126\ [AHOI2009]$最小割 图论

    正解:网络流+$tarjan$ 解题报告: 传送门$QwQ$ $umm$最小割的判定问题$QwQ$,因为并不会做是看的题解才会的,所以也没什么推导过程直接放结论趴$QwQ$ 首先跑个最大流,然后有. ...

  7. 洛谷$P$2468 粟粟的书架 $[SDOI2010]$ 主席树

    正解:主席树 解题报告: 传送门! 题目大意是说,给定一个矩形,然后每次会给一个,这个大矩形中的一个小矩形,询问从小矩形中最少选多少个数字能满足它们之和大于等于给定数字$x$ 看起来很神的样子,完全不 ...

  8. $Noip2013/Luogu1966$ 火柴排队 贪心+离散化+逆序对

    $Luogu$ $Description$ 给定等长的$a,b$两个序列.每次可以交换一个序列中相邻两个数.求最小的交换次数使得$\sum(a_i-b_i)^2$最小. $Sol$ 交换后的序列一定满 ...

  9. Web开发学习目录

    1.MyBaits 1.1mybatis学习入门 1.2mybatis学习之CURD 1.3mybatis学习之连接池 1.4mybatis学习之动态SQL 1.5mybatis学习之多表操作 1.6 ...

  10. 《深入理解JAVA虚拟机》-自己动手编译JDK

    环境 Ubuntu12.4-http://mirrors.163.com/ubuntu-releases/12.04/ --需要安装纯英文版,避免中文引起不必要的麻烦 OpenJDK7-https:/ ...