哈爾濱理工大學第七屆程式設計競賽決賽(網路賽-高年級組)D 數圈圈
阿新 • • 發佈:2019-02-10
時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 32768K,其他語言65536K
64bit IO Format: %lld
]
空間限制:C/C++ 32768K,其他語言65536K
64bit IO Format: %lld
題目描述
tabris有一個習慣,無聊的時候就會數圈圈,無論數字還是字母。 現在tabris更無聊啦,晚上睡不著覺就開始數羊,從a只數到b只。 順便還數了a到b之間有多少個圈。 但是tabris笨啊,雖然數羊不會數錯,但很可能數錯圈的個數。 但是tabris很難接受自己笨這個事實,所以想問問你他一共應該數出多少個圈,這樣tabris才好判斷他到底笨不笨啊。輸入描述:
輸入一個T,表示資料組數每組測試資料包含兩個正整數a,b。T∈[1,1000]a,b∈[1,1014輸出描述:
每組資料輸出結果,並換行。示例1
輸入
11 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 1 100
輸出
0 0 0 1 0 1 0 2 1 1 111
備註:
數字的圈的個數請根據樣例自行理解。這個題的最後一句備註,圈的理解,,,實在是需要理解的:0、4、6、9是一個圈圈,8是兩個圈圈,為啥呢,因為圍成了這麼個圈圈。
然後做法就是數位dp,和之前專題裡做的一個how many zeros 一個意思的,按數位按個判斷求結果就好了。
程式碼如下:
#include<iostream> #include<cstring> using namespace std; typedef long long LL; LL dp[20][200]; int bit[20]; // 數位 個數 前導0 上界 LL dfs(int pos, int sta, bool lead, bool limit) { if(pos==-1) { if(lead) return 1; return sta; } if(!lead && !limit && dp[pos][sta]!=-1) return dp[pos][sta]; int up=limit?bit[pos]:9; LL temp=0; for(int i=0; i<=up; i++) { if(lead==1 && i==0)//一直是0的情況 temp+=dfs(pos-1, 0, i==0, limit && i==up); else//否則 出現哪個 temp + 幾個 { if(i==0) { temp+=dfs(pos-1, sta+1, 0, limit && i==up); } else if(i==4) { temp+=dfs(pos-1, sta+1, 0, limit && i==up); } else if(i==6) { temp+=dfs(pos-1, sta+1, 0, limit && i==up); } else if(i==9) { temp+=dfs(pos-1, sta+1, 0, limit && i==up); } else if(i==8) { temp+=dfs(pos-1, sta+2, 0, limit && i==up); } else temp+=dfs(pos-1, sta, 0, limit && i==up); } } if(!limit && !lead) dp[pos][sta]=temp; return temp; } LL solve(LL x) { int pos=0; while(x) { bit[pos++]=x%10; x/=10; } return dfs(pos-1, 0, 1, 1);//有前導0 有上界 } int main() { int T; LL le, ri; memset(dp, -1, sizeof(dp)); cin>>T; while(T--) { cin>>le>>ri; cout<<solve(ri)-solve(le-1)<<endl; } return 0; }