Cmd2001的毒瘤水题题解
怕不是我再不写题解这题就该成没人做也没人会的千古谜题了......
T1:
仔细分析题面,发现相同就是广义SAM上节点相同,相似就是广义SAM上为从根到某个点路径的前缀。、
直接SAM上跑从根开始,每个点下界为1的最小流即可。
代码:
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #include<map>
- #include<queue>
- #define debug cout
- using namespace std;
- const int maxn=5e3+1e2;
- const int inf=0x3f3f3f3f;
- int in[maxn],n,ans,sum;
- namespace NetworkFlow {
- int s[maxn<<],t[maxn<<],nxt[maxn<<],f[maxn<<],dep[maxn<<],deg[maxn<<],cnt=;
- int bak[maxn<<],bcnt;
- int st,ed,_s,_t;
- inline void coredge(int from,int to,int flow) {
- t[++cnt] = to , f[cnt] = flow ,
- nxt[cnt] = s[from] , s[from] = cnt;
- }
- inline void singledge(int from,int to,int flow) {
- coredge(from,to,flow) , coredge(to,from,);
- }
- inline bool bfs() {
- memset(dep,-,sizeof(dep)) , dep[st] = ;
- queue<int> q; q.push(st);
- while( q.size() ) {
- const int pos = q.front(); q.pop();
- for(int at=s[pos];at;at=nxt[at])
- if( f[at] && !~dep[t[at]] ) {
- dep[t[at]] = dep[pos] + , q.push(t[at]);
- }
- }
- return ~dep[ed];
- }
- inline int dfs(int pos,int flow) {
- if( pos == ed ) return flow;
- int ret = , now = ;
- for(int at=s[pos];at;at=nxt[at])
- if( f[at] && dep[t[at]] > dep[pos] ) {
- now = dfs(t[at],min(flow,f[at])) ,
- ret += now , flow -= now ,
- f[at] -= now , f[at^] += now;
- if( !flow ) return ret;
- }
- if( !ret ) dep[pos] = -;
- return ret;
- }
- inline int dinic() {
- int ret = , now = ;
- while( bfs() ) {
- while( ( now = dfs(st,inf) ) )
- ret += now;
- }
- return ret;
- }
- inline int findflow() {
- for(int at=s[_t];at;at=nxt[at])
- if( t[at] == _s ) return f[at^];
- }
- inline void backup() {
- memcpy(bak,s,sizeof(s)) , bcnt = cnt;
- }
- inline void restore() {
- memcpy(s,bak,sizeof(bak)) , cnt = bcnt;
- }
- }
- namespace SAM {
- map<int,int> ch[maxn<<];
- int fa[maxn<<],len[maxn<<],root,last,cnt;
- inline int NewNode(int ll) {
- len[++cnt] = ll;
- return cnt;
- }
- inline void extend(int x) {
- int p = last;
- int np = NewNode(len[p]+);
- while( p && ch[p].find(x) == ch[p].end() ) ch[p][x] = np , p = fa[p];
- if( !p ) fa[np] = root;
- else {
- int q = ch[p][x];
- if( len[q] == len[p] + ) fa[np] = q;
- else {
- int nq = NewNode(len[p]+);
- ch[nq] = ch[q] , fa[nq] = fa[q];
- fa[np] = fa[q] = nq;
- while( p && ch[p][x] == q ) ch[p][x] = nq , p = fa[p];
- }
- }
- last = np;
- }
- inline void Ex_extend(int* sou,int li) {
- last = root;
- for(int i=;i<=li;i++) {
- if( ch[last].find(sou[i]) != ch[last].end() ) last = ch[last][sou[i]];
- else extend(sou[i]);
- }
- }
- }
- inline void build() {
- using SAM::ch;using SAM::cnt;
- using namespace NetworkFlow;
- _s = cnt * + , _t = _s + , st = _t + , ed = st + ;
- #define cov(x) (x+cnt)
- for(int i=;i<=cnt;i++) {
- if( i != ) ++deg[i] , --deg[cov(i)];
- for(map<int,int>::iterator it=ch[i].begin();it!=ch[i].end();it++) {
- const int tar = it->second;
- if( i == ) singledge(_s,tar,);
- else singledge(cov(i),tar,);
- }
- if( i != ) singledge(cov(i),_t,);
- }
- backup();
- for(int i=;i<=_t;i++) {
- if( !deg[i] ) continue;
- if( deg[i] > ) singledge(i,ed,deg[i]) , sum += deg[i];
- else singledge(st,i,-deg[i]);
- }
- singledge(_t,_s,inf);
- }
- inline int getans() {
- using namespace NetworkFlow;
- int d = dinic();
- if( d != sum ) return -; // No solution .
- int ret = findflow();
- restore();
- st = _t , ed = _s;
- int dd = dinic();
- return ret - dd;
- }
- int main() {
- static int m;
- SAM::root = SAM::NewNode();
- scanf("%d",&m);
- while(m--) {
- scanf("%d",&n);
- for(int i=;i<=n;i++) scanf("%d",in+i);
- SAM::Ex_extend(in,n);
- }
- build();
- ans = getans();
- printf("%d\n",ans);
- return ;
- }
T2:
观察操作数量特点,发现可持久化块状数组可过。
代码:
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- using namespace std;
- const int maxn=1e6+1e2,maxb=1e3,maxu=1e4+1e2,maxr=1e5+1e2;
- inline int* NewArray() {
- static const int maxu = 1e4 + 1e3 + ;
- static int dat[maxu][maxb],cnt;
- return dat[cnt++];
- }
- struct PersistentBlockedArray {
- int* p[maxb];
- inline void insert(int pos,int x) { // insert into this array .
- if( !p[pos/maxb] ) p[pos/maxb] = NewArray();
- p[pos/maxb][pos%maxb] = x;
- }
- inline void modify(int pos,int x) {
- int* t = NewArray();
- memcpy(t,p[pos/maxb],sizeof(int)*maxb);
- t[pos%maxb] = x , p[pos/maxb] = t;
- }
- inline int query(int pos) {
- return p[pos/maxb][pos%maxb];
- }
- }dat[maxu];
- int ptr[maxu+maxr],now,cnt;
- inline void roll(int tar) {
- ptr[++now] = ptr[tar];
- }
- inline void modify(int pos,int x) {
- dat[++cnt] = dat[ptr[now]] , ptr[now] = cnt;
- dat[ptr[now]].modify(pos,x);
- }
- inline int query(int pos) {
- return dat[ptr[now]].query(pos);
- }
- namespace IO {
- const int BS = << ;
- char ibuf[BS],obuf[BS],*ist,*ied,*oed=obuf;
- inline char nextchar() {
- if( ist == ied ) ied = ibuf + fread(ist=ibuf,,BS,stdin);
- return ist == ied ? - : *ist++;
- }
- inline int getint() {
- int ret = , ch;
- while( !isdigit(ch=nextchar()) );
- do ret=ret*+ch-''; while( isdigit(ch=nextchar()) );
- return ret;
- }
- inline void getstr(char* s) {
- char ch;
- while( !isalpha(ch=nextchar()) ) ;
- do *s++=ch; while( isalpha(ch=nextchar()) );
- }
- inline void flush() {
- //cerr<<"in flush delta = "<<oed-obuf<<endl;
- fwrite(obuf,,oed-obuf,stdout) , oed = obuf;
- }
- inline void printchar(const char &x) {
- *oed++ = x;
- //cerr<<"delta = "<<oed-obuf<<endl;
- if( oed == obuf + BS ) flush();
- }
- inline void printint(int x) {
- //cerr<<"x = "<<x<<endl;
- static int stk[],top;
- if( !x ) printchar('');
- else {
- top = ;
- while(x) stk[++top] = x % , x /= ;
- while(top) printchar(''+stk[top--]);
- }
- printchar('\n');
- }
- }
- using IO::getint;
- using IO::printint;
- using IO::getstr;
- using IO::flush;
- int main() {
- static int n,m,lastans,ope;
- static char o[];
- n = getint() , m = getint();
- for(int i=,x;i<n;i++) x = getint() , dat[].insert(i,x);
- for(int i=,p,x,t;i<=m;i++) {
- getstr(o);
- if( *o == 'Q' ) {
- p = ( getint() ^ lastans ) % n;
- printint(lastans=query(p));
- } else if( *o == 'M' ) {
- ++ope , p = ( getint() ^ lastans ) % n , x = getint();
- modify(p,x);
- } else if( *o == 'R' ) {
- ++ope , t = ( getint() ^ lastans ) % ope;
- roll(t);
- }
- }
- flush();
- return ;
- }
T3:
大力反演出phi,后面的sigma(i^k)显然是k+1次多项式,拉格朗日插值即可。
代码:
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #define debug cout
- #define bool unsigned char
- typedef long long int lli;
- using namespace std;
- const int maxn=1e6+1e2,lim=1e6,maxk=1e3+1e1;
- const int mod=1e9+;
- int n,k;
- namespace Sieve {
- lli sum[maxn],mem[maxn];
- bool vis[maxn];
- inline void pre() {
- static int prime[maxn/],cnt;
- static bool vis[maxn];
- sum[] = ;
- for(int i=;i<=lim;i++) {
- if( !vis[i] ) prime[++cnt] = i , sum[i] = i - ;
- for(int j=;j<=cnt&&(lli)i*prime[j]<=lim;j++) {
- const int tar = i * prime[j];
- vis[tar] = ;
- if( i % prime[j] ) sum[tar] = sum[i] * ( prime[j] - );
- else {
- sum[tar] = sum[i] * prime[j];
- break;
- }
- }
- }
- for(int i=;i<=lim;i++) sum[i] = ( sum[i] + sum[i-] ) % mod;
- }
- inline lli getphi(lli x) {
- if( x <= lim ) return sum[x];
- const lli t = n / x;
- if( vis[t] ) return mem[t];
- lli& ret = mem[t]; ret = x * ( x + ) >> , vis[t] = ;
- for(lli i=,j;i<=x;i=j+) {
- j = x / ( x / i );
- ret -= ( j - i + ) * getphi(x/i) % mod , ret %= mod;
- }
- return ret = ( ret % mod + mod ) % mod;
- }
- }
- namespace Inter {
- lli in[maxk],fac[maxk],facrev[maxk],pprv[maxk],ssuf[maxk],*prv=pprv+,*suf=ssuf+;
- inline lli fastpow(lli base,int tim) {
- lli ret = ;
- while(tim) {
- if( tim & ) ret = ret * base % mod;
- if( tim >>= ) base = base * base % mod;
- }
- return ret;
- }
- inline void init() {
- for(int i=;i<k;i++) in[i] = fastpow(i,k-);
- for(int i=;i<k;i++) in[i] = ( in[i] + in[i-] ) % mod;
- }
- inline lli getmul(int p) {
- return p ? fac[p] * facrev[k-p-] % mod : facrev[k-];
- }
- inline lli getval(lli x) {
- lli ret = ;
- prv[-] = ;
- for(int i=;i<k;i++) prv[i] = prv[i-] * (x-i+mod) % mod;
- suf[k] = ;
- for(int i=k-;~i;i--) suf[i] = suf[i+] * (x-i+mod) % mod;
- for(int i=;i<k;i++) {
- lli now = prv[i-] * suf[i+] % mod;
- ret = ret + now * in[i] % mod * getmul(i) % mod , ret %= mod;
- }
- return ret;
- }
- inline void getinv() {
- static lli inv[maxn];
- *fac = ;
- for(int i=;i<=k;i++) fac[i] = fac[i-] * i % mod;
- inv[k] = fastpow(fac[k],mod-);
- for(int i=k;i;i--) inv[i-] = inv[i] * i % mod;
- for(int i=;i<=k;i++) inv[i] = inv[i] * fac[i-] % mod;
- for(int i=;i<=k;i++) fac[i] = fac[i-] * inv[i] % mod;
- facrev[] = ;
- for(int i=;i<=k;i++) facrev[i] = facrev[i-] * ( mod - inv[i] ) % mod;
- }
- }
- inline lli segphi(lli l,lli r) {
- return ( Sieve::getphi(r) - Sieve::getphi(l-) + mod ) % mod;
- }
- inline lli calc(lli n) {
- lli ret = ;
- for(lli i=,j;i<=n;i=j+) {
- j = n / ( n / i );
- ret += segphi(i,j) % mod * Inter::getval(n/i) % mod , ret %= mod;
- }
- return ret;
- }
- int main() {
- scanf("%d%d",&n,&k) , k += , Sieve::pre() , Inter::init() , Inter::getinv();
- printf("%lld\n",calc(n));
- return ;
- }
当然那个RYOI是什么意思?就不告诉你!
Cmd2001的毒瘤水题题解的更多相关文章
- 2019浙大校赛--J--Extended Twin Composite Number(毒瘤水题)
毒瘤出题人,坑了我们好久,从基本的素数筛选,到埃氏筛法,到随机数快速素数判定,到费马小定理,好好的水题做成了数论题. 结果答案是 2*n=n+3*n,特判1,2. 以下为毒瘤题目: 题目大意: 输入一 ...
- World Finals 2017 (水题题解)
看大佬做2017-WF,我这种菜鸡,只能刷刷水题,勉强维持生活. 赛后补补水题. 题目pdf链接,中文的,tls翻译的,链接在这里 个人喜欢在vjudge上面刷题. E Need for Speed ...
- bzoj usaco 金组水题题解(2)
续.....TAT这回不到50题编辑器就崩了.. 这里塞40道吧= = bzoj 1585: [Usaco2009 Mar]Earthquake Damage 2 地震伤害 比较经典的最小割?..然而 ...
- bzoj usaco 金组水题题解(2.5)
bzoj 2197: [Usaco2011 Mar]Tree Decoration 树形dp..f[i]表示处理完以i为根的子树的最小时间. 因为一个点上可以挂无数个,所以在点i上挂东西的单位花费就是 ...
- bzoj usaco 金组水题题解(1)
UPD:我真不是想骗访问量TAT..一开始没注意总长度写着写着网页崩了王仓(其实中午的时候就时常开始卡了= =)....损失了2h(幸好长一点的都单独开了一篇)....吓得赶紧分成两坨....TAT. ...
- 2006-2007 ACM-ICPC | POJ3380 POJ3384 POJ3385 水题题解
// CF比赛链接:http://codeforces.com/gym/101650 // POJ链接:http://poj.org/searchproblem?field=source&ke ...
- LOJ6303:水题——题解
https://loj.ac/problem/6303 题目来自LOJ. 就记一个公式,设f(n,k)为n!里分解得到的k(k为质数)的个数,则f(n,k)=f(n/k,k)+n/k. 证明很好证,显 ...
- leetcode水题题解
344. Reverse String Write a function that takes a string as input and returns the string reversed. E ...
- PAT甲题题解-1011. World Cup Betting (20)-误导人的水题。。。
题目不严谨啊啊啊啊式子算出来结果是37.975样例输出的是37.98我以为是四舍五入的啊啊啊,所以最后输出的是sum+0.005结果告诉我全部错误啊结果直接保留两位小数就可以了啊啊啊啊 水题也不要这么 ...
随机推荐
- indeed招聘
https://cn.indeed.com/%E5%B7%A5%E4%BD%9C-%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD%E5%85%AC%E5%8F%B8-%E5% ...
- 鸟哥的Linux私房菜——第十七章:Linux 账号与身份管理
视频链接:http://www.bilibili.com/video/av10669732/ 1. Linux 的账号与群组1.1 使用者识别: UID 与 GID1.2 使用者账号:/etc/pas ...
- OpenGL ES 2.0 Shader 调试新思路(二): 做一个可用的原型
OpenGL ES 2.0 Shader 调试新思路(二): 做一个可用的原型 目录 背景介绍 请参考前文OpenGL ES 2.0 Shader 调试新思路(一): 改变提问方式 优化 ledCha ...
- [iOS]@synthesize和@dynamic关键字
首先讲@property, 这是iOS6以后出来的关键词. 用它声明一个属性之后, 编译器会自动给你生成setter和getter方法的声明以及实现还有一个以_xxx 的成员变量(xxx是你属性定义的 ...
- [转载]DOMContentLoaded与interactive
http://www.cnblogs.com/muxrwc/archive/2011/01/13/1934379.html ie中inline script执行时竟然第一次进入页面,doc.ready ...
- 第二篇:服务消费者(rest + ribbon)
一. ribbon简介 ribbon是一个负载均衡客户端,可以很好的控制http和tcp的一些行为,Feign也用到了ribbon,当你使用@ FeignClient,ribbon自动被应用. Rib ...
- 使用Docx4j创建word文档
原文标题:Creating Word documents with Docx4j 原文链接:http://blog.iprofs.nl/2012/09/06/creating-word-documen ...
- JS脚本病毒调试脚本-Trojan[Downloader]:JS/Nemucod
1.前言 遇到Trojan[Downloader]:JS/Nemucod需要分析,这款病毒主要为js运行.从网上各种找js调试方法.发现52的帖子还挺沾边的. TrojanDownloader:JS/ ...
- House Robber I & II & III
House Robber You are a professional robber planning to rob houses along a street. Each house has a c ...
- shell中后台进程管理: ps -aux 详解
常用 查找进程id方法: ps -aux | grep "jupyter" 杀进程: kill -9 具体的PID 1.ps命令 要对进 ...