2019牛客国庆集训派对day1
C. Distinct Substrings
大意: 给定串$s$, 字符集$m$, 对于每个字符$c$, 求$s$末尾添加字符$c$后本质不同子串增加多少.
exkmp求出每个前缀与后缀匹配的最大长度, 统计一下贡献即可
#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <cmath> #include <set> #include <map> #include <queue> #include <string> #include <cstring> #include <bitset> #include <functional> #include <random> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl '\n' #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;} //head const int N = 1e6+50; int n,m,a[N],z[N],po[N],h[N],b[N]; void init(int *s, int *z, int n) { int mx=0,l=0; REP(i,1,n-1) { z[i] = i<mx?min(mx-i,z[i-l]):0; while (i+z[i]<n&&s[z[i]]==s[i+z[i]]) ++z[i]; if (i+z[i]>mx) mx=i+z[i],l=i; } } int main() { po[0] = 1; REP(i,1,N-1) po[i] = po[i-1]*3ll%P; while (~scanf("%d%d", &n, &m)) { REP(i,1,m) h[i] = -1; REP(i,1,n) { scanf("%d",a+i); h[a[i]] = 0; } REP(i,1,n) b[i]=a[n-i+1]; init(b+1,z+1,n); REP(i,1,n-1) h[a[i+1]] = max(h[a[i+1]],z[n-i+1]); ll ans = 0; REP(i,1,m) ans ^= (ll)(n-h[i])*po[i]%P; printf("%lld\n", ans); } }
G. 字典序
大意: 给定$nm$矩阵$C$, 求将所有列重排, 使得每一行的字典序都不超过下一行, 输出字典序最小方案.
好题. 假设只有两行, 假设集合$A$为$C_{1,i}<C_{2,i}$的$i$, 集合$B$为$C_{1,i}>C_{2,i}$的$i$.
那么必须满足$B$中所有元素必须全排在某一个$A$中元素后面.
$n$的情况就相当于给了$n-1$个这样的限制. 维护一个堆, 拓排一下即可.
#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <cmath> #include <set> #include <map> #include <queue> #include <string> #include <cstring> #include <bitset> #include <functional> #include <random> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl '\n' #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;} //head const int N = 2e3+10; int n,m,a[N][N],deg[N],ans[N],vis[N]; bitset<N> A[N]; priority_queue<int,vector<int>,greater<int> > q; int main() { while (~scanf("%d%d", &n, &m)) { REP(i,1,n) REP(j,1,m) scanf("%d",a[i]+j); REP(i,1,n) vis[i] = 0; REP(i,1,m) { deg[i] = 0; REP(j,1,n) A[i][j]=0; } REP(i,1,n-1) REP(j,1,m) { if (a[i][j]>a[i+1][j]) ++deg[j]; if (a[i][j]<a[i+1][j]) A[j][i] = 1; } while (q.size()) q.pop(); REP(i,1,m) if (!deg[i]) q.push(i); int ok = 1; REP(i,1,m) { if (q.empty()) {ok = 0;break;} ans[i] = q.top(); q.pop(); REP(j,1,n-1) if (A[ans[i]][j]&&!vis[j]) { vis[j] = 1; REP(x,1,m) if (a[j][x]>a[j+1][x]) { if (!--deg[x]) q.push(x); } } } if (!ok) puts("-1"); else { REP(i,1,m) printf("%d%c", ans[i]," \n"[i==m]); } } }
J. Parity of Tuples (Easy)
大意: 给定$n$个$m$维向量$v_i=(a_{i,1},...,a_{i,m})$
定义$count(x)$表示对于$x$合法的向量数.
一个向量$v_i$对于$x$合法, 要满足对于所有的$j$, $a_{i,j}\wedge x$二进制有奇数个$1$.
求$\sum\limits_{x=0}^{2^k-1}count(x)\cdot 3^{x}$
关键是要发现每行贡献是独立的, 求出每行贡献再加一下即可.
求出$dp_{x,S}$表示考虑到第$x$位, 满足条件的$j$的状态为$S$的贡献.
答案就为$dp_{k-1,2^{m}-1}$
#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <cmath> #include <set> #include <map> #include <queue> #include <string> #include <cstring> #include <bitset> #include <functional> #include <random> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl '\n' #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;} //head int n,m,k,a[100],po[100]; int dp[2][1<<10],nxt[1<<10]; void add(int &a, ll b) {a=(a+b)%P;} int calc() { int cur = 0, mx = (1<<m)-1; REP(i,0,mx) dp[cur][i] = 0; int sta = 0; REP(i,0,m-1) if (a[i]&1) sta ^= 1<<i; dp[cur][0] = 1; add(dp[cur][sta],3); REP(i,1,k-1) { cur ^= 1; //第i位填0 memcpy(dp[cur],dp[!cur],sizeof dp[0]); int sta = 0; REP(j,0,m-1) if (a[j]>>i&1) sta ^= 1<<j; REP(s,0,mx) { //第i位填1 add(dp[cur][s^sta],(ll)dp[!cur][s]*po[i]); } } return dp[cur][mx]; } int main() { int t = 1; REP(i,0,50) { po[i] = qpow(3,t); t = (ll)t*2%(P-1); } while (~scanf("%d%d%d",&n,&m,&k)) { int ans = 0; REP(i,1,n) { REP(j,0,m-1) scanf("%d",a+j); ans = (ans+calc())%P; } printf("%d\n", ans); } }