数码管从某个状态顺序转移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. ubuntu 16.4 安装mysql-python

    sudo apt-get install python-pip python-dev libmysqlclient-dev pip install MySQL-python

  2. python 连接sqlserver: pymssql

    停了一个月,终于还是把这个做了,工作需要!!!在装pymssql时,一直报错,确定了要先装freetds: 1. 安装freetds时报错,搜索到要先进行如下操作: brew unlink freet ...

  3. N分之一 竖式除法模拟

    N分之一 Description Alice越来越痴迷于数学问题了.一天,爸爸出了个数学题想难倒她,让她求1 / n. 可怜的Alice只有一岁零九个月,回答不上来 ~~~~(>_<)~~ ...

  4. ProxySQL Tutorial : setup in a MySQL replication topology

    ProxySQL Tutorial : setup in a MySQL replication topology 时间 2015-09-15 05:23:20 ORACLE数据库技术文刊 原文  h ...

  5. shit layui & select & re-render & bug

    shit layui https://www.layui.com/doc/modules/form.html#onselect https://www.layui.com/doc/element/fo ...

  6. [OJ#40]后宫佳丽

    [OJ#40]后宫佳丽 试题描述 如果机房要关门了,或者有妹子在等你,你可以直接看最后一句话. Fyq 是一只饥渴的鸭子. Fyq 有一个充实的后宫,可惜他总是体力不支,为此他经常苦恼,总是想方设法让 ...

  7. [luoguP1981] 表达式求值(U•ェ•*U)

    传送门 弄两个栈,一个存数,一个存运算符,然后乱搞. 代码 #include <cstdio> #include <cstring> #include <iostream ...

  8. 算法(1):查找&排序

    算法(Algorithm):一个计算过程,解决问题的方法 程序 = 数据结构+算法 时间复杂度: 当算法过程中出现循环折半的时候,复杂度式子中会出现 O(logn) 时间复杂度小结: 1. 时间复杂度 ...

  9. Vue 实例以及生命周期

    最简单的 Vue 实例 //html <div id="app"> {{message}} </div> //javascript var vm = new ...

  10. AOJ -0033 Ball(DFS)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=22516 一道需要思考的搜索题. 题意:十个球按给定顺序从图中所示容器中下落, ...