标签:algorithm vector min 数码管 eve 观察 include segment lib
数码管从某个状态顺序转移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; }
ZJ省赛 2017 E.Seven Segment Display
标签:algorithm vector min 数码管 eve 观察 include segment lib
原文地址:http://www.cnblogs.com/heisenberg-/p/6748621.html