数码管从某个状态顺序转移N个状态 计算总共有多少个数码管被点亮 N<=10^9

观察数码管的变化规律,有明显的周期和重复,利用这个性质,计算相对于初始状态,某一位上的某个状态重复了多少次,就可以在常数时间内求得。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long int LL; int main()
{freopen("t.txt","r",stdin);
LL val[16]={6,2,5,5,4,5,6,3,7,6,6,5,4,5,5,4};
LL hpo[9];
hpo[0]=1;
for(int i=1;i<=9;i++)
hpo[i]=hpo[i-1]*16;
int n;
char cc[9];
int num[8];
int T;
scanf("%d",&T);
LL ans;
while(T--)
{
scanf("%d%s",&n,&cc);
for(int i=0;i<8;i++)
{
if(cc[i]<='9'&&cc[i]>='0')num[i]=cc[i]-'0';
if(cc[i]<='F'&&cc[i]>='A')num[i]=cc[i]-'A'+10;
}
ans=0;
for(int i=0;i<8;i++)
ans+=val[num[i]];
LL nv=n;
ans=ans*nv;
LL res=0;
for(int i=7;i>=0;i--)
{
nv=n;
LL pow=hpo[7-i];
nv+=res;
res+=num[i]*pow;
for(int j=1;j<16&&nv>pow;j++)
{
ans+=(((nv-pow)/(pow*16))*pow)*(val[(num[i]+j)%16]-val[num[i]]);
ans+=min((((nv-pow)%(pow*16))),pow)*(val[(num[i]+j)%16]-val[num[i]]);
nv-=pow;
}
}
printf("%lld\n",ans);
}
return 0;
}

  

ZOJ3962 2017 E.Seven Segment Display的更多相关文章

  1. (2017浙江省赛E)Seven Segment Display

    Seven Segment Display Time Limit: 2 Seconds      Memory Limit: 65536 KB A seven segment display, or ...

  2. 2017浙江省赛 E - Seven Segment Display ZOJ - 3962

    地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3962 题目: A seven segment display, or ...

  3. ZOJ 3962 Seven Segment Display

    Seven Segment Display 思路: 经典数位dp 代码: #include<bits/stdc++.h> using namespace std; #define LL l ...

  4. ZOJ 3962 Seven Segment Display 16进制的八位数加n。求加的过程中所有的花费。显示[0,F]有相应花费。

    Seven Segment Display Time Limit: Seconds Memory Limit: KB A seven segment display, or seven segment ...

  5. ZOJ 3962 E.Seven Segment Display / The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple E.数位dp

    Seven Segment Display Time Limit: 1 Second      Memory Limit: 65536 KB A seven segment display, or s ...

  6. ZOJ 3962:Seven Segment Display(思维)

    https://vjudge.net/problem/ZOJ-3962 题意:有16种灯,每种灯的花费是灯管数目,代表0~F(十六进制),现在从x开始跳n-1秒,每一秒需要的花费是表示当前的数的花费之 ...

  7. zoj 3962 Seven Segment Display 数位dp

    非常好的一个题,可以比赛时想到的状态太奇葩,不方便转移,就一直没能AC. 思路:dp(i, j)表示已经考虑了前i位,前i位的和为j的贡献.如果当前的选择一直是最大的选择,那么就必须从0~下一位的最大 ...

  8. ZOJ 3962 Seven Segment Display(数位DP)题解

    题意:给一个16进制8位数,给定每个数字的贡献,问你贡献和. 思路:数位DP,想了很久用什么表示状态,看题解说用和就行,其他的都算是比较正常的数位DP. 代码: #include<iostrea ...

  9. ZOJ 3962 Seven Segment Display(数位DP)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3962 题目大意: 有t组数据. 给你一个n,和8位的十六进制数s ...

随机推荐

  1. 【转载】什么是Zero-Copy

    转载:https://blog.csdn.net/u013256816/article/details/52589524 概述 考虑这样一种常用的情形:你需要将静态内容(类似图片.文件)展示给用户.那 ...

  2. 大数据平台消息流系统Kafka

    Kafka前世今生 随着大数据时代的到来,数据中蕴含的价值日益得到展现,仿佛一座待人挖掘的金矿,引来无数的掘金者.但随着数据量越来越大,如何实时准确地收集并分析如此大的数据成为摆在所有从业人员面前的难 ...

  3. 使用idea搭建ssh项目

    参考: https://www.cnblogs.com/getchen/p/8036709.html 需要自己提前在数据库中建好表 然后连接数据库通过侧边栏的persistence来生成实体类和相应的 ...

  4. IntelliJ IDEA配置本地Tomcat方法---亲测有效

    https://blog.csdn.net/hello_ljl/article/details/79258165

  5. BNUOJ 2105 Distance Queries

    Distance Queries Time Limit: 2000ms Memory Limit: 30000KB This problem will be judged on PKU. Origin ...

  6. Leetcode 187.重复的DNA序列

    重复的DNA序列 所有 DNA 由一系列缩写为 A,C,G 和 T 的核苷酸组成,例如:"ACGAATTCCG".在研究 DNA 时,识别 DNA 中的重复序列有时会对研究非常有帮 ...

  7. fd最大值和限制

    fd的数量决定了fd的最大值 在Linux下,系统全部能够打开的fd总数为: /proc/sys/fs/file-max,取决于内存 The file-max file /proc/sys/fs/fi ...

  8. [NOIP2007] 提高组 洛谷P1098 字符串的展开

    题目描述 在初赛普及组的“阅读程序写结果”的问题中,我们曾给出一个字符串展开的例子:如果在输入的字符串中,含有类似于“d-h”或者“4-8”的字串,我们就把它当作一种简写,输出时,用连续递增的字母获数 ...

  9. Thinkphp5.0 的使用模型Model查询

    Thinkphp5.0 的使用模型Model查询 一.查询多条记录 获取多个数据可以使用:select()方法和all()方法. 示例一:使用all()方法. //(1)筛选条件使用闭包函数 $res ...

  10. ****Call to a member function item() on a non-object

    A PHP Error was encountered Severity: Error Message: Call to a member function item() on a non-objec ...