Powered By HC TECH : AutoACer Engine

5300-5399
This commit is contained in:
KiritoTRw 2016-09-13 04:54:18 +08:00 committed by GitHub
parent a4744a8c1c
commit 6d0ead9b6a
83 changed files with 7560 additions and 0 deletions

33
HDOJ/5301_autoAC.cpp Normal file
View File

@ -0,0 +1,33 @@
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
int main(void) {
ll n, m, x, y;
while (scanf ("%I64d%I64d%I64d%I64d", &n, &m, &x, &y) == 4) {
if (n > m) {
swap (n, m); swap (x, y);
}
if (x > n - x + 1) {
x = n - x + 1;
}
if (y > m - y + 1) {
y = m - y + 1;
}
ll hm = (m + 1) / 2, hn = (n + 1) / 2;
ll ans = min (hm, hn);
if (m == n) {
if (m % 2 == 1) {
if (x == hn && y == hm) ans--;
}
}
else {
ll t = min (y, n - x);
ans = max (ans, t);
ans = min (ans, hm);
}
printf ("%I64d\n", ans);
}
return 0;
}

41
HDOJ/5302_autoAC.cpp Normal file
View File

@ -0,0 +1,41 @@
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
#define sf scanf
int T;
int a[5],b[5];
int d[1000005];
int main()
{
sf("%d",&T);
while(T--)
{
for(int i = 0;i<3;i++)sf("%d",a+i);
for(int i = 0;i<3;i++)sf("%d",b+i);
int sum = 0;
for(int i = 0;i<3;i++)sum+=a[i];
if( (a[1]&1) || (b[1]&1) )
{
puts("-1");
continue;
}
if(sum==4)
{
puts("4\n1 2 0\n1 3 0\n2 3 1\n3 4 1");
continue;
}
printf("%d\n",a[1]/2+a[2]+b[1]/2+b[2]);
int t = 1;
while(a[2]!=-1){printf("%d %d 0\n",t,t+1);t++;a[2]--;}t++;
while(a[1]!=2){printf("%d %d 0\n",t,t+1);t+=2;a[1]-=2;}
int tt = 0;
for(int i = 1;i<=sum;i+=2) d[tt++] = i;
for(int i = 2;i<=sum;i+=2)d[tt++] = i;
t = 0;
while(b[2]!=-1){printf("%d %d 1\n",min(d[t],d[t+1]),max(d[t],d[t+1]));t++;b[2]--;}t++;
while(b[1]!=2){printf("%d %d 1\n",min(d[t],d[t+1]),max(d[t],d[t+1]));t+=2;b[1]-=2;}
}
}

54
HDOJ/5303_autoAC.cpp Normal file
View File

@ -0,0 +1,54 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int t,n,m,l,num1,num2,cnt;
long long left[500010],right[500010];
long long sum1[500010],sum2[500010];
long long apple[500010];
long long ans;
int main()
{
int i,j,k,a,b;
scanf("%d",&t);
while(t--)
{
cnt=0;num1=num2=0;
memset(apple,0,sizeof(apple));
memset(sum1,0,sizeof(sum1));
memset(sum2,0,sizeof(sum2));
scanf("%d%d%d",&l,&n,&k);
for(i=0;i<n;i++)
{
scanf("%d%d",&a,&b);
for(j=1;j<=b;j++){
apple[++cnt]=a;
}
}
k=min(k,cnt);
for(i=1;i<=cnt;i++){
if(apple[i]*2<=l){
left[++num1]=apple[i];
}
else{
right[++num2]=l-apple[i];
}
}
sort(left+1,left+num1+1);
sort(right+1,right+num2+1);
for(i=1;i<=num1;i++){
if(i<=k) sum1[i]=left[i];
else sum1[i]=sum1[i-k]+left[i];
}
for(i=1;i<=num2;i++){
if(i<=k) sum2[i]=right[i];
else sum2[i]=sum2[i-k]+right[i];
}
ans=(long long)(sum1[num1]+sum2[num2])*2;
for(i=0;i<=num1&&i<=k;i++){
ans=min(ans,(long long)(l+2*sum1[num1-i]+2*sum2[num2-(k-i)]));
}
printf("%lld\n",ans);
}
return 0;
}

88
HDOJ/5304_autoAC.cpp Normal file
View File

@ -0,0 +1,88 @@
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int mo=998244353;
struct atom{
int x,y;
};
atom operator - (const atom k1,const atom k2){
return (atom){(k1.x-k2.x+mo)%mo,(k1.y-k2.y+mo)%mo};
}
atom operator * (const atom k1,const int k2){
return (atom){1ll*k1.x*k2%mo,1ll*k1.y*k2%mo};
}
int x[20][20],n,m,A[20][20],dp[1<<16][17],f[1<<16],pd[20];
int gauss(int n){
int pd=1; n--;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++) x[i][j]=(x[i][j]+mo)%mo;
for (int i=1;i<=n;i++){
int r=0;
for (int j=i;j<=n;j++) if (x[j][i]){r=j; break;}
for (int j=1;j<=n;j++) swap(x[i][j],x[r][j]);
if (r!=i) pd=-pd;
for (int j=i+1;j<=n;j++){
int k1=x[i][i],k2=x[j][i];
atom xx=(atom){1,0},y=(atom){0,1};
while (k2){
int k4=k1/k2;
atom z=y; y=xx-y*k4; xx=z;
k4=k2; k2=k1%k2; k1=k4; pd=-pd;
}
for (int k=1;k<=n;k++){
int k3=x[i][k],k4=x[j][k];
x[i][k]=(1ll*xx.x*k3+1ll*xx.y*k4)%mo;
x[j][k]=(1ll*y.x*k3+1ll*y.y*k4)%mo;
}
}
}
for (int i=1;i<=n;i++) pd=1ll*pd*x[i][i]%mo;
return (pd+mo)%mo;
}
int solve(){
memset(A,0x00,sizeof A);
for (;m;m--){
int k1,k2; scanf("%d%d",&k1,&k2); A[k1][k2]=1; A[k2][k1]=1;
}
memset(f,0x00,sizeof f);
for (int s=1;s<=n;s++){
memset(dp,0x00,sizeof dp);
dp[(1<<s-1)][s]=1;
for (int i=1;i<(1<<n);i++)
for (int j=1;j<=n;j++)
if (dp[i][j]){
for (int k=s+1;k<=n;k++)
if ((i&(1<<k-1))==0&&A[j][k])
dp[i|(1<<k-1)][k]=(dp[i|(1<<k-1)][k]+dp[i][j])%mo;
if (A[j][s]) f[i]=(f[i]+dp[i][j])%mo;
}
}
int ans=0;
for (int i=1;i<=n;i++)
for (int j=i+1;j<=n;j++)
if (A[i][j]) f[(1<<i-1)|(1<<j-1)]=(f[(1<<i-1)|(1<<j-1)]-1+mo)%mo;
int inv2=(mo+1)/2;
for (int i=0;i<(1<<n);i++) f[i]=1ll*f[i]*inv2%mo;
for (int now=0;now<(1<<n);now++) if (f[now]){
for (int i=1;i<=n;i++) pd[i]=0;
for (int i=1;i<=n;i++) if (now&(1<<i-1)) pd[i]=1;
int sign=1;
for (int i=1;i<=n;i++) if (pd[i]==0) pd[i]=++sign;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++) x[i][j]=0;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
if (A[i][j]&&pd[i]!=pd[j]){
x[pd[i]][pd[j]]--; x[pd[i]][pd[i]]++;
}
ans=(ans+1ll*f[now]*gauss(sign))%mo;
}
return ans;
}
int main(){
while (scanf("%d%d",&n,&m)!=EOF) printf("%d\n",solve());
return 0;
}

70
HDOJ/5305_autoAC.cpp Normal file
View File

@ -0,0 +1,70 @@
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
int ans;
int n,m;
int cnt[10];
struct node{
int x,y;
void in(){
scanf("%d%d",&x,&y);
if(x>y)swap(x,y);
cnt[x]++;
cnt[y]++;
}
friend bool operator < (node a,node b){
if(a.x==b.x)return a.y<b.y;
return a.x<b.x;
}
}p[30];
void dfs(int t,int on[],int off[]){
if(t==m){
ans++;
return ;
}
int x=p[t].x;
int y=p[t].y;
on[x]++ , on[y]++;
if(on[x]<=cnt[x]/2 && off[x]<=cnt[x]/2 && on[y]<=cnt[y]/2 && off[y]<=cnt[y]/2 ){
dfs(t+1,on,off);
}
on[x]-- , on[y]--;
off[x]++ , off[y]++;
if(on[x]<=cnt[x]/2 && off[x]<=cnt[x]/2 && on[y]<=cnt[y]/2 && off[y]<=cnt[y]/2 ){
dfs(t+1,on,off);
}
off[x]-- , off[y]-- ;
}
int main(){
int T;scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
int on[10];
int off[10];
memset(cnt,0,sizeof cnt);
memset(on,0,sizeof on);
memset(off,0,sizeof off);
for(int i=0;i<m;i++){
p[i].in();
}
bool ok=false;
for(int i=1;i<=n;i++){
if(cnt[i]&1){
ok=true;
break;
}
}
if(ok){
printf("0\n");
continue;
}
sort(p,p+m);
ans=0;
dfs(0,on,off);
printf("%d\n",ans);
}
return 0;
}

175
HDOJ/5306_autoAC.cpp Normal file
View File

@ -0,0 +1,175 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long int LL;
const int maxn=1000030;
int n,m;
int a[maxn];
struct Node
{
LL ss;
int mx,tag,cv;
void toString() {
printf("ss: %lld mx: %d tag: %d cv: %d\n",ss,mx,tag,cv);
}
}T[maxn<<2];
#define lrt (rt<<1)
#define rrt (rt<<1|1)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
void push_up(int rt)
{
T[rt].ss=T[lrt].ss+T[rrt].ss;
T[rt].mx=max(T[lrt].mx,T[rrt].mx);
T[rt].cv=T[lrt].cv+T[rrt].cv;
}
void pnc(int t,int l,int r,int rt)
{
if(T[rt].tag!=0&&T[rt].tag<=t) return ;
int all=r-l+1;
if(T[rt].cv!=all)
{
T[rt].ss+=(LL)t*(all-T[rt].cv);
T[rt].tag=T[rt].mx=t;
T[rt].cv=all;
}
}
void push_down(int l,int r,int rt)
{
if(T[rt].tag)
{
int m=(l+r)/2;
pnc(T[rt].tag,lson); pnc(T[rt].tag,rson);
}
}
void fix(int t,int l,int r,int rt)
{
if(T[rt].mx<t) return ;
if(T[rt].tag>=t) T[rt].tag=0;
if(l==r)
{
T[rt].ss=T[rt].mx=T[rt].tag;
T[rt].cv=T[rt].tag!=0;
}
else
{
push_down(l,r,rt);
int m=(l+r)/2;
fix(t,lson); fix(t,rson);
push_up(rt);
}
}
void build(int l,int r,int rt)
{
if(l==r)
{
T[rt].ss=T[rt].mx=T[rt].tag=a[l];
T[rt].cv=1;
return ;
}
T[rt].tag=0;
int m=(l+r)/2;
build(lson); build(rson);
push_up(rt);
}
void update(int L,int R,int t,int l,int r,int rt)
{
if(T[rt].mx<=t) return ;
if(L<=l&&r<=R)
{
fix(t,l,r,rt);
if(l==r)
{
T[rt].ss=T[rt].mx=T[rt].tag=t;
T[rt].cv=1;
}
else push_up(rt);
pnc(t,l,r,rt);
}
else
{
push_down(l,r,rt);
int m=(l+r)/2;
if(L<=m) update(L,R,t,lson);
if(R>m) update(L,R,t,rson);
push_up(rt);
}
}
int query_max(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R) return T[rt].mx;
push_down(l,r,rt);
int m=(l+r)/2;
int ret=0;
if(L<=m) ret=max(ret,query_max(L,R,lson));
if(R>m) ret=max(ret,query_max(L,R,rson));
return ret;
}
LL query_sum(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R) return T[rt].ss;
push_down(l,r,rt);
int m=(l+r)/2;
LL ret=0;
if(L<=m) ret+=query_sum(L,R,lson);
if(R>m) ret+=query_sum(L,R,rson);
return ret;
}
void show(int l,int r,int rt)
{
printf("rt: %d %d <---> %d\n ",rt,l,r);
T[rt].toString();
if(l==r) return ;
int m=(l+r)/2;
show(lson); show(rson);
}
char *ch,buf[40*1024000+5];
void nextInt(int& x)
{
x=0;
for(++ch;*ch<=32;++ch);
for(x=0;'0'<=*ch;ch++) x=x*10+*ch-'0';
}
int main()
{
ch=buf-1;
fread(buf,1,1000*35*1024,stdin);
int T_T;
nextInt(T_T);
while(T_T--)
{
nextInt(n); nextInt(m);
for(int i=1;i<=n;i++) nextInt(a[i]);
build(1,n,1);
int k,l,r,t;
while(m--)
{
nextInt(k);
if(k==0)
{
nextInt(l); nextInt(r); nextInt(t);
update(l,r,t,1,n,1);
}
else if(k==1)
{
nextInt(l); nextInt(r);
printf("%d\n",query_max(l,r,1,n,1));
}
else if(k==2)
{
nextInt(l); nextInt(r);
printf("%I64d\n",query_sum(l,r,1,n,1));
}
}
}
return 0;
}

94
HDOJ/5307_autoAC.cpp Normal file
View File

@ -0,0 +1,94 @@
#include <cstdio>
#include <string.h>
using namespace std;
#define P 50000000001507329LL
#define G 3
int T;
int n, s[110000];
long long ans[140000], a[140000], b[140000], c[140000], x[140000], w[140000];
long long pu[110000];
int nn;
long long Mul(long long x, long long y) {
return (x*y-(long long)(x/(long double)P*y+1e-3)*P+P)%P;
}
long long Pow(long long x, long long y) {
long long i, ans = 1;
for (i = 1; i <= y; i *= 2, x = Mul(x, x)) if (y & i) ans = Mul(ans, x);
return ans;
}
void DFT(long long *a, int n) {
int m, i, d, p, q;
for (m = 1; (1 << m) <= n; m++){
for (i = 0; i < (n >> m); i++)
for (q = 0, d = p = i; d < n; q += (n >> m), d += (n >> (m - 1)), p += (n >> m)){
x[p] = (Mul(a[d + (n >> m)], w[q]) + a[d]) % P;
x[p + n / 2] = (Mul(a[d + (n >> m)], w[q + n / 2]) + a[d]) % P;
}
for (i = 0; i < n; i++) a[i] = x[i];
}
}
void DFT1(long long *a, int n){
int m, i, d, p, q;
for (m = 1; (1 << m) <= n; m++) {
for (i = 0; i < (n >> m); i++)
for (q = 0, d = p = i; d < n; q += (n >> m), d += (n >> (m - 1)), p += (n >> m)){
x[p] = (Mul(a[d + (n >> m)], w[n - q]) + a[d]) % P;
x[p + n / 2] = (Mul(a[d + (n >> m)], w[n / 2 - q]) + a[d]) % P;
}
for (i = 0; i < n; i++) a[i] = x[i];
}
}
void doit() {
long long S = Pow(n, P - 2);
DFT(a, n);
DFT(b, n);
for (int i = 0; i < n; i++)
c[i] = Mul(a[i], b[i]);
DFT1(c, n);
for (int i = 0; i < n; i++)
c[i] = Mul(c[i], S);
for (int i = 0; i < n; i++)
ans[i] = (ans[i] + c[i]) % P;
}
int main() {
scanf("%d", &T);
n = 131072;
for (int i = 0; i <= n; i++)
w[i] = Pow(G, (P - 1) / n * i);
s[0] = 0;
while (T--) {
scanf("%d", &nn);
for (int i = 1; i <= nn; i++)
scanf("%d", &s[i]), s[i] += s[i - 1];
memset(ans, 0, sizeof ans);
memset(a, 0, sizeof a);
memset(b, 0, sizeof b);
for (int i = 1; i <= nn; i++)
a[s[i]] += i;
for (int i = 1; i <= nn; i++)
b[s[nn] - s[i - 1]]++;
doit();
memset(a, 0, sizeof a);
memset(b, 0, sizeof b);
for (int i = 0; i <= 50000; i++)
a[i] = b[i] = 0;
for (int i = 1; i <= nn; i++)
a[s[i]] = (a[s[i]] + P - 1) % P;
for (int i = 1; i <= nn; i++)
b[s[nn] - s[i - 1]] += i - 1;
doit();
long long ans0 = 0;
int q, h;
for (int i = 1; i <= nn; i++)
pu[i] = pu[i - 1] + 1LL * i * (i + 1) / 2;
for (q = 1; q <= nn; q = h + 1)
if (s[q] != s[q - 1]) h = q;
else {
for (h = q; h < nn && s[h + 1] == s[q]; h++);
ans0 += pu[h - q + 1];
}
printf("%lld\n", ans0);
for (int i = 1; i <= s[nn]; i++)
printf("%lld\n", ans[i + s[nn]]);
}
}

46
HDOJ/5308_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
int n;
while(~scanf("%d",&n)){
if(n<=3){puts("-1");continue;}
if(n==4){puts("1 * 2");puts("5 + 3");puts("6 + 4");continue; }
if(n==5){puts("2 / 3");puts("6 / 4");puts("1 - 7");puts("8 * 5");continue; }
if(n==6){puts("1 + 2");puts("7 + 3");puts("8 + 4");puts("9 + 5");puts("10 - 6");continue; }
if(n==7){puts("1 + 2");puts("8 + 3");puts("9 / 4");puts("10 + 5");puts("11 + 6");puts("12 + 7");continue; }
if(n==8){puts("1 + 2");puts("9 + 3");puts("10 + 4");puts("5 + 6");puts("11 / 12");puts("13 * 7");puts("14 + 8");continue; }
if(n==9){puts("1 + 2");puts("10 + 3");puts("4 + 5");puts("12 + 6");puts("13 / 7");
puts("11 - 14");puts("15 * 8");puts("16 / 9");continue;}
if(n==10){puts("1 + 2");puts("11 / 3");puts("12 + 4");puts("5 / 6");puts("7 / 8");
puts("14 + 15");puts("13 * 16");puts("17 * 9");puts("18 / 10");continue; }
if(n==11){puts("1 / 2");puts("12 + 3");puts("4 / 5");puts("6 / 7");puts("14 + 15");puts("13 * 16");
puts("17 * 8");puts("18 * 9");puts("19 / 10");puts("20 / 11");continue; }
if(n&1){
puts("1 + 2");printf("%d / 3\n",n+1);
puts("4 + 5");printf("%d / 6\n",n+3);
puts("7 + 8");printf("%d / 9\n",n+5);
puts("10 + 11");printf("%d + 12\n",n+7);printf("%d / 13\n",n+8);
printf("%d * %d\n",n+2,n+4);printf("%d * %d\n",n+10,n+6);
printf("%d * %d\n",n+11,n+9);
int x=n+12,y=14;
for(int i=1;i<=(n-13)/2;i++){
printf("%d * %d\n",x++,y++);
printf("%d / %d\n",x++,y++);
}
}else{
puts("1 + 2");printf("%d + 3\n",n+1);printf("%d + 4\n",n+2);printf("%d / 5\n",n+3);
puts("6 + 7");
for(int i=5;i<=8;i++)
printf("%d + %d\n",n+i,i+3);
printf("%d / 12\n",n+9);
printf("%d * %d\n",n+4,n+10);
int x=n+11,y=13;
for(int i=1;i<=(n-12)/2;i++){
printf("%d * %d\n",x++,y++);
printf("%d / %d\n",x++,y++);
}
}
}
return 0;
}

91
HDOJ/5309_autoAC.cpp Normal file
View File

@ -0,0 +1,91 @@
#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
#define sf scanf
#define mx 100000000
struct node{
int id,v;
//friend bool operator<(node a,node b){return (a.v==b.v)?(a.id>b.id):(a.v>b.v);}
bool operator < (const node &a)const {
if(a.v==v)return id>a.id;
return v<a.v;
}
};
priority_queue<node> que1,que2;
queue<int> que;
int n,m,k;
int g[1000000],sum[1000000],a[1000000],r[1000000];
int main()
{
while(~scanf("%d%d%d",&n,&m,&k))
{
while(!que1.empty()) que1.pop();
while(!que2.empty())que2.pop();
for(int i = 1;i<=n;i++) {sf("%d",a+i);sum[i]=sum[i-1]+a[i];}
int t = 0;
for(int i = 0;i<=n;i++){while(t<=n&&sum[t]<i*k + m) t++;r[i] = t-1;}
for(int i = n;i>0;i--)
{
g[i] = 0;
if(r[i] == n) g[i] = mx + i;
else
{
while(!que1.empty()){
node tmp = que1.top();
if(tmp.id>r[i])que1.pop();
else if(tmp.v*2>=g[tmp.id] + i)
{
que2.push(node{tmp.id,g[tmp.id]-tmp.id});
que1.pop();
}
else break;
}
while(!que2.empty())
{
node tmp = que2.top();
if(tmp.id>r[i])que2.pop();
else break;
}
if(!que1.empty())g[i] = max(g[i],que1.top().v);
if(!que2.empty())g[i] = max(g[i],que2.top().v+i);
}
if(g[i]>i) que1.push((node){i,i});
else que2.push((node){i,g[i]-i});
}
for(int i = 1;i<=n;i++)g[i]-=i;
g[0] = 0;
for(int i = 1;i<=r[0];i++) g[0] = max(g[0],g[i]);
if(g[0] > n) puts("Poor JRY!");
else if(!g[0]) puts("Poor Hero!");
else
{
printf("%d\n",g[0]);
int i = 1;
while(g[i]!=g[0]) i++;
que.push(i);
while(g[i]!=mx)
{
int j=i+g[0];
while(g[j]<g[0]) {
j++;
}
i = j;
que.push(i);
}
printf("%d\n",que.size());
while(!que.empty())
{
int tmp = que.front();
printf("%d",tmp);
que.pop();
if(que.size())printf(" ");
else printf("\n");
}
}
}
return 0;
}

25
HDOJ/5310_autoAC.cpp Normal file
View File

@ -0,0 +1,25 @@
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
int m,n,q,t,p;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d",&n,&m ,&p,&q);
int sum=0;
double op=q/m;
if(op<p)
{
sum+=(n/m)*q;
int pl=n%m;
sum+=min(p*pl,q);
cout<<sum<<endl;
}
else
cout<<p*n<<endl;
}
return 0;
}

104
HDOJ/5311_autoAC.cpp Normal file
View File

@ -0,0 +1,104 @@
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MAX_N = 200 + 10;
int Next[MAX_N];
const char *str = "anniversary";
char pat[MAX_N], pat_sub[MAX_N];
char sub_1[MAX_N], sub_2[MAX_N], sub_3[MAX_N];
void get_nextval(char *W, int Next[])
{
int i = 0, j = -1;
int Len_W = strlen(W);
Next[0] = -1;
if (W[0] == '\0')
return;
while (i < Len_W)
{
if (j == -1 || W[i] == W[j])
{
++i;
++j;
Next[i] = j;
} else
j = Next[j];
}
}
int KMP_Matcher(char T[], char P[], int Next[])
{
int i = 0, j = 0;
int TLen = strlen(T);
int PLen = strlen(P);
if (TLen == 0)
return -2;
while (i < TLen && j < PLen)
{
if (j == -1 || T[i] == P[j])
{
i++;
j++;
} else
j = Next[j];
}
if (j == PLen)
return i - j;
else
return -1;
}
int main()
{
int case_times, len;
scanf("%d", &case_times);
len = strlen(str);
while (case_times--)
{
int len_s;
bool ans = false;
scanf("%s", pat);
strcpy(pat_sub, pat);
len_s = strlen(pat);
for (int i = 1; i < len; ++i)
{
if (ans)
break;
for (int j = i + 1; j < len; ++j)
{
int m_1, m_2, m_3;
strncpy(sub_1, str, i);
sub_1[i] = '\0';
strncpy(sub_2, str + i, j - i);
sub_2[j - i] = '\0';
strncpy(sub_3, str + j, len - j);
sub_3[len - j] = '\0';
strcpy(pat_sub, pat);
get_nextval(sub_1, Next);
m_1 = KMP_Matcher(pat_sub, sub_1, Next);
if (m_1 >= 0)
{
for (int k = 0; k <= len_s - i; ++k)
pat_sub[k] = pat[m_1 + i + k];
}
get_nextval(sub_2, Next);
m_2 = KMP_Matcher(pat_sub, sub_2, Next);
if (m_2 >= 0)
{
for (int k = 0; k <= len_s - j; ++k)
pat_sub[k] = pat_sub[m_2 + j - i + k];
}
get_nextval(sub_3, Next);
m_3 = KMP_Matcher(pat_sub, sub_3, Next);
if (m_1 >= 0 && m_2 >= 0 && m_3 >= 0)
{
ans = true;
break;
}
}
}
if (ans)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

63
HDOJ/5312_autoAC.cpp Normal file
View File

@ -0,0 +1,63 @@
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
ll a[1000000];
int count1;
int main(){
mem0(a);
int m;
for(int i=1;(ll)3*i*(i-1)+1<=1000010000;i++){
a[i]=3*i*(i-1)+1;
m=i;
}
int t;
int n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
if(*lower_bound(a+1,a+m+1,n)==n){
printf("1\n");
continue;
}
int p=1;
int flag=0;
if((n-2)%6==0){
while(2*a[p]<=n){
if(*lower_bound(a+1,a+m+1,n-a[p])==n-a[p]){
printf("2\n");
flag=1;
break;
}
p++;
}
}
if(flag==1)
continue;
for(int i=3;;i++){
if((n-i)%6==0){
printf("%d\n",i);
break;
}
}
}
return 0;
}

55
HDOJ/5313_autoAC.cpp Normal file
View File

@ -0,0 +1,55 @@
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
using namespace std;
int x,y;
vector<int>v[10004];
set<int>s[2];
int dfs(int p,int b)
{
s[b].insert(p);
int l=v[p].size();
for(int i=0;i<l;i++)
if(!s[!b].count(v[p][i]))
dfs(v[p][i],!b);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&x,&y);
for(int i=0;i<y;i++)
{
int tx,ty;
scanf("%d%d",&tx,&ty);
v[tx].push_back(ty);
v[ty].push_back(tx);
}
s[0].clear();
s[1].clear();
for(int i=0;i<x;i++)
if(v[i].size())
{
dfs(i,0);
break;
}
long long line;
long long mmax=(s[0].size(),s[1].size());
if(mmax<(x/2))
{
line=(x/2)*(x-x/2)-y;
}
else
line=(x-mmax)*(x-(x-mmax))-y;
printf("%I64d\n",line);
for(int i=0;i<=x;i++)
v[i].clear();
}
return 0;
}

85
HDOJ/5314_autoAC.cpp Normal file
View File

@ -0,0 +1,85 @@
#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <cmath>
using namespace std;
const int size=200111;
typedef long long ll;
ll inf=-1000111000ll;
typedef pair<int,int> P;
ll mod=1000000007;
int t,n,m,a[size],K;
vector<int>V[size];
int vis[size],sum[size],id,tmp;
void find(int u,int fa,int num){
sum[u]=1;int K=0;
for(int i=0;i<V[u].size();i++){
int to=V[u][i];
if(vis[to]||to==fa)continue;
find(to,u,num);
K=max(K,sum[to]);
sum[u]+=sum[to];
}
K=max(K,num-sum[u]);
if(K<tmp)tmp=K,id=u;
}
P b[size];
int tot;
void dfs(int u,int fa,int mi,int ma){
sum[u]=1;
mi=min(mi,a[u]);
ma=max(ma,a[u]);
if(ma<=mi+K)b[tot++]=P(mi,ma);
for(int i=0;i<V[u].size();i++){
int to=V[u][i];
if(vis[to]||to==fa)continue;
dfs(to,u,mi,ma);
sum[u]+=sum[to];
}
}
ll gao(int u,int mi,int ma){
tot=0;
dfs(u,0,mi,ma);
sort(b,b+tot);
ll ans=0;
for(int i=0;i<tot;i++){
int p=lower_bound(b,b+i,P(b[i].second-K,0))-b;
ans+=i-p;
}
return ans;
}
ll work(int u,int num){
tmp=n;
find(u,0,num);
u=id;
ll ans=gao(u,a[u],a[u]);
vis[u]=1;
for(int i=0;i<V[u].size();i++){
int to=V[u][i];
if(!vis[to])ans-=gao(to,a[u],a[u]);
}
for(int i=0;i<V[u].size();i++){
int to=V[u][i];
if(!vis[to])ans+=work(to,sum[to]);
}
return ans;
}
int main(){
cin>>t;
while(t--){
cin>>n>>K;
for(int i=1;i<=n;i++)V[i].clear(),scanf("%d",&a[i]),vis[i]=0;
for(int i=1;i<n;i++){
int x,y;
scanf("%d%d",&x,&y);
V[x].push_back(y);
V[y].push_back(x);
}
cout<<work(1,n)*2<<endl;
}
}

109
HDOJ/5316_autoAC.cpp Normal file
View File

@ -0,0 +1,109 @@
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#define LL long long
#define INF 0x3f3f3f3f
#define msz 405000
#define Max 50000000000000
using namespace std;
typedef struct LinkTree
{
LL sum[2][2];
}LinkTree;
LinkTree tr[405005];
int n,m;
LinkTree Joint(LinkTree a,LinkTree b)
{
int i,j;
LL t;
LinkTree tmp;
for(i = 0; i < 2; ++i)
{
for(j = 0; j < 2; ++j)
{
tmp.sum[i][j] = max(a.sum[i][j],b.sum[i][j]);
t = max(a.sum[i][0]+b.sum[1][j],a.sum[i][1]+b.sum[0][j]);
tmp.sum[i][j] = max(tmp.sum[i][j],t);
}
}
return tmp;
}
void SetTree(int site,int l,int r)
{
int i,j,x;
if(l == r)
{
scanf("%d",&x);
for(i = 0; i < 2; ++i)
for(j = 0; j < 2; ++j)
tr[site].sum[i][j] = -Max;
tr[site].sum[l&1][r&1] = x;
return;
}
int m = (l+r)>>1;
SetTree(site<<1,l,m);
SetTree(site<<1|1,m+1,r);
tr[site] = Joint(tr[site<<1],tr[site<<1|1]);
}
void Change(int site,int l,int r,int k,int data)
{
if(l == k && r == k)
{
tr[site].sum[l&1][l&1] = data;
return;
}
int m = (l+r)>>1;
if(m >= k)
Change(site<<1,l,m,k,data);
else
Change(site<<1|1,m+1,r,k,data);
tr[site] = Joint(tr[site<<1],tr[site<<1|1]);
}
LinkTree MAX(int site,int l,int r,int ll,int rr)
{
int i,j;
LinkTree tmp;
if(l == ll && r == rr)
{
return tr[site];
}
int m = (l+r)>>1;
if(m >= rr) return MAX(site<<1,l,m,ll,rr);
else if(m < ll) return MAX(site<<1|1,m+1,r,ll,rr);
else
{
return Joint(MAX(site<<1,l,m,ll,m),MAX(site<<1|1,m+1,r,m+1,rr));
}
}
int main()
{
int i,j,t,x,b,a;
LL mm;
LinkTree tmp;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
SetTree(1,1,n);
while(m--)
{
scanf("%d %d %d",&x,&a,&b);
if(x) Change(1,1,n,a,b);
else
{
tmp = MAX(1,1,n,a,b);
mm = -Max;
for(i = 0; i < 2; ++i)
{
for(j = 0; j < 2; ++j)
{
mm = max(mm,tmp.sum[i][j]);
}
}
printf("%lld\n",mm);
}
}
}
return 0;
}

77
HDOJ/5317_autoAC.cpp Normal file
View File

@ -0,0 +1,77 @@
#include <stdio.h>
#include <iostream>
using namespace std;
const int MAX = 1000000+50;
int arr[MAX],prime[MAX];
int dp[MAX][9];
void init()
{
int n=1000000;
int flag = 0;
prime[2]=1;
for(int i=3;i<=n;i++)
{
if(i%2==0)
{
flag=1;
}
else
{
for(int j=3;j*j<=i;j=j+2)
{
if(i%j==0)
{
flag = 1;
break;
}
}
}
if(flag==0)
{
prime[i]=1;
}
flag=0;
}
for(int i = 2;i<=n;i++)
{
if(prime[i])
{
for(int j=i;j<=n;j+=i)
{
arr[j]++;
}
}
}
for(int i=2;i<=n;i++)
{
dp[i][arr[i]]++;
}
for(int i=2;i<=n;i++)
{
for(int j=1;j<8;j++)
{
dp[i][j]+=dp[i-1][j];
}
}
}
int main()
{
init();
int T;
scanf("%d",&T);
while(T--)
{
int L,R;
scanf("%d %d",&L,&R);
int ans=1;
for(int j=2;j<8;j++)
{
if(dp[R][j]-dp[L-1][j]>=2)
{
ans=max(ans,j);
}
}
printf("%d\n",ans);
}
return 0;
}

101
HDOJ/5318_autoAC.cpp Normal file
View File

@ -0,0 +1,101 @@
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <string>
#include <set>
using namespace std;
typedef long long ll;
const int MAXN = 55;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
struct Mat {
int m[MAXN][MAXN];
Mat () {
memset (m, 0, sizeof (m));
}
void init(void) {
for (int i=0; i<MAXN; ++i) m[i][i] = 1;
}
};
int n, m;
int a[MAXN];
int dp[MAXN][MAXN];
bool judge(int x, int y) {
char p[15], q[15];
sprintf (p, "%d", x);
sprintf (q, "%d", y);
int lenp = strlen (p), lenq = strlen (q);
for (int i=0; i<lenp; ++i) {
int k = 0;
while (i + k < lenp && k < lenq && p[i+k] == q[k]) k++;
if (i + k == lenp && k >= 2) return true;
}
return false;
}
Mat operator * (Mat &a, Mat &b) {
Mat ret;
for (int k=1; k<=n; ++k) {
for (int i=1; i<=n; ++i) {
for (int j=1; j<=n; ++j) {
ret.m[i][j] = (ret.m[i][j] + 1LL * a.m[i][k] * b.m[k][j] % MOD) % MOD;
}
}
}
return ret;
}
Mat operator ^ (Mat x, int n) {
Mat ret; ret.init ();
while (n) {
if (n & 1) ret = ret * x;
x = x * x;
n >>= 1;
}
return ret;
}
Mat work(void) {
Mat ret;
for (int i=1; i<=n; ++i) {
for (int j=1; j<=n; ++j) {
if (judge (a[i], a[j])) ret.m[i][j] = 1;
}
}
return ret;
}
void brute_force(void) {
memset (dp, 0, sizeof (dp));
for (int i=1; i<=n; ++i) dp[1][i] = 1;
for (int i=2; i<=m; ++i) {
for (int j=1; j<=n; ++j) {
for (int k=1; k<=n; ++k) if (judge (a[j], a[k])) {
dp[i][k] += dp[i-1][j];
dp[i][k] %= MOD;
}
}
}
int res = 0;
for (int i=1; i<=n; ++i) {
res += dp[m][i]; res %= MOD;
}
printf ("%d\n", res);
}
int main(void) {
int T; scanf ("%d", &T);
while (T--) {
scanf ("%d%d", &n, &m);
for (int i=1; i<=n; ++i) {
scanf ("%d", &a[i]);
}
sort (a+1, a+1+n); n = unique (a+1, a+1+n) - (a + 1);
Mat x = work ();
Mat res = x ^ (m - 1); int ans = 0;
for (int i=1; i<=n; ++i) {
for (int j=1; j<=n; ++j) {
ans = (ans + res.m[i][j]) % MOD;
}
}
printf ("%d\n", ans);
}
return 0;
}

35
HDOJ/5319_autoAC.cpp Normal file
View File

@ -0,0 +1,35 @@
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int MAXN = 55;
const int INF = 0x3f3f3f3f;
char s[MAXN][MAXN];
bool vis[MAXN][MAXN];
int n, m;
int main(void) {
int T; scanf ("%d", &T);
while (T--) {
scanf ("%d", &n);
for (int i=1; i<=n; ++i) scanf ("%s", s[i] + 1);
memset (vis, false, sizeof (vis)); m = strlen (s[1] + 1);
int ans = 0;
for (int i=1; i<=n; ++i) {
for (int j=1; j<=m; ++j) {
if (s[i][j] == 'R' || s[i][j] == 'G') {
if (!(s[i-1][j-1] == 'R' || s[i-1][j-1] == 'G')) ans++;
}
}
}
for (int i=1; i<=n; ++i) {
for (int j=1; j<=m; ++j) {
if (s[i][j] == 'B' || s[i][j] == 'G') {
if (!(s[i-1][j+1] == 'B' || s[i-1][j+1] == 'G')) ans++;
}
}
}
printf ("%d\n", ans);
}
return 0;
}

160
HDOJ/5320_autoAC.cpp Normal file
View File

@ -0,0 +1,160 @@
#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
using namespace std ;
typedef long long LL ;
#define clr( a , x ) memset ( a , x , sizeof a )
#define ls ( o << 1 )
#define rs ( o << 1 | 1 )
#define lson ls , l , m
#define rson rs , m + 1 , r
#define root 1 , 1 , n
#define rt o , l , r
#define mid ( ( l + r ) >> 1 )
const int MAXN = 100005 ;
const int mod = 998244353 ;
struct Node {
int g , idx , L , R ;
Node () {}
Node ( int g , int idx , int L , int R ) : g ( g ) , idx ( idx ) , L ( L ) , R ( R ) {}
bool operator < ( const Node& a ) const {
return g != a.g ? g < a.g : idx < a.idx ;
}
} ;
int a[MAXN] ;
vector < pair < int , int > > G[2] ;
Node node[MAXN * 25] ;
int cnt ;
int vis[MAXN << 2] , Time ;
int maxv[MAXN << 2] ;
int sum[MAXN << 2] ;
int add[MAXN << 2] ;
int query_ , query_ ;
int ans_ , ans_ ;
int n ;
int gcd ( int a , int b ) {
return b ? gcd ( b , a % b ) : a ;
}
void check ( int o ) {
if ( vis[o] < Time ) {
vis[o] = Time ;
maxv[o] = sum[o] = add[o] = 0 ;
}
}
void upd ( int Max , int Add , int o , int len ) {
check ( o ) ;
if ( Max > maxv[o] ) {
maxv[o] = Max ;
add[o] = Add ;
sum[o] = ( LL ) len * Add % mod ;
} else if ( Max == maxv[o] ) {
add[o] = ( add[o] + Add ) % mod ;
sum[o] = ( sum[o] + ( LL ) len * Add ) % mod ;
}
}
void pushdown ( int o , int l , int r ) {
int m = mid ;
if ( add[o] ) {
upd ( maxv[o] , add[o] , ls , m - l + 1 ) ;
upd ( maxv[o] , add[o] , rs , r - m ) ;
add[o] = 0 ;
}
}
void pushup ( int o ) {
check ( ls ) ;
check ( rs ) ;
if ( maxv[ls] == maxv[rs] ) {
sum[o] = ( sum[ls] + sum[rs] ) % mod ;
maxv[o] = maxv[ls] ;
} else if ( maxv[ls] > maxv[rs] ) {
maxv[o] = maxv[ls] ;
sum[o] = sum[ls] ;
} else {
maxv[o] = maxv[rs] ;
sum[o] = sum[rs] ;
}
}
void update ( int L , int R , int x , int v , int o , int l , int r ) {
check ( o ) ;
if ( L <= l && r <= R ) {
upd ( x , v , o , r - l + 1 ) ;
return ;
}
int m = mid ;
pushdown ( rt ) ;
if ( L <= m ) update ( L , R , x , v , lson ) ;
if ( m < R ) update ( L , R , x , v , rson ) ;
pushup ( o ) ;
}
void query ( int L , int R , int o , int l , int r ) {
if ( R < L ) return ;
check ( o ) ;
if ( L <= l && r <= R ) {
if ( query_max < maxv[o] ) {
query_max = maxv[o] ;
query_sum = sum[o] ;
} else if ( query_max == maxv[o] ) query_sum = ( query_sum + sum[o] ) % mod ;
return ;
}
int m = mid ;
pushdown ( rt ) ;
if ( L <= m ) query ( L , R , lson ) ;
if ( m < R ) query ( L , R , rson ) ;
}
void solve () {
cnt = 0 ;
for ( int i = 1 ; i <= n ; ++ i ) {
scanf ( "%d" , &a[i] ) ;
}
int cur = 0 ;
G[cur].clear () ;
G[cur].push_back ( make_pair ( a[n] , n ) ) ;
node[cnt ++] = Node ( a[n] , n , n , n ) ;
for ( int i = n - 1 ; i >= 1 ; -- i ) {
cur ^= 1 ;
G[cur].clear () ;
G[cur].push_back ( make_pair ( a[i] , i ) ) ;
int pre = a[i] ;
int m = 1 ;
for ( int j = 0 ; j < G[cur ^ 1].size () ; ++ j ) {
int v = G[cur ^ 1][j].first ;
int g = gcd ( pre , v ) ;
if ( g != pre ) {
G[cur].push_back ( make_pair ( g , G[cur ^ 1][j].second ) ) ;
node[cnt ++] = Node ( pre , i , G[cur][m - 1].second , G[cur][m].second - 1 ) ;
++ m ;
}
pre = g ;
}
node[cnt ++] = Node ( pre , i , G[cur][m - 1].second , n ) ;
}
ans_sum = ans_max = 0 ;
sort ( node , node + cnt ) ;
for ( int i = 0 , j = 0 ; i < cnt ; i = j ) {
while ( j < cnt && node[j].g == node[i].g ) ++ j ;
{
++ Time ;
update ( node[i].L , node[i].R , 1 , 1 , root ) ;
for ( int l = i + 1 ; l < j ; ++ l ) {
int idx = node[l].idx ;
query_max = query_sum = 0 ;
query ( 1 , idx - 1 , root ) ;
if ( query_max == 0 ) query_sum = 1 ;
update ( node[l].L , node[l].R , query_max + 1 , query_ , root ) ;
}
if ( ans_max < maxv[1] ) {
ans_max = maxv[1] ;
ans_sum = sum[1] ;
} else if ( ans_max == maxv[1] ) {
ans_sum = ( ans_sum + sum[1] ) % mod ;
}
}
}
printf ( "%d %d\n" , ans_ , ans_ ) ;
}
int main () {
clr ( vis , 0 ) ;
Time = 0 ;
while ( ~scanf ( "%d" , &n ) ) solve () ;
}

96
HDOJ/5321_autoAC.cpp Normal file
View File

@ -0,0 +1,96 @@
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define maxn 1000000
#define MOD 258280327
#define LL long long
int notprime[maxn+10];
int prime[maxn+10],tot;
int mu[maxn+10]={0,1};
LL jie[maxn+10];
LL inv[maxn+10];
LL power(LL x,LL k){
LL res=1,t=x;
while(k){
if(k&1)res=res*t%MOD;
t=t*t%MOD;
k>>=1;
}
return res%MOD;
}
void init(){
jie[0]=jie[1]=1;
tot=0;
mu[1]=1;
inv[0]=inv[1]=1;
for(int i=2;i<=maxn;i++){
jie[i]=i*jie[i-1]%MOD;
inv[i]=power(jie[i],MOD-2);
if(!notprime[i]){
prime[tot++]=i;
mu[i]=-1;
}
for(int j=0;j<tot&&i*prime[j]<=maxn;j++){
notprime[i*prime[j]]=1;
if(i%prime[j]==0){
mu[i*prime[j]]=0;
break;
}
mu[i*prime[j]]=-mu[i];
}
}
}
int n;
int ma;
int cnt[maxn+10];
LL F1[maxn+10],F2[maxn+10];
LL ans1,ans2;
void calc(){
for(int i=1;i<=ma;i++){
LL tmp=0;
for(int j=1;j<=cnt[i];j++){
LL a=jie[cnt[i]];
LL b=inv[cnt[i]-j];
tmp=(tmp+a*b%MOD*jie[n-j+1]%MOD)%MOD;
}
F1[i]=tmp;
if(cnt[i]>0)
F2[i]=cnt[i]*power(2,cnt[i]-1);
else F2[i]=0;
}
LL tmp1,tmp2;
ans1=ans2=0;
for(int i=1;i<=ma;i++){
tmp1=tmp2=0;
for(int j=i;j<=ma;j+=i){
tmp1=(tmp1+mu[j/i]*F1[j]%MOD)%MOD;
tmp2=(tmp2+mu[j/i]*F2[j]%MOD)%MOD;
}
ans1=(ans1+i*tmp1%MOD)%MOD;
ans2=(ans2+i*tmp2%MOD)%MOD;
}
}
int main(){
init();
while(~scanf("%d",&n)){
for(int i=1;i<=maxn;i++)cnt[i]=0;
ma=0;
for(int i=0;i<n;i++){
int x;scanf("%d",&x);
if(ma<x)ma=x;
for(int j=1;j*j<=x;j++){
if(j*j==x)cnt[j]++;
else if(x%j==0){
cnt[j]++;
cnt[x/j]++;
}
}
}
calc();
if(ans1==ans2)printf("Equal %lld\n",ans1);
else if(ans1>ans2)printf("Mr. Zstu %lld\n",ans1);
else printf("Mr. Hdu %lld\n",ans2);
}
return 0;
}

103
HDOJ/5322_autoAC.cpp Normal file
View File

@ -0,0 +1,103 @@
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll Mod=998244353;
const ll g=3;
ll fp(ll a,ll k)
{
ll res=1LL;
while(k>0)
{
if(k&1)res=res*a%Mod;
a=a*a%Mod;
k>>=1;
}
return res;
}
ll f[100005],inv[100005];
void change(ll y[],int len)
{
for(int i=1,j=len/2;i<len-1;i++)
{
if(i<j)swap(y[i],y[j]);
int k=len/2;
while(j>=k)
{
j-=k;
k/=2;
}
if(j<k)j+=k;
}
}
void ntt(ll y[],int len,int on)
{
change(y,len);
for(int h=2;h<=len;h<<=1)
{
ll wn=fp(g,(Mod-1)/h);
if(on==-1)wn=fp(wn,Mod-2);
for(int j=0;j<len;j+=h)
{
ll w=1LL;
for(int k=j;k<j+h/2;k++)
{
ll u=y[k];
ll t=w*y[k+h/2]%Mod;
y[k]=(u+t)%Mod;
y[k+h/2]=(u-t+Mod)%Mod;
w=w*wn%Mod;
}
}
}
if(on==-1)
{
ll t=fp(len,Mod-2);
for(int i=0;i<len;i++)
y[i]=y[i]*t%Mod;
}
}
ll dp[100005];
ll x1[200005],x2[200005];
void cdq(int l,int r)
{
if(l==r)return;
int m=(l+r)>>1;
cdq(l,m);
int len=1;
while(len<=r-l+1)len<<=1;
for(int i=0;i<len;i++)
{
x1[i]=(l+i<=m ? dp[l+i]*inv[l+i]%Mod : 0);
x2[i]=(l+i<r ? 1LL*(i+1)*(i+1)%Mod : 0);
}
ntt(x1,len,1);
ntt(x2,len,1);
for(int i=0;i<len;i++)
x1[i]=x1[i]*x2[i]%Mod;
ntt(x1,len,-1);
for(int i=m+1;i<=r;i++)
dp[i]=(dp[i]+f[i-1]*x1[i-l-1]%Mod)%Mod;
cdq(m+1,r);
}
void prepare()
{
f[0]=1LL;
for(int i=1;i<=100000;i++)
f[i]=i*f[i-1]%Mod;
for(int i=0;i<=100000;i++)
inv[i]=fp(f[i],Mod-2)%Mod;
dp[0]=1LL;
cdq(0,100000);
}
int main()
{
prepare();
int n;
while(scanf("%d",&n)!=EOF)printf("%I64d\n",dp[n]);
return 0;
}

40
HDOJ/5323_autoAC.cpp Normal file
View File

@ -0,0 +1,40 @@
#include <iostream>
#include <stdio.h>
using namespace std;
long long ans=0x3f3f3f3f3f3f3f;
void dfs(long long L,long long R,long long RR)
{
if(L<0||R>2*RR)
return;
if(ans&&ans<=R)
return;
if(L==0)
{
ans=min(ans,R);
return;
}
if (2*L<R+1)
return;
dfs(2*L-R-1,R,RR);
dfs(2*L-R-2,R,RR);
dfs(L,2*R-L+1,RR);
dfs(L,2*R-L,RR);
}
int main()
{
long long L,R;
while(~scanf("%I64d %I64d",&L,&R))
{
dfs(L,R,R);
if(ans==0x3f3f3f3f3f3f3f)
{
printf("-1\n");
}
else
{
printf("%I64d\n",ans);
}
ans=0x3f3f3f3f3f3f3f;
}
return 0;
}

191
HDOJ/5324_autoAC.cpp Normal file
View File

@ -0,0 +1,191 @@
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 50010;
struct Node{
Node *ch[2];
int r;
int v;
int len;
int idx;
int mlen;
int midx;
Node() {}
Node(int v, int len, int idx):v(v),len(len),idx(idx),mlen(len),midx(idx) {ch[0] = ch[1] = NULL; r = rand();}
int cmp(int x) const {
if (x == v) return -1;
return x < v ? 0 : 1;
}
void maintain(){
mlen = len;
midx = idx;
if (ch[0] != NULL && (ch[0]->mlen > mlen || (ch[0]->mlen == mlen && ch[0]->midx < midx))){
mlen = ch[0]->mlen;
midx = ch[0]->midx;
}
if (ch[1] != NULL && (ch[1]->mlen > mlen || (ch[1]->mlen == mlen && ch[1]->midx < midx))){
mlen = ch[1]->mlen;
midx = ch[1]->midx;
}
}
};
bool findMax(Node* a, Node* b){
if (a->mlen < b->mlen || (a->mlen == b->mlen && a->midx > b->midx)){
*a = *b;
return true;
}
return false;
}
namespace Treap{
int cntnode;
Node node[maxn*10];
void init(){
cntnode = 0;
}
Node* newNode(int v, int len, int idx){
node[++cntnode] = Node(v, len, idx);
return &node[cntnode];
}
void rotate(Node* &o, int d){
Node* k = o->ch[d^1]; o->ch[d^1] = k->ch[d]; k->ch[d] = o;
o->maintain(); k->maintain(); o = k;
}
void insert(Node* &o, int v, int len, int idx){
if (o == NULL) o = newNode(v, len, idx);
else {
int d = o->cmp(v);
if (d != -1){
insert(o->ch[d], v, len, idx);
if (o->r < o->ch[d]->r) rotate(o, d^1);
}
else
{
if (len >= o->len){
o->len = len;
o->idx = idx;
}
}
}
o->maintain();
}
Node search(Node *o, int v){
if (o == NULL){
return Node(-1, 0, -1);
}
else{
Node re, tmp;
if (o->v == v) {
re = Node(o->v, o->len, o->idx);
if (o->ch[1]){
findMax(&re, o->ch[1]);
}
}
else if (o->v > v){
re = Node(o->v, o->len, o->idx);
if (o->ch[1]){
findMax(&re, o->ch[1]);
}
if (o->ch[0]){
tmp = search(o->ch[0], v);
findMax(&re, &tmp);
}
}
else{
re = search(o->ch[1], v);
}
return re;
}
}
}
namespace BIT{
Node* fwt[maxn];
int N;
void init(int n){
N = n;
memset(fwt, 0, sizeof fwt);
}
void add(int v1, int v2, int len, int idx){
while(v1 < N){
Treap::insert(fwt[v1], v2, len, idx);
v1 += (-v1)&v1;
}
}
Node query(int v1, int v2){
Node re, tmp;
re = Node(-1, 0, -1);
while(v1 > 0){
tmp = Treap::search(fwt[v1], v2);
findMax(&re, &tmp);
v1 -= (-v1)&v1;
}
return re;
}
}
struct Pe{
int L,R;
int i;
bool operator < (const Pe& rhs)const{
return L < rhs.L;
}
};
bool cmp(Pe a, Pe b){
return a.i < b.i;
}
int solo[maxn];
Pe pe[maxn];int pre[maxn];
void print(Node& a){
int id = a.midx;
printf("%d\n", a.mlen);
while(1){
printf("%d", id+1);
if (pre[id] == -1){
break;
}
printf(" ");
id = pre[id];
}
printf("\n");
}
int main(){
int n;
while(scanf("%d", &n) != EOF){
for(int i=0;i<n;i++){
pe[i].i = i;
scanf("%d", &pe[i].L);
}
for(int i=0;i<n;i++){
scanf("%d", &pe[i].R);
}
sort(pe, pe+n);
int m = 0;
solo[0] = ++m;
for(int i=1;i<n;i++){
if (pe[i].L != pe[i-1].L){
solo[i] = ++m;
}
else{
solo[i] = solo[i-1];
}
}
for(int i=0;i<n;i++){
pe[i].L = solo[i];
}
sort(pe, pe+n, cmp);
BIT::init(m+1);
Treap::init();
Node ans = Node(-1, 0, -1), tmp;
for(int i=n-1;i>=0;i--){
tmp = BIT::query(pe[i].L, pe[i].R);
pre[i] = tmp.midx;
tmp.mlen = tmp.mlen + 1;
tmp.midx = i;
BIT::add(pe[i].L, pe[i].R, tmp.mlen, tmp.midx);
findMax(&ans, &tmp);
}
print(ans);
}
return 0;
}

44
HDOJ/5325_autoAC.cpp Normal file
View File

@ -0,0 +1,44 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int MAXN = 5e5 + 10;
const int INF = 0x3f3f3f3f;
int w[MAXN];
int cnt[MAXN];
int deg[MAXN];
vector<int> G[MAXN];
int n;
int BFS(void) {
queue<int> Q; int ret = 0;
for (int i=1; i<=n; ++i) cnt[i] = 1;
for (int i=1; i<=n; ++i) {
if (!deg[i]) Q.push (i);
}
while (!Q.empty ()) {
int u = Q.front (); Q.pop ();
ret = max (ret, cnt[u]);
for (int i=0; i<G[u].size (); ++i) {
int v = G[u][i];
cnt[v] += cnt[u];
if (!(--deg[v])) Q.push (v);
}
}
return ret;
}
int main(void) {
while (scanf ("%d", &n) == 1) {
for (int i=1; i<=n; ++i) scanf ("%d", &w[i]);
for (int i=1; i<=n; ++i) G[i].clear ();
memset (deg, 0, sizeof (deg));
for (int i=1; i<=n-1; ++i) {
int u, v; scanf ("%d%d", &u, &v);
if (w[u] < w[v]) swap (u, v);
G[u].push_back (v); deg[v]++;
}
printf ("%d\n", BFS ());
}
return 0;
}

54
HDOJ/5326_autoAC.cpp Normal file
View File

@ -0,0 +1,54 @@
#include <iostream>
#include <stdio.h>
#include <memory.h>
using namespace std;
const int MAX=100+5;
int a[MAX][MAX];
int b[MAX];
void dfs(int j,int jj,int num,int n)
{
for(int k=jj;k<=n;k++)
{
if(a[j][ k ])
{
b[num]++;
dfs(k,1,num,n);
}
}
}
int main()
{
int n,k;
while(~scanf("%d %d",&n,&k))
{
int ans=0;
int A,B;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(int i=1;i<n;i++)
{
scanf("%d %d",&A,&B);
a[A][B]=1;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(a[i][j])
{
b[i]++;
dfs(j,1,i,n);
}
}
}
for(int i=1;i<=n;i++)
{
if(b[i]==k)
{
ans++;
}
}
printf("%d\n",ans);
}
return 0;
}

31
HDOJ/5327_autoAC.cpp Normal file
View File

@ -0,0 +1,31 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int num[111111];
bool vis[10];
int main()
{
int i,j = 10,t,l,r;
num[0] = 0;
for(i = 1; i <= 100000; ++i)
{
memset(vis,0,sizeof(vis));
j = i;
while(j)
{
if(!vis[j%10]) vis[j%10] = 1;
else break;
j /= 10;
}
num[i] = num[i-1];
if(!j) num[i]++;
}
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&l,&r);
printf("%d\n",num[r]-num[l-1]);
}
return 0;
}

48
HDOJ/5328_autoAC.cpp Normal file
View File

@ -0,0 +1,48 @@
#include <iostream>
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
int main(){
int T,n;
double a,b;
double ap,gp;
int sumap,sumgp,ans;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
sumap = 2,sumgp = 2,ans = 2;
scanf("%lf",&a);
if(n==1) {
printf("1\n");
continue;
}
scanf("%lf",&b);
ap = b-a;
gp = b/a;
a = b;
double tmap,tmgp;
for(int i=2;i<n;i++){
scanf("%lf",&b);
tmap = b-a;
tmgp = b/a;
if(ap == tmap) sumap++;
else{
ans = max(ans,sumap);
sumap = 2;
ap = tmap;
}
if(gp == tmgp) sumgp++;
else{
ans = max(ans,sumgp);
sumgp = 2;
gp = tmgp;
}
a = b;
}
ans = max(ans,sumap);
ans = max(ans,sumgp);
printf("%d\n",ans);
}
return 0;
}

126
HDOJ/5329_autoAC.cpp Normal file
View File

@ -0,0 +1,126 @@
#pragma comment(linker, "/STACK:16777216")
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<functional>
#include<string>
#include<algorithm>
#include<time.h>
#include<bitset>
using namespace std;
const int maxn = 200005;
const int maxm = 400005;
struct Edge
{
int v;
Edge *next;
}E[maxm], *edges, *H[maxn];
int tsum[maxn];
int size[maxn];
int sum[maxn];
int dfn[maxn];
int low[maxn];
int bcc[maxn];
int cnt[maxn];
int fa[maxn];
int a[maxn];
int c[maxn];
int n, m, tcnt, K, dfs_clock, ok;
void addedges(int u, int v)
{
edges->v = v;
edges->next = H[u];
H[u] = edges++;
}
void init()
{
dfs_clock = 0;
tcnt = 0;
edges = E;
for(int i = 0; i <= n; i++) {
H[i] = 0;
dfn[i] = low[i] = bcc[i] = cnt[i] = 0;
}
}
void dfs(int u)
{
dfn[u] = low[u] = ++dfs_clock;
for(Edge *e = H[u]; e; e = e->next) if(e->v != fa[u]) {
int v = e->v;
if(!dfn[v]) {
fa[v] = u;
dfs(v);
low[u] = min(low[u], low[v]);
}
else low[u] = min(low[u], dfn[v]);
if(fa[v] != u && dfn[u] < dfn[v]) {
int vn = v;
bcc[u] = true;
a[++tcnt] = u;
do {
a[++tcnt] = vn;
bcc[vn] = true;
vn = fa[vn];
}while(vn != u);
}
}
}
void DFS(int u, int f)
{
size[u] = 1;
for(Edge *e = H[u]; e; e = e->next) if(!bcc[e->v] && e->v != f) {
int v = e->v;
DFS(v, u);
size[u] += size[v];
}
}
void work()
{
int u, v;
for(int i = 1; i <= n; i++) {
scanf("%d", &v);
addedges(i, v);
addedges(v, i);
}
dfs(1);
int ans = 0;
for(int i = tcnt+1; i <= 2 * tcnt; i++) a[i] = a[i - tcnt];
for(int i = 1; i <= n; i++) if(bcc[i]) DFS(i, i);
for(int i = 1; i <= n; i++) if(!bcc[i]) cnt[size[i]]++;
for(int i = 1; i <= n; i++)
for(int j = i + i; j <= n; j += i)
cnt[i] += cnt[j];
for(int i = 1; i <= n; i++) if(n % i == 0) {
K = i;
int tt = n / K - cnt[K];
for(int i = 1; i <= tcnt; i++) sum[i] = size[a[i]] % K;
for(int i = tcnt + 1; i <= 2 * tcnt; i++) sum[i] = sum[i - tcnt];
for(int i = 1; i <= 2 * tcnt; i++) sum[i] = (sum[i] + sum[i-1]) % K;
for(int i = 0; i <= K; i++) c[i] = -1;
for(int i = 0; i <= 2 * tcnt; i++) {
if(c[sum[i]] != -1) tsum[i] = tsum[c[sum[i]]] + 1;
c[sum[i]] = i;
}
for(int i = tcnt+1; i <= 2 * tcnt; i++) {
if(tsum[i] - tsum[i-tcnt] >= tt) {
ans++;
break;
}
}
}
printf("%d\n", ans);
}
int main()
{
while(scanf("%d", &n) != EOF) {
init();
work();
}
return 0;
}

68
HDOJ/5330_autoAC.cpp Normal file
View File

@ -0,0 +1,68 @@
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<vector>
using namespace std;
int pow3[20];
int dp[2][200000][24];
char w[12];
long long ans[100];
int num[200000];
int main(){
pow3[0] = 1;
for(int i = 1;i <= 11; i++)
pow3[i] = pow3[i-1]*3;
int t,m,n;
scanf("%d",&t);
for(int tt = 1; tt <= t; tt++){
memset(dp,0,sizeof(dp));
memset(num,0,sizeof(num));
scanf("%d%d",&n,&m);
for(int i = 0;i < n; i++){
scanf("%s",w);
int u = 0;
for(int j = 0;j < m; j++){
u = u*3+w[j]-'0';
}
dp[0][u][0]++;
num[u]++;
}
int p = 0, q = 1,s0,s1,s2;
for(int mm=1;mm<=m;mm++){
memset(dp[q],0,sizeof(dp[q]));
for(int i = 0;i < pow3[mm-1];i++){
for(int j = 0;j < pow3[m-mm];j++){
for(int d = 0; d <= 2*m;d++){
s0 = i+j*pow3[mm];
s1 = s0 + pow3[mm-1];
s2 = s1 + pow3[mm-1];
dp[q][s0][d] += dp[p][s0][d];
dp[q][s1][d] += dp[p][s1][d];
dp[q][s2][d] += dp[p][s2][d];
if(d > 0){
dp[q][s0][d] += dp[p][s1][d-1];
dp[q][s1][d] += dp[p][s0][d-1];
dp[q][s1][d] += dp[p][s2][d-1];
dp[q][s2][d] += dp[p][s1][d-1];
}
if(d > 1){
dp[q][s0][d] += dp[p][s2][d-2];
dp[q][s2][d] += dp[p][s0][d-2];
}
}
}
}
swap(p,q);
}
memset(ans,0,sizeof(ans));
for(int i = 0;i < pow3[m]; i++){
ans[0] += 1ll*num[i]*(num[i]-1);
for(int j = 1 ;j <= 2*m; j++)
ans[j] += 1ll*num[i]*dp[p][i][j];
}
for(int i = 0;i <= 2*m; i++)
printf("%I64d\n",ans[i]/2);
}
return 0;
}

43
HDOJ/5332_autoAC.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <iostream>
using namespace std;
long long a[35][35];
long long b[35][35];
long long c[35][35];
int main() {
int cas;
std::cin >> cas;
while (cas --) {
long long k;
std::cin >> k;
std::cout << 30 << ' ' << 20 << "\n";
for (int i = 0; i <= 30; i ++) {
for (int j = 0; j <= 30; j ++) {
a[i][j] = 0;
}
}
for (int i = 1; i <= 10; i ++) {
a[0][i] = 1;
for (int j = 1; j <= 10; j ++) {
a[i][j] = 1;
}
}
for (int i = 11; i <= 28; i++) {
a[i][i + 1] = 1;
}
int cur = 11;
while (k) {
int m = k % 10;
k /= 10;
for (int i = 1; i <= m; i++) {
a[i][cur] = 1;
}
cur ++;
}
for (int i = 0; i <= 29; i ++) {
for (int j = 0; j <= 29; j ++)
std::cout << a[i][j];
std::cout << "\n";
}
}
return 0;
}

229
HDOJ/5333_autoAC.cpp Normal file
View File

@ -0,0 +1,229 @@
#pragma comment(linker, "/STACK:1024000000")
#include <stdio.h>
#include <string.h>
#include <map>
#include <algorithm>
using namespace std ;
typedef long long LL ;
#define clr( a , x ) memset ( a , x , sizeof a )
#define ls ( o << 1 )
#define rs ( o << 1 | 1 )
#define lson ls , l , m
#define rson rs , m + 1 , r
#define root 1 , 1 , n
#define mid ( ( l + r ) >> 1 )
const int MAXN = 100005 ;
const int MAXE = 200005 ;
const int INF = 0x3f3f3f3f ;
struct Edge {
int v , idx , n ;
Edge () {}
Edge ( int v , int idx , int n ) : v ( v ) , idx ( idx ) , n ( n ) {}
} ;
struct Node* null ;
struct Node {
Node* c[2] ;
Node* f ;
bool flip ;
int minv , val ;
int eidx , idx ;
void newnode ( int v , int i ) {
c[0] = c[1] = f = null ;
minv = val = v ;
eidx = idx = i ;
flip = 0 ;
}
void rev () {
if ( this == null ) return ;
swap ( c[0] , c[1] ) ;
flip ^= 1 ;
}
void up () {
if ( this == null ) return ;
if ( val <= c[0]->minv && val <= c[1]->minv ) {
minv = val ;
eidx = idx ;
} else if ( c[0]->minv <= c[1]->minv && c[0]->minv <= val ) {
minv = c[0]->minv ;
eidx = c[0]->eidx ;
} else {
minv = c[1]->minv ;
eidx = c[1]->eidx ;
}
}
void down () {
if ( this == null ) return ;
if ( flip ) {
c[0]->rev () ;
c[1]->rev () ;
flip = 0 ;
}
}
bool is_root () {
return f == null || f->c[0] != this && f->c[1] != this ;
}
void sign_down () {
if ( !is_root () ) f->sign_down () ;
down () ;
}
void setc ( Node* o , int d ) {
c[d] = o ;
o->f = this ;
}
void rot ( int d ) {
Node* p = f ;
Node* g = f->f ;
p->setc ( c[d] , !d ) ;
if ( !p->is_root () ) g->setc ( this , f == g->c[1] ) ;
else f = g ;
setc ( p , d ) ;
p->up () ;
}
void splay () {
sign_down () ;
while ( !is_root () ) {
if ( f->is_root () ) rot ( this == f->c[0] ) ;
else {
if ( f == f->f->c[0] ) {
if ( this == f->c[0] ) f->rot ( 1 ) , rot ( 1 ) ;
else rot ( 0 ) , rot ( 1 ) ;
} else {
if ( this == f->c[1] ) f->rot ( 0 ) , rot ( 0 ) ;
else rot ( 1 ) , rot ( 0 ) ;
}
}
}
up () ;
}
void access () {
Node* o = this ;
for ( Node* x = null ; o != null ; x = o , o = o->f ) {
o->splay () ;
o->setc ( x , 1 ) ;
o->up () ;
}
splay () ;
}
void make_root () {
access () ;
rev () ;
}
void link ( Node* o ) {
make_root () ;
f = o ;
}
void cut () {
access () ;
c[0] = c[0]->f = null ;
up () ;
}
void cut ( Node* o ) {
make_root () ;
o->cut () ;
}
int get_min ( Node* o ) {
make_root () ;
o->access () ;
return o->eidx ;
}
} ;
Node pool[MAXN + MAXE] ;
Node* cur ;
Node* node[MAXN] ;
Node* edge[MAXE] ;
Edge E[MAXE + MAXN] ;
int H[MAXN] , Q[MAXN] , cntE ;
int U[MAXE] , V[MAXE] ;
int ans[MAXN] ;
int p[MAXN] ;
int c[MAXN] ;
int n , m , q ;
int find ( int x ) {
return p[x] == x ? x : ( p[x] = find ( p[x] ) ) ;
}
void init ( int n ) {
cntE = 0 ;
cur = pool ;
cur->newnode ( INF , -1 ) ;
null = cur ++ ;
for ( int i = 1 ; i <= n ; ++ i ) {
p[i] = i ;
H[i] = -1 ;
Q[i] = -1 ;
c[i] = 0 ;
cur->newnode ( INF , -1 ) ;
node[i] = cur ++ ;
}
}
void addedge ( int u , int v , int idx , int H[] ) {
E[cntE] = Edge ( v , idx , H[u] ) ;
H[u] = cntE ++ ;
}
void add ( int x , int v ) {
for ( ; x <= n ; x += x & -x ) c[x] += v ;
}
int sum ( int x , int ans = 0 ) {
for ( ; x > 0 ; x -= x & -x ) ans += c[x] ;
return ans ;
}
void scanf ( int& x , char c = 0 ) {
while ( ( c = getchar () ) < '0' ) ;
x = c - '0' ;
while ( ( c = getchar () ) >= '0' ) x = x * 10 + c - '0' ;
}
void solve () {
int cnt = 0 ;
int u , v ;
init ( n ) ;
for ( int i = 1 ; i <= n ; ++ i ) add ( i , -1 ) ;
for ( int i = 0 ; i < m ; ++ i ) {
scanf ( "%d%d" , &u , &v ) ;
if ( u == v ) continue ;
if ( u < v ) swap ( u , v ) ;
addedge ( u , v , i , H ) ;
U[i] = u ;
V[i] = v ;
cur->newnode ( v , i ) ;
edge[i] = cur ++ ;
}
for ( int i = 0 ; i < q ; ++ i ) {
scanf ( "%d%d" , &u , &v ) ;
addedge ( v , u , i , Q ) ;
}
for ( int i = 1 ; i <= n ; ++ i ) {
++ cnt ;
for ( int j = H[i] ; ~j ; j = E[j].n ) {
int v = E[j].v , idx = E[j].idx ;
if ( v == i ) continue ;
int x = find ( i ) ;
int y = find ( v ) ;
if ( x != y ) {
-- cnt ;
p[x] = y ;
edge[idx]->link ( node[i] ) ;
edge[idx]->link ( node[v] ) ;
add ( v , 1 ) ;
} else {
int eidx = node[i]->get_min ( node[v] ) ;
if ( V[eidx] >= v ) continue ;
edge[eidx]->cut ( node[U[eidx]] ) ;
edge[eidx]->cut ( node[V[eidx]] ) ;
add ( V[eidx] , -1 ) ;
edge[idx]->link ( node[i] ) ;
edge[idx]->link ( node[v] ) ;
add ( v , 1 ) ;
}
}
for ( int j = Q[i] ; ~j ; j = E[j].n ) {
int v = E[j].v , idx = E[j].idx ;
ans[idx] = cnt + sum ( v - 1 ) + n - i + v - 1 ;
}
}
for ( int i = 0 ; i < q ; ++ i ) {
printf ( "%d\n" , ans[i] ) ;
}
}
int main () {
while ( ~scanf ( "%d%d%d" , &n , &m , &q ) ) solve () ;
return 0 ;
}

79
HDOJ/5334_autoAC.cpp Normal file
View File

@ -0,0 +1,79 @@
#include<stdio.h>
#include<string.h>
int a[1000000],ans[1000000];
int cnt;
int f(int x){
int l=1,r=cnt;
int mid;
while(l<r){
mid=(l+r)/2;
if(x>a[mid]) l=mid+1;
else r=mid;
}
return r;
}
int main(){
int k,l,r;
cnt=1;
a[0]=0;
for(cnt=1;;cnt++){
if(cnt+a[cnt-1]>1000000000) {a[cnt]=cnt+a[cnt-1]; break;}
else a[cnt]=cnt+a[cnt-1];
}
while(scanf("%d",&k)!=EOF){
if(k<=100000){
printf("%d\n",k);
for(int i=1;i<k;i++)
printf("1 ");
printf("1\n");
}
else {
int tmp=f(k);
while(k>a[tmp]) tmp++;
if(k==a[tmp]){
printf("%d\n",tmp);
for(int i=1;i<tmp;i++){
printf("%d ",i);
}
printf("%d\n",tmp);
}
else {
l=tmp-1;
r=tmp;
int x=a[tmp]-k,c=1,d;
while(x){
d=f(x);
if(x==a[d]){
ans[c++]=d+1;
break;
}
else {
ans[c++]=d;
x-=a[d-1];
}
}
int t=1,sum=0;
printf("%d\n",tmp);
for(int i=1;i<c;i++){
sum+=ans[i];
for(int j=1;j<=ans[i];j++)
{
printf("%d",t);
if(sum!=tmp) printf(" ");
else {
if(j==ans[i]) printf("\n");
else printf(" ");
}
}
t++;
}
if(sum<tmp)
{for(int i=sum+1;i<tmp;i++){
printf("%d ",t);t++;
}
printf("%d\n",t);}
}
}
}
return 0;
}

141
HDOJ/5335_autoAC.cpp Normal file
View File

@ -0,0 +1,141 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn=1008;
const int INF=0x7f7f7f7f;
char mp[maxn][maxn];
int dir[2][2]={1,0,0,1};
int di[4][2]={1,0,0,1,-1,0,0,-1};
int n,m;
struct poi{
int x,y,t;
}f[maxn*maxn];
struct fuck{
int x,y;
};
fuck pa[maxn][maxn],papa[maxn<<4];
bool flag[maxn<<4],done[maxn][maxn];
int idx=0;
int cmp(poi a,poi b)
{
return a.t>b.t;
}
void rigou()
{
int i,nx,ny,x,y,t;
sort(f+1,f+1+idx,cmp);
queue<poi> q;
q.push((poi){f[1].x,f[1].y,1});
done[f[1].x][f[1].y]=true;
for(i=2;i<=idx;i++)
{
if(f[i].t==f[i-1].t)
{
q.push((poi){f[i].x,f[i].y,1});
done[f[i].x][f[i].y]=true;
}
else
break;
}
memset(done,false,sizeof(done));
memset(flag,false,sizeof(flag));
memset(pa,-1,sizeof(pa));
poi pp;
while(!q.empty())
{
pp=q.front();q.pop();
x=pp.x;y=pp.y;t=pp.t;
if(flag[t]==true&&mp[x][y]=='1') continue;
for(i=0;i<2;i++)
{
nx=x+dir[i][0];
ny=y+dir[i][1];
if(mp[nx][ny]!='\0'&&!done[nx][ny])
{
q.push((poi){nx,ny,t+1});
if(mp[nx][ny]=='0')
flag[t+1]=true;
pa[nx][ny].x=x;pa[nx][ny].y=y;
done[nx][ny]=true;
}
}
}
}
bool bfs(int x,int y)
{
int nx,ny,i;
queue<fuck> q;
if(mp[x][y]=='0')
{
q.push((fuck){x,y});
fuck pp;
memset(done,false,sizeof(done));
while(!q.empty())
{
pp=q.front();q.pop();
x=pp.x;y=pp.y;
for(i=0;i<4;i++)
{
nx=x+di[i][0];
ny=y+di[i][1];
if(mp[nx][ny]=='1'){
f[++idx].x=nx;
f[idx].y=ny;
f[idx].t=nx+ny;
}
if(mp[nx][ny]=='0'&&!done[nx][ny])
{
done[nx][ny]=true;
if(nx==n&&ny==m)
return false;
q.push((fuck){nx,ny});
}
}
}
}
else
{
f[++idx].x=1;f[idx].y=1;f[idx].t=2;
}
return true;
}
int main()
{
int i,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
idx=0;
memset(mp,'\0',sizeof(mp));
for(i=1;i<=n;i++)
scanf("%s",mp[i]+1);
if(bfs(1,1))
{
rigou();
int x=n,y=m,id=0,nx,ny;
papa[id].x=n;papa[id].y=m;
while(pa[x][y].x!=-1)
{
papa[++id].x=pa[x][y].x;
papa[id].y=pa[x][y].y;
nx=pa[x][y].x;
ny=pa[x][y].y;
x=nx;y=ny;
}
for(i=id;i>=0;i--)
{
x=papa[i].x;
y=papa[i].y;
printf("%c",mp[x][y]);
}
}
else
printf("0");
printf("\n");
}
return 0;
}

126
HDOJ/5336_autoAC.cpp Normal file
View File

@ -0,0 +1,126 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
const int dir_x[4]={0,0,-1,1};
const int dir_y[4]={1,-1,0,0};
int r,c,n,T;
int mid[111][111];
int SZ[111],Time[111];
int X[111],Y[111];
void init()
{
memset(SZ,0,sizeof(SZ));
memset(mid,0,sizeof(mid));
memset(Time,0,sizeof(Time));
}
struct Node
{
int x,y,d,t;
void toString()
{
printf("(%d,%d) d: %d t: %d\n",x,y,d,t);
}
};
bool isInside(int x,int y)
{
if((x>=1&&x<=r)&&(y>=1&&y<=c)) return true;
return false;
}
void solve(int _x,int _y)
{
queue<Node> q;
for(int i=0;i<4;i++)
{
Node nd;
nd.x=_x; nd.y=_y; nd.d=i; nd.t=0;
q.push(nd);
}
while(!q.empty())
{
Node u=q.front(); q.pop();
int bbb=-1;
int d=u.d;
Node v;
v.x=u.x+dir_x[d];
v.y=u.y+dir_y[d];
v.d=u.d;
v.t=u.t+1;
if(isInside(v.x,v.y)==false) continue;
int id=mid[v.x][v.y];
if(id==0)
{
if(v.t<T)
{
q.push(v);
}
}
else
{
if(Time[id]!=0)
{
if(Time[id]>=v.t) continue;
else
{
if(v.t<T)
{
q.push(v);
}
}
}
else if(Time[id]==0)
{
SZ[id]++;
if(SZ[id]>4)
{
Time[id]=v.t;
bbb=id;
}
}
}
if(bbb!=-1)
{
for(int j=0;j<4;j++)
{
Node n1;
n1.x=X[bbb]; n1.y=Y[bbb];
n1.d=j; n1.t=v.t;
if(n1.t<T)
{
q.push(n1);
}
}
}
}
}
int main()
{
while(scanf("%d%d%d%d",&r,&c,&n,&T)!=EOF)
{
init();
for(int i=1,s;i<=n;i++)
{
scanf("%d%d%d",X+i,Y+i,&s);
int x=X[i],y=Y[i];
mid[x][y]=i;
SZ[i]=s;
}
int x,y;
scanf("%d%d",&x,&y);
solve(x,y);
for(int i=1;i<=n;i++)
{
if(Time[i]!=0) printf("%d %d\n",0,Time[i]);
else printf("%d %d\n",1,SZ[i]);
}
}
return 0;
}

155
HDOJ/5338_autoAC.cpp Normal file
View File

@ -0,0 +1,155 @@
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int maxn = 100010;
typedef long long LL;
int a[maxn], vidx[maxn];
int vis[maxn], flg[maxn];
namespace SegmentTree{
int maxv[maxn<<2]; int setv[maxn<<2];
#define Lson o<<1
#define Rson o<<1|1
void pushup(int o){
maxv[o] = max(maxv[Lson], maxv[Rson]);
}
void build(int o, int l, int r){
int m = (l+r)>>1;
if(l == r){
maxv[o] = a[l];
setv[o] = -1;
}
else {
build(Lson, l, m);
build(Rson, m+1, r);
setv[o] = -1;
pushup(o);
}
}
void pushdown(int o){
if (setv[o] >= 0){
setv[Lson] = setv[Rson] = setv[o];
maxv[Lson] = maxv[Rson] = setv[o];
setv[o] = -1;
}
}
int v, ul, ur;
void update(int o, int l, int r){
if(l > r) return ;
if (ul <= l && r <= ur){
setv[o] = v;
maxv[o] = v;
}
else{
pushdown(o);
int m = (l+r)>>1;
if (ul <= m) update(Lson, l, m);
if (ur > m) update(Rson, m+1, r);
pushup(o);
}
}
int _max, ql, qr;
void query(int o, int l, int r){
if(l>r) return ;
if (setv[o] >= 0){
_max = max(_max, setv[o]);
}
else if (ql <= l && r <= qr){
_max = max(_max, maxv[o]);
}
else {
int m = (l+r)>>1;
pushdown(o);
if(ql <= m) query(Lson, l, m);
if(qr > m) query(Rson, m+1, r);
}
}
}
namespace BS{
typedef pair<int,int> seg;
#define l first
#define r second
#define MP make_pair
int n;
map<int,int> mp;
void init(){
mp.clear();
}
void add(int l, int r){
seg line = MP(l, r);
mp.insert(line);
}
int search(int v){
map<int,int>::iterator it = mp.upper_bound(v);
if (it == mp.begin()){
return 0;
}
else{
--it;
return it->r;
}
}
}
int ans[maxn];
void link(int l, int r){
for(int i=l; i<r; i++){
ans[a[i]] = a[i+1];
}
ans[a[r]] = a[l];
}
int main(){
int T, n;
scanf("%d",&T);
while(T--){
scanf("%d", &n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
vidx[a[i]] = i;
}
memset(vis, 0, sizeof vis);
memset(flg, 0, sizeof flg);
SegmentTree::build(1,1,n);
BS::init();
for(int _i=1;_i<=n;_i++){
int i = vidx[_i];
if (vis[i]) continue;
int id = i, mx=0;
if(!flg[_i]) mx = _i;
SegmentTree::ql = BS::search(i) + 1;
SegmentTree::qr = id-1;
SegmentTree::_max = 0;
SegmentTree::query(1,1,n);
mx = max(mx, SegmentTree::_max);
if (!vis[i+1] && i < n){
mx = max(mx, a[i+1]);
if (mx == a[i+1]){
ans[_i] = a[i+1];
flg[a[i+1]] = 1;
SegmentTree::ul = i+1;
SegmentTree::ur = i+1;
SegmentTree::v = 0;
SegmentTree::update(1,1,n);
continue;
}
}
int l = min(vidx[mx], i),r = max(vidx[mx], i);
link(l, r);
for(int j = l; j <= r; j++){
vis[j] = 1;
flg[a[j]] = 1;
}
BS::add(l, r);
SegmentTree::ul = l;
SegmentTree::ur = r;
SegmentTree::v = 0;
SegmentTree::update(1,1,n);
}
for(int i=1; i <= n; i++){
printf("%d%c", ans[i], i<n?' ':'\n');
}
}
return 0;
}

41
HDOJ/5339_autoAC.cpp Normal file
View File

@ -0,0 +1,41 @@
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
int b[50],temp[50];
int cmp(int a,int b){
return a>b;
}
int main(){
int n,a,t,i,j,k,cur,num,sum,minn,sign;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&a);
sign=0;
for(i=0;i<n;i++)
scanf("%d",&b[i]);
minn=99999999;
sum=1<<n;
sort(b,b+n,cmp);
for(i=1;i<sum;i++){
j=i;k=cur=0;num=a;
while(j){
if(j&1)temp[cur++]=b[k];
j>>=1;k++;
}
if(cur>=minn)
continue;
for(j=0;j<cur;j++)
num%=temp[j];
if(num==0){
minn=cur;
sign=1;
}
}
if(sign)
printf("%d\n",minn);
else
printf("-1\n");
}
return 0;
}

227
HDOJ/5340_autoAC.cpp Normal file
View File

@ -0,0 +1,227 @@
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <ctime>
#include <deque>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
using namespace std;
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
#ifndef ONLINE_JUDGE
void RI(vector<int>&a,int n){a.resize(n);for(int i=0;i<n;i++)scanf("%d",&a[i]);}
void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>
void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?1:-1;
while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
void print(T*p, T*q){int d=p<q?1:-1;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
#endif // ONLINE_JUDGE
template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}
template<typename T>
void V2A(T a[],const vector<T>&b){for(int i=0;i<b.size();i++)a[i]=b[i];}
template<typename T>
void A2V(vector<T>&a,const T b[]){for(int i=0;i<a.size();i++)a[i]=b[i];}
const double PI = acos(-1.0);
const int INF = 1e9 + 7;
unsigned long long Pre[64], Suf[64];
struct BitSet {
vector<unsigned long long> s;
static void init() {
Pre[0] = 1;
Suf[63] = (unsigned long long)1 << 63;
for (int i = 1; i < 64; i ++) {
Pre[i] = (unsigned long long)1 << i | Pre[i - 1];
}
for (int i = 62; i >= 0; i --) {
Suf[i] = (unsigned long long)1 << i | Suf[i + 1];
}
}
void resize(int n) {
int p = s.size(), t = (n - 1) / 64 + 1;
s.resize(t);
}
BitSet(int n) {
resize(n);
}
BitSet() {}
BitSet operator & (BitSet &that) {
int sz = that.s.size(), n = this->s.size(), len = max(sz, n);
if (sz < len) that.resize(len);
if (n < len) this->resize(len);
BitSet ans(len * 64);
for (int i = len - 1; i >= 0; i --) {
ans.s[i] = this->s[i] & that.s[i];
}
return ans;
}
BitSet operator | (BitSet &that) {
int sz = that.s.size(), n = this->s.size(), len = max(sz, n);
if (sz < len) that.resize(len);
if (n < len) this->resize(len);
BitSet ans(len * 64);
for (int i = len - 1; i >= 0; i --) {
ans.s[i] = this->s[i] | that.s[i];
}
return ans;
}
BitSet operator ^ (BitSet &that) {
int sz = that.s.size(), n = this->s.size(), len = max(sz, n);
if (sz < len) that.resize(len);
if (n < len) this->resize(len);
BitSet ans(len * 64);
for (int i = len - 1; i >= 0; i --) {
ans.s[i] = this->s[i] ^ that.s[i];
}
return ans;
}
BitSet operator << (int x) {
int sz = s.size(), c = x / 64, r = x % 64;
BitSet ans(64 * sz);
for (int i = sz - 1; i - c >= 0; i --) {
ans.s[i] = (s[i - c] & Pre[63 - r]) << r;
if (r && i - c - 1 >= 0) ans.s[i] |= (s[i - c - 1 ] & Suf[64 - r]) >> (64 - r);
}
return ans;
}
BitSet operator >> (int x) {
int sz = s.size(), c = x / 64, r = x % 64;
BitSet ans(64 * sz);
for (int i = 0; i + c < sz; i ++) {
ans.s[i] = (s[i + c] & Suf[r]) >> r;
if (r && i + c + 1 < sz) ans.s[i] |= (s[i + c + 1] & Pre[r - 1]) << (64 - r);
}
return ans;
}
bool get(int p) {
int c = p / 64, r = p % 64;
return s[c] & ((unsigned long long)1 << r);
}
bool zero() {
int n = s.size();
for (int i = 0; i < n; i ++) {
if (s[i]) return false;
}
return true;
}
void setval(int L, int R, bool val) {
int p = L / 64, tp = L % 64, q = R / 64, tq = R % 64;
for (int i = p + 1; i < q; i ++) {
s[i] = val? ((unsigned long long)-1) : 0;
}
if (p == q) {
unsigned long long buf = Suf[tp] & Pre[tq];
s[p] = val? s[p] | buf : s[p] & ~buf;
return ;
}
s[p] = val? s[p] | Suf[tp] : s[p] & ~Suf[tp];
s[q] = val? s[q] | Pre[tq] : s[q] & ~Pre[tq];
}
void print() {
int n = s.size();
for (int i = n - 1; i >= 0; i --) {
unsigned long long x = s[i];
for (int i = 63; i >= 0; i --) {
if (((unsigned long long)1 << i) & x)
putchar('1');
else
putchar('0');
}
}
putchar('\n');
}
};
struct StringHash {
const static unsigned int hack = 1301;
const static int maxn = 1e5 + 7;
unsigned long long H[maxn], C[maxn];
void init(char s[], int n) {
for (int i = 0; s[i]; i ++) {
H[i] = (i? H[i - 1] * hack : 0) + s[i];
}
C[0] = 1;
for (int i = 1; i <= n; i ++) C[i] = C[i - 1] * hack;
}
unsigned long long get(int L, int R) {
return H[R] - (L? H[L - 1] * C[R - L + 1] : 0);
}
} ;
StringHash hsh, hshrev;
const int maxn = 1e5 + 7;
bool pre[maxn], suf[maxn];
char s[maxn], revs[maxn];
int F[maxn];
int main() {
BitSet::init();
int T;
cin >> T;
while (T --) {
scanf("%s", s);
int n = strlen(s), total = n * 2 - 1;
hsh.init(s, n);
for (int i = 0; i < n; i ++) revs[i] = s[n - i - 1];
hshrev.init(revs, n);
for (int i = 0; i < total; i ++) {
int L = i / 2, R = (i + 1) / 2;
int minlen = 0, maxlen = min(L + 1, n - R);
while (minlen < maxlen) {
int midlen = (minlen + maxlen + 1) >> 1;
int lpos = L - midlen + 1, rpos = R + midlen - 1;
if (hsh.get(lpos, L) == hshrev.get(n - rpos - 1, n - R - 1)) minlen = midlen;
else maxlen = midlen - 1;
}
F[i] = minlen;
}
fillchar(pre, 0);
fillchar(suf, 0);
pre[0] = suf[n - 1] = true;
BitSet bs1(n), bs2(n);
bs1.setval(0, 0, 1);
bs2.setval(0, 0, 1);
for (int i = 1; i < n; i ++) {
pre[i] = F[i] == i / 2 + 1;
if (pre[i]) bs1.setval(i, i, 1);
}
for (int i = n - 2; i >= 0; i --) {
suf[i] = F[i + n - 1] == (n - i + 1) / 2;
if (suf[i]) bs2.setval(n - i - 1, n - i - 1, 1);
}
bool ok = false;
for (int i = 0; i < total; i ++) {
int L = i / 2, R = (i + 1) / 2;
int len = F[i], lpos = L - len + 1, rpos = R + len - 1;
if (len == 0) continue;
BitSet buf, result, newbuf(n);
if (n - R >= L + 1) {
buf = bs2 >> (n - R - L - 1);
result = bs1 & buf;
newbuf.setval(max(0, lpos - 1), L - 1, 1);
}
if (L + 1 > n - R) {
buf = bs1 >> (L + 1 - n + R);
result = buf & bs2;
newbuf.setval(max(0, n - rpos - 2), n - R - 2, 1);
}
newbuf = newbuf & result;
if (!newbuf.zero()) {
ok = true;
break;
}
}
puts(ok? "Yes" : "No");
}
return 0;
}

62
HDOJ/5341_autoAC.cpp Normal file
View File

@ -0,0 +1,62 @@
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
#include<string.h>
#define ll long long
using namespace std;
const ll maxn = 10000000+10;
const ll mod = (1ll<<32);
ll t,n,ans;
ll f0[maxn],g0[maxn];
ll sumhe[maxn],sumu[maxn],sumu0[maxn];
bool not_prime[maxn];
ll pri[maxn],cnt;
ll ans0,ans1;
void mobius(){
sumu[1]=1,sumu0[1]=1,sumu[0]=0,sumhe[0]=0,sumu0[0]=0,cnt=0;
for(ll i=2;i<=maxn-5;i++){
if(!not_prime[i]) pri[cnt++]=i,sumu[i]=((-1)%mod+mod)%mod,sumu0[i]=((1-i)%mod+mod)%mod;
for(ll j=0;j<cnt&&i*pri[j]<=maxn-5;j++){
not_prime[i*pri[j]]=1;
if(i%pri[j]==0) { sumu[i*pri[j]]=0,sumu0[i*pri[j]]=sumu0[i]%mod;break; }
else sumu[i*pri[j]]=((-sumu[i])%mod+mod)%mod,sumu0[i*pri[j]]=(sumu0[pri[j]]*sumu0[i])%mod;
}
}
for(ll i=1;i<=maxn-5;i++) sumu[i]=(sumu[i-1]+sumu[i])%mod,f0[i]=g0[i]=mod+1,sumhe[i]=(sumhe[i-1]+i)%mod,sumu0[i]=(sumu0[i-1]+i*sumu0[i]%mod)%mod;
}
ll calf(ll n){
if(f0[n]!=mod+1) return f0[n];
ll ans=0,tmp;
for(ll i=1;i<=n;i=tmp+1){
tmp=n/(n/i);
ans=(ans+((sumu[tmp]-sumu[i-1])%mod+mod)%mod*(n/i)%mod*(n/i)%mod)%mod;
}
return f0[n]=ans;
}
ll calg(ll n){
if(g0[n]!=mod+1) return g0[n];
ll ans=0,tmp;
for(ll i=1;i<=n;i=tmp+1){
tmp=n/(n/i);
ans=(ans+((sumhe[tmp]-sumhe[i-1])%mod+mod)%mod*calf(n/i)%mod)%mod;
}
return g0[n]=ans;
}
int main(){
mobius();
scanf("%lld",&t);
while(t--){
scanf("%lld",&n);
ans=0;
ll tmp=0;
for(ll i=1;i<=n;i=tmp+1){
tmp=n/(n/i);
ll x=calg(n/i);
ans=(ans+((sumu0[tmp]-sumu0[i-1])%mod+mod)%mod*x%mod*x%mod)%mod;
}
printf("%lld\n",ans);
}
return 0;
}

71
HDOJ/5343_autoAC.cpp Normal file
View File

@ -0,0 +1,71 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAX_N = 200007;
typedef unsigned long long ll;
struct SAM {
int val[MAX_N], fa[MAX_N], c[26][MAX_N];
int tot, last;
int which[MAX_N];
inline int newNode(int step) {
val[++tot] = step;
fa[tot] = 0;
for (int i = 0; i < 26; ++i) c[i][tot] = 0;
return tot;
}
inline void extend(int k) {
int p = last;
int np = newNode(val[last] + 1);
which[np] = k;
while (p && !c[k][p]) c[k][p] = np, p = fa[p];
if (!p) fa[np] = 1;
else {
int q = c[k][p];
if (val[q] == val[p] + 1) fa[np] = q;
else {
int nq = newNode(val[p] + 1);
which[nq] = k;
for (int i = 0; i < 26; ++i) c[i][nq] = c[i][q];
fa[nq] = fa[q];
fa[q] = fa[np] = nq;
while (p && c[k][p] == q) c[k][p] = nq, p = fa[p];
}
}
last = np;
}
inline int add(int k) {
extend(k);
}
inline void init() {
tot = 0;
last = newNode(0);
}
} suf;
char A[MAX_N>>1], B[MAX_N>>1];
ll dp[26];
int main() {
int T;
scanf("%d", &T);
while (T-- > 0) {
suf.init();
scanf("%s%s", A, B);
int n = strlen(B);
reverse(B, B + n);
for (int i = 0; B[i]; ++i) suf.add(B[i] - 'a');
memset(dp, 0, sizeof dp);
for (int i = 1; i <= suf.tot; ++i)
dp[suf.which[i]] += suf.val[i] - suf.val[suf.fa[i]];
suf.init();
for (int i = 0; A[i]; ++i) suf.add(A[i] - 'a');
ll ans = 0;
for (int i = 1; i <= suf.tot; ++i) {
for (int j = 0; j < 26; ++j) if (!suf.c[j][i]) {
ans += dp[j] * (suf.val[i] - suf.val[suf.fa[i]]);
}
if (suf.fa[i]) ans += suf.val[i] - suf.val[suf.fa[i]];
}
printf("%I64u\n", ans + 1);
}
return 0;
}

25
HDOJ/5344_autoAC.cpp Normal file
View File

@ -0,0 +1,25 @@
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std ;
typedef __int64 ll ;
int main()
{
ll n , m , l , z ;
int t ;
scanf("%d" , &t) ;
while(t--)
{
scanf("%I64d%I64d%I64d%I64d" , &n , &m , &z, &l) ;
ll ans = 0 ;
ll pre = 0 ;
ll now ;
for(int i = 2;i <= n;i++)
{
now = (pre*m + z)%l ;
ans = ans^(2*now) ;
pre = now ;
}
printf("%I64d\n" , ans) ;
}
}

48
HDOJ/5346_autoAC.cpp Normal file
View File

@ -0,0 +1,48 @@
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const long long PIVOT = 258280327;
long long P;
long long inv(long long x)
{
if(x <= 1)
return 1;
return (PIVOT - PIVOT/x)*inv(PIVOT%x)%PIVOT;
}
long long n, x, y;
long long alive_K[2010];
long long dp[2010][2010];
int main()
{
int T, i, j;
scanf("%d", &T);
while (T--) {
scanf("%lld%lld%lld", &n, &x, &y);
P = x * inv(y) % PIVOT;
alive_K[0] = 1;
for (i = 1; i <= n; ++i) {
alive_K[i] = alive_K[i - 1] * (1 - P + PIVOT) % PIVOT;
}
dp[1][0] = 1;
for (i = 2; i <= n; ++i) {
for (j = 1; j < i; ++j) {
dp[i][j] = dp[i - 1][j - 1] * alive_K[j - 1] % PIVOT + dp[i - 1][j] * (1 - alive_K[j] + PIVOT) % PIVOT;
dp[i][j] %= PIVOT;
}
}
long long res;
int k;
printf("%lld", inv(n));
for (k = 1; k <= n - 1; ++k) {
res = 0;
for (i = 1; i <= n; ++i) {
res += dp[i][k];
res %= PIVOT;
}
res = res * alive_K[k] % PIVOT * inv(n) % PIVOT;
printf(" %lld", res);
}
printf("\n");
}
}

102
HDOJ/5348_autoAC.cpp Normal file
View File

@ -0,0 +1,102 @@
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <stack>
using namespace std;
const int MAXM=300010;
const int MAXN=200010;
struct edge{
int u,v;
}edges[MAXM*2];
int head[MAXN];
int pre[MAXM*2];
int Next[MAXM*2];
int deg[MAXN];
int ans[MAXM];
int ecnt;
int n,m;
int init(){
memset(head,-1,sizeof(head));
memset(Next,-1,sizeof(Next));
memset(deg,0,sizeof(deg));
ecnt=0;
}
void addedge(int u,int v){
edges[ecnt].u=u; edges[ecnt].v=v;
pre[ecnt]=head[u];
head[u]=ecnt;
ecnt++;
edges[ecnt].u=v; edges[ecnt].v=u;
pre[ecnt]=head[v];
head[v]=ecnt;
ecnt++;
}
void dfs(int u){
while(head[u]!=-1){
deg[u]--;
int i=head[u];
int v=edges[i].v;
if(i&1){
ans[(i>>1)+1]=0;
}else{
ans[(i>>1)+1]=1;
}
int pp,nn;
if(head[u]==i){
head[u]=pre[i];
}
pp=pre[i];
nn=Next[i];
if(pp!=-1)Next[pp]=nn;
if(nn!=-1)pre[nn]=pp;
int _i=i^1;
if(head[v]==_i){
head[v]=pre[_i];
}
pp=pre[_i];
nn=Next[_i];
if(pp!=-1)Next[pp]=nn;
if(nn!=-1)pre[nn]=pp;
u=v;
if(deg[v])deg[v]--;
}
}
int main(){
int t;
cin>>t;
while(t--){
init();
cin>>n>>m;
for(int i=1;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v);
deg[u]++;
deg[v]++;
}
for(int i=0;i<ecnt;i++){
if(pre[i]!=-1)Next[pre[i]]=i;
}
for(int i=1;i<=n;i++){
Next[head[i]]=-1;
}
for(int i=1;i<=n;i++){
if(deg[i]&1){
dfs(i);
}
}
for(int i=1;i<=n;i++){
if(deg[i]){
dfs(i);
}
}
for(int i=1;i<=m;i++){
printf("%d\n",ans[i]);
}
}
return 0;
}

38
HDOJ/5349_autoAC.cpp Normal file
View File

@ -0,0 +1,38 @@
#include<iostream>
#include<set>
#include<cstdio>
#include<cstring>
#include<cstdio>
using namespace std;
multiset<int> haha;
multiset<int>::iterator it;
int main(){
int n;
while(scanf("%d",&n)!=EOF){
int p,u;
haha.clear();
int ans ;
while(n--){
scanf("%d",&p);
if(p == 1) {
scanf("%d",&u);
haha.insert(u);
}
else if(p == 2){
if(haha.size() > 0){
haha.erase(haha.begin());
}
}
else if(p==3){
ans = 0;
if(haha.size() > 0){
it = haha.end();
it--;
ans = *it;
}
printf("%d\n",ans);
}
}
}
return 0;
}

45
HDOJ/5350_autoAC.cpp Normal file
View File

@ -0,0 +1,45 @@
#include <iostream>
#include <cstdio>
#include<algorithm>
#include<cstring>
#include<functional>
#include<queue>
typedef long long ll;
using namespace std;
int main()
{
int T;
int n,a;
priority_queue<ll, vector<ll>, greater<ll> >q;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
while(!q.empty())
{
q.pop();
}
for(int i=1; i<=n; i++)
{
scanf("%d",&a);
q.push(a);
}
ll sum = 0;
while(!q.empty())
{
ll x = q.top();
q.pop();
if(q.empty())
{
continue;
}
ll y = q.top();
q.pop();
ll tmp= x + y;
sum+=tmp;
q.push(tmp);
}
printf("%I64d\n",sum);
}
return 0;
}

124
HDOJ/5351_autoAC.cpp Normal file
View File

@ -0,0 +1,124 @@
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define MOD 258280327
int n;
struct s
{
int a[1050];
void set()
{
memset(a,0,sizeof(a));
}
}f[1050];
s ss;
s operator +(s const &m,s const &n)
{
s temp;
temp.set();
for(int i=0;i<1050;i++)
{
temp.a[i]+=m.a[i]+n.a[i];
if(temp.a[i]>9)
{
temp.a[i+1]++;
temp.a[i]-=10;
}
}
return temp;
}
s operator -(s const &m,s const &n)
{
s tem;
tem.set();
for(int i=0;i<1050;i++)
{
tem.a[i]=m.a[i];
}
for(int i=0;i<1050;i++)
{
tem.a[i]-=n.a[i];
if(tem.a[i]<0)
{
tem.a[i]=10+tem.a[i];
tem.a[i+1]--;
}
}
return tem;
}
bool operator >(s const &m,s const &n)
{
int lm,ln;
for(lm=1000;m.a[lm]==0;lm--);
for(ln=1000;n.a[ln]==0;ln--);
if(lm>ln)
return true;
else if(lm<ln)
return false;
for(int i=lm;i>=0;i--)
{
if(m.a[i]>n.a[i])
return true;
else if(m.a[i]<n.a[i])
return false;
}
return false;
}
void init(int n)
{
for(int i=1;i<=n;i++)
f[i].set();
f[1].a[0]=1;
f[2].a[0]=1;
for(int i=3;i<=n;i++)
f[i]=f[i-1]+f[i-2];
}
void change(string m)
{
ss.set();
for(int i=0;i<m.length();i++)
{
ss.a[m.length()-1-i]=m[i]-'0';
}
s k;
k.set();
k.a[0]=1;
while(f[n]>ss+k)
{
n--;
}
}
long long g()
{
long long ans=0;
int i;
s temp=ss-f[n-1];
for(i=1000;temp.a[i]==0;i--);
for(int j=i;j>=0;j--)
ans=((ans*10)%MOD+temp.a[j]%MOD)%MOD;
return ans;
}
int main()
{
int t;
string m;
long long ans;
init(1001);
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
cin >> m;
if(m.length()==1&&(m[0]=='1'))
{
puts("1");
continue;
}
int i;
change(m);
ans=g();
printf("%lld\n",ans);
}
return 0;
}

132
HDOJ/5352_autoAC.cpp Normal file
View File

@ -0,0 +1,132 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iostream>
#define maxn 1000
#define maxm 200000
#define inf 10000000
using namespace std;
int head[maxn],tail;
int queue[maxn],pre[maxn],flag[maxn];
int dist[maxn],maxFlow[maxn];
struct Edge{
int v,u,next,cost,w;
Edge(){}
Edge(int u,int v,int next,int cost,int w):u(u),v(v),next(next),cost(cost),w(w){}
} edge[maxm];
void add_edge(int u,int v,int cost,int w){
edge[tail] = Edge(u,v,head[u],cost,w);
head[u] = tail++;
edge[tail] = Edge(v,u,head[v],-cost,0);
head[v] = tail++;
}
void init(){
memset(head,-1,sizeof(head));
tail=0;
}
int SPFA(int start,int end){
int i,u,v,front,rear;
front = rear = 0;
memset(flag,0,sizeof(flag));
memset(dist,0x1f,sizeof(dist));
memset(pre,-1,sizeof(pre));
dist[start] = 0, dist[end] = inf ,flag[start]=1;
maxFlow[start] = inf, queue[rear++] = start;
while(front != rear){
u = queue[front++];
if(front >= maxn) front = 0;
flag[u] = 0;
for(i = head[u]; i!=-1;i=edge[i].next){
v=edge[i].v;
if(edge[i].w&&dist[v]>dist[u]+edge[i].cost){
dist[v]=dist[u]+edge[i].cost;
maxFlow[v]=min(edge[i].w,maxFlow[u]);
pre[v]=i;
if(!flag[v]){
queue[rear++]=v;
if(rear>=maxn)rear=0;
flag[v] =1;
}
}
}
}
return dist[end] != inf;
}
int MFMC(int start,int end){
int min_cost = 0,v;
while(SPFA(start,end)){
v = end;
while(pre[v]>=0){
edge[pre[v]].w-=maxFlow[end];
edge[pre[v]^1].w+=maxFlow[end];
v=edge[pre[v]].u;
}
min_cost+=dist[end]*maxFlow[end];
}
return min_cost;
}
int map[207][207];
vector<int> stack;
int check[207];
int n;
void dfs(int u){
if(check[u]) return;
check[u] = 1;
stack.push_back(u);
for(int i = 1;i <= n; i++){
if(map[u][i] == 1 && check[i] == 0){
dfs(i);
}
}
}
int main(){
int m,t,k;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&m,&k);
init();
memset(map,0,sizeof(map));
for(int i = 1;i <= n; i++)
add_edge(i,n+1,1,1);
int q,u,v,t,cost = n+2;
for(int i = 0;i < m; i++){
scanf("%d",&t);
if(t == 1){
scanf("%d",&u);
memset(check,0,sizeof(check));
stack.clear();
dfs(u);
add_edge(0,cost,500000-cost*500,k);
for(int j = 0;j < stack.size() ;j++){
add_edge(cost,stack[j],0,1);
}
cost++;
}
else if(t == 2){
scanf("%d%d",&u,&v);
map[v][u] = map[u][v] = 1;
}
else {
scanf("%d",&q);
while(q--){
scanf("%d%d",&u,&v);
map[u][v] = map[v][u] = 0;
}
}
}
int ans = MFMC(0,n+1);
stack.clear();
for(int i = head[0];i != -1; i=edge[i].next){
stack.push_back(k-edge[i].w);
}
printf("%d\n",ans%500);
int flag = 0;
for(int i = stack.size() - 1;i >= 0; i--){
if(i != stack.size()-1) printf(" ");
printf("%d",stack[i]);
}
printf("\n");
}
return 0;
}

66
HDOJ/5353_autoAC.cpp Normal file
View File

@ -0,0 +1,66 @@
#include<cstdio>
#include<cmath>
#include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll maxn=100005;
ll T,n,m,a[maxn],sum,tot;
bool flag;
int f[maxn][2];
int main()
{
scanf("%lld",&T);
while (T--)
{
scanf("%lld",&n);
sum=0; flag=true;
for (int i=0;i<n;i++)
{
scanf("%lld",&a[i]);
sum+=a[i];
}
if (sum%n) flag=false;
else
{
sum=sum/n;
tot=0;
for (int i=0;i<n;i++) a[i]-=sum;
memset(f,0,sizeof(f));
for (int i=0,j,k;i<n+n;i++)
{
j=i%n; k=(i+1)%n;
if (a[j]>0&&a[k]<=0&&!f[j][1]) {
a[k]++;
a[j]--;
if (!f[k][0]) f[j][1]=1;
else f[k][0]=f[j][1]=0;
}
else if (a[j]<0&&a[k]>=0&&!f[k][0]) {
a[k]--;
a[j]++;
if (!f[j][1]) f[k][0]=1;
else f[j][1]=f[k][0]=0;
}
}
for (int i=0;i<n;i++) if (a[i]) {flag=false; break;}
for (int i=0;i<n;i++) tot+=f[i][0]+f[i][1];
if (n==2&&tot==2) flag=false;
}
if (flag)
{
printf("YES\n%d\n",tot);
for (int i=1,j,k;i<=n;i++)
{
j=i-1; if (j==0) j=n;
k=i+1; if (k>n) k=1;
if (f[i-1][0]) printf("%d %d\n",i,j);
if (f[i-1][1]) printf("%d %d\n",i,k);
}
}
else printf("NO\n");
}
return 0;
}

197
HDOJ/5354_autoAC.cpp Normal file
View File

@ -0,0 +1,197 @@
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#include <functional>
#include <numeric>
using namespace std;
#define eps 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LLINF 1LL<<62
#define speed std::ios::sync_with_stdio(false);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef complex<ld> point;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef vector<int> vi;
#define CLR(x,y) memset(x,y,sizeof(x))
#define CPY(x,y) memcpy(x,y,sizeof(x))
#define clr(a,x,size) memset(a,x,sizeof(a[0])*(size))
#define cpy(a,x,size) memcpy(a,x,sizeof(a[0])*(size))
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define lowbit(x) (x&(-x))
#define MID(x,y) (x+((y-x)>>1))
template<class T>
inline bool read(T &n)
{
T x = 0, tmp = 1;
char c = getchar();
while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
if(c == EOF) return false;
if(c == '-') c = getchar(), tmp = -1;
while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
n = x*tmp;
return true;
}
template <class T>
inline void write(T n)
{
if(n < 0)
{
putchar('-');
n = -n;
}
int len = 0,data[20];
while(n)
{
data[len++] = n%10;
n /= 10;
}
if(!len) data[len++] = 0;
while(len--) putchar(data[len]+48);
}
const int MAXN=100010;
int n,m;
int fa[MAXN];
struct Edge
{
int u,v,next;
}e[MAXN<<1];
struct Node
{
int u,v,colu,colv,rku,rkv,fau,fav;
Node(){}
Node(int u,int v,int colu,int colv,int rku,int rkv,int fau,int fav):u(u),v(v),colu(colu),colv(colv),rku(rku),rkv(rkv),fau(fau),fav(fav){}
}s[MAXN];
int col[MAXN];
int head[MAXN],tot;
int rk[MAXN],top;
int ans[MAXN];
void init(int n)
{
CLR(head,-1);
tot=0;top=0;
for(int i=0;i<n+10;i++)
fa[i]=i,col[i]=1,rk[i]=1;
}
void addedge(int u,int v)
{
e[tot].u=u;
e[tot].v=v;
e[tot].next=head[u];
head[u]=tot++;
}
int find_fa(int x)
{
int o=x;
while(fa[o]!=o) o=fa[o];
return o;
}
int find_col(int x)
{
if(fa[x]==x) return col[x];
if(!col[x]) return !find_col(fa[x]);
return find_col(fa[x]);
}
bool merge(int u,int v)
{
int a=find_fa(u),b=find_fa(v);
int x=find_col(u),y=find_col(v);
int root,next;
if(a==b)
{
if(x==y) return false;
return true;
}
if(rk[a]>rk[b])
root=a,next=b;
else
root=b,next=a;
s[top++]=Node(a,b,col[a],col[b],rk[a],rk[b],fa[a],fa[b]);
if(x==y && col[root]==1)
col[next]^=1;
fa[next]=root;
rk[root]+=rk[next];
return true;
}
bool unite(int l,int r,int a,int b)
{
bool flag=true;
int u,v;
for(int t=l;t<=r;t++)
for(int i=head[t];~i;i=e[i].next)
{
u=e[i].u;v=e[i].v;
if(a<=v && v<=b) continue;
if(!merge(u,v))
flag=false;
}
return flag;
}
void back(int x)
{
Node tmp;
while(top>x)
{
--top;
tmp=s[top];
int u=tmp.u,v=tmp.v;
rk[u]=tmp.rku;rk[v]=tmp.rkv;
fa[u]=tmp.fau;fa[v]=tmp.fav;
col[u]=tmp.colu;col[v]=tmp.colv;
}
}
void cdq(int l,int r,bool flag)
{
if(l==r)
{
ans[l]=flag;
return;
}
int mid=MID(l,r);
int pre=top;
bool now=flag&&unite(mid+1,r,l,mid);
cdq(l,mid,now);
back(pre);
now=flag&&unite(l,mid,mid+1,r);
cdq(mid+1,r,now);
back(pre);
}
int main()
{
int T;
read(T);
while(T--)
{
read(n),read(m);
init(n);
for(int i=0,u,v;i<m;i++)
{
read(u);read(v);
addedge(u,v);
addedge(v,u);
}
cdq(1,n,true);
for(int i=1;i<=n;i++)
write(ans[i]);
putchar('\n');
}
return 0;
}

66
HDOJ/5355_autoAC.cpp Normal file
View File

@ -0,0 +1,66 @@
#include<cstdio>
#include<cmath>
#include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll maxn=100005;
ll T,n,m,a[maxn],sum,tot;
bool flag;
int f[maxn][2];
int main()
{
scanf("%lld",&T);
while (T--)
{
scanf("%lld",&n);
sum=0; flag=true;
for (int i=0;i<n;i++)
{
scanf("%lld",&a[i]);
sum+=a[i];
}
if (sum%n) flag=false;
else
{
sum=sum/n;
tot=0;
for (int i=0;i<n;i++) a[i]-=sum;
memset(f,0,sizeof(f));
for (int i=0,j,k;i<n+n;i++)
{
j=i%n; k=(i+1)%n;
if (a[j]>0&&a[k]<=0&&!f[j][1]) {
a[k]++;
a[j]--;
if (!f[k][0]) f[j][1]=1;
else f[k][0]=f[j][1]=0;
}
else if (a[j]<0&&a[k]>=0&&!f[k][0]) {
a[k]--;
a[j]++;
if (!f[j][1]) f[k][0]=1;
else f[j][1]=f[k][0]=0;
}
}
for (int i=0;i<n;i++) if (a[i]) {flag=false; break;}
for (int i=0;i<n;i++) tot+=f[i][0]+f[i][1];
if (n==2&&tot==2) flag=false;
}
if (flag)
{
printf("YES\n%d\n",tot);
for (int i=1,j,k;i<=n;i++)
{
j=i-1; if (j==0) j=n;
k=i+1; if (k>n) k=1;
if (f[i-1][0]) printf("%d %d\n",i,j);
if (f[i-1][1]) printf("%d %d\n",i,k);
}
}
else printf("NO\n");
}
return 0;
}

87
HDOJ/5357_autoAC.cpp Normal file
View File

@ -0,0 +1,87 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <string>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <utility>
#include <iostream>
#include <algorithm>
template<class Num>void read(Num &x)
{
char c; int flag = 1;
while((c = getchar()) < '0' || c > '9')
if(c == '-') flag *= -1;
x = c - '0';
while((c = getchar()) >= '0' && c <= '9')
x = (x<<3) + (x<<1) + (c-'0');
x *= flag;
return;
}
template<class Num>void write(Num x)
{
if(x < 0) putchar('-'), x = -x;
static char s[20];int sl = 0;
while(x) s[sl++] = x%10 + '0',x /= 10;
if(!sl) {putchar('0');return;}
while(sl) putchar(s[--sl]);
}
const int maxn = 1e6 + 50, Mod = 1e9+7;
char str[maxn];
int T, n, a[maxn], b[maxn];
int pre[maxn], match[maxn];
long long ans[maxn];
int stack[maxn], top;
void prework()
{
top = 0;
for(int i = 1; i <= n; i++)
{
match[i] = pre[i] = 0;
if(str[i] == '(')
stack[++top] = i;
else
{
if(top)
{
match[stack[top]] = i;
match[i] = stack[top];
if(top > 1) pre[match[i]] = stack[top - 1];
stack[top--] = 0;
}
}
}
}
void solve()
{
a[0] = b[n + 1] = ans[0] = 0;
for(int i = 1; i <= n; i++)
a[i] = (str[i] == ')' && match[i])?(a[match[i] - 1] + 1):0;
for(int i = n; i >= 1; i--)
b[i] = (str[i] == '(' && match[i])?(b[i] = b[match[i] + 1] + 1):0;
for(int i = 1; i <= n; i++)
ans[i] = (str[i] == '(')?(ans[pre[i]] + ((long long)b[i]*a[match[i]] % Mod) % Mod):ans[match[i]];
}
long long calcu()
{
long long tot = 0;
for(int i = 1; i <= n; i++)
tot = tot + ans[i]*i % Mod;
return tot;
}
int main()
{
read(T);
while(T--)
{
scanf("%s", str + 1);
n = strlen(str + 1);
prework(), solve();
write(calcu()), puts("");
}
return 0;
}

57
HDOJ/5358_autoAC.cpp Normal file
View File

@ -0,0 +1,57 @@
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
#include<cmath>
#include<map>
#include<queue>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1 | 1
#define lowbit(x) x&(-x)
using namespace std;
typedef long long LL;
const int N=1e5+10;
long long inf=1e15;
const int MOD=1e9+7;
int T,n,m,k,x,y,z,l,tot;
long long cnt;
int a[N];
long long sum[N];
long long solve(long long L,long long R){
long long ans=0;
int l,r;
r=0;l=1;
for(int i=1;i<=n;i++){
if(l<i) l=i;
if(r<i-1) r=i-1;
while(l<=n && sum[l]-sum[i-1]<L) l++;
while(r+1<=n && sum[r+1]-sum[i-1]<R) r++;
if(l>r) continue;
if(sum[r]-sum[i-1]<L || sum[r]-sum[i-1]>=R) continue;
if(sum[l]-sum[i-1]<L || sum[l]-sum[i-1]>=R) continue;
ans+=(long long)(r-l+1)*i+(long long)(l+r)*(r-l+1)/2;
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
sum[0]=0;
for(int i=1;i<=n;i++) {
scanf("%d",&a[i]);
sum[i]=sum[i-1]+a[i];
}
long long ans=0;
for(int k=1;k<=34;k++){
ans+=(long long)k*solve(pow(2,k-1),pow(2,k));
}
ans+=solve(0,1);
printf("%I64d\n",ans);
}
return 0;
}

58
HDOJ/5360_autoAC.cpp Normal file
View File

@ -0,0 +1,58 @@
#include<cstdio>
#include<cmath>
#include<iostream>
#include<queue>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll maxn=100005;
int T,n,m,l[maxn],r[maxn],ans[maxn],tot,f[maxn];
struct lr
{
int l,r,id;
lr(int l,int r,int id):l(l),r(r),id(id){};
bool operator <(const lr& a)const
{
if (r==a.r) return l>a.l;
return r>a.r;
}
};
vector<lr> p[maxn];
int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%d",&n); tot=0;
for (int i=1;i<=n;i++) scanf("%d",&l[i]);
for (int i=1;i<=n;i++) scanf("%d",&r[i]);
for (int i=0;i<=n;i++) p[i].clear();
for (int i=1;i<=n;i++) p[l[i]].push_back(lr(l[i],r[i],i));
priority_queue<lr> que;
for (int i=0;i<=n;)
{
for (int j=0;j<p[i].size();j++) que.push(lr(p[i][j]));
while (!que.empty()&&que.top().r<i) que.pop();
if (!que.empty()) {ans[tot++]=que.top().id; que.pop(); i++;}
else break;
}
printf("%d\n",tot);
if (tot)
{
memset(f,0,sizeof(f));
printf("%d",ans[0]);
for (int i=1;i<tot;i++) printf(" %d",ans[i]);
for (int i=0;i<tot;i++) f[ans[i]]=1;
for (int i=1;i<=n;i++) if (!f[i]) printf(" %d",i);
}
else for (int i=1;i<=n;i++)
{
if (i>1) printf(" ");
printf("%d",i);
}
printf("\n");
}
return 0;
}

73
HDOJ/5361_autoAC.cpp Normal file
View File

@ -0,0 +1,73 @@
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<set>
using namespace std;
#define maxn 200007
#define ll long long
int lp[maxn],rp[maxn];
ll cosw[maxn];
ll dist[maxn];
set<int> haha;
struct Node{
int id;
ll cost;
};
bool operator < (Node a,Node b){
if(a.cost == b.cost) return a.id < b.id;
return a.cost < b.cost;
}
set<Node> mind;
int main(){
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i = 0;i < n; i++)
scanf("%d",&lp[i]);
for(int i = 0;i < n; i++)
scanf("%d",&rp[i]);
for(int i = 0;i < n; i++)
scanf("%d",&cosw[i]);
haha.clear();
mind.clear();
memset(dist,-1,sizeof(dist));
dist[0] = 0;
Node x,y;
x.id = 0;
x.cost = cosw[0];
mind.insert(x);
for(int i = 1;i < n; i++)
haha.insert(i);
set<int>::iterator it,it2;
while(mind.size() > 0){
x = *mind.begin();
mind.erase(mind.begin());
it = haha.lower_bound(x.id - rp[x.id]);
while(it != haha.end() && *it <= x.id - lp[x.id]){
y.id = *it;
y.cost = x.cost + cosw[y.id];
dist[y.id] = x.cost;
mind.insert(y);
it2 = it++;
haha.erase(it2);
}
it = haha.lower_bound(x.id + lp[x.id]);
while(it != haha.end() && *it <= x.id + rp[x.id]){
y.id = *it;
y.cost = x.cost + cosw[y.id];
dist[y.id] = x.cost;
mind.insert(y);
it2 = it++;
haha.erase(it2);
}
}
for(int i = 0;i < n; i++){
if(i) printf(" ");
printf("%I64d",dist[i]);
}
printf("\n");
}
return 0;
}

27
HDOJ/5363_autoAC.cpp Normal file
View File

@ -0,0 +1,27 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int m=1000000007;
int pow_mod(__int64 a,__int64 n,int m)
{
if(n==0) return 1;
__int64 x=pow_mod(a,n/2,m);
__int64 ans=(x%m)*(x%m);
ans%=m;
if(n%2==1) ans=ans*a%m;
return (int)ans;
}
int main()
{
int _;
__int64 n;
scanf("%d",&_);
while(_--)
{
scanf("%I64d",&n);
int ans=pow_mod(2,n-1,m);
printf("%d\n",(ans-1)%m);
}
return 0;
}

35
HDOJ/5364_autoAC.cpp Normal file
View File

@ -0,0 +1,35 @@
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn=1e4+5;
int a;
int b[maxn];
int main()
{
int m;
while(scanf("%d",&m)!=EOF)
{
memset(b,0,sizeof(b));
for(int i=0; i<m; i++)
{
scanf("%d",&a);
b[a]++;
}
int maxx=-99;
int flag=0;
for(int i=0; i<maxn; i++)
{
if(maxx<b[i])
{
maxx=b[i];
flag=i;
}
}
if(maxx<=m/2)
printf("-1\n");
else
printf("%d\n",flag);
}
return 0;
}

55
HDOJ/5365_autoAC.cpp Normal file
View File

@ -0,0 +1,55 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
#define ll __int64
using namespace std;
const int N = 25;
const int inf = 1000000000;
const int mod = 1000000007;
int x[N],y[N],s[6];
int sqare(int x1,int y1,int x2,int y2)
{
return (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
}
bool judge(int i,int j,int k,int l)
{
int t=0;
s[t++]=sqare(x[i],y[i],x[j],y[j]);
s[t++]=sqare(x[j],y[j],x[k],y[k]);
s[t++]=sqare(x[k],y[k],x[l],y[l]);
s[t++]=sqare(x[l],y[l],x[i],y[i]);
s[t++]=sqare(x[i],y[i],x[k],y[k]);
s[t++]=sqare(x[l],y[l],x[j],y[j]);
sort(s,s+6);
if(s[0]==s[1]&&s[2]==s[3]&&s[1]==s[2]&&s[4]==2*s[0]&&s[5]==s[4])
return true;
return false;
}
int main()
{
int n,i,j,k,l,t;
while(~scanf("%d",&n))
{
t=0;
for(i=1;i<=n;i++)
scanf("%d%d",&x[i],&y[i]);
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
for(k=j+1;k<=n;k++)
for(l=k+1;l<=n;l++)
if(judge(i,j,k,l))
t++;
printf("%d\n",t);
}
return 0;
}

18
HDOJ/5366_autoAC.cpp Normal file
View File

@ -0,0 +1,18 @@
#include <iostream>
#include <cstdio>
using namespace std;
__int64 dp[62];
int main()
{
int m;
dp[1]=1;
dp[2]=2;
dp[3]=3;
for(int i=4; i<61; i++)
dp[i] = dp[i-1]+dp[i-3]+1;
while(~scanf("%d",&m))
{
printf("%I64d\n",dp[m]);
}
return 0;
}

137
HDOJ/5367_autoAC.cpp Normal file
View File

@ -0,0 +1,137 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std ;
typedef long long LL ;
#define clr( a , x ) memset ( a , x , sizeof a )
const int MAXN = 50005 ;
const int INF = 0x3f3f3f3f ;
struct Node {
int sum , lsum , rsum ;
int lnum , rnum ;
int lf , rf ;
int ls , rs ;
int l , r , len ;
int add ;
void init ( int _l , int _r ) {
add = sum = lnum = rnum = lf = rf = ls = rs = 0 ;
l = _l ;
r = _r ;
lsum = rsum = len = r - l + 1 ;
}
} ;
Node T[MAXN * 60] ;
int root , cur ;
int n , m ;
void check ( int& o , int l , int r ) {
if ( o ) return ;
o = ++ cur ;
T[o].init ( l , r ) ;
if ( l == 1 ) T[o].lf = INF ;
if ( r == n ) T[o].rf = INF ;
}
void pushup ( int o ) {
int m = ( T[o].l + T[o].r ) >> 1 ;
check ( T[o].ls , T[o].l , m ) ;
check ( T[o].rs , m + 1 , T[o].r ) ;
int ls = T[o].ls , rs = T[o].rs ;
T[o].sum = T[ls].sum + T[rs].sum ;
T[o].lnum = T[ls].lnum ;
T[o].rnum = T[rs].rnum ;
T[o].lsum = T[ls].lsum ;
T[o].rsum = T[rs].rsum ;
T[o].lf = T[ls].lf ;
T[o].rf = T[rs].rf ;
if ( T[ls].rnum == T[rs].lnum ) {
if ( T[ls].rf < T[ls].rnum && T[rs].lf < T[rs].lnum ) {
T[o].sum += T[ls].rsum + T[rs].lsum ;
}
if ( T[ls].rsum == T[ls].len ) {
T[o].lf = T[rs].lf ;
T[o].lsum += T[rs].lsum ;
}
if ( T[rs].lsum == T[rs].len ) {
T[o].rf = T[ls].rf ;
T[o].rsum += T[ls].rsum ;
}
} else {
if ( T[ls].lsum == T[ls].len ) {
T[o].lf = T[rs].lnum ;
}
if ( T[ls].rnum > T[ls].rf && T[ls].rnum > T[rs].lnum ) {
T[o].sum += T[ls].rsum ;
}
if ( T[rs].rsum == T[rs].len ) {
T[o].rf = T[ls].rnum ;
}
if ( T[rs].lnum > T[rs].lf && T[rs].lnum > T[ls].rnum ) {
T[o].sum += T[rs].lsum ;
}
}
}
void upd ( int o , int addv ) {
T[o].add += addv ;
T[o].lnum += addv ;
T[o].rnum += addv ;
T[o].lf += addv ;
T[o].rf += addv ;
}
void pushdown ( int o ) {
int m = ( T[o].l + T[o].r ) >> 1 ;
if ( T[o].add ) {
check ( T[o].ls , T[o].l , m ) ;
check ( T[o].rs , m + 1 , T[o].r ) ;
upd ( T[o].ls , T[o].add ) ;
upd ( T[o].rs , T[o].add ) ;
T[o].add = 0 ;
}
}
void update ( int L , int R , int v , int& o , int l , int r ) {
check ( o , l , r ) ;
if ( L <= l && r <= R ) {
upd ( o , v ) ;
return ;
}
pushdown ( o ) ;
int m = ( l + r ) >> 1 ;
if ( L <= m ) update ( L , R , v , T[o].ls , l , m ) ;
if ( m < R ) update ( L , R , v , T[o].rs , m + 1 , r ) ;
pushup ( o ) ;
}
void show ( int o , int l , int r ) {
if ( o == 0 ) {
for ( int i = l ; i <= r ; ++ i ) {
printf ( "%d " , 0 ) ;
}
return ;
} else if ( !T[o].ls && !T[o].rs ) {
for ( int i = l ; i <= r ; ++ i ) {
printf ( "%d " , T[o].lnum ) ;
}
return ;
}
pushdown ( o ) ;
int m = ( l + r ) >> 1 ;
show ( T[o].ls , l , m ) ;
show ( T[o].rs , m + 1 , r ) ;
pushup ( o ) ;
}
void solve () {
int l , r , v ;
root = cur = 0 ;
int ans = 0 ;
for ( int i = 0 ; i < m ; ++ i ) {
scanf ( "%d%d%d" , &l , &r , &v ) ;
l ^= ans ;
r ^= ans ;
v ^= ans ;
if ( l > r ) swap ( l , r ) ;
update ( l , r , v , root , 1 , n ) ;
ans = T[1].sum ;
printf ( "%d\n" , T[1].sum ) ;
}
}
int main () {
while ( ~scanf ( "%d%d%*d" , &n , &m ) ) solve () ;
return 0 ;
}

129
HDOJ/5370_autoAC.cpp Normal file
View File

@ -0,0 +1,129 @@
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std ;
typedef long long LL ;
#define clr( a , x ) memset ( a , x , sizeof a )
const int MAXN = 505 ;
const int mod = 1e9 + 7 ;
int G[MAXN][2] , cnt ;
int f[MAXN] ;
int root[MAXN] , idx ;
int is[MAXN] ;
int num[MAXN] ;
int dp[MAXN] , dp2[MAXN][MAXN] ;
int n , cur ;
void upd ( int& x , int y ) {
x += y ;
if ( x >= mod ) x -= mod ;
}
void preprocess () {
dp[0] = dp[1] = 1 ;
for ( int i = 2 ; i < MAXN ; ++ i ) {
for ( int j = 0 ; j < i ; ++ j ) {
upd ( dp[i] , ( LL ) dp[j] * dp[i - 1 - j] % mod ) ;
}
}
}
void dfs ( int u , int x ) {
int l = G[u][0] , r = G[u][1] ;
if ( is[u] == is[l] ) dfs ( l , x ) ;
if ( is[u] == is[r] ) dfs ( r , x ) ;
if ( is[l] != is[u] && is[r] != is[u] ) {
if ( !l && !r ) {
for ( int i = 0 ; i <= x ; ++ i ) {
dp2[u][i] = dp[i + 1] ;
}
} else if ( !l ) {
for ( int i = 0 ; i <= x ; ++ i ) {
dp2[u][i] = dp[i] ;
}
} else if ( !r ) {
for ( int i = 0 ; i <= x ; ++ i ) {
dp2[u][i] = dp[i] ;
}
} else {
dp2[u][0] = 1 ;
for ( int i = 1 ; i <= x ; ++ i ) {
dp2[u][i] = 0 ;
}
}
} else if ( is[u] != is[l] ) {
if ( !l ) {
for ( int i = 0 ; i <= x ; ++ i ) {
dp2[u][i] = 0 ;
for ( int j = 0 ; j <= i ; ++ j ) {
upd ( dp2[u][i] , ( LL ) dp[j] * dp2[r][i - j] % mod ) ;
}
}
} else {
for ( int i = 0 ; i <= x ; ++ i ) {
dp2[u][i] = dp2[r][i] ;
}
}
} else if ( is[u] != is[r] ) {
if ( !r ) {
for ( int i = 0 ; i <= x ; ++ i ) {
dp2[u][i] = 0 ;
for ( int j = 0 ; j <= i ; ++ j ) {
upd ( dp2[u][i] , ( LL ) dp[j] * dp2[l][i - j] % mod ) ;
}
}
} else {
for ( int i = 0 ; i <= x ; ++ i ) {
dp2[u][i] = dp2[l][i] ;
}
}
} else {
for ( int i = 0 ; i <= x ; ++ i ) {
dp2[u][i] = 0 ;
for ( int j = 0 ; j <= i ; ++ j ) {
upd ( dp2[u][i] , ( LL ) dp2[l][j] * dp2[r][i - j] % mod ) ;
}
}
}
}
int newnode ( int x ) {
++ cnt ;
G[cnt][0] = G[cnt][1] = 0 ;
f[cnt] = cur ;
is[cnt] = x ;
return cnt ;
}
void solve () {
int op , x ;
idx = cnt = 0 ;
cur = 1 ;
newnode ( 0 ) ;
for ( int i = 1 ; i <= n ; ++ i ) {
scanf ( "%d" , &op ) ;
if ( op == 0 ) cur = f[cur] ;
else if ( op <= 2 ) {
if ( !G[cur][op - 1] ) {
G[cur][op - 1] = newnode ( is[cur] ) ;
num[is[cur]] -- ;
}
cur = G[cur][op - 1] ;
} else {
scanf ( "%d" , &x ) ;
G[cur][op - 3] = newnode ( ++ idx ) ;
root[idx] = cnt ;
num[idx] = x - 1 ;
}
}
int ans = 1 ;
for ( int i = 1 ; i <= idx ; ++ i ) if ( num[i] ) {
dfs ( root[i] , num[i] ) ;
ans = ( LL ) ans * dp2[root[i]][num[i]] % mod ;
}
printf ( "%d\n" , ans ) ;
}
int main () {
int cas = 0 ;
preprocess () ;
while ( ~scanf ( "%d" , &n ) ) {
printf ( "Case #%d: " , ++ cas ) ;
solve () ;
}
return 0 ;
}

57
HDOJ/5371_autoAC.cpp Normal file
View File

@ -0,0 +1,57 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std ;
const int maxn = 2e5 + 10 ;
int str[maxn] ;
int p[maxn] ;
void pk(int n)
{
int i;
int mx = 0;
int id=0;
for(i=0; i<n; i++)
{
if( mx > i )
p[i] = min( p[2*id-i], mx-i );
else
p[i] = 1;
for(; str[i+p[i]] == str[i-p[i]] && i - p[i] >= 0 && i + p[i] < n; p[i]++);
if( p[i] + i > mx )
{
mx = p[i] + i;
id = i;
}
}
}
int main()
{
int t ;
int cas = 0 ;
int n ;
scanf("%d" , &t) ;
while(t--)
{
scanf("%d" , &n) ;
for(int i = 0;i <= 2*n+1;i++)
p[i] = 0 ;
int len = 0 ;
str[len++] = -1 ;
for(int i = 1;i <= n;i++)
{
scanf("%d" , &str[len++]) ;
str[len++] = -1 ;
}
int ans = 1 ;
pk(len) ;
for(int i = 2;i < len-1 ;i += 2)
for(int j = ans;j <= p[i] ;j += 2)
if(p[i + j -1]>= j)
ans = j;
ans = ans/2*3 ;
printf("Case #%d: ",++cas) ;
cout<<ans<<endl;
}
return 0 ;
}

52
HDOJ/5372_autoAC.cpp Normal file
View File

@ -0,0 +1,52 @@
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int low(int x) { return x&-x; }
const int maxn = 400005;
int n, m, x[maxn], y[maxn], z[maxn], a[maxn], tot, f[2][maxn], tt = 0, u, v;
void add(int x, int y, int z)
{
for (int i = y; i <= m; i += low(i)) f[x][i] += z;
}
int get(int x, int y)
{
int ans = 0;
for (int i = y; i; i -= low(i)) ans += f[x][i];
return ans;
}
int main()
{
while (scanf("%d", &n) != EOF)
{
tot = 0;
memset(f, 0, sizeof(f));
for (int i = 1; i <= n; i++)
{
scanf("%d%d", &z[i], &x[i]);
if (!z[i]) {
a[++tot] = x[i];
y[i] = x[i] + (++tot) / 2;
a[tot] = y[i];
}
else
{
y[i] = a[x[i] + x[i]];
x[i] = a[x[i] + x[i] - 1];
}
}
sort(a + 1, a + tot + 1);
m = unique(a + 1, a + tot + 1) - a;
printf("Case #%d:\n", ++tt);
for (int i = 1; i <= n; i++)
{
u = lower_bound(a + 1, a + m, x[i]) - a;
v = lower_bound(a + 1, a + m, y[i]) - a;
if (!z[i]) printf("%d\n", get(1, v) - get(0, u - 1));
add(0, u, z[i] ? -1 : 1);
add(1, v, z[i] ? -1 : 1);
}
}
return 0;
}

53
HDOJ/5373_autoAC.cpp Normal file
View File

@ -0,0 +1,53 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <malloc.h>
#define Max(a,b) (a>b?a:b)
#define Min(a,b) (a<b?a:b)
#define MAX 999999999
#define LL long long
#define M 6666666
using namespace std;
int main()
{
int n,m,i,j,k,l,cas=1;
while(~scanf("%d%d",&n,&m))
{
if(n==-1&&m==-1)break;
if(m==0)
{
if(n%11==0)printf("Case #%d: Yes\n",cas++);
else printf("Case #%d: No\n",cas++);
continue;
}
k=n;
l=0;
while(k>0)
{
l+=k%10;
k/=10;
}
while(m--)
{
k=l;
while(k>0)
{
k/=10;
n*=10;
}
n=(n+l)%11;
k=l;
while(k>0)
{
l+=k%10;
k/=10;
}
}
if(n==0)printf("Case #%d: Yes\n",cas++);
else printf("Case #%d: No\n",cas++);
}
return 0;
}

581
HDOJ/5374_autoAC.cpp Normal file
View File

@ -0,0 +1,581 @@
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
char seq[1010];
int type[1010];
int maz[20][20];
int LS;
int now;
int ans;
int N;
void init()
{
memset(maz, 0, sizeof(maz));
}
void out(){
return;
getchar();
system( "cls" );
cout << now << " " << seq[now] << endl;
for( int i = 12 ; i ; i -- )
{
for( int j = 1 ; j <= 9 ; j ++ )
printf("%d ",maz[j][i] );
puts("");
}
}
int tmp[20][20];
void disappear()
{
out();
memset(tmp, 0, sizeof(tmp));
int nx = 1;
for(int y = 1; y <= 12; y++)
{
int cnt = 0;
for(int x = 1; x <= 9; x++)
if(maz[x][y]) cnt++;
if(cnt == 9)
{
ans++;
}
else
{
for(int i = 1; i <= 9; i++)
tmp[i][nx] = maz[i][y];
nx++;
}
}
memset(maz, 0, sizeof(maz));
for(int j = 1; j <= 9; j++)
for(int i = 1; i < nx; i++)
maz[j][i] = tmp[j][i];
out();
return;
}
void OO()
{
int midx = 4, midy = 9;
maz[midx][midy] = maz[midx][midy + 1] = maz[midx + 1][midy] = maz[midx + 1][midy + 1] = 1;
while(now < LS)
{
out();
if(seq[now] == 'w')
{
;
}
else if(seq[now] == 'a')
{
if(midx - 1 >= 1 && !maz[midx - 1][midy] && !maz[midx - 1][midy + 1])
{
maz[midx - 1][midy] = maz[midx - 1][midy + 1] = 1;
maz[midx + 1][midy] = maz[midx + 1][midy + 1] = 0;
midx--;
}
else ;
}
else if(seq[now] == 'd')
{
if(midx + 1 + 1 <= 9 && !maz[midx + 2][midy] && !maz[midx + 2][midy + 1])
{
maz[midx + 2][midy] = maz[midx + 2][midy + 1] = 1;
maz[midx][midy] = maz[midx][midy + 1] = 0;
midx++;
}
else ;
}
else if(seq[now] == 's')
{
if(midy - 1 >= 1 && maz[midx][midy - 1] == 0 && maz[midx + 1][midy - 1] == 0)
{
maz[midx][midy - 1] = maz[midx + 1][midy - 1] = 1;
maz[midx][midy + 1] = maz[midx + 1][midy + 1] = 0;
midy--;
}
else ;
}
now++;
if(midy - 1 >= 1 && maz[midx][midy - 1] == 0 && maz[midx + 1][midy - 1] == 0)
{
maz[midx][midy - 1] = maz[midx + 1][midy - 1] = 1;
maz[midx][midy + 1] = maz[midx + 1][midy + 1] = 0;
midy--;
}
else
{
break;
}
}
disappear();
return;
}
void II()
{
int midx = 4, midy = 9;
for(int i = 0; i < 4; i++)
maz[midx][midy + i] = 1;
int cas = 0;
while(now < LS)
{
out();
if(seq[now] == 'w')
{
if(cas == 0)
{
bool ok = 1;
for(int i = 1; i <= 3; i++)
{
if(midx + i > 9 || maz[midx + i][midy] == 1)
{
ok = 0;
break;
}
}
if(!ok) ;
else
{
for(int i = 1; i <= 3; i++)
maz[midx][midy + i] = 0;
for(int i = 1; i <= 3; i++)
maz[midx + i][midy] = 1;
cas = 1;
}
}
else
{
bool ok = 1;
for(int i = 1; i <= 3; i++)
{
if(maz[midx][midy + i] == 1)
{
ok = 0;
break;
}
}
if(!ok);
else
{
for(int i = 1; i <= 3; i++)
maz[midx][midy + i] = 1;
for(int i = 1; i <= 3; i++)
maz[midx + i][midy] = 0;
cas = 0;
}
}
}
else if(seq[now] == 'a')
{
if(cas == 0)
{
bool ok = 1;
for(int i = 0; i < 4; i++)
if(midx - 1 < 1 || maz[midx - 1][midy + i] == 1)
{
ok = 0;
break;
}
if(!ok);
else
{
for(int i = 0; i < 4; i++)
{
maz[midx - 1][midy + i] = 1;
maz[midx][midy + i] = 0;
}
midx--;
}
}
else
{
if(midx - 1 < 1 || maz[midx - 1][midy] == 1);
else
{
maz[midx - 1][midy] = 1;
maz[midx + 3][midy] = 0;
midx--;
}
}
}
else if(seq[now] == 'd')
{
if(cas == 0)
{
bool ok = 1;
for(int i = 0; i < 4; i++)
if(midx + 1 > 9 || maz[midx + 1][midy + i] == 1)
{
ok = 0;
break;
}
if(!ok);
else
{
for(int i = 0; i < 4; i++)
{
maz[midx + 1][midy + i] = 1;
maz[midx][midy + i] = 0;
}
midx++;
}
}
else
{
if(midx + 4 > 9 || maz[midx + 4][midy] == 1);
else
{
maz[midx + 4][midy] = 1;
maz[midx][midy] = 0;
midx++;
}
}
}
else if(seq[now] == 's')
{
if(cas == 0)
{
if(midy - 1 < 1 || maz[midx][midy - 1] == 1);
else
{
maz[midx][midy + 3] = 0;
maz[midx][midy - 1] = 1;
midy--;
}
}
else
{
int ok = 1;
for(int i = 0; i < 4; i++)
if(midy - 1 < 1 || maz[midx + i][midy - 1] == 1)
{
ok = 0;
break;
}
if(!ok);
else
{
for(int i = 0; i < 4; i++)
maz[midx + i][midy - 1] = 1;
for(int i = 0; i < 4; i++)
maz[midx + i][midy] = 0;
midy--;
}
}
}
now++;
if(cas == 0)
{
if(midy - 1 < 1 || maz[midx][midy - 1] == 1)
break;
else
{
maz[midx][midy + 3] = 0;
maz[midx][midy - 1] = 1;
midy--;
}
}
else
{
int ok = 1;
for(int i = 0; i < 4; i++)
if(midy - 1 < 1 || maz[midx + i][midy - 1] == 1)
{
ok = 0;
break;
}
if(!ok)
break;
else
{
for(int i = 0; i < 4; i++)
maz[midx + i][midy - 1] = 1;
for(int i = 0; i < 4; i++)
maz[midx + i][midy] = 0;
midy--;
}
}
}
disappear();
}
void JJ()
{
int midx = 4, midy = 9;
int cas = 0;
maz[midx][midy] = maz[midx + 1][midy] = maz[midx + 2][midy] = maz[midx][midy + 1] = 1;
while(now < LS)
{
out();
if(seq[now] == 'w')
{
if(cas == 0)
{
if(maz[midx][midy + 2] == 1 || maz[midx + 1][midy + 2] == 1);
else
{
maz[midx + 1][midy] = maz[midx + 2][midy] = 0;
maz[midx][midy + 2] = maz[midx + 1][midy + 2] = 1;
cas = 1;
}
}
else if(cas == 1)
{
if(maz[midx + 1][midy + 1] == 1 || maz[midx + 2][midy + 1] == 1 || maz[midx + 2][midy] == 1);
else
{
maz[midx][midy] = maz[midx][midy + 2] = maz[midx + 1][midy + 2] = 0;
maz[midx + 1][midy + 1] = maz[midx + 2][midy] = maz[midx + 2][midy + 1] = 1;
cas = 2;
}
}
else if(cas == 2)
{
if(maz[midx][midy] == 1 || maz[midx + 1][midy] == 1 || maz[midx + 1][midy + 2] == 1);
else
{
maz[midx][midy] = maz[midx + 1][midy] = maz[midx + 1][midy + 2] = 1;
maz[midx][midy + 1] = maz[midx + 2][midy] = maz[midx + 2][midy + 1] = 0;
cas = 3;
}
}
else if(cas == 3)
{
if(maz[midx][midy + 1] == 1 || maz[midx + 2][midy] == 1);
else
{
maz[midx][midy + 1] = maz[midx + 2][midy] = 1;
maz[midx + 1][midy + 1] = maz[midx + 1][midy + 2] = 0;
cas = 0;
}
}
}else if(seq[now] == 'a')
{
if(cas == 0)
{
if(midx - 1 < 1 || maz[midx - 1][midy] == 1 || maz[midx - 1][midy + 1] == 1);
else
{
maz[midx - 1][midy] = maz[midx - 1][midy + 1] = 1;
maz[midx][midy + 1] = maz[midx + 2][midy] = 0;
midx--;
}
}
else if(cas == 1)
{
if(midx - 1 < 1 || maz[midx - 1][midy] || maz[midx - 1][midy + 1] || maz[midx - 1][midy + 2]);
else
{
maz[midx - 1][midy] = maz[midx - 1][midy + 1] = maz[midx - 1][midy + 2] = 1;
maz[midx][midy] = maz[midx][midy + 1] = maz[midx + 1][midy + 2] = 0;
midx--;
}
}
else if(cas == 2)
{
if(midx - 1 < 1 || maz[midx - 1][midy + 1] || maz[midx + 1][midy]);
else
{
maz[midx - 1][midy + 1] = maz[midx + 1][midy] = 1;
maz[midx + 2][midy] = maz[midx + 2][midy + 1] = 0;
midx--;
}
}
else if(cas == 3)
{
if(midx - 1 < 1 || maz[midx - 1][midy] || maz[midx][midy + 1] || maz[midx][midy + 2]);
else
{
maz[midx - 1][midy] = maz[midx][midy + 1] = maz[midx][midy + 2] = 1;
maz[midx + 1][midy] = maz[midx + 1][midy + 1] = maz[midx + 1][midy + 2] = 0;
midx--;
}
}
}
else if(seq[now] == 'd')
{
if(cas == 0)
{
if(midx + 3 > 9 || maz[midx + 3][midy] || maz[midx + 1][midy + 1]);
else
{
maz[midx + 3][midy] = maz[midx + 1][midy + 1] = 1;
maz[midx][midy] = maz[midx][midy + 1] = 0;
midx++;
}
}
else if(cas == 1)
{
if(midx + 2 > 9 || maz[midx + 2][midy + 2] || maz[midx + 1][midy] || maz[midx + 1][midy + 1]);
else
{
maz[midx + 2][midy + 2] = maz[midx + 1][midy] = maz[midx + 1][midy + 1] = 1;
maz[midx][midy] = maz[midx][midy + 1] = maz[midx][midy + 2] = 0;
midx++;
}
}
else if(cas == 2)
{
if(midx + 3 > 9 || maz[midx + 3][midy] || maz[midx + 3][midy + 1]);
else
{
maz[midx + 3][midy] = maz[midx + 3][midy + 1] = 1;
maz[midx][midy + 1] = maz[midx + 2][midy] = 0;
midx++;
}
}
else if(cas == 3)
{
if(midx + 2 > 9 || maz[midx + 2][midy] || maz[midx + 2][midy + 1] || maz[midx + 2][midy + 2]);
else
{
maz[midx + 2][midy] = maz[midx + 2][midy +1] = maz[midx + 2][midy + 2] = 1;
maz[midx][midy] = maz[midx + 1][midy + 1] = maz[midx +1][midy + 2] = 0;
midx++;
}
}
}
else if(seq[now] == 's')
{
if(cas == 0)
{
bool ok = 1;
for(int i = 0; i < 3; i++)
if(midy - 1 < 1 || maz[midx + i][midy - 1])
{
ok = 0;
break;
}
if(!ok);
else
{
for(int i = 0; i < 3; i++)
maz[midx + i][midy - 1] = 1;
maz[midx + 1][midy] = maz[midx + 2][midy] = maz[midx][midy + 1] = 0;
midy--;
}
}
else if(cas == 1)
{
if(midy - 1 < 1 || maz[midx][midy - 1] || maz[midx + 1][midy + 1]);
else
{
maz[midx][midy - 1] = maz[midx + 1][midy + 1] = 1;
maz[midx][midy + 2] = maz[midx + 1][midy + 2] = 0;
midy--;
}
}
else if(cas == 2)
{
if(midy - 1 < 1 || maz[midx + 2][midy - 1] || maz[midx][midy] || maz[midx + 1][midy]);
else
{
maz[midx + 2][midy - 1] = maz[midx][midy] = maz[midx + 1][midy] = 1;
maz[midx][midy + 1] = maz[midx + 1][midy + 1] = maz[midx + 2][midy + 1] = 0;
midy--;
}
}
else if(cas == 3)
{
if(midy - 1 < 1 || maz[midx][midy - 1] || maz[midx + 1][midy - 1]);
else
{
maz[midx][midy - 1] = maz[midx + 1][midy - 1] = 1;
maz[midx][midy] = maz[midx + 1][midy + 2] = 0;
midy--;
}
}
}
now++;
if(4)
{
if(cas == 0)
{
bool ok = 1;
for(int i = 0; i < 3; i++)
if(midy - 1 < 1 || maz[midx + i][midy - 1])
{
ok = 0;
break;
}
if(!ok)
break;
else
{
for(int i = 0; i < 3; i++)
maz[midx + i][midy - 1] = 1;
maz[midx + 1][midy] = maz[midx + 2][midy] = maz[midx][midy + 1] = 0;
midy--;
}
}
else if(cas == 1)
{
if(midy - 1 < 1 || maz[midx][midy - 1] || maz[midx + 1][midy + 1])
break;
else
{
maz[midx][midy - 1] = maz[midx + 1][midy + 1] = 1;
maz[midx][midy + 2] = maz[midx + 1][midy + 2] = 0;
midy--;
}
}
else if(cas == 2)
{
if(midy - 1 < 1 || maz[midx + 2][midy - 1] || maz[midx][midy] || maz[midx + 1][midy])
break;
else
{
maz[midx + 2][midy - 1] = maz[midx][midy] = maz[midx + 1][midy] = 1;
maz[midx][midy + 1] = maz[midx + 1][midy + 1] = maz[midx + 2][midy + 1] = 0;
midy--;
}
}
else if(cas == 3)
{
if(midy - 1 < 1 || maz[midx][midy - 1] || maz[midx + 1][midy - 1])
break;
else
{
maz[midx][midy - 1] = maz[midx + 1][midy - 1] = 1;
maz[midx][midy] = maz[midx + 1][midy + 2] = 0;
midy--;
}
}
}
}
disappear();
}
void start()
{
init();
for(int i = 1; i <= N; i++)
{
switch(type[i])
{
case 0: OO(); break;
case 1: II(); break;
case 2: JJ(); break;
}
}
}
int main()
{
int T;
scanf("%d", &T);
for(int cas = 1; cas <= T; cas++)
{
scanf("%d", &N);
scanf("%s", seq);
for(int i = 1; i <= N; i++)
scanf("%d", &type[i]);
LS = strlen(seq);
now = 0;
ans = 0;
start();
printf("Case %d: %d\n", cas, ans);
}
return 0;
}

37
HDOJ/5375_autoAC.cpp Normal file
View File

@ -0,0 +1,37 @@
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std ;
const int maxn = 2e5+10 ;
const int inf = 0x3f3f3f3f ;
int dp[maxn][2] ;
char str[maxn] ;
int a[maxn] ;
int main()
{
int t ;int cas = 0 ;
scanf("%d" , &t) ;
while(t--)
{
scanf("%s" , str) ;
int n = strlen(str) ;
for(int i = 0 ;i < n;i++)
scanf("%d" , &a[i]) ;
dp[0][1] = dp[0][0] = -inf ;
if(str[0] == '1' || str[0] == '?')
dp[0][1] = a[0] ;
if(str[0] == '0' || str[0] == '?')
dp[0][0] = 0 ;
for(int i = 1;i < n;i++)
{
dp[i][0] = dp[i][1] = -inf ;
if(str[i] == '0' || str[i] == '?')
dp[i][0] = max(dp[i-1][0] , dp[i-1][1] + a[i]) ;
if(str[i] == '1' || str[i] == '?')
dp[i][1] = max(dp[i-1][1] , dp[i-1][0] + a[i]) ;
}
printf("Case #%d: " , ++cas) ;
cout<<max(dp[n-1][0] , dp[n-1][1])<<endl;
}
return 0 ;
}

212
HDOJ/5377_autoAC.cpp Normal file
View File

@ -0,0 +1,212 @@
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <map>
#include <tr1/unordered_map>
#define clr(x, y) memset(x, y, sizeof x)
using namespace std;
typedef long long LL;
const double eps=1e-8;
const int maxn=100100;
const int mod=1e6+10;
LL prime[maxn];
LL X[maxn],Y[maxn];
LL A[maxn],B[maxn];
LL ans[maxn];
LL M;
LL get_prime(LL n)
{
LL cnt=0;
for(LL i=2;i*i<=n;i++)
{
if(n%i==0)
{
prime[cnt++]=i;
while(n%i==0)
n/=i;
}
}
if(n!=1)
prime[cnt++]=n;
return cnt;
}
LL pow_mod(LL a,LL p,LL n)
{
LL ans=1;
while(p)
{
if(p&1)
ans=(ans*a)%n;
p>>=1;
a=(a*a)%n;
}
return ans;
}
vector<LL>a;
bool g_test(LL g,LL p)
{
for(LL i=0;i<a.size();i++)
{
if(pow_mod(g,(p-1)/a[i],p)==1)
return 0;
}
return 1;
}
LL primitive_root(LL p)
{
LL tmp=p-1;
a.clear();
for(LL i=2;i<=tmp/i;i++)
{
if(tmp%i==0)
{
a.push_back(i);
while(tmp%i==0)
tmp/=i;
}
}
if(tmp!=1)
a.push_back(tmp);
LL g=1;
while(true)
{
if(g_test(g,p))
return g;
g++;
}
}
void gcd(LL a,LL b,LL &d,LL &x,LL &y)
{
if(!b) { d=a; x=1; y=0;}
else
{
gcd(b,a%b,d,y,x);
y-=x*(a/b);
}
}
LL inv(LL a,LL n)
{
LL d,x,y;
gcd(a,n,d,x,y);
return d==1 ? (x+n)%n : -1;
}
struct HashTable
{
int top,head[mod];
struct Node
{
int x,y,next;
}node[mod];
void init()
{
top=0;
memset(head,0,sizeof(head));
}
void insert(LL x,LL y)
{
node[top].x=x;
node[top].y=y;
node[top].next=head[x%mod];
head[x%mod]=top++;
}
LL query(LL x)
{
for(int tx=head[x%mod];tx;tx=node[tx].next)
{
if(node[tx].x==x)
return node[tx].y;
}
return -1;
}
}hs;
void log_mod(LL a,LL n)
{
LL m,v,e=1;
m=mod;
v=inv(pow_mod(a,m,n),n);
hs.init();
for(int i=0;i<m;i++)
{
hs.insert(e,i);
e=e*a%n;
}
for(int j=0;j<M;j++)
{
LL b=X[j]%n;
for(int i=0;i<m;i++)
{
LL s=hs.query(b);
if(s!=-1)
{
A[j]=i*m+s;
break;
}
b=b*v%n;
}
b=Y[j]%n;
for(int i=0;i<=n/m;i++)
{
LL s=hs.query(b);
if(s!=-1)
{
B[j]=i*m+s;
break;
}
b=b*v%n;
}
}
}
LL get_K(LL a,LL b,LL c)
{
LL d,x,y;
gcd(a,b,d,x,y);
if(c%d!=0)
return -1;
x=x*c/d;
b=b/d;
x=x-(x/b*b);
if(x<0)
x+=b;
return x;
}
int main()
{
int T;
LL sum;
scanf("%d",&T);
for(int t=1;t<=T;t++)
{
memset(ans,-1,sizeof(ans));
scanf("%I64d %I64d",&sum,&M);
printf("Case #%d:\n",t);
for(int i=0;i<M;i++)
{
scanf("%I64d %I64d",&X[i],&Y[i]);
}
LL cnt=get_prime(sum);
LL temp;
for(int j=0;j<cnt;j++)
{
LL d=primitive_root(prime[j]);
log_mod(d,prime[j]);
for(int i=0;i<M;i++)
{
temp=get_K(A[i],prime[j]-1,B[i]);
if(temp!=-1)
{
if(ans[i]==-1)
ans[i]=temp;
else
ans[i]=min(temp,ans[i]);
}
}
}
for(int i=0;i<M;i++)
printf("%I64d\n",ans[i]);
}
return 0;
}

121
HDOJ/5378_autoAC.cpp Normal file
View File

@ -0,0 +1,121 @@
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
using namespace std;
template <class T>
inline bool rd(T &ret) {
char c; int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c<'0' || c>'9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
template <class T>
inline void pt(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) pt(x / 10);
putchar(x % 10 + '0');
}
typedef pair<int, int> pii;
typedef long long ll;
const int N = 1005;
const int mod = 1e9 + 7;
const int inf = 1e9;
int Pow(int x, int y) {
int ans = 1;
while (y) {
if (y & 1)ans = (ll)ans*x%mod;
y >>= 1;
x = (ll)x*x%mod;
}return ans;
}
vector<int>G[N];
int n, k;
int dp[N][N], siz[N], lef[N];
int A[N];
int mul(int x, int y) {
x = (ll)x*y%mod;
return x;
}
inline void add(int &x, int y) {
x += y; if (x >= mod)x -= mod;
}
inline void sub(int &x, int y) {
x -= y; if (x < 0)x += mod;
}
inline void dv(int &x, int y) {
x = (ll)x*Pow(y, mod - 2) % mod;
}
int g[N];
void dfs(int u, int fa) {
siz[u] = 1; lef[u] = 0;
for (auto v : G[u]) {
if (v == fa)continue;
dfs(v, u);
siz[u] += siz[v];
}
if (siz[u] == 1) {
dp[u][0] = 0; dp[u][1] = 1;
lef[u] = 1;
return;
}
dp[u][0] = 1;
int x1 = A[siz[u] - 1], x2 = A[siz[u]];
siz[u] = 0;
for (auto v : G[u]) {
if (v == fa)continue;
for (int i = lef[u] + lef[v]; i <= min(k, siz[u] + siz[v]); i++)g[i] = 0;
for (int i = lef[u]; i <= min(k, siz[u]); i++)
{
for (int j = lef[v]; j <= min(k, siz[v]) && i + j <= k; j++)
{
add(g[i + j], mul(dp[v][j], dp[u][i]));
}
}
for (int i = lef[u] + lef[v]; i <= min(k, siz[u] + siz[v]); i++)dp[u][i] = g[i];
siz[u] += siz[v];
lef[u] += lef[v];
dv(x1, A[siz[v]]);
dv(x2, A[siz[v]]);
}
siz[u]++;
sub(x2, x1);
for (int i = min(siz[u], k); i >= lef[u]; i--) {
int tmp = 0;
add(tmp, mul(dp[u][i], x2));
if (i - 1 >= lef[u])
add(tmp, mul(dp[u][i - 1], x1));
dp[u][i] = tmp;
}
}
int main() {
A[0] = 1;
for (int i = 1; i < N; i++)A[i] = (ll)A[i - 1] * i%mod;
int T, Cas = 1; rd(T);
while (T--) {
rd(n); rd(k);
for (int i = 1; i <= n; i++)G[i].clear(), memset(dp[i], 0, sizeof dp[i]);
for (int i = 1, u, v; i < n; i++) {
rd(u); rd(v);
G[u].push_back(v); G[v].push_back(u);
}
dfs(1, 1);
printf("Case #%d: ", Cas++);
pt(dp[1][k]); puts("");
}
return 0;
}

72
HDOJ/5379_autoAC.cpp Normal file
View File

@ -0,0 +1,72 @@
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define Max 100010
const int MOD = 1e9+7;
vector <int > q[Max];
int vis[Max];
long long res=1;
int flag=1;
int n;
void dfs(int a)
{
int k=q[a].size();
int cnt=0;
vis[a]=1;
for(int i=0;i<k;i++)
{
if(vis[q[a][i]]) continue;
if(q[q[a][i]].size()==1&&vis[q[a][i]]==0) cnt++;
else dfs(q[a][i]);
}
if(a!=1)k--;
if(k-cnt>2){flag=0;return ;}
if(cnt!=k)
{
res*=2;
res%=MOD;
}
while(cnt>0)
{
res*=cnt;res%=MOD;cnt--;
}
//cout<<a<<" "<<res<<endl;
//cout<<res<<endl;
}
int main()
{
int T;
cin>>T;
int _case=0;
while(T--)
{
cin>>n;
flag=1;
for(int i=1;i<=n;i++) q[i].clear();
memset(vis,0,sizeof(vis));
int a,b;
for(int i=1;i<n;i++)
{
scanf("%d %d",&a,&b);
q[b].push_back(a);
q[a].push_back(b);
}
res=2;
if(n!=1)
dfs(1);
else res=1;
if(!flag) res=0;
cout<<"Case #"<<++_case<<": "<<res%MOD<<endl;
}
return 0;
}

92
HDOJ/5380_autoAC.cpp Normal file
View File

@ -0,0 +1,92 @@
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <queue>
#define mp make_pair
#define INF 1000000000
#define MAXN 200050
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define P pair<int,int>
#define LL long long
using namespace std;
int a[MAXN];
LL buy[MAXN],sell[MAXN];
int len[MAXN];
LL now[MAXN];
int q[MAXN];
void work(){
int st=0,ed=0;
int n,c;
scanf("%d%d",&n,&c);
for(int i=1;i<=n;++i)scanf("%d",&a[i]);
for(int i=0;i<=n;++i)scanf("%I64d%I64d",&buy[i],&sell[i]);
now[0]=c;
q[ed++]=0;
LL ans=c*buy[0];
for(int i=1;i<=n;++i){
int dis=a[i]-a[i-1];
LL sum=c-dis;
while(dis){
int x=q[st];
if(now[x]>dis){
now[x]-=dis;
dis=0;
}else{
dis-=now[x];
now[x]=0;
st++;
}
}
int era=0;
while(st<ed){
int x=q[st];
if(buy[x]<sell[i]){
era+=now[x];
st++;
}else
break;
}
if(era!=0){
st--;
now[q[st]]=era;
buy[q[st]]=sell[i];
}
bool flag=true;
while(flag&&st<ed){
flag=false;
int x=q[ed-1];
if(buy[x]>=buy[i]){
flag=true;
ans-=now[x]*buy[x];
sum-=now[x];
now[x]=0;
ed--;
}
}
q[ed++]=i;
now[i]=c-sum;
ans+=(c-sum)*buy[i];
}
while(st<ed){
int x=q[st++];
ans-=now[x]*buy[x];
}
printf("%I64d\n",ans);
}
int main()
{
len[0]=len[1]=0;
for(int i=2;i<MAXN;++i){
int k=0;
while((1<<k)<=i)k++;
len[i]=k-1;
}
int tt;
scanf("%d",&tt);
while(tt--){
work();
}
}

136
HDOJ/5381_autoAC.cpp Normal file
View File

@ -0,0 +1,136 @@
#include <stdio.h>
#include <algorithm>
#include <math.h>
const int N = (int)(1e4)+5;
int t,n,m,a[N],ppos[N];
long long ans[N];
int v1[N][35], v2[N][35], v3[N][35], v4[N][35];
int size1[N], size2[N];
struct Q{
int l,r,id;
bool operator < (const Q &a ) const{
if(ppos[l] == ppos[a.l]) return r<a.r;
return ppos[l] < ppos[a.l];
}
}q[N];
int gcd(int a, int b){
return !b?a:gcd(b, a%b);
}
int pool[50], pos[50], cnt;
void unique(int &cnt){
int id = 0, p = pos[0];
for(int i=0; i<cnt; i++){
if(pool[i] != pool[id]){
pos[id] = p; id++;
p = pos[i]; pool[id] = pool[i];
}
}
pos[id] = p;
cnt = id+1;
}
void init(){
cnt = 0;
for(int i=n; i>=1; i--){
for(int j=0; j<cnt; j++) pool[j] = gcd(pool[j], a[i]);
pool[cnt] = a[i]; pos[cnt++]=i;
unique(cnt);
for(int j=cnt-1; j>=0; j--){
v1[i][cnt-1-j] = pool[j];
v2[i][cnt-1-j] = pos[j];
}
size1[i] = cnt;
}
cnt = 0;
for(int i=1; i<=n; i++){
for(int j=0; j<cnt; j++) pool[j] = gcd(pool[j], a[i]);
pool[cnt] = a[i]; pos[cnt++] = i;
unique(cnt);
for(int j=cnt-1; j>=0; j--){
v3[i][cnt-1-j] = pool[j];
v4[i][cnt-1-j] = pos[j];
}
size2[i] = cnt;
}
}
int l,r;
long long sum;
void add_l(int v){
int s = l, t = r;
long long tmp = 0;
int last = s;
for(int i=0; i<size1[l]; i++){
if(v2[l][i] < s) continue;
else if(v2[l][i] > t){
tmp += (t - last + 1) *1LL *v1[l][i];
}
else{
tmp += (v2[l][i] - last + 1) *1LL* v1[l][i];
last = v2[l][i] + 1;
}
if(v2[l][i] >= t) break;
}
sum += v*tmp;
}
void add_r(int v){
int s = l, t = r;
long long tmp = 0;
int last = t;
for(int i=0; i<size2[r]; i++){
if(v4[r][i] > t) continue;
else if(v4[r][i] < s){
tmp += (last - s + 1) * 1LL * v3[r][i];
}else{
tmp += (last - v4[r][i] + 1) *1LL * v3[r][i];
last = v4[r][i]-1;
}
if(v4[t][i] <= s) break;
}
sum += v*tmp;
}
int main(){
scanf("%d", &t);
for(int ca=1; ca<=t; ca++){
scanf("%d", &n);
int S = (int)(sqrt(n*1.0) + 0,5);
for(int i=1; i<=n; i++){
ppos[i] = (i-1)/100;
}
for(int i=1; i<=n; i++) scanf("%d", &a[i]);;
init(); scanf("%d", &m);
for(int i=0; i<m; i++){
scanf("%d%d", &q[i].l, &q[i].r);
q[i].id = i;
}
std::sort(q, q+m);
sum = 0, l = 1, r = 0;
for(int i=0; i<m; i++){
if(r<q[i].r){
for(r=r+1; r<=q[i].r; r++){
add_r(1);
}
r--;
}
if(r>q[i].r){
for(;r>q[i].r; r--){
add_r(-1);
}
}
if(l>q[i].l){
for(l=l-1; l>=q[i].l; l--){
add_l(1);
}
l++;
}
if(l<q[i].l){
for(; l<q[i].l; l++){
add_l(-1);
}
}
ans[q[i].id] = sum;
}
for(int i=0; i<m; i++){
printf("%I64d\n", ans[i]);
}
}
return 0;
}

62
HDOJ/5382_autoAC.cpp Normal file
View File

@ -0,0 +1,62 @@
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <map>
#include <tr1/unordered_map>
#define clr(x, y) memset(x, y, sizeof x)
using namespace std;
typedef long long LL;
const double eps=1e-8;
const int maxn=1000100;
const int mod=258280327;
int valid[maxn];
LL G[maxn],T[maxn],F[maxn],S[maxn];
void init()
{
for(int i=0;i<maxn;i++)
{
G[i]=valid[i]=1;
T[i]=0;
}
G[0]=0;
for(int i=2;i<maxn;i++)
{
if(valid[i])
{
for(int j=i;j<maxn;j+=i)
{
G[j]=G[j]*2%mod;
valid[j]=0;
}
}
}
for(int i=1;i<maxn;i++)
{
for(int j=i;j<maxn;j+=i)
{
T[j]=(T[j]+G[j/i-1])%mod;
}
}
F[1]=S[1]=1;
for(int i=2;i<maxn;i++)
{
F[i]=(((F[i-1]+2*i-1)%mod-T[i-1])%mod+mod)%mod;
S[i]=(S[i-1]+F[i])%mod;
}
}
int main()
{
init();
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
printf("%I64d\n",S[n]);
}
return 0;
}

127
HDOJ/5383_autoAC.cpp Normal file
View File

@ -0,0 +1,127 @@
#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 805
typedef long long LL;
const LL INF = 1000000007;
struct edge{
int to,next,cost,cap;
}E[2000000];
struct node{
int lev,tun,atk;
}A[MAXN];
int head[MAXN],si,prevv[MAXN],preve[MAXN],que[200000],n,m,used[MAXN],C[400][400];
LL dist[MAXN];
void add_edge(int u,int v,int c,int cap)
{
//printf("%d->%d(cap:%d,cost:%d)\n",u,v,cap,c);
E[si].to=v;
E[si].cost=c;
E[si].cap=cap;
E[si].next=head[u];
head[u]=si++;
E[si].to=u;
E[si].cost=-c;
E[si].cap=0;
E[si].next=head[v];
head[v]=si++;
}
bool SPFA(int s,int t)
{
for(int i=0;i<=2*n+1;i++)dist[i]=INF,used[i]=false;
dist[s]=0;
int h=0,tail=0;
for(que[tail++]=s,used[s]=true;h<tail;h++){
int v=que[h];used[v]=false;
for(int i=head[v];~i;i=E[i].next){
if(E[i].cap>0){
int u=E[i].to;
if(dist[u]>dist[v]+E[i].cost){
// printf("dist[%d]=%I64d___dist[%d]=%I64d___cost:%d\n",u,dist[u],v,dist[v],E[i].cost);
dist[u]=dist[v]+E[i].cost;
prevv[u]=v;
preve[u]=i;
if(!used[u]){
que[tail++]=u;
used[u]=true;
}
}
}
}
}
// printf("dist:%I64d\n",dist[t]);
if(dist[t]<0)return true;
return false;
}
int min_cost_flow(int s,int t)
{
LL ans=0;
while(SPFA(s,t)){
int d=INF;
for(int v=t;s!=v;v=prevv[v]){
d=std::min(d,E[preve[v]].cap);
}
ans+=d*dist[t];
for(int v=t;v!=s;v=prevv[v]){
E[preve[v]].cap-=d;
E[preve[v]^1].cap+=d;
}
}
return ans;
}
int main()
{
int T;scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
memset(head,-1,sizeof head);si=0;
memset(C,0,sizeof C);
for(int i=1;i<=n;i++){
scanf("%d%d%d",&A[i].tun,&A[i].lev,&A[i].atk);
add_edge(0,i,0,1);
add_edge(i,i+n,-A[i].atk,1);
add_edge(i+n,n*2+1,0,1);
}
for(int i=1,lev,atk,r,u,v;i<=m;i++){
scanf("%d%d%d",&lev,&atk,&r);
if(r==0){
for(int j=1;j<=n;j++){
if(!A[j].tun)
for(int k=1;k<=n;k++)if(A[k].tun&&A[j].lev+A[k].lev==lev){
C[j][k]=std::min(C[j][k],-atk);
}
}
}
else if(r==1){
scanf("%d",&v);
if(A[v].tun){
for(int j=1;j<=n;j++)if(!A[j].tun&&A[j].lev+A[v].lev==lev){
// add_edge(j,v+n,-atk,1);
C[j][v]=std::min(C[j][v],-atk);
}
}
else{
for(int j=1;j<=n;j++)if(A[j].tun&&A[j].lev+A[v].lev==lev){
add_edge(v,j+n,-atk,1);
C[v][j]=std::min(C[v][j],-atk);
}
}
}
else{
scanf("%d%d",&u,&v);
if(A[u].tun)std::swap(u,v);
C[u][v]=std::min(C[u][v],-atk);
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++)if(C[i][j]<0&&C[i][j]<-A[i].atk-A[j].atk){
add_edge(i,j+n,C[i][j],1);
}
}
LL ans=-min_cost_flow(0,n*2+1);
printf("%I64d\n",ans);
}
return 0;
}

69
HDOJ/5384_autoAC.cpp Normal file
View File

@ -0,0 +1,69 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
typedef long long ll;
const int MAXN = (int)1e5 + 10;
const int maxnode = (int)6e5 + 10;
const int sigma_size = 26;
struct Trie {
int ch[maxnode][sigma_size];
int val[maxnode];
int sz;
void clear() { sz = 1; memset(ch[0], 0, sizeof(ch[0]));}
Trie() {clear();}
int idx(char c) { return c - 'a';}
void insert(char *s, int v = 1) {
int u = 0, n = strlen(s);
for(int i = 0; i < n; i++) {
int c = idx(s[i]);
if(!ch[u][c]) {
memset(ch[sz], 0, sizeof(ch[u]));
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] += v;
}
ll find(const char* s) {
int u = 0, n = strlen(s);
ll ret = 0;
for(int i = 0; i < n; i++) {
int c = idx(s[i]);
if(!ch[u][c]) return ret;
u = ch[u][c];
ret += val[u];
}
return ret;
}
} trie;
int n, m;
string A[MAXN];
char B[MAXN];
int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
trie.clear();
for(int i = 0; i < n; i++) {
cin >> A[i];
}
for(int i = 0; i < m; i++) {
scanf("%s", B);
trie.insert(B);
}
ll ans = 0;
for(int i = 0; i < n; i++) {
ans = 0;
for(int j = 0; j < A[i].size(); j++) {
ans += trie.find(A[i].c_str()+j);
}
printf("%lld\n", ans);
}
}
return 0;
}

71
HDOJ/5385_autoAC.cpp Normal file
View File

@ -0,0 +1,71 @@
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1 | 1
#define lowbit(x) x&(-x)
using namespace std;
const int N=1e5+10;
long long inf=1e15;
const int MOD=258280327;
int T,n,m,k,x,y,z,l,tot,t,A,B,q;
int cas=1;
int u[N],v[N];
vector<int> vec[N];
bool vis[N];
int dis[N];
int fa[N];
void relax(int x){
for(int i=0;i<vec[x].size();i++){
int v1=vec[x][i];
if(!vis[v1] && !fa[v1]) fa[v1]=x;
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) vec[i].clear();
for(int i=1;i<=n;i++) vis[i]=false,fa[i]=0;
for(int i=1;i<=m;i++){
scanf("%d%d",u+i,v+i);
vec[u[i]].push_back(v[i]);
}
vis[1]=true;
dis[1]=0;
fa[1]=-1;
int now=1;
int pre=2,last=n;
relax(1);
while(pre<=last){
if(fa[pre]){
dis[pre]=now++;
vis[pre]=true;
relax(pre);
pre++;
continue;
}
if(fa[last]){
dis[last]=now++;
vis[last]=true;
relax(last);
last--;
continue;
}
}
for(int i=1;i<=m;i++){
if(fa[v[i]]!=u[i]) printf("%d\n",n);
else printf("%d\n",dis[v[i]]-dis[u[i]]);
}
}
return 0;
}

66
HDOJ/5386_autoAC.cpp Normal file
View File

@ -0,0 +1,66 @@
#include "stdio.h"
#include "string.h"
struct Mark
{
int x,y;
char op;
}mark[510];
int a[110][110],ans[510];
int main()
{
int t,n,m,i,cnt,j,k;
char str[2];
scanf("%d",&t);
while (t--)
{
scanf("%d%d",&n,&m);
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);
for (i=1;i<=m;i++)
{
scanf("%s%d%d",str,&mark[i].x,&mark[i].y);
mark[i].op=str[0];
}
cnt=m;
while (cnt!=0)
{
for (i=1;i<=m;i++)
if (mark[i].x!=0)
{
k=mark[i].x;
if (mark[i].op=='H')
{
for (j=1;j<=n;j++)
if (a[k][j]!=0 && a[k][j]!=mark[i].y) break;
if (j==n+1)
{
for (j=1;j<=n;j++)
a[k][j]=0;
mark[i].x=0;
ans[cnt--]=i;
}
}
else
{
for (j=1;j<=n;j++)
if (a[j][k]!=0 && a[j][k]!=mark[i].y) break;
if (j==n+1)
{
for (j=1;j<=n;j++)
a[j][k]=0;
mark[i].x=0;
ans[cnt--]=i;
}
}
}
}
for (i=1;i<=m;i++)
printf("%d ",ans[i]);
printf("\n");
}
return 0;
}

50
HDOJ/5387_autoAC.cpp Normal file
View File

@ -0,0 +1,50 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <algorithm>
using namespace std;
int gcd(int u,int v)
{
if(u<v) swap(u,v);
return v==0?u:gcd(v,u%v);
}
int abs(int x)
{
if(x<0) return -x;
else return x;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int hh,mm,ss;
scanf("%d:%d:%d",&hh,&mm,&ss);
int ts,tm,th;
ts=6*ss;
tm=60*mm+ss;
th=3600*(hh%12)+60*mm+ss;
int difms=abs(10*ts-tm);
int difhm=abs(12*tm-th);
int difhs=abs(120*ts-th);
if(difms>10*180) difms=10*360-difms;
if(difhm>120*180) difhm=120*360-difhm;
if(difhs>120*180) difhs=120*360-difhs;
if(difhm%120==0)
printf("%d ",difhm/120);
else
printf("%d/%d ",difhm/gcd(difhm,120),120/gcd(difhm,120));
if(difhs%120==0)
printf("%d ",difhs/120);
else
printf("%d/%d ",difhs/gcd(difhs,120),120/gcd(difhs,120));
if(difms%10==0)
printf("%d \n",difms/10);
else
printf("%d/%d \n",difms/gcd(difms,10),10/gcd(difms,10));
}
return 0;
}

67
HDOJ/5389_autoAC.cpp Normal file
View File

@ -0,0 +1,67 @@
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std;
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 258280327;
int cal(int x, int y)
{
int ret = x + y;
ret %= 9;
if(ret == 0) return 9;
return ret;
}
int dp[MAXN][10];
int a[MAXN];
int main(){
int T;
int n, s1, s2;
scanf("%d", &T);
while(T--){
scanf("%d%d%d", &n, &s1, &s2);
int sum = 0;
for(int i = 1; i <= n; i++){
scanf("%d", &a[i]);
sum = cal(sum, a[i]);
}
int ret = cal(s1, s2);
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= n; i++){
dp[i][a[i]] = 1;
}
for(int i = 1; i <= n; i++){
for(int j = 0; j <= 9; j++){
dp[i][j] = (dp[i][j] + dp[i-1][j]) % MOD;
int x = cal(a[i], j);
dp[i][x] = (dp[i][x] + dp[i-1][j]) % MOD;
}
}
int ans = 0;
if(ret == sum) {
ans += dp[n][s1];
if(s1 == sum) ans--;
}
if(s1 == sum) ans++;
if(s2 == sum) ans++;
printf("%d\n", ans);
}
return 0;
}

203
HDOJ/5390_autoAC.cpp Normal file
View File

@ -0,0 +1,203 @@
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#define maxn 100005
using namespace std;
struct OP
{
int op, id, x;
}op[maxn];
struct Seg
{
int l, r;
int root;
Seg() {}
Seg(int _l, int _r, int _ro)
{
l = _l;
r = _r;
root = _ro;
}
};
vector<Seg> T[20];
void build(int L, int R, int dep)
{
T[dep].push_back(Seg(L, R, 0));
if (L != R)
{
int mid = L + R >> 1;
build(L, mid, dep + 1);
build(mid + 1, R, dep + 1);
}
}
int ch[4000000][2];
int num[4000000];
int cnt;
void insert_Dic(int root, int x)
{
num[root]++;
for (int i = 30; i >= 0; i--)
{
int c = (x&(1 << i)) ? 1 : 0;
if (!ch[root][c]) ch[root][c] = ++cnt;
root = ch[root][c];
num[root]++;
}
}
void erase_Dic(int root, int x)
{
num[root]--;
for (int i = 30; i >= 0; i--)
{
int c = (x&(1 << i)) ? 1 : 0;
root = ch[root][c];
num[root]--;
}
}
int find_Dic(int root, int x)
{
int res = 0;
for (int i = 30; i >= 0; i--)
{
int c = (x&(1 << i)) ? 1 : 0;
if (ch[root][c ^ 1] && num[ch[root][c ^ 1]])
{
res |= 1 << i;
root = ch[root][c ^ 1];
}
else root = ch[root][c];
}
return res;
}
vector<Seg> Q;
void Select(int L, int R, int l, int r, int dep, int deep)
{
if (dep > deep) return;
if (l <= L&&R <= r)
{
if(dep==deep)
Q.push_back(Seg(L, R, 0));
}
else
{
int mid = L + R >> 1;
if (l <= mid) Select(L, mid, l, r, dep + 1, deep);
if (mid < r) Select(mid + 1, R, l, r, dep + 1, deep);
}
}
struct EDGE
{
int to, next;
EDGE() {}
EDGE(int _t, int _n) { to = _t, next = _n; }
}edge[maxn];
int head[maxn], edgecnt;
void init_edge()
{
memset(head, -1, sizeof(head));
edgecnt = 0;
}
void add(int s, int t)
{
edge[edgecnt] = EDGE(t, head[s]);
head[s] = edgecnt++;
}
int dfs_clk;
int Left[maxn], Right[maxn];
void dfs(int u)
{
Left[u] = ++dfs_clk;
for (int i = head[u]; ~i; i = edge[i].next)
dfs(edge[i].to);
Right[u] = dfs_clk;
}
int val[maxn], _val[maxn];
int ans[maxn];
int bin(int idx, int x)
{
int L = 0, R = T[idx].size();
while (L < R)
{
int mid = (L + R) >> 1;
if (T[idx][mid].l <= x) L = mid + 1;
else R = mid;
}
return L - 1;
}
int main()
{
int Ts;
scanf("%d", &Ts);
while (Ts--)
{
init_edge();
int n, m;
scanf("%d %d", &n, &m);
for (int i = 2; i <= n; i++)
{
int fa;
scanf("%d", &fa);
add(fa, i);
}
dfs_clk = 0;
dfs(1);
for (int i = 1; i <= n; i++) scanf("%d", &val[i]);
int Qc = 0;
for (int i = 1; i <= m; i++)
{
scanf("%d %d", &op[i].op, &op[i].id);
if (op[i].op == 0) scanf("%d", &op[i].x);
else Qc++;
}
for (int i = 0; i < 20; i++) T[i].clear();
build(1, dfs_clk, 0);
memset(ans, 0, sizeof(ans));
for (int i = 19; i >= 0; i--)
if (T[i].size())
{
int Tsize = T[i].size();
memcpy(_val, val, sizeof(_val));
memset(ch, 0, sizeof(ch));
memset(num, 0, sizeof(num));
cnt = 0;
for (int j = 0; j < Tsize; j++) T[i][j].root = ++cnt;
for (int j = 1; j <= n; j++)
{
Q.clear();
Select(1, dfs_clk, Left[j], Right[j], 0, i);
for (int k = 0; k < Q.size(); k++)
{
int pos = bin(i, Q[k].l);
insert_Dic(T[i][pos].root, _val[j]);
}
}
int quecnt = 0;
for (int j = 1; j <= m;j++)
if (op[j].op == 0)
{
Q.clear();
Select(1, dfs_clk, Left[op[j].id], Right[op[j].id], 0, i);
for (int k = 0; k < Q.size(); k++)
{
int pos = bin(i, Q[k].l);
erase_Dic(T[i][pos].root, _val[op[j].id]);
insert_Dic(T[i][pos].root, op[j].x);
}
_val[op[j].id] = op[j].x;
}
else
{
quecnt++;
int pos = bin(i, Left[op[j].id]);
if (pos >= 0 && T[i][pos].l <= Left[op[j].id] && Left[op[j].id] <= T[i][pos].r)
{
ans[quecnt] = max(ans[quecnt], find_Dic(T[i][pos].root, _val[op[j].id]));
}
}
}
for (int i = 1; i <= Qc; i++) printf("%d\n", ans[i]);
}
return 0;
}

40
HDOJ/5391_autoAC.cpp Normal file
View File

@ -0,0 +1,40 @@
#include<iostream>
#include<cstring>
#include<cstdio>
#include<math.h>
#include<algorithm>
using namespace std;
bool isPrimeNum(int num)
{
if (num <= 2)
{
return num == 2;
}
if (num % 2 == 0)
{
return false;
}
int iSqrt = sqrt(num);
for (int i = 3; i <= iSqrt; i+=2)
{
if (num % i == 0)
{
return false;
}
}
return true;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
if(n==4) printf("2\n");
else if(isPrimeNum(n)) printf("%d\n",n-1);
else printf("0\n");
}
return 0;
}

96
HDOJ/5392_autoAC.cpp Normal file
View File

@ -0,0 +1,96 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
using namespace std;
#define ll unsigned long long
#define maxn 3000007
int check[maxn];
vector<int> pri;
void init(){
memset(check,0,sizeof(check));
pri.clear();
for(int i = 2;i < maxn; i++){
if(check[i] == 0) {
pri.push_back(i);
}
for(int j = 0;j < pri.size() ; j++){
if(i*pri[j] >= maxn) break;
check[i*pri[j]] = 1;
if(i%pri[j] == 0) break;
}
}
}
int getn(){
int a = 0;
char x;
do{
x = getchar();
if(x == ' ' || x == '\n') return a;
a = a*10+x-'0';
}while(1);
return 1;
}
int num[maxn];
int ok[maxn];
vector<int> hehe;
ll mod = 3221225473ll;
int pnum[maxn];
ll cal(int u,int p){
ll ans = 1;
ll v = u;
while(p){
if(p & 1) ans = ans*v % mod;
p/=2;
v = v*v%mod;
}
return ans;
}
int main(){
init();
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
getchar();
for(int i = 1;i <= n; i++)
num[i] = getn();
for(int i = 1;i <= n; i++)
ok[i] = pnum[i] = 0;
hehe.clear();
int mu = 1;
for(int i = 1;i <= n; i++){
if(ok[i] != 1){
int u = i;
int x = 0;
while(ok[u] == 0){
x++;
ok[u] = 1;
u = num[u];
}
hehe.push_back(x);
}
}
for(int i = 0;i < hehe.size(); i++){
for(int j = 0;j < pri.size() && pri[j] <= hehe[i]; j++){
if(hehe[i] % pri[j] == 0){
int x = 0;
while(hehe[i] % pri[j] == 0){
hehe[i] /= pri[j];
x++;
}
pnum[pri[j]] = max(pnum[pri[j]],x);
mu = max(mu,pri[j]);
}
}
}
ll ans = 1;
for(int i = 1;i <= mu; i++){
if(pnum[i] != 0)
ans = ans*cal(i,pnum[i])%mod;
}
printf("%I64d\n",ans);
}
return 0;
}

82
HDOJ/5393_autoAC.cpp Normal file
View File

@ -0,0 +1,82 @@
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long Int;
int getphi(int x)
{
int ret=1;
for(int i=2;i*(Int)i<=x;i++)
if(x%i==0)
{
ret=ret*(i-1);
x/=i;
while(x%i==0)x/=i,ret*=i;
}
if(x>1)ret=ret*(x-1);
return ret;
}
int powmod(int x,int y,int mod)
{
int ret=1;
while(y){if(y&1)ret=ret*(Int)x%mod;y>>=1;x=x*(Int)x%mod;}
return ret;
}
int check(int x,int y,int mod)
{
vector<int>V;
while(y){V.push_back(y&1);y>>=1;}
reverse(V.begin(),V.end());
int ret=0,tp=1;
for(int i=0;i<V.size();i++)
{
ret=(ret+ret*(Int)tp%mod)%mod;
tp=tp*(Int)tp%mod;
if(V[i])
{
tp=tp*(Int)x%mod;
ret=(ret+tp)%mod;
}
}
return (ret+1)%mod;
}
int main()
{
int _;scanf("%d",&_);
while(_--)
{
int k,b,x,p;
scanf("%d%d%d%d",&k,&b,&x,&p);
if(!k){puts(b==x?"1":"-1");continue;}
if(k==1)
{
if(!b)puts("1");
else
{
printf("%d\n",p/__gcd(b,p));
}
continue;
}
int q=p/__gcd((Int)p,(Int)(k-1)*x+b);
if(q==1){puts("1");continue;}
if(__gcd(k,q)!=1){puts("-1");continue;}
int t=getphi(q);
int rep=t;
for(int i=2;i*(Int)i<=t;i++)
{
if(t%i==0)
{
if(powmod(k,i,q)==1){rep=i;break;}
if(powmod(k,t/i,q)==1)rep=t/i;
}
}
int ans=check(k,rep-1,q);
if(!ans){printf("%d\n",rep);continue;}
printf("%lld\n",q/__gcd(ans,q)*(Int)rep);
}
}

45
HDOJ/5396_autoAC.cpp Normal file
View File

@ -0,0 +1,45 @@
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 110;
const LL mod = 1000000007;
LL dp[maxn][maxn],c[maxn][maxn] = {1},f[maxn] = {1};
void init() {
for(int i = 1; i < maxn; ++i) {
c[i][0] = c[i][i] = 1;
f[i] = f[i-1]*i%mod;
for(int j = 1; j < i; ++j)
c[i][j] = (c[i-1][j-1] + c[i-1][j])%mod;
}
}
char op[maxn];
int main() {
init();
int n;
while(~scanf("%d",&n)) {
memset(dp,0,sizeof dp);
for(int i = 0; i < n; ++i)
scanf("%I64d",&dp[i][i]);
scanf("%s",op);
for(int i = 2; i <= n; ++i) {
for(int j = 0; j + i <= n; ++j) {
for(int k = j,t = j + i -1; k < t; ++k) {
LL tmp;
if(op[k] == '+')
tmp = (f[t-k-1]*dp[j][k] + f[k-j]*dp[k+1][t])%mod;
else if(op[k] == '-') {
tmp = (f[t-k-1]*dp[j][k] - f[k-j]*dp[k+1][t])%mod;
tmp = (tmp + mod)%mod;
} else if(op[k] == '*') tmp = dp[j][k]*dp[k+1][t]%mod;
tmp = tmp*c[t-j-1][k-j]%mod;
dp[j][t] = (dp[j][t] + tmp + mod)%mod;
}
}
}
printf("%I64d\n",dp[0][n-1]);
}
return 0;
}

123
HDOJ/5398_autoAC.cpp Normal file
View File

@ -0,0 +1,123 @@
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <ctime>
#include <cassert>
#include <complex>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define ACCU accumulate
#define TWO(x) (1<<(x))
#define TWOL(x) (1ll<<(x))
#define clr(a) memset(a,0,sizeof(a))
#define POSIN(x,y) (0<=(x)&&(x)<n&&0<=(y)&&(y)<m)
#define PRINTC(x) cout<<"Case #"<<++__<<": "<<x<<endl
#define POP(x) (__builtin_popcount(x))
#define POPL(x) (__builtin_popcountll(x))
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long ll;
typedef long double LD;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef vector<ll> VL;
typedef vector<PII> VPII;
typedef complex<double> CD;
const int inf=0x20202020;
const ll mod=1000000007;
const double eps=1e-9;
const double pi=3.1415926535897932384626;
const int DX[]={1,0,-1,0},DY[]={0,1,0,-1};
ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll powmod(ll a,ll b,ll mod) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
const int N=101000;
struct node {
node *s[2],*f;
int val,d;
bool rev;
bool isr() { return !f||(f->s[0]!=this && f->s[1]!=this);}
bool dir() { return f->s[1]==this;}
void setc(node *c,int d) { s[d]=c;if (c) c->f=this;}
void push() {
if (rev) { swap(s[0],s[1]); rep(i,0,2) if (s[i]) s[i]->rev^=1;} rev=0;
}
void upd() {
val=d;
rep(i,0,2) if (s[i]&&s[i]->val<val) val=s[i]->val;
}
}pool[N],*cur;
stack<node*> sta;
void rot(node *x) {
node *p=x->f;bool d=x->dir();
if (!p->isr()) p->f->setc(x,p->dir()); else x->f=p->f;
p->setc(x->s[!d],d);x->setc(p,!d);
p->upd();
}
void splay(node *x) {
node *q=x;
while (1) { sta.push(q);if (q->isr()) break; q=q->f; }
while (!sta.empty()) sta.top()->push(),sta.pop();
while (!x->isr()) {
if (x->f->isr()) rot(x);
else if (x->isr()==x->f->isr()) rot(x->f),rot(x);
else rot(x),rot(x);
}
x->upd();
}
node *expose(node *x) {
node *q=NULL;
for (;x;x=x->f) splay(x),x->s[1]=q,(q=x)->upd();
return q;
}
void evert(node *x) { expose(x); splay(x); x->rev^=1; x->push();}
void expose(node *x,node *y) { evert(x); expose(y); splay(x);}
void link(node *x,node *y) { evert(x); evert(y); x->setc(y,1);}
void cut(node *x,node *y) { expose(x,y); x->s[1]=y->f=NULL;}
const int R=100000;
int ans,ret[N],n;
VI d[N];
int main() {
for (int i=2;i<=R;i++) for (int j=i+i;j<=R;j+=i) d[j].pb(i);
for (int i=1;i<=R;i++) {
pool[i].d=pool[i].val=i;
}
for (int i=2;i<=R;i++) {
link(pool+i,pool+1); ans+=1;
per(j,0,SZ(d[i])) {
int v=d[i][j];
expose(pool+i,pool+v);
int u=pool[i].val;
if (u>=v) continue;
ans=ans-u+v;
splay(pool+u);
if (pool[u].s[0]) pool[u].s[0]->f=NULL,pool[u].s[0]=NULL;
else if (pool[u].s[1]) pool[u].s[1]->f=NULL,pool[u].s[1]=NULL;
else assert(0);
link(pool+i,pool+v);
}
ret[i]=ans;
}
while (scanf("%d",&n)!=EOF) {
printf("%d\n",ret[n]);
}
}

46
HDOJ/5399_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const LL base = 1e9 + 7;
const int maxn = 105;
LL T, n, m, f[maxn], a[maxn][maxn];
inline void read(int &x)
{
char ch;
while ((ch = getchar())<'0' || ch>'9');
x = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') x = x * 10 + ch - '0';
}
int main()
{
for (int i = f[0] = 1; i <= 100; i++) f[i] = f[i - 1] * i % base;
while (scanf("%I64d%I64d", &n, &m) != EOF)
{
LL tot = 0, ans = 1;
for (int i = 1; i <= m; i++)
{
scanf("%I64d", &a[i][1]);
if (a[i][1] == -1) tot++;
else for (int j = 2; j <= n; j++)
{
scanf("%I64d", &a[i][j]);
for (int k = j - 1; k; k--) if (a[i][j] == a[i][k]) ans = 0;
}
}
for (int i = 1; i < tot; i++) ans = ans * f[n] % base;
if (tot == 0)
{
for (int i = 1; i <= n; i++) a[0][i] = i;
for (int i = m; i; i--)
for (int j = 1; j <= n; j++) a[0][j] = a[i][a[0][j]];
for (int i = 1; i <= n; i++) if (a[0][i] != i) ans = 0;
}
printf("%I64d\n", ans);
}
return 0;
}