CodeForces - 1301C Ayoub's function(数学)
生活随笔
收集整理的這篇文章主要介紹了
CodeForces - 1301C Ayoub's function(数学)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:規定函數 f(s) 為01字符串 s 中至少包含一個 1 的所有子串的數量,比如 f(10101) = 12 ,其中十二個子串分別為(1,2),(1,3),(1,4),(1,5),(2,2),(2,3),(2,4),(2,5),(3,4),(3,5),(4,4),(4,5),現在給出一個01字符串的長度為 n ,其中包含了 m 個 1 ,問所有情況中 f(s) 的最大值
題目分析:通過樣例不難看出,如果想要使得 f(s) 最大,那么必須使得 0 的連續部分盡量多,而這個題正難則反,直接求至少包含 1 的子串比較難求,我們可以考慮用總的子串個數減去只含有 0 的子串個數,而對于一個字符串而言,其子串的個數為:1+2+3+...+n-1+n=(n+1)*n/2,而一個字符串中有 m 個 1 ,相應的可以將 (n-m) 個 0 分割為 (m+1) 個區間中,其中:
然后直接計算就好了,上面的等式看似很復雜,但自己推一下其實也就那回事
代碼:
#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> #include<unordered_map> using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=1e3+100;LL C(LL n) {return n*(n+1)/2; }int main() { //#ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); //#endif // ios::sync_with_stdio(false);int w;cin>>w;while(w--){LL n,m;scanf("%lld%lld",&n,&m);LL zero=n-m;//有這么多 0,共分成了 m+1 段LL t=zero/(m+1);//每段 0 含有 t 個 0 LL k2=zero%(m+1);//有k2段 0 含有 t+1 個 0 LL k1=(m+1)-k2;//有k1段 0 含有 t 個 0 printf("%lld\n",C(n)-C(t)*k1-C(t+1)*k2);}return 0; }?
總結
以上是生活随笔為你收集整理的CodeForces - 1301C Ayoub's function(数学)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CodeForces - 1301F S
- 下一篇: CodeForces - 1304D S