数码管从某个状态顺序转移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. ubuntu14.04 mysql-workbench Connecting to MySQL server ... Native table 'performance_schema'.'session_variables' has the wrong structure错误解决

    使用的mysql版本: mysql  Ver 14.14 Distrib 5.7.9, for Linux (x86_64) using  EditLine wrapper 打开shell命令 1.输 ...

  2. 安装nvm 切换nodejs版本

    删除已安装的nodejs--------------------------------------------------------------- #查看已经安装在全局的模块,以便删除这些全局模块 ...

  3. 51NOD 2370 奈芙莲的护符

    >>这是原题传送门<< 答案参考来自 http://www.cnblogs.com/sugewud/p/9822933.html 思路:看到取值范围之后,仅有的思路还是暴力

  4. 圆角计算 Shader

    圆角的计算 在Shader中,我们使用UV坐标来计算需要显示的部分和不需要显示的部分,使用透明来处理显示与不显示.UV坐标如下图1,我们将坐标平移到图2位置,面片的UV坐标原点在面片中心,UV坐标范围 ...

  5. 最近编译POCO 库和 Boost库的笔记

    最近在编译POCO库和BOOST库 先讲一下编译POCO库,我编译的是1.9.0,过程相当曲折,要OPENSSL修改版本的,个OPENSSL在这里下载,如果你用一般未修改的OPENSSL 是编译不了, ...

  6. 网络编程基础:网络基础之网络协议、socket模块

    操作系统(简称OS)基础: 应用软件不能直接操作硬件,能直接操作硬件的只有操作系统:所以,应用软件可以通过操作系统来间接操作硬件 网络基础之网络协议: 网络通讯原理: 连接两台计算机之间的Intern ...

  7. msp430入门编程04

    msp430中C语言的变量与数据类型 msp430入门学习 msp430入门编程

  8. Thinkphp5.0 的响应方式

    Thinkphp5.0 的响应方式 $res = config('default_return_type'); dump($res);//默认是html //修改为json \think\Config ...

  9. Free Goodies UVA - 12260

    Petra and Jan have just received a box full of free goodies, and want to divide the goodies between ...

  10. JSON与对象之间的转换

    import com.alibaba.fastjson.JSON;import com.fasterxml.jackson.databind.ObjectMapper;import com.faste ...