a:怪盜-1412題目描述
乙個長度為n+m+k包含n個數字1,m個數字2和k個數字4的陣列,最多可能有多少個子序列1412?
如果乙個序列是陣列的子串行,當且僅當這個序列可以由陣列刪去任意個元素,再將陣列中的剩餘元素按順序排列而成。思路
將序列排列成111444441111222222,這樣可以得到最大化的子串行數量,答案為
m*k*(n/2)*(n/2+(n&1))
**
#includeusing namespace std;
#define fi first
#define se second
#define lowbit(x) x&(-x)
#define pb push_back
#define iter set::iterator
typedef long long ll;
typedef pairpii;
typedef pairpll;
const int n = 1e5+5;
const ll mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const int dir[4][2]=;
const int exdir[4][2]=;
ll qpow(ll x,ll y)
return ans%mod;
}void solve()
b:dis2題目描述給出一顆n個點n−1條邊的樹,點的編號為1,2,...,n−1,n,對於每個點i(1<=i<=n),輸出與點i距離為2的點的個數。思路兩個點的距離定義為兩個點最短路徑上的邊的條數。
與每個點距離為1的點為這個點的度數。對於每個點u,列舉和它有邊相連的點v,點v對點u的貢獻為與點v距離為1的點的個數減去1**
#includeusing namespace std;
#define fi first
#define se second
#define lowbit(x) x&(-x)
#define pb push_back
#define iter set::iterator
typedef long long ll;
typedef pairpii;
typedef pairpll;
const int n = 2e5+5;
const ll mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const int dir[4][2]=;
const int exdir[4][2]=;
ll qpow(ll x,ll y)
return ans%mod;
}vectorv[n];
void solve()
for(int i=1;i<=n;i++)
cout<>t;
//while(t--)solve(),cout<
solve();
return 0;
}
c:序列卷積之和直接推出公式比較困難,打表找一下規律
容易知道,記錄一下
**
#includeusing namespace std;
#define fi first
#define se second
#define lowbit(x) x&(-x)
#define pb push_back
#define iter set::iterator
typedef long long ll;
typedef pairpii;
typedef pairpll;
const int n = 2e5+5;
const ll mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const int dir[4][2]=;
const int exdir[4][2]=;
ll qpow(ll x,ll y)
return ans%mod;
}ll sum[n],a[n];
void solve()
ll ans=0;
for(int i=1;i<=n;i++)
cout<>t;
//while(t--)solve(),cout<
solve();
return 0;
}
d:寶石裝箱題目描述n顆寶石裝進n個箱子使得每個箱子中都有一顆寶石。第i顆寶石不能裝入第a[i]
個箱子。求合法的裝箱方案對998244353取模。思路
兩種裝箱方案不同當且僅當兩種方案中存在一顆編號相同的寶石裝在不同編號的箱子中。
設g(x)表示選出x個箱子裝不合法的寶石的方案數。求g(x)時採用滾動陣列優化空間,其實就是個01揹包每個可選可不選,即:
至少有x箱子不合法為:
根據容斥定理即可計算出答案(奇加偶減)**
#includeusing namespace std;
#define fi first
#define se second
#define lowbit(x) x&(-x)
#define pb push_back
#define iter set::iterator
typedef long long ll;
typedef pairpii;
typedef pairpll;
const int n = 1e4+5;
const ll mod = 998244353;
const int inf = 0x3f3f3f3f;
const int dir[4][2]=;
const int exdir[4][2]=;
ll qpow(ll x,ll y)
return ans%mod;
}ll f[n],dp[n];
int n,a[n];
void solve()
dp[0]=1;
for(int i=1;i<=n;i++)
for(int j=i;j>=0;j--)
(dp[j+1]+=dp[j]*a[i])%=mod;
ll ans=0;
for(int i=0,p=1;i<=n;i++,p*=-1)
ans=(ans+p*dp[i]*f[n-i]%mod)%mod;
ans=(ans+mod)%mod;
cout<>t;
//while(t--)solve(),cout<
solve();
return 0;
}
牛客練習賽22題解
簡單瞎搞題 不會用bitset,所以沒做出來。實際上還是比較簡單的。include using namespace std bitset 1000005 dp 2 int main cout 2 count 簡單資料結構1 這裡要運用拓展尤拉定理。ab modp ab p b p p 0 modp ...
題解 牛客練習賽51
字首a的數量,字尾c的數量,遇到b就計算一次答案。includeusing namespace std typedef long long ll const int n 1e5 100 char s n int cnt n int main int tmp 0 for int i 1 i n i p...
牛客練習賽42題解
寫題寫的感覺自己彷彿是個傻子 給定兩個等長的由小寫字母構成的串 a,b,其中 a b n 現在你需要求出乙個子區間 l,r 使得 lcp a l,r b l,r lcs a l,r b l,r lcp a l,r b l,r lcs a l,r b l,r 最大,並輸出這個值。lcp s,t 表示s...