1. 程式人生 > 實用技巧 >《牛客IOI周賽18-提高組》

《牛客IOI周賽18-提高組》

太菜了太菜了太菜了~~

A:首先,要先理解這題的題意。

所有的區間都翻轉了,才算做一次,然後求k次後的位置。

那麼,我們可以先預處理出一次後每個點去到的位置,即一開始a[i] = i。

那麼,我們就知道一次翻轉的相對位置改變了。

那麼,現在樸素的解法:k次遍歷,每次都讓i位置跳到a[i]即可。//這裡的a[i]為第一次翻轉後處理出來的跳的位置。

複雜度o(kn)。

顯然不行。考慮倍增跳。那麼就很快了。

注意倍增跳的時候,最外層按位處理,這樣可以保證i-1位置都已經處理好了。

這裡倍增跳從大到小跳和從小到大跳都是可以的。

B:容斥思想。

ans = 無限制總數 - 全都是不喜歡的組成的方案數。

無限制的總數:

考慮dp[i]表示i長度能組成的方案數。

那麼$dp[i] = \sum_{j = 1}^{i-1} dp[j] + 1$即可以由任意非0長度組合轉移來,然後自身組成一個長度為1的所以加1.

那麼我們進行化簡。

$dp[i] = \sum_{j = 1}^{i-1} dp[j] + 1 = \sum_{j = 1}^{i-2} dp[j] + 1 + dp[i-1]= dp[i-1] + dp[i-1] = 2*dp[i-1] $

顯然,這裡邊界為dp[1] = 1,那麼dp[n] = 2^(n-1)

所以總方案即為2^(n-1)。

那麼全是不喜歡的組成的方案數:

可以轉換一下題意:由不喜歡的集合組成的方案數,那麼就和

《洛谷P5343 【XR-1】分塊》這題一樣了。

矩陣快速冪求出即可。最後減一下,注意加模數防負數

// Author: levil
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<string,int> pii;
const int N = 1e6+5;
const int M = 2e5+5;
const LL Mod = 1e9+7;
#define rg register
#define pi acos(-1)
#define INF 1e9
#define
CT0 cin.tie(0),cout.tie(0) #define IO ios::sync_with_stdio(false) #define dbg(ax) cout << "now this num is " << ax << endl; namespace FASTIO{ inline LL read(){ LL x = 0,f = 1;char c = getchar(); while(c < '0' || c > '9'){if(c == '-') f = -1;c = getchar();} while(c >= '0' && c <= '9'){x = (x<<1)+(x<<3)+(c^48);c = getchar();} return x*f; } void print(int x){ if(x < 0){x = -x;putchar('-');} if(x > 9) print(x/10); putchar(x%10+'0'); } } using namespace FASTIO; void FRE(){/*freopen("data1.in","r",stdin); freopen("data1.out","w",stdout);*/} LL n,p,a[105],f[105]; struct Mat{ LL m[105][105]; Mat operator * (const Mat a)const{ Mat c;memset(c.m,0,sizeof(c.m)); for(rg int i = 1;i <= 100;++i) for(rg int j = 1;j <= 100;++j) for(rg int k = 1;k <= 100;++k) c.m[i][j] = (c.m[i][j]+m[i][k]*a.m[k][j]%p)%p; return c; } }; LL quick_mi(LL a,LL b) { LL re = 1; while(b) { if(b&1) re = re*a%p; a = a*a%p; b >>= 1; } return re; } Mat Mat_mi(Mat a,LL b) { Mat res;memset(res.m,0,sizeof(res.m)); for(rg int i = 1;i <= 100;++i) res.m[i][i] = 1; while(b) { if(b&1) res = res*a; a = a*a; b >>= 1; } return res; } int main() { n = read(),p = read(); int k;k = read(); for(rg int i = 1;i <= k;++i) a[i] = read(); LL tmp = quick_mi(2,n-1); Mat ans;memset(ans.m,0,sizeof(ans.m)); f[0] = 1; for(rg int i = 1;i < 100;++i) { for(rg int j = 1;j <= k;++j) if(i-a[j] >= 0) f[i] = (f[i]+f[i-a[j]])%p; } if(n < 100) { LL ma = ((tmp-f[n])%p+p)%p; printf("%lld\n",ma); } else { Mat base;memset(base.m,0,sizeof(base.m)); for(rg int i = 1;i <= k;++i) base.m[1][a[i]] = 1; for(rg int i = 2;i <= 100;++i) base.m[i][i-1] = 1; base = Mat_mi(base,n-99); for(rg int i = 1;i <= 100;++i) ans.m[i][1] = f[100-i]; ans = base*ans; LL ma = (tmp-ans.m[1][1]+p)%p; printf("%lld\n",ma); } system("pause"); } /* 125 10541 5 2 6 4 5 2 */
View Code