T1
打表出奇迹,发现结论为(E(a_n)=n+1)即可。
#include <iostream>
#include <cstdio>
#include <cctype>
#include <cstring>
using namespace std;
int n,Q;
long long rd() {
long long x=0,f=1;char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
while(isdigit(ch)) {x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int main() {
Q=rd();
while(Q--) {printf("%lld
",rd()+1);}
}
T2
正解应该是kmp,求出来nxt数组为偶数的就行了。但是蒟蒻了一下,并且受到200000的数据范围引导,写了个nlogn的做法,对于每个位置能延伸出去的长度,二分一下,check一下hash即可,荣幸成为全场除了写SA的老哥以外跑的最慢的。。。全场唯一一个hash+二分。。。(自从写单hash被卡后老老实实写双hash)
UPD:单hash并没有被卡。。
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
long long n,m,ans;
char s[200005];
struct Hsh{
unsigned long long hsh[200005],fac[200005],mod,p;
void build(unsigned long long M,unsigned long long P) {
mod=M,p=P;
fac[0]=1;
for(int i=1;i<=n;i++) {hsh[i]=(hsh[i-1]*p+s[i]-'a'+1)%mod;fac[i]=fac[i-1]*p%mod;}
}
unsigned long long gethash(int l,int r){return (hsh[r]-hsh[l-1]*fac[r-l+1]%mod+mod)%mod;}
}hsh1,hsh2;
struct Node{unsigned long long h1,h2;bool operator ==(const Node &rhs) const {return h1==rhs.h1&&h2==rhs.h2;}}h[100005],t;
Node gethsh(int l,int r) {return {hsh1.gethash(l,r),hsh2.gethash(l,r)};}
unsigned long long h1[200005],h2[200005];
void ck(int pos) {int l=1,r=n/2,mid,dd=0;while(l<=r) {mid=l+r>>1;if(h[mid]==gethsh(pos,pos+(mid<<1)-1)) l=mid+1,dd=mid;else r=mid-1;}ans+=dd;}
int main() {
scanf("%s",s+1);n=strlen(s+1);
hsh1.build(1e9+7,31);hsh2.build(1e9+9,37);
for(int i=2;i<=n;i+=2) h[i>>1]=gethsh(1,i);
for(int i=1;i<=n;i++)ck(i);
cout<<ans<<endl;
}
T3
讲过
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define init for(int O=1;O<=n;O++) fa[O]=O;
using namespace std;
int n,m,fa[1005];
struct Node{int ans,l;bool operator < (const Node &rhs)const {return (ans==rhs.ans)?(l>rhs.l):(ans<rhs.ans);}}ans;
int find(int x) {return x==fa[x]?x:fa[x]=find(fa[x]);}
struct Edge{int a,b,l,r;}e[3005];
bool cmp(Edge x,Edge y) {return x.r>y.r;}
int main() {
scanf("%d%d",&n,&m);
ans.ans=0,ans.l=0;
for(int i=1;i<=m;i++) scanf("%d%d%d%d",&e[i].a,&e[i].b,&e[i].l,&e[i].r);
sort(e+1,e+1+m,cmp);
for(int i=1,u,v;i<=m;i++) {
init
for(int j=1;j<=m;j++) {
if(e[j].l<=e[i].l) {
u=find(e[j].a),v=find(e[j].b),fa[u]=v;
if(find(1)==find(n)) {ans=max(ans,{e[j].r-e[i].l+1,e[i].l});break;}
}
}
}
printf("%d
",ans.ans);for(int i=ans.l;i<=ans.l+ans.ans-1;i++) printf("%d ",i);
}