diff --git a/HDOJ/3001_autoAC.cpp b/HDOJ/3001_autoAC.cpp new file mode 100644 index 0000000..4b69e98 --- /dev/null +++ b/HDOJ/3001_autoAC.cpp @@ -0,0 +1,60 @@ +#include +#include +#include +#include +using namespace std; +#define inf 0x3f3f3f3f +int State[12]; +int visited[60000][12]; +int dp[60000][12]; +int map[12][12]; +int n,m; +void Initiate() +{ + State[0]=1; + for(int i=1;i<=10;i++) + State[i]=State[i-1]*3; + for(int i=0;i<=State[10];i++){ + int tmp=i; + for(int j=0;j<=10;j++){ + visited[i][j]=tmp%3; + tmp/=3; + } + } +} +int main() +{ + Initiate(); + int u,v,w,ans; + while(~scanf("%d%d",&n,&m)){ + memset(dp,inf,sizeof(dp)); + memset(map,inf,sizeof(map)); + for(int i=0;i=2)continue; + if(map[j][k]==inf)continue; + dp[i+State[k]][k]=min(dp[i+State[k]][k],dp[i][j]+map[j][k]); + } + } + if(flag){ + for(int j=0;j +#include +const int N=105; +const int INF=1<<30; +int map[N][N]; +int wet[N]; +int vis[N]; +int combine[N]; +int S,T,mincut,n; +void search() +{ + int i,j,max,tmp; + memset(vis,0,sizeof(vis)); + memset(wet,0,sizeof(wet)); + S=T=-1; + for(i=0;imax) + { + tmp=j; + max=wet[j]; + } + } + if(T==tmp) return; + S=T;T=tmp; + mincut=max; + vis[tmp]=1; + for(j=0;j +#include +#include +#include +#include +#include +using namespace std; +__int64 cal(__int64 n) +{ + __int64 t=1; + __int64 ans=2; + __int64 m=n+1; + while(n!=1) + { + if(n&1) + { + t=t*ans; + n--; + t%=m; + } + else + { + ans*=ans; + n=n/2; + ans%=m; + } + } + return t*ans%m; +} +int main() +{ + __int64 n; + while(scanf("%I64d",&n) && n) + { + if(n==1) + { + printf("0\n"); + continue; + } + __int64 ans; + ans=cal(n-1); + printf("%I64d\n",(ans+1)%n); + } + return 0; +} diff --git a/HDOJ/3004_autoAC.cpp b/HDOJ/3004_autoAC.cpp new file mode 100644 index 0000000..0e45a0d --- /dev/null +++ b/HDOJ/3004_autoAC.cpp @@ -0,0 +1,129 @@ +#include +#include +#include +using namespace std; +const int size = 11; +struct node +{ + int mx, my; + int px, py; + int cx, cy; + int step; +}; +int n, m; +int sx, sy; +bool has[size][size][size][size][size][size]; +node start, ne, pre; +const int GoM[8][2] = {{1, 2}, {1, -2}, {-1, 2}, {-1, -2}, {2, 1}, {2, -1}, {-2, 1}, {-2, -1}}; +const int GoM1[8][2] = {{0, 1}, {0, -1}, {0, 1}, {0, -1}, {1, 0}, {1, 0}, {-1, 0}, {-1, 0}}; +const int GoC[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; +char mapp[size][size]; +int ans; +bool check(int x, int y) +{ + return (x >= 0 && x < n && y >= 0 && y < m); +} +void bfs() +{ + memset(has, false, sizeof(has)); + start.step = 0; + queue Que; + Que.push(start); + has[start.mx][start.my][start.px][start.py][start.cx][start.cy] = true; + while (!Que.empty()){ + node pre = Que.front(); + Que.pop(); + for (int i = 0; i < 8; i ++){ + int xx = pre.mx + GoM[i][0]; + int yy = pre.my + GoM[i][1]; + int judx = pre.mx + GoM1[i][0]; + int judy = pre.my + GoM1[i][1]; + if (!check(xx, yy) )continue; + if ((judx == pre.px && judy == pre.py) || (judx == pre.cx && judy == pre.cy)||mapp[judx][judy] == 'D' || mapp[judx][judy] == 'S')continue; + if ((xx == pre.px && yy == pre.py) || (xx == pre.cx && yy == pre.cy) || mapp[xx][yy] == 'D')continue; + if (!has[xx][yy][pre.px][pre.py][pre.cx][pre.cy]){ + if (mapp[xx][yy] == 'S'){ + ans = pre.step+1; + return ; + } + has[xx][yy][pre.px][pre.py][pre.cx][pre.cy] = true; + ne = pre; + ne.mx = xx, ne.my = yy; + ne.step ++; + Que.push(ne); + } + } + for (int i = 0; i < 4; i ++){ + for (int k = 1; k <= 10; k ++){ + int xx = pre.cx + k*GoC[i][0]; + int yy = pre.cy + k*GoC[i][1]; + if (!check(xx, yy) || (pre.mx == xx && pre.my == yy) || (pre.px == xx && pre.py == yy) || mapp[xx][yy] == 'D')break; + if (!has[pre.mx][pre.my][pre.px][pre.py][xx][yy]){ + if (mapp[xx][yy] == 'S'){ + ans = pre.step+1; + return ; + } + has[pre.mx][pre.my][pre.px][pre.py][xx][yy] = true; + ne = pre; + ne.cx = xx, ne.cy = yy; + ne.step ++; + Que.push(ne); + } + } + } + for (int i = 0; i < 4; i ++){//p + int flag = 0; + for (int k = 1; k <= 10; k ++){ + int xx = pre.px + k*GoC[i][0]; + int yy = pre.py + k*GoC[i][1]; + if (!check(xx, yy))break; + if ((pre.mx == xx && pre.my == yy) || (pre.cx == xx && pre.cy == yy) || mapp[xx][yy] == 'D' || mapp[xx][yy] == 'S')flag ++; + if (flag == 1 && mapp[xx][yy] == 'S')break; + if (flag == 1 )continue; + if (flag > 2 )break; + if (flag == 2 && mapp[xx][yy] != 'S')break; + if (!has[pre.mx][pre.my][xx][yy][pre.cx][pre.cy]){ + if (mapp[xx][yy] == 'S'){ + ans = pre.step+1; + return ; + } + has[pre.mx][pre.my][xx][yy][pre.cx][pre.cy] = true; + ne = pre; + ne.px = xx, ne.py = yy; + ne.step ++; + Que.push(ne); + } + } + } + } +} +int main() +{ + int nc = 0; + while (scanf("%d%d", &n, &m) != EOF){ + for (int i = 0; i < n; i ++){ + scanf("%s", mapp[i]); + for (int j = 0; j < m; j ++){ + if (mapp[i][j] == 'M'){ + start.mx = i, start.my = j; + mapp[i][j] = '.'; + } + if (mapp[i][j] == 'C'){ + start.cx = i, start.cy = j; + mapp[i][j] = '.'; + } + if (mapp[i][j] == 'P'){ + start.px = i, start.py = j; + mapp[i][j] = '.'; + } + } + } + ans = -1; + printf("Scenario #%d\n", ++nc); + bfs(); + if (ans == -1)printf("OH!That's impossible!\n\n"); + else + printf("%d\n\n", ans); + } + return 0 ; +} diff --git a/HDOJ/3006_autoAC.cpp b/HDOJ/3006_autoAC.cpp new file mode 100644 index 0000000..63c930e --- /dev/null +++ b/HDOJ/3006_autoAC.cpp @@ -0,0 +1,33 @@ +#include +#include +int s[1<<15]; +int n,m,k,element; +int main() +{ + while(scanf("%d%d",&n,&m)!=EOF) + { + int ans=0; + memset(s,0,sizeof(s)); + while(n--) + { + int set=0; + scanf("%d",&k); + while(k--) + { + scanf("%d",&element); + set=set|(1<<(element-1)); + } + s[set]=1; + for(int j=0;j<=(1<<14);j++) + { + if(s[j]){s[set|j]=1;} + } + } + for(int i=0;i<=1<<14;i++) + { + if(s[i]){ans++;} + } + printf("%d\n",ans); + } + return 0; +} diff --git a/HDOJ/3007_autoAC.cpp b/HDOJ/3007_autoAC.cpp new file mode 100644 index 0000000..5f8fc20 --- /dev/null +++ b/HDOJ/3007_autoAC.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +#include +using namespace std; +struct Point +{ + double x,y; +}; +Point a[1005],d; +double r; +double dist(Point A,Point B) +{ + return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y)); +} +double cross(Point A,Point B,Point C) +{ + return (B.x-A.x)*(C.y-A.y)-(C.x-A.x)*(B.y-A.y); +} +void MiniDiscWith2Point(Point p,Point q,int n) +{ + d.x=(p.x+q.x)/2.0; + d.y=(p.y+q.y)/2.0; + r=dist(p,q)/2; + int k; + double c1,c2,t1,t2,t3; + for(k=1;k<=n;k++) + { + if(dist(d,a[k])<=r) continue; + if(cross(p,q,a[k])!=0.0) + { + c1=(p.x*p.x+p.y*p.y-q.x*q.x-q.y*q.y)/2.0; + c2=(p.x*p.x+p.y*p.y-a[k].x*a[k].x-a[k].y*a[k].y)/2.0; + d.x=(c1*(p.y-a[k].y)-c2*(p.y-q.y))/((p.x-q.x)*(p.y-a[k].y)-(p.x-a[k].x)*(p.y-q.y)); + d.y=(c1*(p.x-a[k].x)-c2*(p.x-q.x))/((p.y-q.y)*(p.x-a[k].x)-(p.y-a[k].y)*(p.x-q.x)); + r=dist(d,a[k]); + } + else + { + t1=dist(p,q); + t2=dist(q,a[k]); + t3=dist(p,a[k]); + if(t1>=t2&&t1>=t3) + { + d.x=(p.x+q.x)/2.0; + d.y=(p.y+q.y)/2.0; + r=dist(p,q)/2.0; + } + else if(t2>=t1&&t2>=t3) + { + d.x=(a[k].x+q.x)/2.0; + d.y=(a[k].y+q.y)/2.0; + r=dist(a[k],q)/2.0; + } + else + { + d.x=(a[k].x+p.x)/2.0; + d.y=(a[k].y+p.y)/2.0; + r=dist(a[k],p)/2.0; + } + } + } +} +void MiniDiscWithPoint(Point pi,int n) +{ + d.x=(pi.x+a[1].x)/2.0; + d.y=(pi.y+a[1].y)/2.0; + r=dist(pi,a[1])/2.0; + int j; + for(j=2;j<=n;j++) + { + if(dist(d,a[j])<=r) continue; + else + { + MiniDiscWith2Point(pi,a[j],j-1); + } + } +} +int main() +{ + int i,n; + while(scanf("%d",&n)&&n) + { + for(i=1;i<=n;i++) + scanf("%lf %lf",&a[i].x,&a[i].y); + if(n==1) + { + printf("%.2lf %.2lf 0.00\n",a[1].x,a[1].y); + continue; + } + r=dist(a[1],a[2])/2.0; + d.x=(a[1].x+a[2].x)/2.0; + d.y=(a[1].y+a[2].y)/2.0; + for(i=3;i<=n;i++) + { + if(dist(d,a[i])<=r)continue; + else MiniDiscWithPoint(a[i],i-1); + } + printf("%.2lf %.2lf %.2lf\n",d.x,d.y,r); + } + return 0; +} diff --git a/HDOJ/3008_autoAC.cpp b/HDOJ/3008_autoAC.cpp new file mode 100644 index 0000000..60c2a4f --- /dev/null +++ b/HDOJ/3008_autoAC.cpp @@ -0,0 +1,58 @@ +#include +#include +using namespace std; +#define inf 110 +int dp[inf][inf]; +#define Max(a,b) a>b?a:b +struct node +{ + int ai,bi; +}b[inf]; +int cmp(struct node a,struct node b){return a.ai>n>>t>>p) + { + if(n==0&&t==0&&p==0) + break; + memset(b,0,sizeof(b)); + for(i=1;i<=n;i++) + scanf("%d%d",&b[i].ai,&b[i].bi); + sort(b+1,b+n+1,cmp); + b[0].ai=0; + b[0].bi=1; + b[n+1].bi=b[n+1].ai=100; + memset(dp,0,sizeof(dp)); + int T=(100+p-1)/p; + int k; + for(i=1;i<=T;i++) + { + for(j=0;j<=100;j++) + { + int max=0; + for(k=0;k<=n;k++) + { + if(b[k].ai+j>100) + break; + max=Max(max,dp[i-1][j+b[k].ai]+b[k].bi); + } + dp[i][j]=max; + } + for(j=99;j>100-t;j--) + dp[i][100]=Max(dp[i][j],dp[i][100]); + for(j=100-t;j>=0;j--) + dp[i][j+t]=Max(dp[i][j],dp[i][j+t]); + } + for(i=1;i<=T;i++) + { + for(j=0;j<=100;j++) + if(dp[i][j]>=100) + break; + if(j<=100) + {printf("%d\n",i);break;} + } + if(i>T)printf("My god\n"); + } + return 0; +} diff --git a/HDOJ/3011_autoAC.cpp b/HDOJ/3011_autoAC.cpp new file mode 100644 index 0000000..98580f4 --- /dev/null +++ b/HDOJ/3011_autoAC.cpp @@ -0,0 +1,29 @@ +#include +#include +int main() +{ + double r,a,b,c,t; + double x1,x2; + while(scanf("%lf%lf",&r,&a)!=EOF) + { + t=-1.0; + b=r-a; + c=r*r*r/a/2-2*r*r+a*r; + if(b*b-4*c<0) + { + printf("Oh, my god!\n"); + continue; + } + x1=(-b+sqrt(b*b-4*c))/2; + x2=(-b-sqrt(b*b-4*c))/2; + if(x1>=0&&x1<=r) + t=x1; + else if(x2>=0&&x2<=r) + t=x2; + if(t>=0&&t<=r) + printf("%.2lf\n",t); + else + printf("Oh, my god!\n"); + } + return 0; +} diff --git a/HDOJ/3013_autoAC.cpp b/HDOJ/3013_autoAC.cpp new file mode 100644 index 0000000..90318ee --- /dev/null +++ b/HDOJ/3013_autoAC.cpp @@ -0,0 +1,89 @@ +#include +#include +#include +using namespace std; +int dir[7][4][4][2]={ +{{{0, 0}, {0, 1}, {0, 2}, {0, 3}}, {{0, 0}, {1, 0}, {2, 0}, {3, 0}}, {{0, 0}, {0, 1}, {0, 2}, {0, 3}}, {{0, 0}, {1, 0}, {2, 0}, {3, 0}}}, +{{{0, 0}, {0, 1}, {0, 2}, {1, 0}}, {{0, 0}, {1, 0}, {2, 0}, {2, 1}}, {{0, 0}, {0, 1}, {0, 2}, {-1, 2}}, {{0, 0}, {0, 1}, {1, 1}, {2, 1}}}, +{{{0, 0}, {0, 1}, {0, 2}, {1, 2}}, {{0, 0}, {0, 1}, {1, 0}, {2, 0}}, {{0, 0}, {1, 0}, {1, 1}, {1, 2}}, {{0, 0}, {0, 1}, {-1, 1}, {-2, 1}}}, +{{{0, 0}, {0, 1}, {1, 0}, {1, 1}}, {{0, 0}, {0, 1}, {1, 0}, {1, 1}}, {{0, 0}, {0, 1}, {1, 0}, {1, 1}}, {{0, 0}, {0, 1}, {1, 0}, {1, 1}}}, +{{{0, 0}, {0, 1}, {1, 1}, {1, 2}}, {{0, 0}, {1, 0}, {0, 1}, {-1, 1}}, {{0, 0}, {0, 1}, {1, 1}, {1, 2}}, {{0, 0}, {1, 0}, {0, 1}, {-1, 1}}}, +{{{0, 0}, {0, 1}, {0, 2}, {1, 1}}, {{0, 0}, {1, 0}, {2, 0}, {1, 1}}, {{0, 0}, {0, 1}, {0, 2}, {-1, 1}}, {{0, 0}, {0, 1}, {-1, 1}, {1, 1}}}, +{{{0, 0}, {0, 1}, {-1, 1}, {-1, 2}}, {{0, 0}, {1, 0}, {1, 1}, {2, 1}}, {{0, 0}, {0, 1}, {-1, 1}, {-1, 2}}, {{0, 0}, {1, 0}, {1, 1}, {2, 1}}} +}; +int map[30][15]; +int state[30][15]; +int sum[30]; +int safe(int x, int y) +{ + if(x>0&&y>0&&y<=10&&map[x][y]==0) return 1; + return 0; +} +int main() +{ + int m, id, de, pos, i, j, k, x1, y1, x2, y2, x3, y3, x4, y4, jj; + int frx1, frx2, frx3, frx4, fry1, fry2, fry3, fry4; + while(scanf("%d", &m)!=EOF) + { + memset(map, 0, sizeof(map)); + memset(sum, 0, sizeof(sum)); + memset(state, 0, sizeof(state)); + int flag=0; + for(i=0; i0; j--) + { + x1=j+dir[id][de][0][0], y1=pos+dir[id][de][0][1]; + x2=j+dir[id][de][1][0], y2=pos+dir[id][de][1][1]; + x3=j+dir[id][de][2][0], y3=pos+dir[id][de][2][1]; + x4=j+dir[id][de][3][0], y4=pos+dir[id][de][3][1]; + if(safe(x1, y1)&&safe(x2, y2)&&safe(x3, y3)&&safe(x4, y4)) + { + frx1=x1, frx2=x2, frx3=x3, frx4=x4; + fry1=y1, fry2=y2, fry3=y3, fry4=y4; + } + else break; + } + if(j==23) flag=1; + if(flag) continue; + map[frx1][fry1]=1, map[frx2][fry2]=1, map[frx3][fry3]=1, map[frx4][fry4]=1; + sum[frx1]++, sum[frx2]++, sum[frx3]++, sum[frx4]++; + for(j=1; j<=20; j++) + { + while(sum[j]==10) + { + for(jj=j+1; jj<25; jj++) + { + for(k=1; k<=10; k++) + map[jj-1][k]=map[jj][k]; + sum[jj-1]=sum[jj]; + } + } + } + for(j=21; j<25; j++) + if(sum[j]) flag=1; + if(flag!=1) + { + for(j=0; j<=20; j++) + for(jj=0; jj<=10; jj++) + state[j][jj]=map[j][jj]; + } + } + printf("+--------------------+\n"); + for(i=20; i>=1; i--) + { + printf("|"); + for(j=1; j<=10; j++) + { + if(state[i][j]==1) printf("[]"); + else printf(".."); + } + printf("|\n"); + } + printf("+--------------------+\n\n"); + } + return 0; +} diff --git a/HDOJ/3015_autoAC.cpp b/HDOJ/3015_autoAC.cpp new file mode 100644 index 0000000..fa6a9cc --- /dev/null +++ b/HDOJ/3015_autoAC.cpp @@ -0,0 +1,119 @@ +#include +#include +using namespace std; +#define M 100005 +struct node +{ + __int64 a,b; + int num1,num2; +}t[M]; +int cmpa(node &p,node &q) +{ + if(p.a0) + { + s+=c[x]; + x-=lowbit(x); + } + return s; +} +__int64 getsumd(int x) +{ + __int64 s=0; + while(x>0) + { + s+=d[x]; + x-=lowbit(x); + } + return s; +} +int main() +{ + while(scanf("%d",&n)>0) + { + memset(c,0,sizeof(c)); + memset(d,0,sizeof(d)); + int i; + for(i=1;i<=n;i++) + { + scanf("%I64d%I64d",&t[i].a,&t[i].b); + } + sort(t+1,t+1+n,cmpa); + for(i=1;i<=n;i++) + { + if(i!=1&&t[i].a==t[i-1].a) + t[i].num1=t[i-1].num1; + else + t[i].num1=i; + } + int max=t[n].num1; + sort(t+1,t+1+n,cmpb); + for(i=1;i<=n;i++) + { + if(i!=1&&t[i].b==t[i-1].b) + t[i].num2=t[i-1].num2; + else + t[i].num2=i; + } + sort(t+1,t+1+n,cmpn); + for(i=1;i<=n;i++) + { + updatac(t[i].num1,t[i].num1); + updatad(t[i].num1,1); + } + __int64 sum=0,m=n,tmp1,tmp2; + for(i=1;i +#include +#include +using namespace std; +#define N 100010 +#define lch(i) ((i)<<1) +#define rch(i) ((i)<<1|1) +#define max(a,b) ((a)>(b)?(a):(b)) +#define min(a,b) ((a)<(b)?(a):(b)) +int n; +int dp[N]; +struct seg +{ + int h,l,r,v; + int ln,rn; +}s[N]; +struct node +{ + int mark,l,r,n; + int mid(){ + return (l+r)>>1; + } +}t[4*N]; +int cmp_h(struct seg x ,struct seg y) +{ + return x.h < y.h; +} +void build(int l , int r , int rt) +{ + t[rt].l = l; t[rt].r = r; + t[rt].mark = 0; + t[rt].n = -1; + if(l == r) return ; + int mid = t[rt].mid(); + int ll = lch(rt); + int rr = rch(rt); + build(l , mid , ll); + build(mid+1 , r , rr); +} +int query(int x ,int rt) +{ + if(t[rt].l == t[rt].r) + return t[rt].n; + int mid = t[rt].mid(); + int ll = lch(rt); + int rr = rch(rt); + if(t[rt].mark != -1) + { + t[ll].mark = t[rr].mark = t[rt].mark; + t[ll].n = t[rr].n = t[rt].n; + return t[rt].n; + } + if(x<=mid) return query(x , ll); + else return query(x , rr); +} +void updata(int l , int r ,int m , int rt) +{ + if(t[rt].l == l && t[rt].r == r) + { + t[rt].mark = 1; + t[rt].n = m; + return ; + } + int mid = t[rt].mid(); + int ll = lch(rt); + int rr = rch(rt); + if(t[rt].mark != -1) + { + t[ll].mark = t[rr].mark = t[rt].mark; + t[ll].n = t[rr].n = t[rt].n; + t[rt].mark = -1; + } + if(r<=mid) updata(l,r,m,ll); + else if(l>mid) updata(l,r,m,rr); + else + { + updata(l,mid,m,ll); + updata(mid+1,r,m,rr); + } +} +int main() +{ + while(scanf("%d",&n)!=EOF) + { + n++; s[0].h = 0; s[0].l = 1; s[0].r = 100000; s[0].v = 0; + for(int i=1; i=0; i--) + { + int ln = s[i].ln; + int rn = s[i].rn; + dp[ln] = max(dp[ln] , dp[i] + s[ln].v); + dp[rn] = max(dp[rn] , dp[i] + s[rn].v); + } + printf("%d\n",dp[0] = (dp[0]>0?dp[0]:-1) ); + } + return 0; +} diff --git a/HDOJ/3018_autoAC.cpp b/HDOJ/3018_autoAC.cpp new file mode 100644 index 0000000..3f02cab --- /dev/null +++ b/HDOJ/3018_autoAC.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include +using namespace std; +const int maxn = 100005; +int n, m; +int fa[maxn]; +int deg[maxn]; +int used[maxn]; +int odd[maxn]; +vector v; +void init() +{ + v.clear(); + for (int i=1; i<=n; i++) fa[i] = i, deg[i] = 0, used[i] = 0, odd[i] = 0; +} +int find(int x) +{ + while (x != fa[x]) x = fa[x]; + return x; +} +int main() +{ + while (scanf("%d %d", &n, &m) != EOF) + { + init(); + int i; + for (i=0; i +#include +#include +#include +#define N 1001 +using namespace std; +int fx[8][2] = { 0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1 }, n, m, sx, sy, ex, ey, tp, tb; +struct Map +{ + int cb; + int path; + int x; + int y; + char c; + friend bool operator<(Map t1, Map t2) + { + if (t1.cb == t2.cb) + { + return t1.path>t2.path; + } + return t1.cb>t2.cb; + } +}; +Map map[N][N]; +void input() +{ + int i, j, k; + scanf("%d%d%d%d", &sx, &sy, &ex, &ey); + for (i = 1; i <= n; i++) + { + getchar(); + for (j = 1; j <= m; j++) + { + map[i][j].c = getchar(); + map[i][j].path = -1; + } + } +} +bool isvalide(int x, int y) +{ + if (x<1 || x>n || y<1 || y>m || map[x][y].c == '0') + return false; + return true; +} +void BFS() +{ + int i, j, k; + priority_queue qu; + Map t1, t2; + int tx, ty; + map[sx][sy].path = 0; + map[sx][sy].cb = 0; + t1 = map[sx][sy]; + t1.x = sx; + t1.y = sy; + qu.push(t1); + while (!qu.empty()) + { + t2 = qu.top(); + if (t2.cb>tb || (t2.cb == tb&&t2.path >= tp)) + return; + qu.pop(); + for (i = 0; i<8; i++) + { + tx = t2.x + fx[i][0]; + ty = t2.y + fx[i][1]; + if (isvalide(tx, ty)) + { + if (map[tx][ty].path != -1) + { + if (map[tx][ty].c != t2.c) + { + if (map[tx][ty].cb +#include +#include +#include +#include +#include +#include +#include +using namespace std; +#define maxn 480 +int dp[maxn][maxn][2]; +int pos[maxn]; +inline int min(int a,int b) +{ + return a>b?b:a; +} +int main() +{ + int t; scanf("%d",&t); + while(t--){ + int n; scanf("%d",&n); + for(int i = 1;i <= n;i++) scanf("%d",&pos[i]); + sort(pos+1,pos+1+n); + for(int i = 1;i <= n;i++) dp[i][i][0] = dp[i][i][1] = abs(pos[i])*n; + for(int i = n;i >= 1;i--) + for(int j = i+1;j <= n;j++){ + dp[i][j][0] = min(dp[i + 1][j][0] + abs(pos[i + 1] - pos[i]) * (n - j + i), dp[i + 1][j][1] + abs(pos[j] - pos[i]) * (n - j + i)); + dp[i][j][1] = min(dp[i][j - 1][0] + abs(pos[i] - pos[j]) * (n - j + i), dp[i][j - 1][1] + abs(pos[j - 1] - pos[j]) * (n - j + i)); + } + printf("%d\n",min(dp[1][n][0],dp[1][n][1])); + } + return 0; +} diff --git a/HDOJ/3028_autoAC.cpp b/HDOJ/3028_autoAC.cpp new file mode 100644 index 0000000..4df55e2 --- /dev/null +++ b/HDOJ/3028_autoAC.cpp @@ -0,0 +1,86 @@ +#include +#include +#include +using namespace std; +#define max(a,b) ((a)>(b)?(a):(b)) +int dp[1005][105][2]; +int times[105][1005]; +int alltime[105][1005]; +struct node +{ + int x,t; + bool operator < (const node &a) const + { + return x line[105]; +int check(int x,int t) +{ + int sumt=0; + for(set::iterator it=line[x].begin(); it!=line[x].end(); ++it) + sumt+=times[it->x][t]; + return sumt+times[x][t]; +} +int main() +{ + int n,m,t,p,q,d,x,a,k; + for(; ~scanf("%d%d%d",&n,&m,&t);) + { + int maxx[2]; + maxx[0]=maxx[1]=0; + memset(dp,-1,sizeof(dp)); + memset(times,0,sizeof(times)); + for(int i=0; i<=n; ++i) line[i].clear(); + for(int i=0; i::iterator it=line[j].begin(); it!=line[j].end(); ++it) + { + if(i+it->t<=t) + { + dp[i+it->t][it->x][0]=max(dp[i+it->t][it->x][0],dp[i][j][0]+times[it->x][i+it->t]); + dp[i+it->t][it->x][1]=max(dp[i+it->t][it->x][1],dp[i][j][0]+alltime[it->x][i+it->t]); + dp[i+it->t][it->x][1]=max(dp[i+it->t][it->x][1],dp[i][j][1]+times[it->x][i+it->t]); + maxx[0]=max(maxx[0],dp[i+it->t][it->x][0]); + maxx[1]=max(maxx[1],dp[i+it->t][it->x][1]); + } + } + if(i+1<=t) + { + dp[i+1][j][0]=max(dp[i+1][j][0],dp[i][j][0]+times[j][i+1]); + dp[i+1][j][1]=max(dp[i+1][j][1],dp[i][j][0]+alltime[j][i+1]); + dp[i+1][j][1]=max(dp[i+1][j][1],dp[i][j][1]+times[j][i+1]); + maxx[0]=max(maxx[0],dp[i+1][j][0]); + maxx[1]=max(maxx[1],dp[i+1][j][1]); + } + } + } + printf("%d %d\n",maxx[1],maxx[0]); + } + return 0; +} diff --git a/HDOJ/3029_autoAC.cpp b/HDOJ/3029_autoAC.cpp new file mode 100644 index 0000000..fe46e6d --- /dev/null +++ b/HDOJ/3029_autoAC.cpp @@ -0,0 +1,63 @@ +#include +#include +using namespace std; +int num[30]; +int ind; +void solve(int n) +{ + ind = 0; + while(n) + { + num[ind++] = n % 3; + n /= 3; + } +} +void ss(int ii) +{ + num[ii]+=1; + for(int i=ii;i2) + { + num[i] = 0; + num[i+1]++; + if(i == ind-1) ind += 1; + } + } +} +int main() +{ + int n; + while( cin >> n) + { + memset(num , 0 ,sizeof(num)); + solve(n); + int re[30]; + int sum = 0,countd=0; + memset(re,0,sizeof(re)); + for(int i=0;i +#include +#include +#define lowbit(x) x&(-x) +using namespace std; +const int maxn = 500050; +const __int64 mod =1000000007; +__int64 a[maxn],b[maxn],c[maxn],e[maxn]; + int k; +int find(__int64 key) +{ + int l=1,r=k; + while(l<=r) + { + int mid = (l+r)>>1; + if(b[mid]==key) return mid; + if(b[mid]0;i-=lowbit(i)) + { + sum+=e[i]; + sum%=mod; + } + return sum; + } +int main(int argc, char *argv[]) +{ + int t,cas=1; + __int64 n,m,x,y,z; + scanf("%d",&t); + while(t--) + { + scanf("%I64d%I64d%I64d%I64d%I64d",&n,&m,&x,&y,&z); + for(int i=0;i +#include +#include +#include +#include +using namespace std; +#define MAXN 2000010 +int r, n, m, cnt[10010], p[10010]; +int fa[10010], rt[2], s[2]; +int tot, x, sh, sw, h, w; +char op[4]; +struct Node +{ + int v, l, r, dis; + Node() {} + Node(int _v, int _l, int _r, int _d): + v(_v), l(_l), r(_r), dis(_d) {} +}nn[MAXN]; +int merge(int x, int y) +{ + if(!x) return y; + if(!y) return x; + if(nn[x].v < nn[y].v) swap(x, y); + nn[x].r = merge(nn[x].r, y); + if(nn[nn[x].l].dis < nn[nn[x].r].dis) swap(nn[x].l, nn[x].r); + nn[x].dis = nn[nn[x].r].dis + 1; + return x; +} +int main() +{ +// freopen("G.in", "r", stdin); + while(~scanf("%d", &r)) + { + h = w = 0; + while(r--) + { + tot = 1; + s[0] = s[1] = rt[0] = rt[1] = 0; + memset(fa, 0, sizeof(fa)); + scanf("%d%d", &n, &m); + for(int i = 1; i <= m; i++) scanf("%d", cnt + i); + for(int i = 1; i <= m; i++) for(int j = 1; j <= cnt[i]; j++) + { + scanf("%d", &x); + nn[tot] = Node(x, 0, 0, 0); + fa[i] = merge(fa[i], tot); + tot++; + } + for(int i = 1; i <= n; i++) + { + scanf("%s", op); + if(op[0] == 'T') + { + scanf("%d", &x); + rt[1 & i] = merge(rt[1 & i], fa[x]); + s[1 & i] += cnt[x]; + } + if(op[0] == 'C') + { + if(nn[rt[0]].v > nn[rt[1]].v) + { + rt[0] = merge(rt[0], rt[1]); + s[0] += s[1]; rt[1] = s[1] = 0; + } + if(nn[rt[0]].v < nn[rt[1]].v) + { + rt[1] = merge(rt[0], rt[1]); + s[1] += s[0]; rt[0] = s[0] = 0; + } + } + if(op[0] == 'L') + { + rt[1 & i] = merge(nn[rt[1 & i]].l, nn[rt[1 & i]].r); + s[1 & i]--; + } + if(op[0] == 'A') + { + scanf("%d", &x); + nn[rt[1 & i]].v += x; + } + if(op[0] == 'E') + { + scanf("%d", &x); + int lson = nn[rt[1 & i]].l; + int rson = nn[rt[1 & i]].r; + nn[rt[1 & i]] = Node(x, 0, 0, 0); + rt[1 & i] = merge(merge(lson, rson), rt[1 & i]); + } + } + if(s[0] > s[1]) h++; else w++; + printf("%d:%d\n", s[1], s[0]); + } + if(h > w) puts("I will be back!!"); else puts("Hahaha...I win!!"); + } + return 0; +} diff --git a/HDOJ/3032_autoAC.cpp b/HDOJ/3032_autoAC.cpp new file mode 100644 index 0000000..df416f9 --- /dev/null +++ b/HDOJ/3032_autoAC.cpp @@ -0,0 +1,25 @@ +#include +int main () +{ + int i,s,n,a,t; + scanf("%d",&t); + while(t--) + { + scanf("%d",&n); + for(s=0,i=1;i<=n;i++) + { + scanf("%d",&a); + if(a%4==0) + s^=(a-1); + else if(a%4==3) + s^=(a+1); + else + s^=a; + } + if(s) + printf("Alice\n"); + else + printf("Bob\n"); + } + return 0; +} diff --git a/HDOJ/3033_autoAC.cpp b/HDOJ/3033_autoAC.cpp new file mode 100644 index 0000000..dad54a1 --- /dev/null +++ b/HDOJ/3033_autoAC.cpp @@ -0,0 +1,45 @@ +#include +#include +int dp[15][10010]; +int max(int a,int b) +{ + if(a>b) + return a; + else + return b; +} +int main() +{ + int i,j,n,m; + int k,T,num[15],v[15][151],c[15][150]; + while(scanf("%d%d%d",&n,&m,&T)!=EOF) + { + memset(v,0,sizeof(v)); + memset(c,0,sizeof(c)); + memset(num,0,sizeof(num)); + memset(dp,-1,sizeof(dp)); + memset(dp[0],0,sizeof(dp[0])); + for(i=1;i<=n;i++) + { + scanf("%d",&j); + num[j]++; + scanf("%d%d",&c[j][num[j]],&v[j][num[j]]); + } + for(i=1;i<=T;i++) + { + for(k=1;k<=num[i];k++) + for(j=m;j>=c[i][k];j--) + { + if(dp[i][j-c[i][k]]!=-1) + dp[i][j]=max(dp[i][j],dp[i][j-c[i][k]]+v[i][k]); + if(dp[i-1][j-c[i][k]]!=-1) + dp[i][j]=max(dp[i][j],dp[i-1][j-c[i][k]]+v[i][k]); + } + } + if(dp[T][m]<0) + printf("Impossible\n"); + else + printf("%d\n",dp[T][m]); + } + return 0; +} diff --git a/HDOJ/3034_autoAC.cpp b/HDOJ/3034_autoAC.cpp new file mode 100644 index 0000000..f0ba566 --- /dev/null +++ b/HDOJ/3034_autoAC.cpp @@ -0,0 +1,109 @@ +#include +#include +#include +#define MaxN 40 +#define MaxS 2000010 +using namespace std; +int n, m, N, px, st, ed, hs, dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; +int d[MaxS], dist[MaxN], len[MaxN], g[MaxN][4], pN[5]; +bool adj[MaxN][MaxN]; +char s[MaxN][MaxN]; +struct HeapNode { + int s, f; + HeapNode(){} + HeapNode(int s, int f):s(s),f(f){} +}heap[MaxS]; +void sink(int p) +{ + HeapNode a = heap[p]; + for (int q = p<<1; q <= hs; p = q, q <<= 1) { + if (q < hs && heap[q+1].f < heap[q].f) ++q; + if (heap[q].f >= a.f) break; + heap[p] = heap[q]; + } + heap[p] = a; +} +HeapNode del() +{ + HeapNode r = heap[1]; + heap[1] = heap[hs--]; + sink(1); + return r; +} +void swim(int p) +{ + HeapNode a = heap[p]; + for (int q = p>>1; q && a.f < heap[q].f; p = q, q >>= 1) + heap[p] = heap[q]; + heap[p] = a; +} +void ins(HeapNode a) +{ + heap[++hs] = a; + swim(hs); +} +int h(int s) +{ + int ret = 0; + for (int i = 0; i < m; ++i, s /= N) ret += dist[s%N]; + return ret; +} +int AStar() +{ + memset(d, 0x3f, sizeof(d)); + int p[5]; + hs = 0; + ins(HeapNode(st, h(st))); + d[st] = 0; + while (hs > 0) { + HeapNode u = del(); + int s = u.s, first; + for (int i = 0; i < m; ++i, s/=N) p[i] = s%N; + for (int i = 0; i < m; ++i) if (p[i] != px) {first = i; break;} + for (int i = first; i < m; ++i) + for (int j = 0; j < len[p[i]]; ++j) { + int np = g[p[i]][j]; + bool fail = 0; + for (int k = first; k < m; ++k) if (k != i && adj[np][p[k]]) {fail = 1;break;} + if (!fail) { + int v = u.s+pN[i]*(np-p[i]); + if (d[u.s] + 1 < d[v]) { + d[v] = d[u.s] + 1; + if (v == ed) return d[v]; + ins(HeapNode(v, d[v] + h(v))); + } + } + } + } + return -1; +} +int main() +{ + int T; + scanf("%d", &T); + while (T--) { + scanf("%d%d", &n, &m); N = n*n; st = ed = 0; + memset(adj, 0, sizeof(adj)); + memset(len, 0, sizeof(len)); + pN[0] = 1; for (int i = 1; i <= m; ++i) pN[i] = pN[i-1] * N; + for (int i = 0; i < n; ++i) scanf("%s", s[i]); + for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) { + int s1 = i*n+j; + if (s[i][j] == 'x') px = s1; + else if (s[i][j] != 'o') st += pN[s[i][j]-'1'] * s1; + for (int d = 0; d < 4; ++d) { + int x = i + dx[d], y = j + dy[d], s2 = x*n+y; + if (x >= 0 && x < n && y >= 0 && y < n) { + g[s1][len[s1]++] = s2; + adj[s1][s2] = 1; + } + } + } + for (int i = 0; i < m; ++i) ed = ed*N + px; + for (int i = 0; i < n; ++i) + for (int j = 0; j < n; ++j) + dist[i*n+j] = abs(i-px/n) + abs(j-px%n); + printf("%d\n", AStar()); + } + return 0; +} diff --git a/HDOJ/3035_autoAC.cpp b/HDOJ/3035_autoAC.cpp new file mode 100644 index 0000000..00d6844 --- /dev/null +++ b/HDOJ/3035_autoAC.cpp @@ -0,0 +1,118 @@ +#include +#include +#include +using namespace std; +#define FOR(i,a,b) for(i = (a); i < (b); ++i) +#define FORE(i,a,b) for(i = (a); i <= (b); ++i) +#define CLR(a,b) memset(a,b,sizeof(a)) +const int inf = 1 << 30; +const int MAXN = 502000; +const int MAXM = 3100000; +struct node { + int c,u,v,next; +}edge[MAXM]; +int N, M; +int ne, head[MAXN]; +int cur[MAXN], ps[MAXN], dep[MAXN]; +void addedge(int u, int v,int w) { + edge[ne].u = u; + edge[ne].v = v; + edge[ne].c = w; + edge[ne].next = head[u]; + head[u] = ne ++; + edge[ne].u = v; + edge[ne].v = u; + edge[ne].c = w; + edge[ne].next = head[v]; + head[v] = ne ++; +} +int dinic(int s, int t) { + int tr, res = 0; + int i, j, k, f, r, top; + while(1) { + memset(dep, -1, sizeof(dep)); + for(f = dep[ps[0] = s] = 0, r = 1; f != r;) + for(i = ps[f++], j = head[i]; j != -1; j = edge[j].next) + if(edge[j].c > 0 && dep[k = edge[j].v] == -1) { + dep[k] = dep[i] + 1; + ps[r++] = k; + if(k == t) { + f = r; break; + } + } + if(dep[t] == -1) break; + memcpy(cur, head, sizeof(cur)); + i = s, top = 0; + while(1) { + if(i == t) { + for(tr = inf, k = 0; k < top; k++) + if(edge[ps[k]].c < tr) + tr = edge[ps[f = k]].c; + for(k = 0; k < top; k++) { + edge[ps[k]].c -= tr; + edge[ps[k]^1].c += tr; + } + i = edge[ps[top=f]].u; + res += tr; + } + for(j = cur[i]; cur[i] != -1; j = cur[i] = edge[cur[i]].next) { + if(edge[j].c > 0 && dep[i]+1 == dep[edge[j].v]) break; + } + if(cur[i] != -1) { + ps[top++] = cur[i]; + i = edge[cur[i]].v; + } + else { + if(top == 0) break; + dep[i] = -1; + i = edge[ps[--top]].u; + } + } + } + return res; +} +int main() { + int i, j, x, y; + while(scanf("%d %d", &N, &M) == 2) { + ne = 2; + CLR(head, -1); + CLR(cur, 0); + CLR(ps, 0); + CLR(dep, 0); + FORE(i, 1, N + 1) { + FORE(j, 1, M) { + scanf("%d", &x); + addedge(j + (i - 1) * (M + 1), j + (i - 1) * (M + 1) + 1, x); + } + } + FORE(i, 1, N) { + FORE(j, 1, M + 1) { + scanf("%d", &x); + addedge(j + (i - 1) * (M + 1), j + (i - 1) * (M + 1) + (M + 1), x); + } + } + int tmp_cnt1 = (M + 1) * (N + 1) + 1, tmp_cnt2 = (M + 1) * (N + 1) + 1; + int cntt = 1; + FORE(i, 1, N) { + int tt1 = tmp_cnt1, tt2 = tmp_cnt2; + FORE(j, 1, M) { + scanf("%d %d", &x, &y); + addedge(cntt, tt1++, x); + addedge(cntt + 1, tt2++, y); + ++cntt; + } + ++cntt; + FORE(j, 1, M) { + scanf("%d %d", &x, &y); + addedge(cntt, tmp_cnt1++, x); + addedge(cntt + 1, tmp_cnt2++, y); + ++cntt; + } + ++cntt; + cntt -= (M + 1); + } + int endd = dinic(1, (N + 1) * (M + 1)); + printf("%d\n", endd); + } + return 0; +} diff --git a/HDOJ/3036_autoAC.cpp b/HDOJ/3036_autoAC.cpp new file mode 100644 index 0000000..89424bd --- /dev/null +++ b/HDOJ/3036_autoAC.cpp @@ -0,0 +1,158 @@ +#include +#include +#include +#include +#include +#include +using namespace std; +#define ll int +#define N 20050 +#define M 105000 +#define inf 10737418 +struct Edge{ + ll from, to, cap, nex; +}edge[M*4]; +ll head[N], edgenum; +void add(ll u, ll v, ll cap){ + Edge E = { u, v, cap, head[u]}; + edge[ edgenum ] = E; + head[u] = edgenum ++; + Edge E2= { v, u, 0, head[v]}; + edge[ edgenum ] = E2; + head[v] = edgenum ++; +} +ll sign[N]; +bool BFS(ll from, ll to){ + memset(sign, -1, sizeof(sign)); + sign[from] = 0; + queueq; + q.push(from); + while( !q.empty() ){ + int u = q.front(); q.pop(); + for(ll i = head[u]; i!=-1; i = edge[i].nex) + { + ll v = edge[i].to; + if(sign[v]==-1 && edge[i].cap) + { + sign[v] = sign[u] + 1, q.push(v); + if(sign[to] != -1)return true; + } + } + } + return false; +} +ll Stack[N], top, cur[N]; +ll dinic(ll from, ll to){ + ll ans = 0; + while( BFS(from, to) ) + { + memcpy(cur, head, sizeof(head)); + ll u = from; top = 0; + while(1) + { + if(u == to) + { + ll flow = inf, loc; + for(ll i = 0; i < top; i++) + if(flow > edge[ Stack[i] ].cap) + { + flow = edge[Stack[i]].cap; + loc = i; + } + for(ll i = 0; i < top; i++) + { + edge[ Stack[i] ].cap -= flow; + edge[Stack[i]^1].cap += flow; + } + ans += flow; + top = loc; + u = edge[Stack[top]].from; + } + for(ll i = cur[u]; i!=-1; cur[u] = i = edge[i].nex) + if(edge[i].cap && (sign[u] + 1 == sign[ edge[i].to ]))break; + if(cur[u] != -1) + { + Stack[top++] = cur[u]; + u = edge[ cur[u] ].to; + } + else + { + if( top == 0 )break; + sign[u] = -1; + u = edge[ Stack[--top] ].from; + } + } + } + return ans; +} +void init(){memset(head,-1,sizeof head);edgenum = 0;} +char mp[15][15]; +int n, m, T; +int Hash(int x,int y){return x*m+y;} +vectorE,P; +int dis[150][150], step[4][2]={1,0,-1,0,0,1,0,-1}; +bool vis[150][150]; +void bfs(int sx,int sy){ + int start = Hash(sx,sy); + memset(vis, 0, sizeof vis); + vis[sx][sy] = 1; + dis[start][start] = 0; + queueqx,qy; while(!qx.empty())qx.pop(); while(!qy.empty())qy.pop(); + qx.push(sx), qy.push(sy); + while(!qx.empty()){ + int x = qx.front(), y = qy.front(); + qx.pop(); qy.pop(); + for(int i = 0; i < 4; i++){ + int dx = x + step[i][0], dy = y + step[i][1]; + if(!(0<=dx&&dx>1; + if(ok(mid))ans = min(ans, mid), r = mid-1; + else l = mid+1; + } + if(T +#include +#include +#include +using namespace std; +typedef long long ll; +ll fac[100003]; +void init(ll p) { + fac[0] = 1; + for (int i=1; i<=p; i++) fac[i] = fac[i-1]*i%p; +} +ll PowerMod(ll a, ll b, ll k) { + ll tmp = a, ret = 1; + while (b) { + if (b & 1) ret = ret * tmp % k; + tmp = tmp * tmp % k; + b >>= 1; + } + return ret; +} +ll Lucas(ll n, ll m, ll p) { + ll ret = 1; + while (n && m) { + ll nn = n%p, mm = m%p; + if (nn < mm) return 0; + ret = ret*fac[nn]*PowerMod(fac[mm]*fac[nn-mm]%p, p-2, p)%p; + n /= p; + m /= p; + } + return ret; +} +int main() { + int T; + ll n, m, p; + cin >> T; + while (T--) { + cin >> n >> m >> p; + init(p); + cout << Lucas(n+m, m, p) << endl; + } + return 0; +} diff --git a/HDOJ/3038_autoAC.cpp b/HDOJ/3038_autoAC.cpp new file mode 100644 index 0000000..a425453 --- /dev/null +++ b/HDOJ/3038_autoAC.cpp @@ -0,0 +1,57 @@ +#include +#define MAXN 200010 +int f[MAXN],r[MAXN]; +int find(int x) +{ + if(x==f[x]) + return f[x]; + int t=find(f[x]); + r[x]=r[x]+r[f[x]]; + f[x]=t; + return f[x]; +} +int fun(int x,int y) +{ + if(x>y) + return x-y; + else y-x; +} +int Union(int x,int y,int sum) +{ + int a=find(x); + int b=find(y); + if(a==b) + { + if(fun(r[x],r[y])==sum) + return 0; + else return 1; + } + else + { + f[a]=b; + r[a]=r[y]+sum-r[x]; + return 0; + } +} +int main() +{ + int n,m,i,ans,a,b,s; + while(scanf("%d %d",&n,&m)!=EOF) + { + ans=0; + for(i=0;i<=n;i++) + { + f[i]=i; + r[i]=0; + } + for(i=1;i<=m;i++) + { + scanf("%d %d %d",&a,&b,&s); + a--; + if(Union(a,b,s)) + ans++; + } + printf("%d\n",ans); + } + return 0; +} diff --git a/HDOJ/3039_autoAC.cpp b/HDOJ/3039_autoAC.cpp new file mode 100644 index 0000000..48d62b4 --- /dev/null +++ b/HDOJ/3039_autoAC.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +using namespace std; +double a[105][205],b[105],ans; +int main() +{ + int n,q,m; + while(scanf("%d%d",&n,&q)!=EOF) + { + m=2*n; + for(int i=1;i<=n;i++) + for(int j=1;j<=n;j++) + { + scanf("%lf",&a[i][j]); + if(i==j) + a[i][n+j]=1; + else + a[i][n+j]=0; + } + for(int i=1;i<=n;i++) + { + int pos=i; + for(;pos<=n;pos++) + if(fabs(a[pos][i])>1e-6)break; + if(pos!=i) + for(int k=i;k<=m;k++) + swap(a[i][k],a[pos][k]); + double temp=a[i][i]; + for(int j=i;j<=m;j++)a[i][j]/=temp; + for(int j=1;j<=n;j++) + { + if(j==i)continue; + temp=a[j][i]; + for(int k=i;k<=m;k++) + a[j][k]-=a[i][k]*temp; + } + } + while(q--) + { + for(int i=1;i<=n;i++) + scanf("%lf",&b[i]); + for(int i=1;i<=n;i++) + { + ans=0; + for(int j=n+1;j<=m;j++) + ans+=a[i][j]*b[j-n]; + printf("%.2f%c",ans,i==n?'\n':' '); + } + } + } +} diff --git a/HDOJ/3040_autoAC.cpp b/HDOJ/3040_autoAC.cpp new file mode 100644 index 0000000..2ae6838 --- /dev/null +++ b/HDOJ/3040_autoAC.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +#define N 50005 +struct node +{ + int x; + __int64 tt; + int id; +}a[N],b[N]; +int mark[15]; +int fun(char *s) +{ + int a,b,c; + sscanf(s,"%d:%d:%d",&a,&b,&c); + return a*3600+b*60+c; +} +int cmp(const void *a,const void *b) +{ + node *c,*d; + c=(node *)a;d=(node *)b; + if(c->tt!=d->tt) + return c->tttt?1:-1; + else + return c->x-d->x; +} +int main() +{ + int n; + while(scanf("%d",&n)!=EOF) + { + int i,j,k; + j=0; + char s[15]; + while(scanf("%s",s),strcmp(s,"#end")!=0) + { + a[j].x=fun(s); + scanf("%I64d%d",&a[j].tt,&a[j].id); + j++; + } + qsort(a,j,sizeof(a[0]),cmp); + k=1; + b[0]=a[0]; + for(i=1;i=60) + b[k++]=a[i]; + } + memset(mark,0,sizeof(mark)); + for(i=0;i +#include +#include +#include +int main() +{ + int T; + double s, l1, l2, v1, v2; + scanf("%d", &T); + while(T--) + { + scanf("%lf%lf%lf%lf", &l1, &l2, &v1, &v2); + s=cos(1)*1.55741*v2*(l1-10)/v1/sin(1); + if(s<0)printf("0\n"); + else if(s>l2)printf("%.0lf\n", l2); + else printf("%.0lf\n", s); + } + return 0; +} diff --git a/HDOJ/3045_autoAC.cpp b/HDOJ/3045_autoAC.cpp new file mode 100644 index 0000000..b0880f5 --- /dev/null +++ b/HDOJ/3045_autoAC.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include +using namespace std; +const int MAXN=400010; +int q[MAXN]; +long long sum[MAXN]; +long long a[MAXN]; +long long dp[MAXN]; +long long getDP(int i,int j) +{ + return dp[j]+(sum[i]-sum[j])-a[j+1]*(i-j); +} +long long getUP(int j,int k) +{ + return dp[j]-sum[j]+j*a[j+1]-(dp[k]-sum[k]+k*a[k+1]); +} +long long getDOWN(int j,int k) +{ + return a[j+1]-a[k+1]; +} +int main() +{ + int n,t; + int head,tail; + while(scanf("%d%d",&n,&t)==2) + { + dp[0]=sum[0]=0; + a[0]=0; + for(int i=1;i<=n;i++) + { + scanf("%I64d",&a[i]); + } + sort(a+1,a+n+1); + for(int i=1;i<=n;i++) + sum[i]=sum[i-1]+a[i]; + head=tail=0; + q[tail++]=0; + for(int i=1;i<=n;i++) + { + while(head+1 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +#define CL(x,v); memset(x,v,sizeof(x)); +#define INF 0x3f3f3f3f +#define LL long long +#define REP(i,r,n) for(int i=r;i<=n;i++) +#define RREP(i,n,r) for(int i=n;i>=r;i--) +const int MAXN=200*200+100; +struct Edge{ + int from,to,cap,flow; +}; +bool cmp(const Edge& a,const Edge& b){ + return a.from < b.from || (a.from == b.from && a.to < b.to); +} +struct Dinic{ + int n,m,s,t; + vector edges; + vector G[MAXN]; + bool vis[MAXN]; + int d[MAXN]; + int cur[MAXN]; + void init(int n){ + this->n=n; + for(int i=0;i<=n;i++)G[i].clear(); + edges.clear(); + } + void AddEdge(int from,int to,int cap){ + edges.push_back((Edge){from,to,cap,0}); + edges.push_back((Edge){to,from,cap,0}); + m=edges.size(); + G[from].push_back(m-2); + G[to].push_back(m-1); + } + bool BFS(){ + CL(vis,0); + queue Q; + Q.push(s); + d[s]=0; + vis[s]=1; + while(!Q.empty()){ + int x=Q.front(); + Q.pop(); + for(int i=0;ie.flow){ + vis[e.to]=1; + d[e.to]=d[x]+1; + Q.push(e.to); + } + } + } + return vis[t]; + } + int DFS(int x,int a){ + if(x==t||a==0)return a; + int flow=0,f; + for(int& i=cur[x];i0){ + e.flow+=f; + edges[G[x][i]^1].flow-=f; + flow+=f; + a-=f; + if(a==0)break; + } + } + return flow; + } + int Maxflow(int s,int t,int need){ + this->s=s;this->t=t; + int flow=0; + while(BFS()){ + CL(cur,0); + flow+=DFS(s,INF); + if(flow>need)return flow; + } + return flow; + } + vector Mincut(){ + BFS(); + vector ans; + for(int i=0;i0)ans.push_back(i); + } + return ans; + } + void Reduce(){ + for(int i = 0; i < edges.size(); i++) edges[i].cap -= edges[i].flow; + } + void ClearFlow(){ + for(int i = 0; i < edges.size(); i++) edges[i].flow = 0; + } +}; +Dinic solver; +int main() +{ + int n,m; + int cas=0; + while(~scanf("%d%d",&n,&m)) + { + int s=n*m; + int t=n*m+1; + int N=n*m+1; + solver.init(N); + int a; + for(int i=0;i +#include +using namespace std; +#define N 50005 +int f[N], rank[N], n, m; +void init(){ + for(int i=0; i<=n; ++i) + f[i]=i, rank[i]=0; +} +int find(int x){ + if(x==f[x]) return f[x]; + int t=f[x]; + f[x] = find(f[x]); + rank[x] += rank[t]; + return f[x]; +} +bool Union(int x,int y, int m){ + int a=find(x), b=find(y); + if(a==b){ + if(rank[x]+m!=rank[y]) + return false; + return true; + } + f[b] = a; + rank[b] = rank[x]+m-rank[y]; + return true; +} +int main(){ + int a,b,x; + while(~scanf("%d%d",&n,&m)){ + init(); + int cnt=0; + for(int i=0; i +#include +int main() +{ + int T,n,x; + __int64 num[40002]; + scanf( "%d",&T ); + for( int i=1; i<=T; i++ ) + { + scanf( "%d",&n ); + __int64 ans=0,m=1000003; + m*=n; + num[0]=1; + for( int j=1;j<=40000;j++ ) + { + num[ j ]=num[j-1]<<1; + if( num[j]>=m ) + num[j]-= m; + } + for( int j=0;j= m ) + ans -= m; + } + ans/=n; + printf( "Case %d:%I64d\n",i,ans ); + } + return 0; +} diff --git a/HDOJ/3053_autoAC.cpp b/HDOJ/3053_autoAC.cpp new file mode 100644 index 0000000..b84072d --- /dev/null +++ b/HDOJ/3053_autoAC.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#define MAXD 3010 +#define INF 0x3f3f3f3f +int N, P, f[MAXD][MAXD], K[MAXD][MAXD], A[MAXD], a[MAXD]; +void init() +{ + int i; + scanf("%d%d", &N, &P); + for(i = 1; i <= N; i ++) scanf("%d", &a[i]); + std::sort(a + 1, a + 1 + N); + for(i = 1, A[0] = 0; i <= N; i ++) A[i] = A[i - 1] + a[i]; +} +int getw(int x, int y) +{ + int t = x + y >> 1; + return (t - x) * a[t] - A[t - 1] + A[x - 1] + A[y] - A[t] - (y - t) * a[t]; +} +void solve() +{ + int i, j, k, p, t; + for(i = 0; i <= N; i ++) K[i][i] = i, f[i][i] = 0; + for(p = 1; p <= N - P; p ++) + { + for(i = 0; (j = i + p) <= N; i ++) f[i][j] = INF; + for(i = 1; (j = i + p) <= N; i ++) + for(k = K[i][j - 1]; k <= K[i + 1][j]; k ++) + if(f[i][j] > (t = f[i - 1][k - 1] + getw(k, j))) + f[i][j] = t, K[i][j] = k; + } + printf("%d\n", f[P][N]); +} +int main() +{ + int t; + scanf("%d", &t); + while(t --) + { + init(); + if(P >= N) printf("0\n"); + else solve(); + } + return 0; +} diff --git a/HDOJ/3054_autoAC.cpp b/HDOJ/3054_autoAC.cpp new file mode 100644 index 0000000..ff587e2 --- /dev/null +++ b/HDOJ/3054_autoAC.cpp @@ -0,0 +1,34 @@ +#include +int solve( int start , int i) { + int times = 1,s; + if(start == 1) times = 15; + else if(start == 2) times = 150; + else { + times = 750; + start = start - 3; + while(start--) { + times *= 10; + } + } + if(start == 2) { + if( i % 4 == 0) + s = (i + i/4 - 1)*times; + else + s = (i + i/4)*times; + } else { + if( i % 9 == 0) + s = (i + i/9 - 1)*times; + else + s = (i + i/9)*times; + } + return s; +} +int main(void) { + int T,m,k; + scanf("%d",&T); + while(T--) { + scanf("%d %d",&m,&k); + printf("%d\n",solve(m,k)); + } + return 0; +} diff --git a/HDOJ/3058_autoAC.cpp b/HDOJ/3058_autoAC.cpp new file mode 100644 index 0000000..e139a52 --- /dev/null +++ b/HDOJ/3058_autoAC.cpp @@ -0,0 +1,184 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long ll; +#define INF (INT_MAX/10) +#define SQR(x) ((x)*(x)) +#define rep(i, n) for (int i=0; i<(n); ++i) +#define repf(i, a, b) for (int i=(a); i<=(b); ++i) +#define repd(i, a, b) for (int i=(a); i>=(b); --i) +#define clr(ar,val) memset(ar, val, sizeof(ar)) +#define pb(i) push_back(i) +#define exp 0.000000001 +#define N 101 +double a[N][N]; +int n,m,tot; +char s[20]; +bool vis[50]; +vectorvec; +struct node +{ + bool sign; + int len,fal; + int next[26]; +}; +node ac[10000]; +void init() +{ + ac[0].sign=false;tot=0; + ac[0].len=0; ac[0].fal=0; + memset(ac[0].next,-1,sizeof(ac[0].next)); + memset(a,0,sizeof(a)); + memset(vis,false,sizeof(vis)); + vec.clear(); +} +void tree() +{ + int len=strlen(s); + int u=0; + rep(i,len) + { + int x=s[i]-'A'; + if(ac[u].next[x]==-1) + { + ++tot; ac[tot].sign=false; + memset(ac[tot].next,-1,sizeof(ac[tot].next)); + ac[u].next[x]=tot; + } + u=ac[u].next[x]; + if(i==len-1) ac[u].sign=true; + } +} +int fail(int u,int k) +{ + if(ac[u].next[k]!=-1) return ac[u].next[k]; + if(u==0) return 0; + return fail(ac[u].fal,k); +} +void bulid() +{ + queueq; + q.push(0); + while(!q.empty()) + { + int x=q.front(); q.pop(); + rep(i,26) + if(ac[x].next[i]!=-1) + { + q.push(ac[x].next[i]); + if(x==0) ac[ac[x].next[i]].fal=0; + else ac[ac[x].next[i]].fal=fail(ac[x].fal,i); + } + } + int len=0; + q.push(0); + while(!q.empty()) + { + int x=q.front(); q.pop(); + if(ac[ac[x].fal].sign==true) + ac[x].sign=true; + if(ac[x].sign==false) + ac[x].len=len++; + rep(i,26) + if(ac[x].next[i]!=-1) + q.push(ac[x].next[i]); + } +} +void work() +{ + queueq; q.push(0); + int len=vec.size(); + int Max=0; + while(!q.empty()) + { + int x=q.front(); q.pop(); + int l=ac[x].len; + Max=max(Max,l); + a[l][l]-=n; + a[l][0]+=(n-len); + rep(j,len) + { + int u=x; int y=vec[j]; + int sign=0; + while(true) + { + if(ac[u].next[y]!=-1) + { + if(ac[ac[u].next[y]].sign==false) + a[l][ac[ac[u].next[y]].len]+=1; + sign=1; + break; + } + if(ac[u].next[y]!=-1) sign=1; + if(u==0) break; + u=ac[u].fal; + } + if(sign==0) + a[l][0]++; + } + rep(i,26) + if(ac[x].next[i]!=-1 && ac[ac[x].next[i]].sign==false) + q.push(ac[x].next[i]); + } + repf(i,0,Max) a[i][Max+1]=n; + n=Max+1,m=Max+2; +} +void guess() +{ + rep(i,n) + { + int k=-1; + repf(j,i,n-1) + if(a[j][i]!=0){ + k=j; break; + } + if(k==-1) continue; + if(k!=i) + rep(j,m) swap(a[i][j],a[k][j]); + rep(j,n) + { + if(j!=i) + { + double x=a[i][i],y=a[j][i]; + rep(k,m) + a[j][k]=a[j][k]-a[i][k]*y/x; + } + } + } + if(fabs(0-a[0][0]) +#include +#include +#include +#include +const int maxn = 555; +const int maxisn = 10; +const double eps = 1e-8; +const double pi = acos(-1.0); +int dcmp(double x) +{ + if(x > eps) return 1; + return x < -eps ? -1 : 0; +} +inline double min(double a, double b) +{return a < b ? a : b;} +inline double max(double a, double b) +{return a > b ? a : b;} +inline double Sqr(double x) +{return x * x;} +struct Point +{ + double x, y; + Point(){x = y = 0;} + Point(double a, double b) + {x = a, y = b;} + inline Point operator-(const Point &b)const + {return Point(x - b.x, y - b.y);} + inline Point operator+(const Point &b)const + {return Point(x + b.x, y + b.y);} + inline double dot(const Point &b)const + {return x * b.x + y * b.y;} + inline double cross(const Point &b, const Point &c)const + {return (b.x - x) * (c.y - y) - (c.x - x) * (b.y - y);} +}; +Point LineCross(const Point &a, const Point &b, const Point &c, const Point &d) +{ + double u = a.cross(b, c), v = b.cross(a, d); + return Point((c.x * v + d.x * u) / (u + v), (c.y * v + d.y * u) / (u + v)); +} +double PolygonArea(Point p[], int n) +{ + if(n < 3) return 0.0; + double s = p[0].y * (p[n - 1].x - p[1].x); + p[n] = p[0]; + for(int i = 1; i < n; ++ i) + s += p[i].y * (p[i - 1].x - p[i + 1].x); + return fabs(s * 0.5); +} +double CPIA(Point a[], Point b[], int na, int nb)//ConvexPolygonIntersectArea +{ + Point p[maxisn], tmp[maxisn]; + int i, j, tn, sflag, eflag; + a[na] = a[0], b[nb] = b[0]; + memcpy(p, b, sizeof(Point) * (nb + 1)); + for(i = 0; i < na && nb > 2; ++ i) + { + sflag = dcmp(a[i].cross(a[i + 1], p[0])); + for(j = tn = 0; j < nb; ++ j, sflag = eflag) + { + if(sflag >= 0) tmp[tn ++] = p[j]; + eflag = dcmp(a[i].cross(a[i + 1], p[j + 1])); + if((sflag ^ eflag) == -2) + tmp[tn ++] = LineCross(a[i], a[i + 1], p[j], p[j + 1]); + } + memcpy(p, tmp, sizeof(Point) * tn); + nb = tn, p[nb] = p[0]; + } + if(nb < 3) return 0.0; + return PolygonArea(p, nb); +} +double SPIA(Point a[], Point b[], int na, int nb)//SimplePolygonIntersectArea +{ + int i, j; + Point t1[4], t2[4]; + double res = 0, if_clock_t1, if_clock_t2; + a[na] = t1[0] = a[0], b[nb] = t2[0] = b[0]; + for(i = 2; i < na; ++ i) + { + t1[1] = a[i - 1], t1[2] = a[i]; + if_clock_t1 = dcmp(t1[0].cross(t1[1], t1[2])); + if(if_clock_t1 < 0) std::swap(t1[1], t1[2]); + for(j = 2; j < nb; ++ j) + { + t2[1] = b[j - 1], t2[2] = b[j]; + if_clock_t2 = dcmp(t2[0].cross(t2[1], t2[2])); + if(if_clock_t2 < 0) std::swap(t2[1], t2[2]); + res += CPIA(t1, t2, 3, 3) * if_clock_t1 * if_clock_t2; + } + } + return PolygonArea(a, na) + PolygonArea(b, nb) - res; +} +Point p1[maxn], p2[maxn]; +int n1, n2; +int main() +{ + int i; + while(scanf("%d%d", &n1, &n2) != EOF) + { + for(i = 0; i < n1; ++ i) scanf("%lf%lf", &p1[i].x, &p1[i].y); + for(i = 0; i < n2; ++ i) scanf("%lf%lf", &p2[i].x, &p2[i].y); + printf("%.2f\n", SPIA(p1, p2, n1, n2) + eps); + } + return 0; +} diff --git a/HDOJ/3061_autoAC.cpp b/HDOJ/3061_autoAC.cpp new file mode 100644 index 0000000..69d86ff --- /dev/null +++ b/HDOJ/3061_autoAC.cpp @@ -0,0 +1,121 @@ +#include +#include +#define VM 10000 +#define EM 120000 +#define inf 0x3f3f3f3f +struct E +{ + int to,cap,nxt; +}edge[EM]; +int head[VM],gap[VM],dist[VM],cur[VM],pre[VM]; +int ep; +void addedge (int cu,int cv,int cw) +{ + edge[ep].to = cv; + edge[ep].cap = cw; + edge[ep].nxt = head[cu]; + head[cu] = ep; + ep ++; + edge[ep].to = cu; + edge[ep].cap = 0; + edge[ep].nxt = head[cv]; + head[cv] = ep; + ep ++; +} +int min (int a ,int b) +{ + return a > b ? b : a; +} +int sap (int src,int des,int n) +{ + memset (dist,0,sizeof(dist)); + memset (gap,0,sizeof (dist)); + memcpy (cur,head,sizeof(dist)); + int res = 0; + int u = pre[src] = src; + int aug = inf; + gap[0] = n; + while (dist[src] < n) + { +loop: + for (int &i = cur[u];i != -1;i = edge[i].nxt) + { + int v = edge[i].to; + if (edge[i].cap && dist[u] == dist[v] + 1) + { + aug = min (aug,edge[i].cap); + pre[v] = u; + u = v; + if (v == des) + { + res += aug; + for (u = pre[u];v != src;v = u,u = pre[u]) + { + edge[cur[u]].cap -= aug; + edge[cur[u]^1].cap += aug; + } + aug = inf; // + } + goto loop; + } + } + int mindist = n; // + for (int i = head[u];i != -1;i = edge[i].nxt) + { + int v = edge[i].to; + if (edge[i].cap && mindist > dist[v]) + { + cur[u] = i; + mindist = dist[v]; + } + } + if ((--gap[dist[u]]) == 0) + break; + dist[u] = mindist + 1; + gap[dist[u]] ++; + u = pre[u]; + } + return res; +} +int vis[VM]; +void dfs(int u,int v) +{ + if(u==v) return ; + vis[u]=1; + for(int i=head[u];i!=-1;i=edge[i].nxt) + if(edge[i].cap>0&&!vis[edge[i].to]) + dfs(edge[i].to,v); +} +int main () +{ + int n,m,u,v,p; + int src,des; + int sum; + while (scanf("%d %d",&n,&m)!=EOF) + { + ep = 0; + sum=0; + src = 0; + des = n + 1; + memset (head,-1,sizeof(head)); + for(int i=1;i<=n;i++) + { + scanf("%d",&p); + if(p>0) + { + addedge(src,i,p); + sum+=p; + } + else + addedge(i,des,-p); + } + for (int i=1; i<=m; i++) + { + scanf("%d%d",&u,&v); + addedge(u,v,inf); + } + int max_flow=sap(src,des,n + 2);; + printf("%d\n",sum-max_flow); + } + return 0; +} diff --git a/HDOJ/3062_autoAC.cpp b/HDOJ/3062_autoAC.cpp new file mode 100644 index 0000000..9ba39b3 --- /dev/null +++ b/HDOJ/3062_autoAC.cpp @@ -0,0 +1,104 @@ +#include +#include +#include +using namespace std; +int n,m; +struct edge_node +{ + int to; + int next; +}; +edge_node edge[5000000]; +int head[100000]; +int dfn[100000]; +int low[100000]; +int visit[100000]; +int N; +stacksta; +void tarjan_bfs(int cur,int time) +{ + visit[cur]=1; + visit[cur^1]=1; + low[cur]=dfn[cur]=time; + sta.push(cur); + for(int i=head[cur];i!=-1;i=edge[i].next) + { + if(visit[edge[i].to]==0) + { + tarjan_bfs(edge[i].to,time++); + time++; + } + if(visit[edge[i].to]==1&&low[cur]>low[edge[i].to]) + { + low[cur]=low[edge[i].to]; + } + } + if(dfn[cur]==low[cur]) + { + N++; + while(!sta.empty()&&sta.top()!=cur) + { + low[sta.top()]=N; + visit[sta.top()]=2; + sta.pop(); + } + } + return; +} +void tarjan() +{ + N=0; + memset(dfn,0,sizeof(dfn)); + memset(low,0,sizeof(low)); + memset(visit,0,sizeof(visit)); + for(int i=0;i<2*n;i++) + { + if(visit[i]==0) + { + tarjan_bfs(i,1); + } + } + return; +} +int main() +{ + while(scanf("%d%d",&n,&m)!=EOF) + { + memset(head,-1,sizeof(head)); + int counter=0; + while(m--) + { + int a1,a2,c1,c2; + scanf("%d%d%d%d",&a1,&a2,&c1,&c2); + int temp1; + int temp2; + temp1=2*a1+c1; + temp2=2*a2+c2; + edge[counter].to=temp2; + edge[counter].next=head[temp1^1]; + head[temp1^1]=counter++; + edge[counter].to=temp1; + edge[counter].next=head[temp2^1]; + head[temp2^1]=counter++; + } + tarjan(); + int sign=1; + for(int i=0;i<2*n;i++) + { + if(low[i]==low[i^1]) + { + sign=0; + break; + } + } + if(sign==1) + { + printf("YES\n"); + } + else + { + printf("NO\n"); + } + } + return 0; +} diff --git a/HDOJ/3063_autoAC.cpp b/HDOJ/3063_autoAC.cpp new file mode 100644 index 0000000..543b6b6 --- /dev/null +++ b/HDOJ/3063_autoAC.cpp @@ -0,0 +1,29 @@ +#include +#include +int main() +{ + using namespace std; + long long n; + long long sum; + int len; + int i; + int p=4*9999; + char num[110]; + while(scanf("%s",num)!=EOF) + { + n=0; + len=strlen(num); + for(i=0; ip) + { + n=n%p; + } + } + n=n%p; + sum=n*(n+1)*(n+2)*(n+3)%p; + cout< +#include +#include +#include +using namespace std; +char ss[1005][55], T[2000005]; +int cnt[1005]; +struct Node +{ + int flag; + Node *ch[26], *fail; +}; +void getstr( char *s ) +{ + int p = 0; + char c; + while( c = getchar(), c == '\n' ) ; + s[p++] = c; + while( c = getchar(), c != '\n' ) + s[p++] = c; + s[p] = '\0'; +} +Node *init( ) +{ + Node *p = new Node; + p->flag = -1; + p->fail = NULL; + memset( p->ch, 0, sizeof( p->ch ) ); + return p; +} +void BuildTree( Node *p, char *in, int No ) +{ + int dx = *in - 'A'; + if( *in ) + { + if( !p->ch[dx] ) + p->ch[dx] = init(); + BuildTree( p->ch[dx], in + 1, No ); + } + else + p->flag = No; +} +void BuildFailPoint( Node *r ) +{ + queueq; + q.push( r ); + while( !q.empty() ) + { + Node *pos = q.front(); + q.pop(); + for( int i = 0; i <26; ++i ) + { + Node *f = pos->fail; + if( pos->ch[i] ) + { + while( f ) + { + if( f->ch[i] ) + { + pos->ch[i]->fail = f->ch[i]; + break; + } + f = f->fail; + } + if( !f ) + { + pos->ch[i]->fail = r; + } + q.push( pos->ch[i] ); + } + } + } +} +void AcAuto( Node *r, char *text ) +{ + int len = strlen( text ); + Node *f = r; + for( int i = 0; i < len; ++i ) + { + int dx = text[i] - 'A'; + if( dx < 0 || dx >= 26 ) + { + f = r; + continue; + } + while( f ) + { + if( f->ch[dx] ) + { + f = f->ch[dx]; + break; + } + f = f->fail; + } + if( !f ) + f = r; + else + { + Node *t = f; + while( t ) + { + if( t->flag!= -1 ) + { + cnt[t->flag]++; + } + t = t->fail; + } + } + } +} +void _free( Node *p ) +{ + for( int i = 0; i < 26; ++i ) + { + if( p->ch[i] ) + _free( p->ch[i] ); + } + free( p ); +} +int main() +{ + int N; + while( scanf( "%d", &N ) != EOF ) + { + Node *r = init(); + memset( cnt, 0, sizeof( cnt ) ); + for( int i = 0; i < N; ++i ) + { + getstr( ss[i] ); + BuildTree( r, ss[i], i ); + } + BuildFailPoint( r ); + getstr( T ); + AcAuto( r, T ); + for( int i = 0; i < N; ++i ) + { + if( cnt[i] ) + printf( "%s: %d\n", ss[i], cnt[i] ); + } + _free( r ); + } + return 0; +} diff --git a/HDOJ/3067_autoAC.cpp b/HDOJ/3067_autoAC.cpp new file mode 100644 index 0000000..6ccede3 --- /dev/null +++ b/HDOJ/3067_autoAC.cpp @@ -0,0 +1,27 @@ +#include +int main() +{ + int i,a,n,sum,ans; + while(~scanf("%d",&n)) + { + for(i = 0,sum = 0; i +#include +#include +#include +#include +#include +using namespace std; +char s[211000],c[111000]; +int p[211000]; +void init() +{ + int i,j; + s[0]='@'; + for(i=0;c[i]!=0;i++) + { + s[2*i+1]='#'; + s[2*i+2]=c[i]; + } + s[2*i+1]='#'; + s[2*i+2]=0; +} +int manacher() +{ + int id=0,mx=0,i; + for(i=1;s[i]!=0;i++) + { + p[i]=mx>i?min(p[2*id-i],mx-i):1; + while(s[i+p[i]] == s[i-p[i]])p[i]++; + if(i+p[i]>mx) + { + mx=i+p[i]; + id=i; + } + } + mx=0; + for(i=1;s[i]!=0;i++) + { + if(p[i]>mx)mx=p[i]; + } + return mx-1; +} +int main() +{ + while(~scanf("%s",c)) + { + init(); + printf("%d\n",manacher()); + } + return 0; +} diff --git a/HDOJ/3069_autoAC.cpp b/HDOJ/3069_autoAC.cpp new file mode 100644 index 0000000..01910d9 --- /dev/null +++ b/HDOJ/3069_autoAC.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +using namespace std; +#define maxn 1005 +int head[maxn],tot,u,v,n; +struct Edge +{ + int to,next; +}e[maxn*2]; +void addedge(int u,int v) +{ + e[tot].to=v; + e[tot].next=head[u]; + head[u]=tot++; +} +int dfs(int u,int pre) +{ + int ans=-1,cnt=0; + vector check; + for(int i=head[u];i!=-1;i=e[i].next) + { + int v=e[i].to,t; + if(v==pre) continue; + t=dfs(v,u); + check.push_back(t); + ans=max(ans,t); + } + if(check.size()==0) return 1; + for(int i=0;i1) return ans+1; + else return ans; +} +int main() +{ + int n; + while(scanf("%d",&n)!=EOF) + { + tot=0; + memset(head,-1,sizeof(head)); + for(int i=0;i +#include +using namespace std; +#define MAXN 150 +#define MOD 20090829 +__int64 dp[MAXN][MAXN],c[MAXN][MAXN]; +__int64 a[MAXN]; +__int64 b[MAXN]; +int gcd(int x,int y) +{ + if (y==0) return x; + else return gcd(y,x%y); +} +__int64 C(int k,int n) +{ + int i,j; + if (c[k][n]!=0) return c[k][n]; + __int64 temp,ans; + for(i=n-k+1;i<=n;i++) a[i]=i; + for(i=1;i<=k;i++) b[i]=i; + for(i=1;i<=k;i++) + for(j=n-k+1;j<=n;j++) + { + temp=gcd(b[i],a[j]); + b[i]/=temp; + a[j]/=temp; + } + ans=1; + for(i=n-k+1;i<=n;i++) + ans=ans*a[i]%MOD; + c[k][n]=ans; + return ans; +} +int min(int x,int y) +{ + if (x +const int maxn=444444; +int prime[]={ 2, 3, 5, 7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97}; +int dpos[]={28,25,23,21,20,19,18,17,16,15,14,13,12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; +int a[]={1,2,4,8,16,32,64}; +int b[]={1,3,9,27,81}; +int c[]={1,5,25}; +int d[]={1,7,49}; +#define lson l,mid,lrt +#define rson mid+1,r,rrt +#define mid ((l+r)>>1) +#define lrt rt<<1 +#define rrt rt<<1|1 +int MAX[maxn],MIN[maxn]; +inline int turn(int x){ + int cnt,y=0; + for(int i=0;i<25&&x>1;i++){ + for(cnt=0;x%prime[i]==0;x/=prime[i]) cnt++; + y|=cnt<>dpos[0];y=y*a[k]%p;x^=k<>dpos[1];y=y*b[k]%p;x^=k<>dpos[2];y=y*c[k]%p;x^=k<>dpos[3];y=y*d[k]%p;x^=k<(y)?(x):(y)) +inline int min(int x,int y){ + return _min(x&0x70000000,y&0x70000000)|_min(x&0x0e000000,y&0x0e000000)|_min(x&0x01800000,y&0x01800000)|_min(x&0x00600000,y&0x00600000)|((x&0x001fffff)&(y&0x001fffff)); +} +inline int max(int x,int y){ + return _max(x&0x70000000,y&0x70000000)|_max(x&0x0e000000,y&0x0e000000)|_max(x&0x01800000,y&0x01800000)|_max(x&0x00600000,y&0x00600000)|((x&0x001fffff)|(y&0x001fffff)); +} +inline void pushup(int rt){ + MAX[rt]=max(MAX[lrt],MAX[rrt]); + MIN[rt]=min(MIN[lrt],MIN[rrt]); +} +void build(int l,int r,int rt){ + if(l==r){ + int x;scanf("%d",&x); + MIN[rt]=MAX[rt]=turn(x); + return; + } + build(lson);build(rson); + pushup(rt); +} +void update(int k,int x,int l,int r,int rt){ + if(l==r){ + MAX[rt]=MIN[rt]=turn(x); + return; + } + if(k<=mid) update(k,x,lson); + else update(k,x,rson); + pushup(rt); +} +int query_max(int s,int t,int l,int r,int rt){ + if(s<=l&&t>=r) return MAX[rt]; + int ret=0; + if(s<=mid) ret=max(ret,query_max(s,t,lson)); + if(t>mid) ret=max(ret,query_max(s,t,rson)); + return ret; +} +int query_min(int s,int t,int l,int r,int rt){ + if(s<=l&&t>=r) return MIN[rt]; + int ret=0x7fffffff; + if(s<=mid) ret=min(ret,query_min(s,t,lson)); + if(t>mid) ret=min(ret,query_min(s,t,rson)); + return ret; +} +int main() +{ + int n,q; + while(scanf("%d%d",&n,&q)!=EOF){ + build(1,n,1); + char s[2]; + while(q--){ + scanf("%s",s); + if(s[0]=='C'){ + int k,v; + scanf("%d%d",&k,&v); + update(k,v,1,n,1); + } + else if(s[0]=='L'){ + int k1,k2,p; + scanf("%d%d%d",&k1,&k2,&p); + int x=query_max(k1,k2,1,n,1); + printf("%u\n",back(x,p)); + } + else{ + int k1,k2,p; + scanf("%d%d%d",&k1,&k2,&p); + int x=query_min(k1,k2,1,n,1); + printf("%u\n",back(x,p)); + } + } + } + return 0; +} diff --git a/HDOJ/3072_autoAC.cpp b/HDOJ/3072_autoAC.cpp new file mode 100644 index 0000000..985065b --- /dev/null +++ b/HDOJ/3072_autoAC.cpp @@ -0,0 +1,211 @@ +#include +#include +#include +#include +#define INF 0x7ffffff +#define mm(x) memset(x,0,sizeof(x)) +using namespace std; +const int N=50000+9; +const int M=100000+9; +stacks; +struct node +{ + int to,next; + int w; +}G[M]; +struct edge +{ + int u,v,w; +}NG[M]; +int indegree[N]; +int instack[N],paint[N],col,index,dfn[N],low[N]; +int head[N],cnt,Ncnt; +class tarjan +{ + public: + int MIN(int a,int b) + { + if(a +#include +using namespace std; +#define N 50005 +const int MOD = 1000000007; +#define LL(x) ((x)<<1) +#define RR(x) ((x)<<1|1) +struct Seg_Tree +{ + int left,right,mid; + __int64 num; +}bt[3*N]; +__int64 num[N]; +void build(int l,int r,int nid) +{ + bt[nid].left=l; + bt[nid].right=r; + bt[nid].mid=(l+r)>>1; + if(l!=r) + { + build(l,bt[nid].mid,LL(nid)); + build(bt[nid].mid+1,r,RR(nid)); + bt[nid].num=(bt[LL(nid)].num%MOD)*(bt[RR(nid)].num%MOD)%MOD; + } + else + bt[nid].num=num[l]; +} +void update(int id,int x,int nid) +{ + if(bt[nid].left==bt[nid].right) + { + bt[nid].num=x; + return ; + } + if(id<=bt[nid].mid) + update(id,x,LL(nid)); + else + update(id,x,RR(nid)); + bt[nid].num=(bt[2*nid].num*bt[2*nid+1].num)%MOD; +} +__int64 query(int l,int r,int nid) +{ + if(l==bt[nid].left&&r==bt[nid].right) + return bt[nid].num%MOD; + if(r<=bt[nid].mid) + return query(l,r,LL(nid))%MOD; + else if(l>bt[nid].mid) + return query(l,r,RR(nid))%MOD; + else + return (query(l,bt[nid].mid,LL(nid))%MOD)*(query(bt[nid].mid+1,r,RR(nid))%MOD)%MOD; +} +int main() +{ + int T,i,j; + scanf("%d",&T); + while(T--) + { + int n; + scanf("%d",&n); + for(i = 1 ; i <= n; i ++) + scanf("%I64d",&num[i]); + build(1,n,1); + int cas,oper,a,b; + scanf("%d",&cas); + while(cas--) + { + scanf("%d",&oper); + scanf("%d%d",&a,&b); + switch(oper) + { + case 0: + printf("%I64d\n",query(a,b,1)); + break; + case 1: + update(a,b,1); + break; + default: + break; + } + } + } + return 0; +} diff --git a/HDOJ/3076_autoAC.cpp b/HDOJ/3076_autoAC.cpp new file mode 100644 index 0000000..2adbc6c --- /dev/null +++ b/HDOJ/3076_autoAC.cpp @@ -0,0 +1,55 @@ +#include +using namespace std; +#define N 2005 +double a[7],b[7],dp[N][N]; +double pa,pb,ptie; +int hp1,hp2; +int main() +{ + int i,j; + while(scanf("%d%d",&hp1,&hp2)!=EOF) + { + swap(hp1,hp2); + for(i=1;i<=6;i++) + scanf("%lf",&a[i]); + for(i=1;i<=6;i++) + scanf("%lf",&b[i]); + pa=pb=0; + for(i=2;i<=6;i++) + { + for(j=1;j<=i-1;j++) + { + pa+=a[i]*b[j]; + pb+=a[j]*b[i]; + } + } + ptie=1-pa-pb; + if(ptie!=1) + { + pa=pa/(1-ptie); + pb=pb/(1-ptie); + } + //dp[i][j]->probability of A won i,B won j + dp[0][0]=1; + for(i=0;i1) + ans=1; + printf("%.6lf\n",ans); + } +} diff --git a/HDOJ/3078_autoAC.cpp b/HDOJ/3078_autoAC.cpp new file mode 100644 index 0000000..ba59ef4 --- /dev/null +++ b/HDOJ/3078_autoAC.cpp @@ -0,0 +1,118 @@ +#include +#include +#include +#include +#include +#include +using namespace std; +#define N 80010 +int __pow[25]; +int fa[N],val[N],p[N]; +int node[2*N],first[N],dep[2*N],dp[2*N][25]; +bool vis[N]; +vectore[N]; +void dfs(int &index , int u ,int d , int par) +{ + ++index; vis[u] = true; + first[u] = index; node[index] = u; dep[index] = d; fa[u] = par; + for(int i=0; i y) swap(x,y); + int index = RMQ(x,y); + return node[index]; +} +bool cmp(int a, int b) +{ + return a > b; +} +void path(int &index , int s , int t) +{ + while(s != t) + { + p[index++] = val[s]; + s = fa[s]; + } + p[index++] = val[t]; +} +void solve(int kth , int u,int v) +{ + int lca = LCA(u,v); + int tot = 0; + path(tot,u,lca); + path(tot,v,lca); + tot--; + if(kth > tot) + { + printf("invalid request!\n"); + return ; + } + sort(p,p+tot,cmp); + printf("%d\n",p[kth-1]); +} +int main() +{ + for(int i=0; i<25; i++) __pow[i] = 1 << i; + int n,q; + scanf("%d%d",&n,&q); + for(int i=1; i<=n; i++) scanf("%d",&val[i]); + for(int i=1; i +#include +int main() +{ + int i,j,t; + char str[55]; + while(scanf("%d",&t)!=EOF) + { + for(i=0;i='A' && str[j]<='Z') + str[j]+=32; + } + printf("%s\n",str); + } + } + return 0; +} diff --git a/HDOJ/3080_autoAC.cpp b/HDOJ/3080_autoAC.cpp new file mode 100644 index 0000000..cb0a94e --- /dev/null +++ b/HDOJ/3080_autoAC.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +using namespace std; +const int MAX=210; +int n,cnt,ret; +int p[MAX],ran[MAX]; +bool ext[MAX]; +struct Edge +{ + int from; + int to; + int weight; + bool operator<(const Edge& edge) const + { + if(ext[from]==false||ext[to]==false) + return false; + if(ext[edge.from]==false||ext[edge.to]==false) + return true; + return weight +#include +using namespace std; +int mp[210][210]; +int m[210][210]; +int set[410]; +int N, M, f, sum, ans; +const int inf = 0x7f7f7f7f; +bool Find; +int vh[410]; +int h[410]; +int find( int x) +{ + return x == set[x] ? x : set[x] = find( set[x] ); +} +void init_flow( ) +{ + memset(h, 0, sizeof(h)); + memset(vh, 0, sizeof(vh)); +} +void init( ) +{ + for( int i = 0; i < 110; i++) + set[i] = i; + memset(mp, 0, sizeof(mp)); + memset(m, 0, sizeof(m)); +} +//婧0锛姹N + 1 +void make_graph(int mid) +{ + //婧瑰板濂崇 + memset(mp, 0, sizeof(mp)); + for( int i = 1; i <= N; i++) + { + mp[0][i] = mid; + } + //风版 + for( int i = N + 1; i <= 2 * N; i++) + { + mp[i][2 * N + 1] = mid; + } + for( int i = 1; i <= 2 * N; i++) + for( int j = 1; j <= 2 * N; j++) + mp[i][j] = m[i][j]; + //崇郴浠ヤ + for( int i = 1; i <= 2 * N; i++) + { + for( int j = 1; j <= 2 * N; j++) + { + if( set[i] == set[j]) + { + for( int k = 1; k <= 2 * N; k++) + { + if ( mp[j][k] && k != i) + mp[i][k] = 1; + } + } + } + } +} +void dfs( int x ) +{ + int hmin = 2 * N + 1, i; + if( x == 2 * N + 1) + { + sum += ans; + Find = true; + return; + } + for( i = 0; i <= 2 * N + 1; i++) + { + if( mp[x][i] > 0) + { + if( h[x] == h[i] + 1 ) + { + if( mp[x][i] < ans ) + ans = mp[x][i]; + dfs( i ); + if( Find ) + break; + if( h[0] >= 2 * N + 1 ) + return; + } + if( h[i] < hmin ) + hmin = h[i]; + } + } + if( Find ) + { + mp[x][i] -= ans; + mp[i][x] += ans; + } + else + { + vh[h[x]]--; + if( vh[h[x]] == 0 ) + h[0] = 2 * N + 1; + h[x] = hmin + 1; + vh[h[x]]++; + } +} +int sap( ) +{ + sum = 0; + init_flow( ); + vh[0] = 2 * N + 2; + while( h[0] < 2 * N + 2 ) + { + ans = inf; + Find = false; + dfs( 0 ); + } + return sum; +} +int solve( ) +{ + int l = 0, r = N, temp = 0; + while( l <= r ) + { + int mid = (l + r) / 2; + make_graph(mid); + if( sap( ) == mid * N ) + { + temp = mid; + l = mid + 1; + } + else + r = mid - 1; + } + return temp; +} +int main( ) +{ + int T, a, b; + cin>>T; + while( T-- ) + { + cin>>N>>M>>f; + init( ); + for( int i = 1; i <= M; i++) + { + cin>>a>>b; + mp[a][b + N] = 1; + m[a][b + N] = 1; + } + for( int i = 1; i <= f; i++) + { + cin>>a>>b; + int x = find( a ); + int y = find( b ); + if( x != y ) + set[x] = y; + } + for( int i = 1; i <= N; i++) + { + set[i] = find( i ); + } + printf("%d\n", solve( )); + } + return 0; +} diff --git a/HDOJ/3082_autoAC.cpp b/HDOJ/3082_autoAC.cpp new file mode 100644 index 0000000..e05f678 --- /dev/null +++ b/HDOJ/3082_autoAC.cpp @@ -0,0 +1,43 @@ +#include +using namespace std; +int main() +{ + int t; + cin >> t; + while(t--) + { + char buf[120]; + int n; + cin >> n; + int dz[22]; + for(int j = 0; j < n; j++) + { + int cnt = 0; + int a=0, sum = 0; + getchar(); + gets(buf); + int len = strlen(buf); + for(int i = 0; i < len; i++) + { + if(isdigit(buf[i])) + { + a = a * 10 + buf[i] - '0'; + } + else + { + sum += a; + a = 0; + } + } + dz[j] = sum; + } + double result = 0; + for(int i = 0 ; i < n; i++) + { + result += 1.0 / dz[i]; + } + result = 1.0 / result; + printf("%.2lf\n", result); + } + return 0; +} diff --git a/HDOJ/3083_autoAC.cpp b/HDOJ/3083_autoAC.cpp new file mode 100644 index 0000000..f10be1e --- /dev/null +++ b/HDOJ/3083_autoAC.cpp @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +const int MOD = 20091226; +typedef long long LL; +inline int getS(LL n) { + LL mod = MOD * 6; + LL ret = (n % mod) * ((n + 1) % mod); + ret %= mod; + ret *= (n + 2) % mod; + ret %= mod; + ret /= 6; + return (int)ret; +} +inline int getX(LL n) { + if(n <= 1) { + return 0; + } + int mod = MOD * 24; + LL ret = (n + 1) % mod; + if(n % 2 == 1) { + ret = (((2 * ret - 3) * ret) % mod - 2) * ret; + }else { + ret = (n % mod) * ((n + 2) % mod); + ret %= mod; + ret *= (2 * n - 1) % mod; + } + ret %= mod; + ret /= 24; + return ret; +} +inline int getP(LL n) { + if(n < 1) { + return 0; + } + int mod = MOD * 8; + LL ret = n % mod; + ret = ((((ret * (ret + 1))% mod) * (ret + 2))% mod) * (ret + 3); + ret %= mod; + ret /= 8; + return ret; +} +int main() { + int n; + LL t, d, p, s, x; + while(scanf("%d", &n) == 1) { + s = getS(n); + x = getX(n); + t = s + x; + d = 3 * x; + p = getP(n - 1); + printf("Triangle: %d\n", t % MOD); + printf("Diamond: %d\n", d % MOD); + printf("Parallelogram: %d\n", p % MOD); + } + return 0; +} diff --git a/HDOJ/3085_autoAC.cpp b/HDOJ/3085_autoAC.cpp new file mode 100644 index 0000000..5d8231c --- /dev/null +++ b/HDOJ/3085_autoAC.cpp @@ -0,0 +1,107 @@ +#include +#include +#include +#include +#include +#include +#include +#define M 800 +using namespace std; +struct node +{ + int x,y; + node(){} + node(int xx,int yy) + { + x=xx; + y=yy; + } +}gg,mm,zz[2],f[2][M*M]; +int step,front[2],rear[2]; +char ma[M][M]; +int n,m; +int dir[4][2]={{-1,0},{0,1},{0,-1},{1,0}}; +bool maphaten(int x,int y) +{ + if(x>=0&&x=0&&yq[3]; +void clear() +{ + for(int i=0;i<3;i++) + while(!q[i].empty()) + q[i].pop(); +} +bool bfs(int num,int move_step,char s,char e) +{ + q[2]=q[num]; + for(int i=0;i +#include +using namespace std; +const double eps = 1e-8; +int main() +{ + int t; + double s, ax, bx, ay, by; + double a, b, c, p, ans1, ans2; + scanf("%d", &t); + while(t --) + { + scanf("%lf%lf%lf%lf%lf", &s, &ax, &bx, &ay, &by); + a = (ax - ay) / 2; + b = bx - by; + c = s; + if(fabs(a) < eps) + { + if(b > eps) + { + printf("Good driver,Jiaoshou!\n"); + } + else if(fabs(b) < eps) + { + printf("Good driver,Jiaoshou!\n"); + } + else + { + printf("Oh,God!Jiaoshou will be catched after %.3f minutes.\n", -s / b); + } + } + else{ + p = b * b - 4 * a * c; + if(p < -eps) + { + printf("Good driver,Jiaoshou!\n"); + } + else + { + ans1 = (-b - sqrt(p)) / (2 * a); + ans2 = (-b + sqrt(p)) / (2 * a); + if(ans1 < 0 && ans2 < 0) + { + printf("Good driver,Jiaoshou!\n"); + } + else if(ans1 > 0) + { + printf("Oh,God!Jiaoshou will be catched after %.3f minutes.\n", ans1); + } + else + { + printf("Oh,God!Jiaoshou will be catched after %.3f minutes.\n", ans2); + } + } + } + } + return 0; +} diff --git a/HDOJ/3088_autoAC.cpp b/HDOJ/3088_autoAC.cpp new file mode 100644 index 0000000..40c6cdc --- /dev/null +++ b/HDOJ/3088_autoAC.cpp @@ -0,0 +1,98 @@ +#include +#include +#include +#include +using namespace std; +char str[15]; +int map[15]; +int len; +int vis[3][3][3][3][3][3][3][3][3][3]; +int cor[3]; +int flag; +struct node +{ + int m[15]; + int step; +}now,temp; +void bfs() +{ + queue q; + for(int i=0; i<10; i++) + { + now.m[i] = map[i]; + } + vis[map[0]][map[1]][map[2]][map[3]][map[4]][map[5]][map[6]][map[7]][map[8]][map[9]] = 1; + now.step = 0; + q.push(now); + while(!q.empty()) + { + now = q.front(); + q.pop(); + int k; + for(k=0; k= len-1) + { + cout< len) + { + map[i] = 3; + } + if(str[i] == 'r') + map[i] = 0; + else if(str[i] == 'g') + map[i] = 1; + else + map[i] = 2; + } + flag = 0; + bfs(); + if(!flag) + cout<<"No solution!"< +#define LL long long +LL solve(LL n,LL k,LL s=1) +{ + if(k==1) return (n-1+s)%n; + LL ans=0; + for(LL i=2;i<=n;) + { + if(ans+kn) return ((ans+(n+1-i)*k)+s)%n; + i+=leap; + ans+=leap*k; + } + else + { + ans=(ans+k)%i; + i++; + } + } + return (ans+s)%n; +} +int main() +{ + LL n,k; + while(scanf("%I64d%I64d",&n,&k)!=EOF) + { + LL ans=solve(n,k); + if(ans==0) printf("%I64d\n",n); + else printf("%I64d\n",ans); + } +} diff --git a/HDOJ/3090_autoAC.cpp b/HDOJ/3090_autoAC.cpp new file mode 100644 index 0000000..f7ad2c4 --- /dev/null +++ b/HDOJ/3090_autoAC.cpp @@ -0,0 +1,42 @@ +#include +#include +using namespace std; +struct Node +{ + int d,p; +}; +Node roads[10001]; +int n,m; +int sum; +bool cmp(Node n1,Node n2) +{ + return n1.p > n2.p; +} +int main() +{ + while(scanf("%d %d", &n, &m), m!=0 || n!=0) + { + sum = 0; + for(int i=0; i roads[i].d) + { + m -= roads[i].d; + sum -= roads[i].d * roads[i].p; + } + else + { + sum -= m * roads[i].p; + break; + } + } + printf("%d\n",sum); + } + return 0; +} diff --git a/HDOJ/3091_autoAC.cpp b/HDOJ/3091_autoAC.cpp new file mode 100644 index 0000000..358eb82 --- /dev/null +++ b/HDOJ/3091_autoAC.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define INF 99999999 +typedef __int64 LL; +using namespace std; +const int MAX=(1<<18)+10; +int n,m; +LL dp[MAX][20]; +bool edge[20][20]; +void DP(){ + int bit=1< +#include +using namespace std; +const int maxp=44; +int p[maxp]; +int pp; +void sai() +{ +int i,j; +pp=0; +for (i=2;i<3010;i++) +{ + for (j=2;j*j<=i;j++) + if (i%j==0) break; + if (j*j>i) p[pp++]=i; + if (pp>=maxp) break; +} +} +int res[maxp],a[maxp]; +int n,m; +double ber[3010][maxp]={0}; +double fres; +void dfs(int k,double mut,int s) +{ +if (ber[n-s][k]>mut+0.01) return ; +else if (mut>ber[n-s][k]) ber[n-s][k]=mut; +int i=p[k]; +if (k>=pp||p[k]>s) +{ + if (mut>fres) + { + memcpy(res,a,sizeof(a)); + fres=mut; + } + return ; +} +dfs(k+1,mut,s); +for (a[k]=1;i<=s;a[k]++) +{ + mut=mut+log(p[k]); + dfs(k+1,mut,s-i); + i*=p[k]; +} +a[k]=0; +} +void oot() +{ +int i,j,k; +k=1; +for (i=0;i +#include +#include +#include +using namespace std; +const int N = 100000+10; +vector g[N]; +bool vis[N]; +int w[N]; +void dfs(int u) +{ + vis[u]=true; + int size=g[u].size(); + w[u]=0; + for(int i=0;i +#include +int dir[18][2] = +{ + { -1, -1,}, { -1, 0}, {0, -1}, {0, 1}, {1, 0}, {1, 1}, + { -1, -1,}, { -1, 0}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, + { -1, 0,}, { -1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0} +}; +__int64 mlen[21]; +__int64 msle[21]; +__int64 bl, m, l, sn; +__int64 n; +__int64 map[101]; +__int64 res[101]; +__int64 ret[101][101]; +__int64 Mat[101][101]; +__int64 temp[101][101]; +void make_mlen() +{ + int i, j; + sn = bl * 2 - 1; + for( i = 0 ; i < bl; i++) + mlen[i] = i + bl; + for( j = bl - 2; j >= 0 ; j--) + mlen[i++] = mlen[j]; + msle[0] = 0; + for( i = 1; i <= sn ; i++) + msle[i] = msle[i - 1] + mlen[i - 1]; + n = msle[sn]; +} +void input() +{ + int i; + for( i = 0 ; i < n; i++) + scanf("%I64d", &map[i]); +} +void make_matrix() +{ + int i, j, k, ii, jj, kk; + memset(Mat, 0, sizeof(Mat)); + for( i = 0 ; i < sn; i++) + { + for(j = 0 ; j < mlen[i] ; j++) + { + Mat[msle[i] + j][msle[i] + j] = 1; + for( k = 0 ; k < 6 ; k++) + { + if( i < bl - 1 ) + kk = 0; + else if( i == bl - 1) + kk = 6; + else + kk = 12; + ii = i + dir[kk + k][0]; + jj = j + dir[kk + k][1]; + if( ii < 0 || ii >= sn || jj < 0 || jj >= mlen[ii]) + continue; + Mat[msle[i] + j][msle[ii] + jj] = 1; + } + } + } +} +void matmul(__int64 a[][101], __int64 b[][101]) +{ + int i, k, j; + memset(temp, 0, sizeof(temp)); + for(i = 0; i < n; i++) + for(k = 0; k < n; k++) + if(a[i][k]) + for(j = 0; j < n; j++) + if( b[k][j] ) + temp[i][j] = (temp[i][j] + a[i][k] * b[k][j]) % m; + memcpy(a, temp, sizeof(temp)); +} +void init_() +{ + int i, j; + for(i = 0; i < n; i++) + for(j = 0; j < n; j++) + ret[i][j] = (i == j); +} +void q_mod(int k) +{ + init_(); + for(; k ; k >>= 1) + { + if(k & 1) + matmul(ret, Mat); + matmul(Mat, Mat); + } +} +__int64 solve() +{ + __int64 i, j, sum, ans = 0; + for(i = 0; i < n; i++) + { + sum = 0; + for( j = 0; j < n; j++) + { + sum = (sum + map[j] * ret[i][j]) % m; + } + res[i] = sum; + } + for( i = 0 ; i < n ; i++) + ans += res[i]; + return ans; +} +int main() +{ + int i, j, k = 0; + while(scanf("%I64d%I64d%I64d", &bl, &m, &l), bl + m + l) + { + k++; + make_mlen(); + make_matrix(); + input(); + q_mod(l); + printf("Case %d: %I64d\n", k, solve()); + } + return 0; +} diff --git a/HDOJ/3097_autoAC.cpp b/HDOJ/3097_autoAC.cpp new file mode 100644 index 0000000..a47e06a --- /dev/null +++ b/HDOJ/3097_autoAC.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +using namespace std; +const int N=1005; +int pre[N],count[N]; +void build(int n) +{ + int i; + for(i=1;i<=n;i++) {pre[i]=i;count[i]=0;} +} +int find(int k) +{ + if(pre[k]==k) return k; + pre[k]=find(pre[k]); + return pre[k]; +} +void get_map(int m) +{ + int a,b,f1,f2; + while(m--) + { + scanf("%d%d",&a,&b); + f1=find(a); + f2=find(b); + if(f1==f2) count[f1]++; + else + { + pre[f2]=f1; + count[f1]+=count[f2]+1; + } + } +} +int Judge(int n) +{ + int flag=0; + for(int i=1;i<=n;i++) + { + if(pre[i]!=i) continue; + if(count[i]%2) {flag=1;break;} + } + return flag; +} +int main() +{ + int n,m; + while(scanf("%d%d",&n,&m),n||m) + { + build(n); + get_map(m); + if(Judge(n)) cout<<"No"<