題目鏈接:點擊查看
題目大意:給出一個 n ,表示出 0 ~ 10^n - 1 內的所有整數,且用前導零補齊,即所有的數長度都為 n ,規定連續的且數值相同的一段稱為block,現在問對于每個 i ∈ [ 1 , n ] ,10^n 個數中共有多少個長度為 i 的 block
題目分析:比較簡單的一道組合數學題目,分情況討論一下就好了:
i == n:顯然答案只有十種情況i < n:考慮答案在兩頭的情況,即連續的block為橙色的部分:00000 或 00000,此時的答案為10*9*2*10^(n-i-1) 10是橙色部分的取值范圍9是與橙色部分相鄰的首個黑色數字的取值范圍2是橙色部分在左側或右側兩種情況10^(n-i-1)是其余黑色部分的取值范圍i < n - 1:考慮答案不在兩頭的情況,即連續的block為橙色部分:00000 00000 等等,此時答案為10*(n-i-1)*9*9*10^(n-i-2) 10是橙色部分的取值范圍(n-i-1)是橙色部分通過平移,可以在不同位置有(n-i-1)種情況,如上面 i = 2,n = 5 時,就有 5 - 2 - 1 = 2 (種)情況9是橙色部分左側的第一個黑色數字的取值范圍9是橙色部分右側的第一個黑色數字的取值范圍10^(n-i-2)是其余黑色部分的取值范圍
然后配合快速冪實現就好了
代碼:
?
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=2e5+100;const int mod=998244353;LL q_pow(LL a,LL b)
{LL ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod;b>>=1;}return ans;
}int main()
{
#ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);int n;scanf("%d",&n);for(int i=1;i<=n;i++){LL ans=0;if(i==n)ans=10;if(i<n)//在兩頭的情況ans=(ans+10*9*2*q_pow(10,n-i-1))%mod;if(i<n-1)//不在兩頭的情況ans=(ans+10*(n-i-1)*9*9*q_pow(10,n-i-2))%mod;printf("%lld ",ans);}return 0;
}
?
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生
總結
以上是生活随笔為你收集整理的CodeForces - 1327E Count The Blocks(组合数学)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。