Codeforces 1194F. Crossword Expert

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 1194F. Crossword Expert相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/bsp' title='bsp'>bsp   code   include   ORC   浣嶇疆   i+1   namespace   max   target   

浼犻€侀棬

鑰冭檻姣忎竴涓綅缃殑鏈熸湜璐$尞 $P[i]$

瀵逛簬绗?$k$ 涓綅缃紝璁?$sum=sum_{i=1}^{k}t[k]$锛岄偅涔?$T-sum$ 鍗充负鐢ㄦ渶鐭椂闂村畬鎴愬畬浣嶇疆 $k$ 鍚庡鍑烘潵鐨勭┖闂叉椂闂?/span>

濡傛灉 $T-sum>=k$ 閭d箞杩欎釜浣嶇疆涓€瀹氳兘瀹屾垚锛岃础鐚负 $1$

濡傛灉 $T<sum$ 锛岄偅涔堣繖涓綅缃竴瀹氭病娉曞畬鎴愶紝璐$尞涓?$0$

鍚﹀垯璁?$mx=max(T-sum,k)$锛岄偅涔堣繖涓綅缃畬鎴愮殑鎬绘儏鍐垫暟灏辨槸鍦ㄥ鍑烘潵鐨勬椂闂村唴浠婚€夊嚑涓綅缃鑺辫垂 $1$

閭d箞杩欎釜浣嶇疆鏈?$sum_{i=0}^{mx} inom {mx}{i} $ 绉嶄笉鍚岀殑鍚堟硶鏂规锛屽啀闄や互鎬绘柟妗堟暟 $2^k$ 鍗充负姒傜巼

鍥犱负浠峰€间负 $1$锛岄偅涔堟湡鏈涜础鐚?$P[i]$ 灏辨槸 $frac {1} {2^k} sum_{i=0}^{mx} inom {mx}{i} $

鐒跺悗鍙戠幇鐩存帴璁$畻涓€鍫嗙粍鍚堟暟鐨勫鏉傚害鏄?$n^2$ 鐨勶紝鑰冭檻濡備綍浼樺寲璁$畻杩囩▼

娉ㄦ剰鍒版垜浠瘡娆$畻缁勫悎鏁版椂鐨?$inom {mx} {i} $ 鐨?$mx$ 鏄崟璋冮€掑噺鐨勶紙鏄剧劧鍙鑰冭檻 $T-sum<k$ 鏃剁殑鎯呭喌锛夛紝骞朵笖姣忔 $i$ 鍔犱竴

鑰冭檻鏉ㄨ緣涓夎閫掓帹缁勫悎鏁版椂锛?2sum_{j=0}^{x}inom {i}{j}=(sum_{j=0}^{x}inom {i+1}{j})+inom {i}{x}$锛堣繖涓嚜宸辩敾涓€涓嬫潹杈変笁瑙掑氨鐭ラ亾浜嗭級

閭d箞鎴戜滑灏卞彲浠ュ姩鎬佺淮鎶?$now=sum_{i=0}^{mx} inom {mx}{i}$锛岀劧鍚庢瘡鍒颁笅涓€灞傚氨 $now=now*2-inom {i-1}{mx_{i-1}}$

鐒跺悗鍐嶅埄鐢?$mx$ 鐨勫崟璋冩€х洿鎺ユ毚鍔涚淮鎶や竴涓?$now$ 鍗冲彲鍋氬埌 $O(n)$

鐒跺悗灏辫繃浜嗭紝娉ㄦ剰 $long long$

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
inline ll read()
{
    ll x=0,f=1; char ch=getchar();
    while(ch<鈥?/span>0鈥?/span>||ch>鈥?/span>9鈥?/span>) { if(ch==鈥?/span>-鈥?/span>) f=-1; ch=getchar(); }
    while(ch>=鈥?/span>0鈥?/span>&&ch<=鈥?/span>9鈥?/span>) { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
    return x*f;
}
const int N=4e5+7,mo=1e9+7;
inline int fk(int x) { return x>=mo ? x-mo : x; }
int n,t[N],fac[N],finv[N],ans;
ll T;
inline int ksm(int x,int y)
{
    int res=1;
    while(y) { if(y&1) res=1ll*res*x%mo; x=1ll*x*x%mo; y>>=1; }
    return res;
}
inline int C(int x,int y) { return 1ll*fac[x]*finv[y]%mo*finv[x-y]%mo; }
int main()
{
    n=read(); T=read();
    fac[0]=1; finv[0]=1;
    for(int i=1;i<=n;i++)
    {
        t[i]=read();
        fac[i]=1ll*fac[i-1]*i%mo;
        finv[i]=ksm(fac[i],mo-2);
    }
    int i2=1,now=0,pre=0; ll sum=0;
    for(int i=1;i<=n;i++)
    {
        sum+=t[i]; i2=fk(i2+i2);
        if(T-sum>=i) { ans++; continue; }
        if(T-sum<0) break;
        int mx=T-sum;
        if(!now)
        {
            for(int j=0;j<=mx;j++) now=fk(now+C(i,j));
            pre=mx; ans=fk(ans+1ll*now*ksm(i2,mo-2)%mo);
            continue;
        }
        now=fk( fk(now+now)-C(i-1,pre)+mo );
        while(pre>mx) now=fk(now-C(i,pre)+mo),pre--;
        ans=fk(ans+1ll*now*ksm(i2,mo-2)%mo);
    }
    printf("%d
",ans);
    return 0;
}

 

以上是关于Codeforces 1194F. Crossword Expert的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces - 1194C - From S To T - 排序

codeforces 1194 D. 1-2-K Game

Coreforces-1194D1-2-K Game

(板子) 最小生成树 买礼物 luogu P1194

MySQL 说:#1194 - 表 'tablename' 被标记为崩溃,应该修复

P1194 买礼物kruskal