Powered By HC TECH : AutoACer Engine

3800-3899
This commit is contained in:
KiritoTRw 2016-09-04 14:17:41 +08:00 committed by GitHub
parent 1bdb3c4e35
commit e7ac37406c
84 changed files with 7615 additions and 0 deletions

128
HDOJ/3802_autoAC.cpp Normal file
View File

@ -0,0 +1,128 @@
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
typedef __int64 LL;
LL mod;
struct Matrix
{
LL m[3][3];
}E,D;
LL a,b,n;
void init()
{
for(int i=1;i<=2;i++)
for(int j=1;j<=2;j++)
E.m[i][j]=(i==j);
D.m[1][1]=1;
D.m[1][2]=1;
D.m[2][1]=1;
D.m[2][2]=0;
}
Matrix Multi(Matrix A,Matrix B)
{
Matrix ans;
for(int i=1;i<=2;i++)
for(int j=1;j<=2;j++)
{
ans.m[i][j]=0;
for(int k=1;k<=2;k++)
ans.m[i][j]=(ans.m[i][j]+A.m[i][k]*B.m[k][j])%mod;
}
return ans;
}
Matrix Pow(Matrix A,LL k)
{
Matrix ans=E;
while(k)
{
if(k&1)
{
k--;
ans=Multi(ans,A);
}
else
{
k/=2;
A=Multi(A,A);
}
}
return ans;
}
LL Fib(LL n)
{
if(n==0)
return 1;
Matrix ans=Pow(D,n);
return ans.m[1][1]%mod;
}
void Print(Matrix A)
{
for(int i=1;i<=2;i++)
{
for(int j=1;j<=2;j++)
cout<<A.m[i][j]<<" ";
cout<<endl;
}
}
LL get(Matrix A,Matrix B,LL fib)
{
Matrix ans=Pow(A,fib-1);
ans=Multi(ans,B);
return ans.m[1][1];
}
LL Pow(LL a,LL b)
{
LL ans=1;
while(b)
{
if(b&1)
{
b--;
ans=(ans*a)%mod;
}
else
{
b/=2;
a=(a*a)%mod;
}
}
return ans;
}
int main()
{
init();
int t;
cin>>t;
init();
while(t--)
{
cin>>a>>b>>n>>mod;
LL aa=(1+Pow(a,(mod-1)/2))%mod;
LL bb=(1+Pow(b,(mod-1)/2))%mod;
if(aa==0||bb==0)
{
cout<<0<<endl;
continue;
}
Matrix T,cnt;
T.m[1][1]=2*(a+b)%mod;
T.m[1][2]=-(a-b)*(a-b)%mod;
T.m[2][1]=1;
T.m[2][2]=0;
cnt.m[1][1]=2*(a+b)%mod;
cnt.m[2][1]=2;
LL tmp;
mod--;
tmp=Fib(n);
mod++;
LL ans=get(T,cnt,tmp);
ans=4*ans%mod;
if(ans<0)
ans+=mod;
cout<<ans<<endl;
}
return 0;
}

211
HDOJ/3803_autoAC.cpp Normal file
View File

@ -0,0 +1,211 @@
#include<stdio.h>
#include<iostream>
using namespace std;
#define sign(a) ((a)>0?1:(((a)<0?-1:0)))
struct point{__int64 x,y;};
__int64 pp1,pp2,qq1,qq2;
__int64 xmult(point p1,point p2,point p0){
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
__int64 xmult(__int64 x1,__int64 y1,__int64 x2,__int64 y2,__int64 x0,__int64 y0){
return (x1-x0)*(y2-y0)-(x2-x0)*(y1-y0);
}
__int64 dmult(point p1,point p2,point p0){
return (p1.x-p0.x)*(p2.x-p0.x)+(p1.y-p0.y)*(p2.y-p0.y);
}
__int64 dmult(__int64 x1,__int64 y1,__int64 x2,__int64 y2,__int64 x0,__int64 y0){
return (x1-x0)*(x2-x0)+(y1-y0)*(y2-y0);
}
__int64 dots_inline(point p1,point p2,point p3){
return !xmult(p1,p2,p3);
}
__int64 dots_inline(__int64 x1,__int64 y1,__int64 x2,__int64 y2,__int64 x3,__int64 y3){
return !xmult(x1,y1,x2,y2,x3,y3);
}
__int64 dot_online_in(point p,point l1,point l2){
return !xmult(p,l1,l2)&&(l1.x-p.x)*(l2.x-p.x)<=0&&(l1.y-p.y)*(l2.y-p.y)<=0;
}
__int64 dot_online_in(__int64 x,__int64 y,__int64 x1,__int64 y1,__int64 x2,__int64 y2){
return !xmult(x,y,x1,y1,x2,y2)&&(x1-x)*(x2-x)<=0&&(y1-y)*(y2-y)<=0;
}
__int64 dot_online_ex(point p,point l1,point l2){
return dot_online_in(p,l1,l2)&&(p.x!=l1.x||p.y!=l1.y)&&(p.x!=l2.x||p.y!=l2.y);
}
__int64 dot_online_ex(__int64 x,__int64 y,__int64 x1,__int64 y1,__int64 x2,__int64 y2){
return dot_online_in(x,y,x1,y1,x2,y2)&&(x!=x1||y!=y1)&&(x!=x2||y!=y2);
}
__int64 same_side(point p1,point p2,point l1,point l2){
return sign(xmult(l1,p1,l2))*xmult(l1,p2,l2)>0;
}
__int64 opposite_side(point p1,point p2,point l1,point l2){
return sign(xmult(l1,p1,l2))*xmult(l1,p2,l2)<0;
}
__int64 parallel(point u1,point u2,point v1,point v2){
return (u1.x-u2.x)*(v1.y-v2.y)==(v1.x-v2.x)*(u1.y-u2.y);
}
__int64 __int64ersect_in(point u1,point u2,point v1,point v2){
if (!dots_inline(u1,u2,v1)||!dots_inline(u1,u2,v2))
return !same_side(u1,u2,v1,v2)&&!same_side(v1,v2,u1,u2);
return dot_online_in(u1,v1,v2)||dot_online_in(u2,v1,v2)||dot_online_in(v1,u1,u2)||dot_online_in(v2,u1,u2);
}
__int64 __int64ersect_ex(point u1,point u2,point v1,point v2){
return opposite_side(u1,u2,v1,v2)&&opposite_side(v1,v2,u1,u2);
}
void __int64ersection(point u1,point u2,point v1,point v2){
point ret=u1;
pp2=qq2=(__int64)(u1.x-u2.x)*(__int64)(v1.y-v2.y)-(__int64)(u1.y-u2.y)*(__int64)(v1.x-v2.x);
pp1=u1.x;
qq1=u1.y;
pp1*=pp2;
qq1*=qq2;
pp1+=((__int64)(u1.x-v1.x)*(__int64)(v1.y-v2.y)-(__int64)(u1.y-v1.y)*(__int64)(v1.x-v2.x))*(__int64)(u2.x-u1.x);
qq1+=((__int64)(u1.x-v1.x)*(__int64)(v1.y-v2.y)-(__int64)(u1.y-v1.y)*(__int64)(v1.x-v2.x))*(__int64)(u2.y-u1.y);
}
point m1,m2,n1,n2;
__int64 ansx[2],ansy[2];
__int64 fab(__int64 k)
{
return k>0?k:-k;
}
__int64 gcd(__int64 m,__int64 n)
{
while (1)
{
m=m%n;
if (m==0) return n;
n=n%m;
if (n==0) return m;
}
}
void dod(__int64 kk1,__int64 kk2,__int64 kk)
{
__int64 t1,t2,t3,t4;
__int64 sign1=1;
if(kk2==0) while(1);
if(kk1==0)
{
ansx[kk]=kk1;
ansy[kk]=1;
return ;
}
if(kk2==1 || kk1==1 )
{
ansx[kk]=kk1;
ansy[kk]=kk2;
return ;
}
if((kk1<0 && kk2>0) || (kk1>0 && kk2<0))
{
sign1=-1;
}
t1=fab(kk1);
t2=fab(kk2);
if(t1>t2)
{
t3=gcd(t1,t2);
t1/=t3;
t2/=t3;
ansx[kk]=sign1*t1;ansy[kk]=t2;
}
else if(t1<t2)
{
t3=gcd(t2,t1);
t1/=t3;
t2/=t3;
ansx[kk]=sign1*t1;ansy[kk]=t2;
}
else {ansx[kk]=sign1*1;ansy[kk]=1;}
}
__int64 check(point aa,point bb)
{
if(aa.x==bb.x && aa.y==bb.y) return 1;
return 0;
}
int main()
{
int t;
int ans=0;
point res;
scanf("%d",&t);
while(t--)
{
ans=0;
cin>>m1.x>>m1.y>>m2.x>>m2.y>>n1.x>>n1.y>>n2.x>>n2.y;
if(__int64ersect_in(m1,m2,n1,n2))
{
if(__int64ersect_ex(m1,m2,n1,n2))
{
ans=1;
__int64ersection(m1,m2,n1,n2);
dod(pp1,pp2,0);
dod(qq1,qq2,1);
}
else if(parallel(m1,m2,n1,n2))
{
if( check(m1,m2) && check(n1,n2))
{
res=m1;
cout<<'1'<<endl<<res.x<<' '<<res.y<<endl;
continue;
}
else if( check(m1,m2) || check(n1,n2))
{
if(check(m1,m2)) {ans=1; res=m1;}
else {ans=1; res=n1;}
if(ans==1) {cout<<'1'<<endl<<res.x<<' '<<res.y<<endl;continue;}
}
else if( 1== check(m1,n1) + check(m1,n2) + check(m2,n1) + check(m2,n2))
{
ans=-1;
if(check(m1,n1))
{
if(dmult(m2,n2,m1)<0) {ans=1;res=m1;}
}
else if(check(m1,n2))
{
if(dmult(m2,n1,m1)<0) {ans=1;res=m1;}
}
else if(check(m2,n1))
{
if(dmult(m1,n2,m2)<0) {ans=1;res=m2;}
}
else if(check(m2,n2))
{
if(dmult(m1,n1,m2)<0) {ans=1;res=m2;}
}
if(ans==1) {cout<<'1'<<endl<<res.x<<' '<<res.y<<endl;continue;}
}
else ans=-1;
}
else
{
if(check(m1,n1) || check(m1,n2))
{
res=m1;ans=1;
}
if(check(m2,n1) || check(m2,n2))
{
res=m2;ans=1;
}
if(ans==1) {cout<<'1'<<endl<<res.x<<' '<<res.y<<endl;continue;}
ans=1;
__int64ersection(m1,m2,n1,n2);
dod(pp1,pp2,0);
dod(qq1,qq2,1);
}
}
if(ans==-1) puts("INF");
else
{
printf("%d\n",ans);
if(ans==1)
{
if(ansy[0]==1) cout<<ansx[0]<<" ";
else cout<<ansx[0]<<'/'<<ansy[0]<<" ";
if(ansy[1]==1) cout<<ansx[1]<<endl;
else cout<<ansx[1]<<'/'<<ansy[1]<<endl;
}
}
}
return 0;
}

115
HDOJ/3804_autoAC.cpp Normal file
View File

@ -0,0 +1,115 @@
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <set>
#include <vector>
#include <stack>
#include <iterator>
using namespace std;
#define N 100005
struct res
{
int i,x;
};
struct pt
{
int p,v;
};
struct que
{
int x,y,ans;
};
vector<pt> a[N];
vector<que> query;
vector<int> hasq[N];
stack<res> stk;
multiset<int> v;
multiset<int>::iterator it;
bool instack[N];
int iv[N];
int main(int argc, char** argv)
{
int tcase,i,j,n,q,x,y,w;
scanf("%d",&tcase);
while(tcase--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
a[i].clear();
iv[1]=-1;
for(i=1;i<=n-1;i++)
{
scanf("%d%d%d",&x,&y,&w);
iv[y]=w;
pt temp;
temp.p=y;
temp.v=w;
a[x].push_back(temp);
temp.p=x;
temp.v=w;
a[y].push_back(temp);
}
scanf("%d",&q);
for(i=1;i<=n;i++)
hasq[i].clear();
for(i=1;i<=q;i++)
query.clear();
for(i=1;i<=q;i++)
{
scanf("%d%d",&x,&y);
que temp;
temp.x=x;
temp.y=y;
temp.ans=-1;
query.push_back(temp);
hasq[x].push_back(query.size()-1);
}
v.clear();
v.insert(-1);
v.insert(-1);
v.insert(-1);
while(!stk.empty()) stk.pop();
memset(instack,0,sizeof(instack));
res temp;
temp.x=1;
temp.i=0;
instack[1]=1;
stk.push(temp);
while(!stk.empty())
{
res now=stk.top();
stk.pop();
if (now.i==0)
{
if (hasq[now.x].size()>0)
for(int i=0;i<hasq[now.x].size();i++)
{
int qi=hasq[now.x][i];
int qx=query[qi].x;
int qy=query[qi].y;
int ans;
it=v.upper_bound(qy);
it--;
ans=(*it);
query[qi].ans=ans;
}
}
if (now.i==a[now.x].size()) {v.erase(v.find(iv[now.x]));continue;}
now.i++;
stk.push(now);
res next;
next.i=0;
next.x=a[now.x][now.i-1].p;
if (!instack[next.x])
{
v.insert(a[now.x][now.i-1].v);
stk.push(next);
instack[next.x]=1;
}
}
for(i=0;i<query.size();i++)
printf("%d\n",query[i].ans);
}
return 0;
}

48
HDOJ/3805_autoAC.cpp Normal file
View File

@ -0,0 +1,48 @@
#include<stdio.h>
int a[1000],b[1000],c[1000];
int main(){
int t,n,i,j,k,a0,b0,c0;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
if(n<5){
if(n==3) printf("6\n1 2\n5 3 4\n");
else printf("9\n5 4\n1 3\n8 2 6 7\n");
continue;
}
if(n&1){
i=3*(n-2);
a0=b0=c0=0;
while(i>0){
c[c0++]=i--;
b[b0++]=i--;
a[a0++]=i--;
if(i<=0) break;
a[a0++]=i--;
b[b0++]=i--;
c[c0++]=i--;
}
}
else{
i=3*(n-2);
c[0]=2;c[1]=6;
b[0]=3;b[1]=4;
a[0]=1;a[1]=5;
a0=b0=c0=2;
while(i>6){
c[c0++]=i--;
b[b0++]=i--;
a[a0++]=i--;
a[a0++]=i--;
b[b0++]=i--;
c[c0++]=i--;
}
}
printf("%d\n",3*(n-1));
for(i=0;i<a0;i++) printf("%d %d\n",a[i],b[i]);
printf("%d",3*(n-1)-1);
for(i=0;i<c0;++i) printf(" %d",c[i]);
printf(" %d\n",3*(n-1)-2);
}
return 0;
}

49
HDOJ/3807_autoAC.cpp Normal file
View File

@ -0,0 +1,49 @@
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
#define INF 0x7f7f7f7f
using namespace std;
int num[1010];
vector<int> ans;
int main()
{
int t, n, x, A, B;
scanf("%d", &t);
while(t--)
{
memset(num, 0, sizeof(num));
ans.clear();
scanf("%d", &n);
for(int i = 0; i < n; ++ i)
{
scanf("%d",&x);
num[x]++;
}
scanf("%d%d", &A, &B);
for(int i = 1; i <= n; ++ i)
{
for(int j = 0; j <= A; ++ j)
{
int lie = n-num[i];
if(i-j >= 0 && lie-j >= 0 && (n-(i-j+lie) >= 0 && n-(i-j+lie) <= B))
{
ans.push_back(i);
break;
}
}
}
if((int)ans.size() == 0)
printf("impossible\n");
else
{
int len = ans.size();
for(int i = 0; i < len-1; ++ i)
printf("%d ", ans[i]);
printf("%d\n", ans[len-1]);
}
}
return 0;
}

25
HDOJ/3808_autoAC.cpp Normal file
View File

@ -0,0 +1,25 @@
#include <iostream>
using namespace std;
char a[11][100]={"Yu Jian","Tin O O","I Missed","Stefanie","The Same","About","Honey Honey","Unfinished","Hey Jude","When Winter Fades"};
void fun(int i)
{
if(i>0 && i<6)
cout<<a[i-1]<<endl;
else if(i>7 && i<13)
cout<<a[i-3]<<endl;
}
int main()
{
int t,n;
cout<<"Best wishes to Stefanie\n\n";
cin>>t;
while(t--)
{
cin>>n;
if(n<14)
fun(n);
else
fun(n%14);
}
return 0;
}

20
HDOJ/3809_autoAC.cpp Normal file
View File

@ -0,0 +1,20 @@
#include<iostream>
#include<stdio.h>
#include<cmath>
using namespace std;
int main(){
int t,tt;
double x,y,x1,y1;
scanf("%d",&t);
tt=t;
while (t--){
scanf("%lf%lf",&x1,&y1);
x=x1,y=y1;
for (int i=0;i<30;i++){
x=x1+sqrt(y);
y=y1+sqrt(x);
}
printf("Case %d: %.6lf %.6lf\n",tt-t,x,y);
}
return 0;
}

117
HDOJ/3810_autoAC.cpp Normal file
View File

@ -0,0 +1,117 @@
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#define LL long long
using namespace std;
const int INF = 1000000000;
const int N = 51;
struct node
{
int t,g;
bool operator<(const node& tt) const
{
if(g == tt.g) return t > tt.t;
return g < tt.g;
}
}s[N],now,ne;
int n,m,tot,min1;
int t[N],g[N],vst[N],num[N];
int adj[N][N];
int d[N][N];
priority_queue<node> q1,q2;
void dfs(int i)
{
for(int j = 1; j <= n; j++)
{
if(vst[j]) continue;
if(adj[i][j])
{
vst[j] = 1;
d[tot][num[tot]++] = j;
dfs(j);
}
}
}
void init()
{
tot = 0;
for(int i = 1; i <= n; i++)
{
if(!vst[i])
{
vst[i] = 1;
d[tot][num[tot]++] = i;
dfs(i);
tot++;
}
}
}
void solve()
{
int i,j,k;
min1 = INF;
for(i = 0; i < tot; i++)
{
while(!q1.empty()) q1.pop();
while(!q2.empty()) q2.pop();
now.t = now.g = 0;
q1.push(now);
for(j = 0; j < num[i]; j++)
{
while(!q1.empty())
{
now = q1.top();
q1.pop();
q2.push(now);
ne.t = now.t+s[d[i][j]].t;
ne.g = now.g+s[d[i][j]].g;
if(ne.g >= m) {min1 = min(min1, ne.t); continue;}
if(ne.t < min1) q2.push(ne);
}
int tem = INF;
while(!q2.empty())
{
now = q2.top();
q2.pop();
if(tem >= now.t)
{
q1.push(now);
tem = now.t;
}
}
}
}
}
int main()
{
int T;
int i,j,k;
scanf("%d", &T);
for(int cc = 1; cc <= T; cc++)
{
scanf("%d%d", &n, &m);
memset(adj, 0, sizeof(adj));
memset(num, 0, sizeof(num));
memset(vst, 0, sizeof(vst));
for(i = 1; i <= n; i++)
{
scanf("%d%d%d", &s[i].t, &s[i].g, &k);
for(j = 0; j < k; j++)
{
int aa;
scanf("%d", &aa);
adj[i][aa] = adj[aa][i] = 1;
}
}
printf("Case %d: ", cc);
init();
solve();
if(min1 == INF) puts("Poor Magina, you can't save the world all the time!");
else printf("%d\n", min1);
}
return 0;
}

55
HDOJ/3811_autoAC.cpp Normal file
View File

@ -0,0 +1,55 @@
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
int n,m;
__int64 fac[18];
int ban[18][18];
__int64 dp[1<<17];
void ini()
{
int i;
fac[0]=1;
for(i=1;i<=17;i++) fac[i]=fac[i-1]*i;
}
void getban()
{
int a,b;
memset(ban,0,sizeof(ban));
while(m--)
{
scanf("%d%d",&a,&b);
a--;b--;
ban[a][b]=1;
}
}
void getdp()
{
int i,j,k;
memset(dp,0,sizeof(dp));
dp[0]=1;
for(i=0;i<n;i++)
for(j=(1<<n)-1;j>=0;j--)
{
if(!dp[j]) continue;
for(k=0;k<n;k++)
{
if(j&(1<<k)) continue;
if(ban[i][k]) continue;
dp[j|(1<<k)]+=dp[j];
}
}
}
int main()
{
int T,Case;
ini();
scanf("%d",&T);
for(Case=1;Case<=T;Case++)
{
scanf("%d%d",&n,&m);
getban();
getdp();
printf("Case %d: %I64d\n",Case,fac[n]-dp[(1<<n)-1]);
}
return 0;
}

133
HDOJ/3812_autoAC.cpp Normal file
View File

@ -0,0 +1,133 @@
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
char real[105][30];
int n,w=0,mm[105][105],st,et;
char temp1[208][30];
int record[105],vis[105],vis1[105];
int yes[105],root[105];
int Find(int r)
{
if(r==root[r]) return r;
else return root[r]=Find(root[r]);
}
int add(char *a)
{
int i;
for(i=0;i<w;i++){
if(strcmp(a,real[i])==0)
return i;
}
strcpy(real[w++],a);
}
void init()
{
int i,num=0,j;
char s1[30],s2[30];
for(i=0;i<n;i++){
scanf("%s %s",s1,s2);
add(s1);
add(s2);
strcpy(temp1[num++],s1);
strcpy(temp1[num++],s2);
}
for(i=0;i<w-1;i++)
for(j=i+1;j<w;j++){
if(strcmp(real[i],real[j])>0){
char temp[30];
strcpy(temp,real[j]);
strcpy(real[j],real[i]);
strcpy(real[i],temp);
}
}
for(i=0;i<w;i++) root[i]=i;
for(i=0;i<num;i+=2){
int nowx=add(temp1[i]),nowy=add(temp1[i+1]);
mm[nowx][nowy]=mm[nowy][nowx]=1;
int ra=Find(nowx),rb=Find(nowy);
if(ra!=rb) root[rb]=ra;
}
}
int ansl=0;
int dfs(int now,int tt)
{
if(tt>=w) return 0;
int i;
vis[now]=1;
record[tt]=now;
if(now==et){
if(ansl<tt) {
ansl=tt;
for(i=0;i<=ansl;i++) yes[i]=record[i];
}
if(tt==w-1) return 1;
}
for(i=0;i<w;i++){
if(vis[i]==0&&mm[now][i]==1){
vis[i]=1;
if(dfs(i,tt+1)) return 1;
vis[i]=0;
}
}
return 0;
}
int dfs1(int ttt,int ending)
{
int i;
if(ttt==ending) return 1;
for(i=0;i<w;i++){
if(!vis1[i]&&mm[ttt][i]&&(!vis[i])){
vis1[i]=1;
if(dfs1(i,ending)) return 1;
//vis1[i]=0;
}
}
return 0;
}
int main()
{
int i,cc,Case=0;
scanf("%d",&cc);
while(cc--){
Case++;
scanf("%d",&n);
memset(vis,0,sizeof(vis));
memset(mm,0,sizeof(mm));
w=0;
init();
st=-1;et=-1;
for(i=0;i<w;i++){
if(strcmp(real[i],"sea")==0) st=i;
if(strcmp(real[i],"sky")==0) et=i;
}
printf("Case %d:",Case);
if(st==-1||et==-1){
printf(" what a pity\n");
continue;
}
if(Find(st)!=Find(et)) {
printf(" what a pity\n");
continue;
}
for(i=0;i<w;i++){
memset(vis1,0,sizeof(vis1));
vis1[st]=1;
if(!dfs1(i,et)) vis[i]=1;
}
for(i=0;i<w;i++){
memset(vis1,0,sizeof(vis1));
vis1[et]=1;
if(!dfs1(i,st)) vis[i]=1;
}
ansl=0;
dfs(st,0);
for(i=0;i<=ansl;i++){
printf(" %s",real[yes[i]]);
}
printf("\n");
}
}

188
HDOJ/3814_autoAC.cpp Normal file
View File

@ -0,0 +1,188 @@
#include<iostream>
#include<algorithm>
#include<cmath>
#define eps 1e-9
using namespace std;
double f_abs(double x){
return x<0?-x:x;
}
struct Point{
double x,y;
bool up,dn;
void disp(){
printf("%lf %lf\n",x,y);
}
void get(){
scanf("%lf%lf",&x,&y);
}
bool friend operator<(Point a,Point b){
if(f_abs(a.x-b.x)<eps)return a.y<b.y;
return a.x<b.x;
}
bool friend operator>(Point a,Point b){
return b<a;
}
bool friend operator==(Point a,Point b){
return f_abs(a.x-b.x)<eps&&f_abs(a.y-b.y)<eps;
}
};
struct Line{
Point s,e;
bool friend operator<(Line a,Line b){
return a.s<b.s;
}
void st(){
Point t;
if(s>e){
t=s;s=e;e=t;
}
}
void get(){
s.get();e.get();
st();
}
};
Line line[200000],myl[2];
Point ep[200000];
int en;
double get_cross(Point a,Line b){
double ax,ay,bx,by;
ax=b.s.x-a.x;
ay=b.s.y-a.y;
bx=b.e.x-a.x;
by=b.e.y-a.y;
return ax*by-ay*bx;
}
int inter(Line a,Line b,Point &rp=Point()){
double cj[2];
cj[0]=get_cross(b.s,a);cj[1]=get_cross(b.e,a);
if(cj[0]*cj[1]>eps)return 0;
cj[0]=get_cross(a.s,b);cj[1]=get_cross(a.e,b);
if(cj[0]*cj[1]>eps)return 0;
if(f_abs(cj[0])<eps&&f_abs(cj[1])<eps){
return 4;
}
if(f_abs(cj[0])<eps){
rp=a.s;
if(cj[1]>eps)return 2;
else return 3;
}else if(f_abs(cj[1])<eps){
rp=a.e;
if(cj[0]>eps)return 2;
else return 3;
}else{
double key1=(a.e.x-a.s.x)*(b.e.y-b.s.y);
double key2=(b.e.x-b.s.x)*(a.e.y-a.s.y);
double key=key1-key2;
rp.x=(a.s.y-b.s.y)*(a.e.x-a.s.x)*(b.e.x-b.s.x);
rp.x-=key2*a.s.x-key1*b.s.x;
rp.x/=key;
if(f_abs(b.e.x-b.s.x)<eps)rp.y=(a.e.y-a.s.y)/(a.e.x-a.s.x)*(rp.x-a.s.x)+a.s.y;
else rp.y=(b.e.y-b.s.y)/(b.e.x-b.s.x)*(rp.x-b.s.x)+b.s.y;
return 1;
}
}
int ln;
void get_myl2(){
double dy=myl[0].e.y-myl[0].s.y,dx=myl[0].e.x-myl[0].s.x;
double t=sqrt(dy*dy+dx*dx);
dy/=t;dx/=t;
myl[1].e=myl[0].s;
myl[1].s.x=myl[1].e.x-300000*dx;
myl[1].s.y=myl[1].e.y-300000*dy;
}
void get_data(){
myl[0].get();
int t,n,i;
int pcnt=0;
Point p[2],ini;
ln=0;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
ini.get();
if(n==1)continue;
p[0]=ini;
bool f=0;
for(i=1;i<n;i++){
f^=1;
p[f].get();
line[ln].s=p[f^1];
line[ln].e=p[f];
line[ln++].st();
}
line[ln].s=p[f];
line[ln].e=ini;
line[ln++].st();
}
get_myl2();
}
double get_len(Point a,Point b){
return sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y));
}
void get_ep(Line l){
en=0;
int t,i;
for(i=0;i<ln;i++){
t=inter(line[i],l,ep[en]);
if(!t||t==4)continue;
if(t==1){
ep[en].up=1;
ep[en++].dn=1;
}else if(t==2){
ep[en].up=1;
ep[en++].dn=0;
}else{
ep[en].up=0;
ep[en++].dn=1;
}
}
sort(ep,ep+en);
int ten=0;
for(i=0;i<en;i++){
if(ten&&ep[i]==ep[ten-1]){
ep[ten-1].up^=ep[i].up;
ep[ten-1].dn^=ep[i].dn;
}else ep[ten++]=ep[i];
}
en=ten;
}
void run(){
if(myl[0].s==myl[0].e){
printf("0.00%%\n");
return;
}
int i;
bool in=0,left=0,right=0;
get_ep(myl[1]);
for(i=0;i<en;i++){
left^=ep[i].up;
right^=ep[i].dn;
}
if(left||right)in=1;
else in=0;
get_ep(myl[0]);
double len=get_len(myl[0].s,myl[0].e),res=0;
Point lp=myl[0].s;
ep[en++]=myl[0].e;
for(i=0;i<en;i++){
if(ep[i]==myl[0].s)continue;
if(in)res+=get_len(ep[i],lp);
left^=ep[i].up;
right^=ep[i].dn;
if(left||right)in=1;
else in=0;
lp=ep[i];
}
printf("%.2lf%%\n",res*100/len);
}
int main(){
int i,t;
scanf("%d",&t);
for(i=1;i<=t;i++){
get_data();
printf("Case %d: ",i);
run();
}
return 0;
}

21
HDOJ/3816_autoAC.cpp Normal file
View File

@ -0,0 +1,21 @@
#include<stdio.h>
int main()
{
printf("2 3 6\n");
printf("2 4 6 12\n");
printf("2 5 6 12 20\n");
printf("2 5 7 12 20 42\n");
printf("3 5 6 7 12 20 42\n");
printf("3 5 6 8 12 20 42 56\n");
printf("3 5 6 8 16 20 42 48 56\n");
printf("3 5 6 12 16 20 24 42 48 56\n");
printf("3 5 6 16 20 21 24 28 42 48 56\n");
printf("3 5 6 16 21 24 28 36 42 45 48 56\n");
printf("3 5 6 16 24 28 30 36 42 45 48 56 70\n");
printf("3 5 6 16 28 30 33 36 42 45 48 56 70 88\n");
printf("3 5 6 16 28 33 36 39 42 45 48 56 70 88 130\n");
printf("3 5 6 16 33 36 39 42 44 45 48 56 70 77 88 130\n");
printf("3 5 6 20 33 36 39 42 44 45 48 56 70 77 80 88 130\n");
printf("3 5 6 24 33 36 39 42 44 45 48 56 70 77 80 88 120 130\n");
return 0;
}

28
HDOJ/3817_autoAC.cpp Normal file
View File

@ -0,0 +1,28 @@
#include<iostream>
using namespace std;
int main()
{
int s,a,b,c,t,d=1;
cin>>s;
while(s--)
{
cin>>a>>b>>c;
if(a<b)
{
t=a;
a=b;
b=t;
}
if(a<c)
{
t=a;
a=c;
c=t;
}
cout<<"Case "<<d++<<": ";
if(b*b+c*c==a*a) cout<<"Right triangle"<<endl;
else if(b*b+c*c<a*a) cout<<"Obtuse triangle"<<endl;
else cout<<"Acute triangle"<<endl;
}
return 0;
}

61
HDOJ/3818_autoAC.cpp Normal file
View File

@ -0,0 +1,61 @@
#include<stdio.h>
#include<string.h>
#define MAX 1000000
int a[1000010];
int b[1000];
int main()
{
int x,n,i,j,num,max=0,sum,ji=0;
scanf("%d",&x);
while(x--)
{
ji++;
memset( a, 0, sizeof(a) );
for( i=1; i<=2; i++ )
{
scanf("%d",&n);
for( j=1; j<=n;j++ )
{
scanf("%d",&num);
if( num>max )max=num;
a[num]++;
}
}
for( i=max; i>=2; i--)
{
if(a[i]>=2){
a[i]-=2;
a[i+1]+=1;
a[i-2]+=1;
}
}
if(a[1]!=0)a[2]+=a[1];
for( i=max+1; i>=3; i-- )
{
if(a[i]==1&&a[i-1]==1)
{
a[i]=a[i-1]=0;
a[i+1]+=1; ;
}
}
for( i=max+2; i>=3; i-- )
{
if(a[i]==1&&a[i-1]==1)
{
a[i]=a[i-1]=0;
a[i+1]+=1; ;
}
}
j=0;
sum=0;
for(i=2; i<=max+5; i++)
{
if(a[i]!=0){b[j++]=i;sum++;}
}
printf("Case %d:\n",ji);
printf("%d",sum);
for( i=0; i<j; i++)
printf(" %d",b[i]);
printf("\n");
}
}

38
HDOJ/3819_autoAC.cpp Normal file
View File

@ -0,0 +1,38 @@
#include<iostream>
#include<cmath>
#include<string>
using namespace std;
char s[110000];
int ct[110000];
int main()
{
int t,i,sum,len,j,max,move;
scanf("%d",&t);
for(i=1;i<=t;i++)
{
scanf("%s",&s);
len=strlen(s);
memset(ct,0,sizeof(ct));
sum=0;
for(j=0;j<len;j++)
{
if(s[j]=='A'){sum++;}
ct[j]=sum;
}
max=ct[sum-1];
for(j=0;j+sum<=len;j++)
{
move=ct[j+sum-1]-ct[j-1];
if (move>max)
max=move;
}
for(;j<len;j++)
{
move=ct[(j+sum)%len-1]+ct[len-1]-ct[j-1];
if (move>max)
max=move;
}
printf("Case %d: %d\n",i,sum-max);
}
return 0;
}

120
HDOJ/3820_autoAC.cpp Normal file
View File

@ -0,0 +1,120 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int VM=101000;
const int EM=500100;
const int INF=0x3f3f3f3f;
struct Edge{
int to,nxt;
int cap;
}edge[EM<<1];
int n,m,G,S,cnt,head[VM],src,des,map1[110][110],map2[110][110];
int dep[VM],gap[VM],cur[VM],aug[VM],pre[VM];
void addedge(int cu,int cv,int cw){
edge[cnt].to=cv; edge[cnt].cap=cw; edge[cnt].nxt=head[cu];
head[cu]=cnt++;
edge[cnt].to=cu; edge[cnt].cap=0; edge[cnt].nxt=head[cv];
head[cv]=cnt++;
}
int SAP(int n){
int max_flow=0,u=src,v;
int id,mindep;
aug[src]=INF;
pre[src]=-1;
memset(dep,0,sizeof(dep));
memset(gap,0,sizeof(gap));
gap[0]=n;
for(int i=0;i<=n;i++)
cur[i]=head[i];
while(dep[src]<n){
int flag=0;
if(u==des){
max_flow+=aug[des];
for(v=pre[des];v!=-1;v=pre[v]){
id=cur[v];
edge[id].cap-=aug[des];
edge[id^1].cap+=aug[des];
aug[v]-=aug[des];
if(edge[id].cap==0)
u=v;
}
}
for(int i=cur[u];i!=-1;i=edge[i].nxt){
v=edge[i].to;
if(edge[i].cap>0 && dep[u]==dep[v]+1){
flag=1;
pre[v]=u;
cur[u]=i;
aug[v]=min(aug[u],edge[i].cap);
u=v;
break;
}
}
if(!flag){
if(--gap[dep[u]]==0)
break;
mindep=n;
cur[u]=head[u];
for(int i=head[u];i!=-1;i=edge[i].nxt){
v=edge[i].to;
if(edge[i].cap>0 && dep[v]<mindep){
mindep=dep[v];
cur[u]=i;
}
}
dep[u]=mindep+1;
gap[dep[u]]++;
if(u!=src)
u=pre[u];
}
}
return max_flow;
}
int main(){
int t,cases=0;
scanf("%d",&t);
while(t--){
scanf("%d%d%d%d",&n,&m,&G,&S);
cnt=0;
memset(head,-1,sizeof(head));
src=0; des=n*m*2+1;
int sum=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
scanf("%d",&map1[i][j]);
sum+=map1[i][j];
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
scanf("%d",&map2[i][j]);
sum+=map2[i][j];
}
int tmp;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
tmp=(i-1)*m+j;
if((i+j)%2==0){
addedge(src,tmp,map1[i][j]);
addedge(tmp,tmp+n*m,INF);
addedge(tmp+n*m,des,map2[i][j]);
if(i>1) addedge(tmp,tmp-m+n*m,G);
if(i<n) addedge(tmp,tmp+m+n*m,G);
if(j>1) addedge(tmp,tmp-1+n*m,G);
if(j<m) addedge(tmp,tmp+1+n*m,G);
}else{
addedge(src,tmp,map2[i][j]);
addedge(tmp,tmp+n*m,INF);
addedge(tmp+n*m,des,map1[i][j]);
if(i>1) addedge(tmp,tmp-m+n*m,S);
if(i<n) addedge(tmp,tmp+m+n*m,S);
if(j>1) addedge(tmp,tmp-1+n*m,S);
if(j<m) addedge(tmp,tmp+1+n*m,S);
}
}
}
printf("Case %d: %d\n",++cases,sum-SAP(des+1));
}
return 0;
}

59
HDOJ/3821_autoAC.cpp Normal file
View File

@ -0,0 +1,59 @@
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
#define maxn 1005
int x[maxn],y[maxn];
int map[maxn];
int main()
{
int i,j,T,C=1,n,m;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
int t;
scanf("%d",&t);
if(t==1)
{
x[i]=j;
map[j]=i;
}
}
}
for(i=0; i<n; i++)
{
y[i]=x[i];
}
vector<int>v;
for(i=1; i<n; i++)
{
for(j=i; j>=1; j--)
{
if(y[j]<y[j-1])
{
swap(y[j],y[j-1]);
v.push_back(j-1);
}
}
}
printf("Case %d: %d\n",C++,v.size());
int len=v.size();
for(i=0;i<len-1;i++)
{
printf("%d ",v[i]);
}
if(len!=0)
{
printf("%d",v[len-1]);
}
printf("\n");
}
return 0;
}

53
HDOJ/3823_autoAC.cpp Normal file
View File

@ -0,0 +1,53 @@
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 21000000
bool isprime[maxn];
long long prime[maxn],nprime;
void getprime()
{
memset(isprime,1,sizeof(isprime));
nprime=0;
long long i,j;
for(i=2; i<maxn; i++)
if(isprime[i])
{
prime[nprime++]=i;
for(j=i*i; j<maxn; j+=i)
isprime[j]=0;
}
}
bool isd[150];
int main()
{
getprime();
int t,ca=0;
long long a,b;
for(int i=1; i<nprime; i++)
if(prime[i]-prime[i-1]<150)
isd[prime[i]-prime[i-1]]=1;
scanf("%d",&t);
while(t--)
{
scanf("%I64d%I64d",&a,&b);
if(a>b)
swap(a,b);
long long ans=-1,d=b-a;
printf("Case %d: ",++ca);
if(!isd[d]||a==b)
{
puts("-1");
continue;
}
for(int i=1; i<nprime; i++)
if(prime[i]-prime[i-1]==d&&a<=prime[i-1])
{
ans=prime[i-1]-a;
break;
}
printf("%I64d\n",ans);
}
return 0;
}

62
HDOJ/3824_autoAC.cpp Normal file
View File

@ -0,0 +1,62 @@
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 1000
#define max (1<<29)
typedef __int64 ll;
const ll inf = (1ll)<<50;
int t, n, m, s, a, i, j, u, v, w, now, tmp1, tmp2;
int dis[maxn][maxn];
ll best[maxn];
ll cost, k21, k22;
bool vis[maxn];
int head, tail;
int q[maxn*maxn];
bool flag;
int main() {
scanf("%d", &t);
for (int cases = 1; cases <= t; cases++) {
scanf("%d %d %d %d", &n, &m, &s, &a);
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
dis[i][j] = -1;
for (i = 0; i < m; i++) {
scanf("%d %d %d", &u, &v, &w);
dis[u][v] = dis[v][u] = w;
}
head = 1; tail = 1; q[1] = a;
for (i = 0; i < n; i++) best[i] = inf;
for (i = 0; i < n; i++) vis[i] = false;
best[a] = 0;vis[a] = true;
while (head <= tail) {
now = q[head];
for (i = 0; i < n; i++)
if ((dis[now][i] != -1) && (dis[now][i]*3 <= s)) {
if ( best[now]+3*dis[now][i] <= s) {
cost = best[now]+2*dis[now][i];
flag = true;
} else {
tmp1 = s-3*dis[now][i];
tmp2 = s-5*dis[now][i];
if(tmp2 > 0){
k21 = (best[now]-tmp1)/tmp2;
k22 = (best[now]-tmp1)%tmp2;
cost = 1ll*(s-dis[now][i])*(1+k21)+1ll*(k22!=0)*(k22+4*dis[now][i]);
flag = true;
} else flag = false;
}
if (flag && cost < best[i]){
best[i] = cost;
if (!vis[i]) {
vis[i] = true;
q[++tail] = i;
}
}
}
head++; vis[now] = false;
}
if (best[0] == inf) { printf("Case %d: Poor princess, we will miss you!\n", cases); } else {
printf("Case %d: %I64d\n", cases, best[0]);
}
}
}

85
HDOJ/3825_autoAC.cpp Normal file
View File

@ -0,0 +1,85 @@
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
double x , y; int time;
} a[1000];
int n , v;
int f[1000] ; double d[1000] , dis[200][200];
int cmp(node p , node q)
{
if (p.time < q.time) return 1;
return 0;
}
double dist(int p , int q)
{
return sqrt( (a[p].x - a[q].x) * (a[p].x - a[q].x) + (a[p].y - a[q].y) * (a[p].y - a[q].y));
}
void pre_deal()
{
int i , j;
for (i=1;i<=n;i++)
for (j=i+1;j<=n;j++)
dis[i][j] = dist(i , j);
}
void deal()
{
int i , j , k , ans1; double ans2 , t;
pre_deal();
ans1 = 0;
for(i=1;i<=n;i++)
{
memset(f , 0 , sizeof(f));
f[i] = 1; d[i] = 0;
for (j=i+1;j<=n;j++) d[j]=1e10;
for (j=i+1;j<=n;j++)
if (dis[i][j]/v-1e-8 <= a[j].time - a[i].time)
{
if (f[j] < f[i] + 1 || f[j] == f[i] + 1 && d[j] > d[i] + dis[i][j])
{
f[j] = f[i] + 1;
d[j] = d[i] + dis[i][j];
}
for (k=j+1;k<=n;k++)
if (f[k] < f[j] + 1 || f[k] == f[j] + 1 && d[k] > d[j] + dis[j][k])
if (dis[j][k] / v - 1e-8 <= a[k].time - a[j].time)
{
f[k] = f[j] + 1;
d[k] = d[j] + dis[j][k];
}
}
for (j=i;j<=n;j++)
{
t = dis[i][j];
if (f[j] > ans1 || f[j] == ans1 && d[j] + t < ans2)
{
ans1 = f[j]; ans2 = d[j] + t;
}
}
}
printf("%d %.3lf\n", ans1 , ans2);
}
void init()
{
int i;
scanf("%d%d", &n , &v);
for(i=1;i<=n;i++)
{
scanf("%lf%lf%d",&a[i].x,&a[i].y, &a[i].time);
}
sort(a+1 , a+n+1 ,cmp);
}
int main()
{
int tot;
scanf("%d", &tot);
for (int i=1;i<=tot;i++)
{
printf("Case %d: " , i);
init();
deal();
}
}

58
HDOJ/3826_autoAC.cpp Normal file
View File

@ -0,0 +1,58 @@
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int hash[500024]={0};
int prime( int num[] )
{
for( int i=3;i<=1000;i+=2 )
{
if( hash[i/2] )
continue;
int x=i<<1;
for( int j=i*i; j<=1000000; j+=x )
hash[j/2]=1;
}
int count=0;
num[++count]=2;
for( int i=1;i<=500000;i++ )
{
if( hash[i]==0 )
num[++count]=( i<<1 )+1;
}
return count;
}
bool judge( int num[], int count , long long number )
{
long long t=( long long )sqrt( number ) ;
for( int i=1;i<=count; i++ )
{
if( number==1 ) break;
if( 0==(number%num[i]) )
{
number/=num[i];
if( number%num[i]==0 ) return true;
}
}
if( number!=1 )
{
t=( long long )sqrt( number ) ;
if( (t*t)==number )
return true;
}
return false;
}
int main( )
{
int T,num[78550];
int count = prime( num );
scanf( "%d",&T );
for( int i=1;i<=T; i++ )
{
long long number;
scanf( "%I64d",&number );
printf( "Case %d: ",i );
bool t=judge( num,count,number );
printf( t?"No\n":"Yes\n" );
}
return 0;
}

91
HDOJ/3827_autoAC.cpp Normal file
View File

@ -0,0 +1,91 @@
#include<string.h>
#include<stdio.h>
char ch[201];
int xue[3] , sta[201] , n , flag = 1;
void init()
{
int i; char t;
scanf("%d\n", &n);
for (i=1;i<=n;i++)
{
scanf("%c ", &ch[i]);
sta[i] = 0;
}
for (i=1;i<=4;i++) sta[i] = 1;
for (i=5;i<=8;i++) sta[i] = 2;
xue[1] = 3; xue[2] = 3;
}
int find_need(char now , int x)
{
int i;
for (i=1;i<=n;i++)
if (ch[i] == now && sta[i] == x) return i;
return 0;
}
int find(int x)
{
int i;
for (i=1;i<=n;i++)
if (sta[i] == x) return i;
return 0;
}
void deal()
{
int i , j , t , x , tail , m , now = 0;
flag = 1; tail = 8; m = n;
while (1)
{
now++;
if (now > m+1) break;
if (tail + 1 <= m) sta[tail+1] = flag;
if (tail + 2 <= m) sta[tail+2] = flag;
if (tail+2 <= m) tail = tail + 2; else tail = m;
for (j=1;j<=n;j++)
if (ch[j] == 'S' && sta[j] == flag)
{
sta[j] = 3;
t = find_need('D' , 3-flag);
if (t == 0)
{
x = find(3-flag);
if (x == 0) continue;
sta[x] = 3;
n++;
sta[n] = flag;
ch[n] = ch[x];
}
else sta[t] = 3;
}
for (j=1;j<=n;j++)
if (ch[j] == 'K' && sta[j] == flag)
{
sta[j] = 3;
t = find_need('E' , 3-flag);
if (t == 0)
{
xue[3-flag] --;
if (xue[3-flag] < 1)
{
if (flag == 1) printf("The winner is the first.\n");
else printf("The winner is the second.\n");
return;
}
}
else sta[t] = 3;
break;
}
flag = 3-flag;
}
printf("Just a tie.\n");
}
int main()
{
int i , tot;
scanf("%d", &tot);
for (i = 1; i<= tot;i++)
{
printf("Case %d: " , i);
init();
deal();
}
}

153
HDOJ/3828_autoAC.cpp Normal file
View File

@ -0,0 +1,153 @@
#include <stdio.h>
#include <string.h>
#include <string>
#include <stdlib.h>
#include <vector>
#include <math.h>
#include <algorithm>
#include <iostream>
#define N 16
#define M 1<<16
#define MS 1000000009
#define LL long long
using namespace std;
int n,goal;
string str[N];
LL da[N];
int gra[N][N],mark[N];
int slen[N];
int dp[M][N];
LL ans;
void ini(void)
{
goal=(1<<n)-1;
for(int i=0;i<n;i++)
str[i].clear();
for(int i=0;i<n;i++)
{
LL k=da[i];
while(k)
{
str[i].push_back((k&1)+'0');
k=k>>1;
}
int j=str[i].size();
string tempstr;
while(j--)
tempstr.push_back(str[i][j]);
str[i]=tempstr;
}
sort(str,str+n);
for(int i=0;i<n;i++)
slen[i]=str[i].size();
memset(mark,0,sizeof(mark));
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(i!=j&&slen[i]>slen[j])
for(int k1=0;k1<slen[i]-slen[j]+1;k1++)
{
int flag=0;
for(int k2=0;k2<slen[j];k2++)
if(str[i][k1+k2]!=str[j][k2])
{
flag=1;
break;
}
if(flag==0)
{
if(!mark[j])
{
mark[j]=1;
goal-=(1<<j);
}
break;
}
}
for(int i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
if(!mark[i]&&!mark[j]&&str[i]==str[j])
mark[j]=1,goal-=(1<<j);
memset(gra,0,sizeof(gra));
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(!mark[i]&&!mark[j]&&i!=j)
for(int k=0;k<slen[i];k++)
{
int flag=0;
for(int u=0;u+k<slen[i];u++)
if(str[i][k+u]!=str[j][u])
{
flag=1;
break;
}
if(!flag)
{
gra[i][j]=slen[i]-k;
break;
}
}
}
void dfs(int s,int p)
{
if(~dp[s][p])return ;
if(s==(1<<p))
{
dp[s][p]=slen[p];
return ;
}
int minn=0x3f3f3f3f;
int t=s-(1<<p);
for(int i=0;i<n;i++)
if(!mark[i]&&i!=p&&(s&(1<<i)))
{
dfs(t,i);
minn=min(minn,dp[t][i]-gra[p][i]);
}
dp[s][p]=minn+slen[p];
}
void rans(int s,int p,int e)
{
for(int i=e;i<slen[p];i++)
ans=(ans*2+str[p][i]-'0')%MS;
if(s==(1<<p))return ;
int t=s-(1<<p);
int k=-1;
string a;
for(int i=0;i<n;i++)
if(!mark[i]&&(s&(1<<i))&&dp[t][i]-gra[p][i]+slen[p]==dp[s][p])
{
string b(str[i],gra[p][i],slen[p]-gra[p][i]);
if(k==-1||a>b)
a=b,k=i;
}
rans(t,k,gra[p][k]);
}
void re(void)
{
for(int i=0;i<n;i++)
scanf("%lld",&da[i]);
}
void run(void)
{
ini();
memset(dp,-1,sizeof(dp));
for(int i=0;i<n;i++)
if(!mark[i])
dfs(goal,i);
int p,minn=0x3f3f3f3f;
for(int i=0;i<n;i++)
if(!mark[i]&&dp[goal][i]<minn)
p=i,minn=dp[goal][i];
ans=0;
rans(goal,p,0);
printf("%lld\n",ans);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
re();
run();
}
return 0;
}

72
HDOJ/3829_autoAC.cpp Normal file
View File

@ -0,0 +1,72 @@
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
#define M 510
#define N 250010
int Head[M], Next[N], Key[N];
int match[M];
string like[M], dislike[M];
bool use[M];
int num;
int child;
void add(int u, int v)
{
Key[num] = v;
Next[num] = Head[u];
Head[u] = num++;
}
bool find(int u)
{
int temp;
for(int i = Head[u]; i != -1; i = Next[i])
{
temp = Key[i];
if(!use[temp])
{
use[temp] = true;
if(match[temp] == -1 || find(match[temp]))
{
match[temp] = u;
return true;
}
}
}
return false;
}
int hungary()
{
int sum = 0;
for(int i = 0; i < child; ++i)
{
memset(use, false, sizeof(use));
if(find(i))
sum++;
}
return sum;
}
int main()
{
int cat, dog;
string likeit, dislikeit;
while(scanf("%d%d%d", &cat, &dog, &child) != EOF)
{
num = 0;
memset(Head, -1, sizeof(Head));
memset(match, -1, sizeof(match));
for(int i = 0; i < child; ++i)
{
cin>>likeit>>dislikeit;
like[i] = likeit;
dislike[i] = dislikeit;
}
for(int i = 0; i < child; ++i)
for(int j = 0; j < child; ++j)
if(like[i].compare(dislike[j]) == 0 || dislike[i].compare(like[j]) == 0)
add(i, j);
printf("%d\n", child - hungary() / 2);
}
return 0;
}

129
HDOJ/3830_autoAC.cpp Normal file
View File

@ -0,0 +1,129 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
#define ll __int64
struct State
{
ll x,y,z;
ll dep;
};
State S,T;
inline bool cmp_state(State a , State b)
{
if(a.x == b.x && a.y == b.y && a.z == b.z)
return true;
return false;
}
inline ll Abs(ll x)
{
return x > 0LL ? x : -x;
}
inline void SORT(State &a)
{
if(a.y < a.x) swap(a.x , a.y);
if(a.z < a.x) swap(a.x , a.z);
if(a.y > a.z) swap(a.y , a.z);
}
State Root(State &a)
{
State tmp = a;
tmp.dep = 0;
ll dep = 0;
while(Abs(tmp.x - tmp.y) != Abs(tmp.y - tmp.z))
{
ll len = Abs(tmp.x - tmp.y);
ll __len = Abs(tmp.y- tmp.z);
if(__len > len)
{
ll c = (__len - 1)/ len;
dep += c;
tmp.y += c * len;
tmp.x += c * len;
}
else
{
ll c = (len - 1) / __len;
dep += c;
tmp.y -= c * __len;
tmp.z -= c * __len;
}
// printf("%d %d %d\n",tmp.x , tmp.y , tmp.z);
}
a.dep = dep;
return tmp;
}
void updata(State &a ,ll delta)
{
ll count = 0;
while(count < delta)
{
ll len = Abs(a.x - a.y);
ll __len = Abs(a.y - a.z);
ll k = Abs(count - delta);
if(len < __len)
{
ll c = (__len - 1) / len;
ll Min = min(k , c);
a.x += Min * len;
a.y += Min * len;
count += Min;
if(Min == k) break;
}
else
{
ll c = (len - 1) / __len;
ll Min = min(k , c);
a.y -= Min * __len;
a.z -= Min * __len;
count += Min;
if(Min == k) break;
}
}
a.dep -= delta;
}
ll solve()
{
State tS,tT;
ll low = 0 , high = S.dep;
while(low <= high)
{
ll mid = (low + high) >> 1;
ll delta = S.dep - mid;
tS = S; tT = T;
updata(tS , delta); //SORT(tS);
updata(tT , delta); //SORT(tT);
if(!cmp_state(tS , tT))
high = mid - 1;
else
low = mid + 1;
}
return 2 * (S.dep - high);
}
int main()
{
while( scanf("%I64d%I64d%I64d",&S.x,&S.y,&S.z) != EOF)
{
scanf("%I64d%I64d%I64d",&T.x,&T.y,&T.z);
S.dep = T.dep = 0;
SORT(S); SORT(T);
State RS = Root(S);
State RT = Root(T);
if(!cmp_state(RS,RT))
{
printf("NO\n");
continue;
}
ll tmpr = Abs(S.dep - T.dep);
if(S.dep > T.dep)
updata(S , S.dep - T.dep);
else
updata(T , T.dep - S.dep);
ll res = solve();
printf("YES\n");
printf("%I64d\n",res + tmpr);
}
return 0;
}

66
HDOJ/3831_autoAC.cpp Normal file
View File

@ -0,0 +1,66 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<cmath>
#include<vector>
using namespace std;
#define mod 1000000007
#define inf 0x7f7f7f7f
#define N 505
int dp[N][N][55];
char s[N],p[N];
int a[N],b[N];
int la, lb;
int main()
{
int i, j, k, u, v;
while(scanf("%s",s+1),s[1]!='#'){
scanf("%s",p+1);
for(i=1;s[i];i++)if(islower(s[i]))a[i] = s[i]-'a';else a[i] = s[i]-'A'+26;
for(i=1;p[i];i++)if(islower(p[i]))b[i] = p[i]-'a';else b[i] = p[i]-'A'+26;
memset(dp, inf, sizeof dp);
la = strlen(s+1), lb = strlen(p+1);
dp[0][0][52] = 0;
for(i = 1; i <= la; i++)dp[0][i][52]=dp[i][0][52] = i;
for(i = 1; i <= la; i++)
{
for(j = 1; j <= lb; j++)
{
for(k = 0; k < 52; k++)
{
u = inf;
if(b[j]==k)
u = dp[i-1][j-1][k];
else
{
u = min(u, dp[i-1][j][k]+1);
u = min(u, dp[i][j-1][k]+1);
u = min(u, dp[i-1][j-1][k] +1);
dp[i][j][b[j]] = min(dp[i][j][b[j]],dp[i-1][j-1][k]+1);
}
dp[i][j][k] = min(u, dp[i][j][k]);
}
u = inf;
if(a[i]==b[j])
u = dp[i-1][j-1][52];
else {
u = min(u, dp[i-1][j][52]+1);
u = min(u, dp[i][j-1][52]+1);
u = min(u, dp[i-1][j-1][52] +1);
dp[i][j][b[j]] = min(dp[i][j][b[j]], dp[i-1][j-1][52]+1);
}
dp[i][j][52] = min(dp[i][j][52], u);
}
}
int ans = 1000000000;
for(i = 0; i <= 52; i++)ans = min(ans, dp[la][lb][i]);
printf("%d\n",ans);
}
return 0;
}

93
HDOJ/3832_autoAC.cpp Normal file
View File

@ -0,0 +1,93 @@
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <cstdlib>
#define inf 99999999
using namespace std;
int map[205][205];
struct node
{
int x,y,r;
};
node a[205];
int d1[205];
int d2[205];
int d3[205];
int n;
void bfs(int u,int *d)
{
int i;
for(i=1;i<=n;i++)
d[i]=inf;
int vis[205];
memset(vis,0,sizeof(vis));
queue<int> q;
q.push(u);
vis[u]=1;
d[u]=0;
while(!q.empty())
{
u=q.front();
q.pop();
int i;
for(i=1;i<=n;i++)
{
if(!vis[i]&&map[u][i])
{
vis[i]=1;
d[i]=d[u]+1;
q.push(i);
}
}
}
}
inline int dis(node a,node b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
inline int isarc(node a,node b)
{
if(dis(a,b)<=(a.r+b.r)*(a.r+b.r))
return 1;
else
return 0;
}
int main()
{
int t;
scanf("%d",&t);
int cas;
for(cas=0;cas<t;cas++)
{
memset(a,0,sizeof(a));
memset(map,0,sizeof(map));
scanf("%d",&n);
int i;
for(i=1;i<=n;i++)
scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].r);
int j;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(isarc(a[i],a[j]))
map[i][j]=map[j][i]=1;
}
}
bfs(1,d1);
bfs(2,d2);
bfs(3,d3);
int res=inf;
for(i=1;i<=n;i++)
{
if(d1[i]!=inf&&d2[i]!=inf&&d3[i]!=inf&&d1[i]+d2[i]+d3[i]<res)
res=d1[i]+d2[i]+d3[i];
}
if(res==inf)
printf("-1\n");
else
printf("%d\n",n-res-1);
}
return 0;
}

39
HDOJ/3833_autoAC.cpp Normal file
View File

@ -0,0 +1,39 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int a[10005];
int has[10005];
int main()
{
int t,n,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i = 0; i<n; i++)
{
scanf("%d",&a[i]);
has[a[i]] = i;
}
int flag = 0;
for(i = 0; i<n; i++)
{
for(j = i+1; j<n; j++)
{
int s = a[i]+a[j];
if(s%2)
continue;
if(has[s/2]>i && has[s/2]<j)
{
flag = 1;
break;
}
}
if(flag)
break;
}
printf("%s\n",flag?"Y":"N");
}
return 0;
}

91
HDOJ/3834_autoAC.cpp Normal file
View File

@ -0,0 +1,91 @@
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
const double eps=1e-12;
const double pi=acos(-1.0);
using namespace std;
typedef long long ll;
inline double sqr(double x)
{
return x*x;
}
struct point
{
double x, y, r;
point(){}
point(double _x, double _y, double _r){x=_x;y=_y;r=_r;}
void read(){scanf("%lf%lf%lf", &x, &y, &r);}
point operator -(const point &tp)const
{
return point(x-tp.x, y-tp.y, r-tp.r);
}
point operator +(const point &tp)const
{
return point(x+tp.x, y+tp.y, r+tp.r);
}
point operator *(const double &k)const
{
return point(x*k, y*k, r*k);
}
}table, ball;
double dx, dy, tot;
int cross(point op, point sp, point ep)
{
double ans=(sp.y-op.y)*(ep.x-op.x)-(ep.y-op.y)*(sp.x-op.x);
if(ans>eps)return 1;
return -1;
}
double angle(point a, point b)
{
double area=a.x*b.x+a.y*b.y;
double ablen=sqrt((a.x*a.x+a.y*a.y)*(b.x*b.x+b.y*b.y));
return acos(area/ablen);
}
int main()
{
int ca;
scanf("%d", &ca);
while(ca-->0)
{
table.read();
ball.read();
scanf("%lf%lf%lf", &dx, &dy, &tot);
ball.x-=table.x;
ball.y-=table.y;
table.r-=ball.r;
double a=sqr(dx)+sqr(dy);
double b=2.0*(ball.x*dx*1.0+ball.y*dy*1.0);
double c=sqr(ball.x)+sqr(ball.y)-sqr(table.r);
double sti=(sqrt(sqr(b)-4.0*a*c)-b)/(2.0*a);
if(sti-tot>eps)
{
printf("%.1lf %.1lf\n", (ball.x+tot*dx+table.x), (ball.y+tot*dy+table.y));
continue;
}
point zero=point(0, 0, 0);
point first=point(sti*dx+ball.x, sti*dy+ball.y, 0);
int dir=cross(zero, first, ball);
double ref=angle(ball-first, zero-first);
double dis=cos(ref)*table.r*2;
double v=sqrt(dx*dx+dy*dy);
tot-=sti;
double len=tot*v;
double dti=len/dis;
ll lti=(ll)(dti+eps);
double perti=dis/v;
double left=tot-perti*lti;
double pang=(pi-2*ref)*lti;
double stang=atan2(first.y, first.x);
stang=stang+pang*dir;
point now=point(cos(stang)*table.r, sin(stang)*table.r, 0);
double vang=stang+(pi-2*ref)*dir;
point next=point(cos(vang)*table.r, sin(vang)*table.r, 0);
double rate=left/perti;
point vec=next-now;
now=now+(vec*rate);
printf("%.1lf %.1lf\n", now.x+table.x, now.y+table.y);
}
return 0;
}

29
HDOJ/3835_autoAC.cpp Normal file
View File

@ -0,0 +1,29 @@
#include<stdio.h>
#include<math.h>
int main()
{
int k,i,j,n,ans;
while(~scanf("%d",&n))
{
if(!n)
{
printf("1\n");
continue;
}
ans=0;
k=(int)sqrt(n/2.0);
for(i=0;i<=k;++i)
{
j=(int)sqrt(n-i*i);
if(i*i+j*j==n)
{
if(i==j||i==0||j==0)
ans+=4;
else
ans+=8;
}
}
printf("%d\n",ans);
}
return 0;
}

97
HDOJ/3836_autoAC.cpp Normal file
View File

@ -0,0 +1,97 @@
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"stack"
#define N 30000
using namespace std;
int n,m;
int index_s;
int instack[N],DFN[N],LOW[N];
int belong[N],indegree[N],outdegree[N];
struct Eage
{
int from,to,next;
}eage[2*N];
int tot,head[N];
void add(int a,int b)
{
eage[tot].from=a;
eage[tot].to=b;
eage[tot].next=head[a];
head[a]=tot++;
}
void getmap()
{
int i,l;
int a,b;
tot=0;
memset(head,-1,sizeof(head));
while(m--) {scanf("%d%d",&a,&b);add(a,b);}
}
stack<int>st;
void Tarjan(int k)
{
int j,v;
st.push(k);
instack[k]=1;
DFN[k]=LOW[k]=++index_s;
for(j=head[k];j!=-1;j=eage[j].next)
{
v=eage[j].to;
if(instack[v]) LOW[k]=LOW[k]>DFN[v]?DFN[v]:LOW[k];
else if(DFN[v]==-1)
{
Tarjan(v);
LOW[k]=LOW[k]>LOW[v]?LOW[v]:LOW[k];
}
}
if(DFN[k]==LOW[k])
{
do
{
j=st.top();
st.pop();
instack[j]=0;
belong[j]=k;
}while(j!=k);
}
}
void getdegree()
{
int i,l;
memset(indegree,0,sizeof(indegree));
memset(outdegree,0,sizeof(outdegree));
for(i=0;i<tot;i++)
{
if(belong[eage[i].from]==belong[eage[i].to]) continue;
indegree[belong[eage[i].to]]++;
outdegree[belong[eage[i].from]]++;
}
}
int main()
{
int i;
int temp,t1,t2,ans;
while(scanf("%d%d",&n,&m)!=-1)
{
getmap();
index_s=0;
memset(DFN,-1,sizeof(DFN));
memset(LOW,-1,sizeof(LOW));
memset(instack,0,sizeof(instack));
for(i=1;i<=n;i++) if(DFN[i]==-1) Tarjan(i);
getdegree();
temp=t1=t2=0;
for(i=1;i<=n;i++)
{
if(belong[i]!=i) continue;
temp++;
if(indegree[i]==0) t1++;
if(outdegree[i]==0) t2++;
}
ans=t1>t2?t1:t2;
if(n<1 || temp==1) ans=0;
printf("%d\n",ans);
}
return 0;
}

160
HDOJ/3838_autoAC.cpp Normal file
View File

@ -0,0 +1,160 @@
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#define PI pair
#define MP make_pair
#define FI first
#define SE second
const double eps = 1e-8;
const double pi = acos(-1.);
using namespace std;
struct point
{
int x, y;
bool operator<( const point r ) const
{
if( x == r.x ) return y < r.y;
return x < r.x;
}
point( int _x = 0, int _y = 0 ) : x(_x), y(_y){}
} A[10], B[10], C[10];
struct T
{
point rot, tra;
} ans[40000];
int snap( double x )
{
if( x < 0 ) return -snap(-x);
return (int)(x+0.5);
}
void rot( double rx, double ry )
{
double d, ang = atan2(ry, rx);
for( int i = 0; i < 3; ++i )
{
d = atan2(A[i].y+0., A[i].x+0.);
double L = sqrt(A[i].x*A[i].x + A[i].y*A[i].y + 0.);
C[i].x = snap(L*cos(d+ang));
C[i].y = snap(L*sin(d+ang));
}
}
int e;
bool line;
void solve( int rx, int ry )
{
rot(rx, ry);
int i, j, k;
int kx, cx, ky, cy;
bool st;
sort(C, C+3);
do
{
st = 0;
if( C[1].x - C[2].x )
{
if( B[1].x-B[2].x == 0 ) continue;
if( (B[1].x-B[2].x)%(C[1].x-C[2].x) == 0 )
{
kx = (B[1].x-B[2].x)/(C[1].x-C[2].x);
cx = B[1].x-C[1].x*kx;
}
else continue;
}
else if( C[0].x - C[1].x )
{
if( B[0].x-B[1].x == 0 ) continue;
if( (B[0].x-B[1].x)%(C[0].x-C[1].x) == 0 )
{
kx = (B[0].x-B[1].x)/(C[0].x-C[1].x);
cx = B[0].x-C[0].x*kx;
}
else continue;
}
else
{
if( B[0].x == B[1].x && B[0].x == B[2].x )
{
kx = 1, cx = B[0].x-C[0].x*kx;
st = 1;
}
else continue;
}
if( C[1].y - C[2].y )
{
if( B[1].y-B[2].y == 0 ) continue;
if( (B[1].y-B[2].y)%(C[1].y-C[2].y) == 0 )
{
ky = (B[1].y-B[2].y)/(C[1].y-C[2].y);
cy = B[1].y-C[1].y*ky;
}
else continue;
}
else if( C[0].y - C[1].y )
{
if( B[0].y-B[1].y == 0 ) continue;
if( (B[0].y-B[1].y)%(C[0].y-C[1].y) == 0 )
{
ky = (B[0].y-B[1].y)/(C[0].y-C[1].y);
cy = B[0].y-C[0].y*ky;
}
else continue;
}
else
{
if( B[0].y == B[1].y && B[0].y == B[2].y )
{
ky = 1, cy = B[0].y-C[0].y*ky;
st = 1;
}
else continue;
}
for( i = 0; i < 3; ++i )
{
if( B[i].x != C[i].x*kx + cx ) break;
if( B[i].y != C[i].y*ky + cy ) break;
}
if( i != 3 ) continue;
ans[e].rot = point(rx, ry);
ans[e].tra = point(kx, ky);
e++;
if( st ) line = 1;
} while( next_permutation(C, C+3) );
}
int main()
{
int cases = 1;
int i, j, k;
double d;
while( 1 )
{
for( i = 0; i < 3; ++i )
scanf("%d %d", &A[i].x, &A[i].y);
if( !A[0].x && !A[0].y && !A[1].x && !A[1].y && !A[2].x && !A[2].y )
break;
for( i = 0; i < 3; ++i )
scanf("%d %d", &B[i].x, &B[i].y);
e = 0;
line = 0;
for( i = -10; i <= 10; ++i )
{
solve(i, 10); solve(i, -10);
solve(10, i); solve(-10, i);
}
for( i = 1; i < e; ++i )
{
if( ans[i].rot.x == ans[i-1].rot.x && ans[i].rot.y == ans[i-1].rot.y &&
ans[i].tra.x == ans[i-1].tra.x && ans[i].tra.y == ans[i-1].tra.y )
continue;
if( ans[i].rot.x == -ans[i-1].rot.x && ans[i].rot.y == -ans[i-1].rot.y &&
ans[i].tra.x == -ans[i-1].tra.x && ans[i].tra.y == -ans[i-1].tra.y )
continue;
break;
}
printf("Case %d: ", cases++);
if( e == 0 ) puts("no solution");
else if( line || i < e ) puts("inconsistent solutions");
else puts("equivalent solutions");
}
return 0;
}

101
HDOJ/3839_autoAC.cpp Normal file
View File

@ -0,0 +1,101 @@
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <map>
using namespace std;
int H,W;
char Map[220][440];
int vis[220][440];
int chr[220][440];
int wblc_cnt,bblc_cnt;
char hie[6]={'W','A','K','J','S','D'};
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
map<int,bool> Hsh[80010];
int bccnt[80010];
char ans[80010];
int comp(const void *_a,const void *_b)
{
char a=*(char *)_a;
char b=*(char *)_b;
return a-b;
}
void dfs1(int x,int y)
{
vis[x][y]=wblc_cnt;
int nx,ny;
for(int i=0;i<4;i++){
nx=dx[i]+x;
ny=dy[i]+y;
if(nx>=0&&nx<H&&ny>=0&&ny<W && !vis[nx][ny] && Map[nx][ny]=='0')dfs1(nx,ny);
}
}
void dfs2(int x,int y)
{
chr[x][y]=bblc_cnt;
int nx,ny;
for(int i=0;i<4;i++){
nx=dx[i]+x;
ny=dy[i]+y;
if(nx>=0&&nx<H&&ny>=0&&ny<W&&!chr[nx][ny]){
if(Map[nx][ny]=='0'){
if(Hsh[bblc_cnt].find(vis[nx][ny]) != Hsh[bblc_cnt].end())continue;
Hsh[bblc_cnt].insert(pair<int,bool>(vis[nx][ny],true));
}else dfs2(nx,ny);
}
}
}
int main()
{
char str[60];
int kase=1;
while(~scanf("%d%d",&H,&W)&&(H+W)){
printf("Case %d: ",kase++);
getchar();
memset(Map,0,sizeof(Map));
memset(vis,0,sizeof(vis));
memset(chr,0,sizeof(chr));
for(int i=0;i<=4*W+8;i++)Map[0][i]=Map[H+1][i]='0';
for(int i=1;i<=H;i++){
gets(str);
for(int j=0;j<4;j++)
Map[i][j]=Map[i][4+W*4+j]='0';
for(int j=0;j<W;j++){
int x;
if('0'<=str[j] && str[j]<='9')
x=str[j]-'0';
else
x=10+str[j]-'a';
for(int k=0;k<4;k++){
if(x & 1<<(3-k)) Map[i][4+j*4+k]='1';
else Map[i][4+j*4+k]='0';
}
}
}
W+=2;W*=4;
H+=2;
wblc_cnt=0;
for(int i=0;i<H;i++)for(int j=0;j<W;j++)
if(vis[i][j]==0 && Map[i][j]=='0'){
wblc_cnt++;
dfs1(i,j);
}
bblc_cnt=0;
for(int i=0;i<H*W;i++) Hsh[i].clear();
for(int i=0;i<H;i++)for(int j=0;j<W;j++)
if(chr[i][j]==0 && Map[i][j]=='1'){
bblc_cnt++;
dfs2(i,j);
}
for(int i=1;i<=bblc_cnt;i++)
bccnt[i]=Hsh[i].size();
for(int i=1;i<=bblc_cnt;i++)
ans[i]=hie[bccnt[i]-1];
qsort(ans+1,bblc_cnt,sizeof(char),comp);
ans[bblc_cnt+1]='\0';
puts(ans+1);
}
return 0;
}

63
HDOJ/3841_autoAC.cpp Normal file
View File

@ -0,0 +1,63 @@
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn=1010;
int s[maxn*2][maxn*2];
int get(int x1,int y1,int x2,int y2)
{
return s[x2][y2]+s[x1-1][y1-1]-s[x1-1][y2]-s[x2][y1-1];
}
int N;
inline void gao(int &n)
{
if(n<1)
n=1;
if(n>N)
n=N;
}
int main()
{
int dx,dy,n,q,i,j,k;
int u,v;
int ii=0;
while(scanf("%d%d%d%d",&dx,&dy,&n,&q),dx+dy)
{
N=dx+dy;
for(i=1;i<=dx+dy;i++)
for(j=1;j<=dx+dy;j++)
s[i][j]=0;
while(n--)
{
scanf("%d%d",&u,&v);
s[u+v][v-u+dx]=1;
}
for(i=1;i<=dx+dy;i++)
for(j=1;j<=dx+dy;j++)
s[i][j]+=s[i][j-1];
for(i=1;i<=dx+dy;i++)
for(j=1;j<=dx+dy;j++)
s[i][j]+=s[i-1][j];
printf("Case %d:\n",++ii);
while(q--)
{
int m;
scanf("%d",&m);
int ans=0,x,y;
int ansx=1,ansy=1;
for(j=1;j<=dy;j++)
for(i=1;i<=dx;i++)
{
u=i,v=j-m;
x=(u+v),y=(v-u+dx);
u=x+m*2,v=y+m*2;
gao(x),gao(y);
gao(u),gao(v);
u=get(x,y,u,v);
if(u>ans)
ans=u,ansx=i,ansy=j;
}
printf("%d (%d,%d)\n",ans,ansx,ansy);
}
}
}

79
HDOJ/3842_autoAC.cpp Normal file
View File

@ -0,0 +1,79 @@
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <set>
#include <vector>
#include <map>
#define MAXN 11111
#define MAXM 55555
#define INF 1000000007
using namespace std;
long long f[111111];
struct node {
int d, p, g, r;
}p[111111];
bool cmp(node x, node y) {
return x.d < y.d;
}
int n, d;
typedef pair<int, long long> PA;
PA A[111111], C[111111];
long long h(int j) {
return f[j] + (long long)p[j].r - (long long)p[j].p - (long long)p[j].g * (long long)(p[j].d + 1);
}
int slopecomp(PA a, PA b, PA c) {
long long xa = b.first - a.first;
long long xb = c.first - a.first;
long long ya = b.second - a.second;
long long yb = c.second - a.second;
double tmp = (double)xa * yb - (double)xb * ya;
return tmp < 0;
}
void cdq(int l, int r) {
if(l + 1 <= r) {
int m = (l + r) >> 1;
cdq(l, m);
int na = 0, nb = 0, nc = 0;
for(int j = l; j <= m; j++) {
if(f[j] >= p[j].p) A[na++] = PA(p[j].g, h(j));
}
sort(A, A + na);
for(int i = 0; i < na; i++) {
while(nc > 1 && !slopecomp(C[nc - 1], C[nc], A[i])) nc--;
C[++nc] = A[i];
}
int j = 0;
for(int i = m + 1; i <= r; i++) {
long long a1, a2, b1, b2, x;
x = p[i].d;
while(j < nc) {
a1 = C[j].first;
a2 = C[j + 1].first;
b1 = C[j].second;
b2 = C[j + 1].second;
if(a1 * x + b1 >= a2 * x + b2) break;
j++;
}
f[i] = max(f[i], (long long)C[j].first * x + C[j].second);
}
cdq(m + 1, r);
}
}
int main() {
int cas = 0;
while(scanf("%d%I64d%d", &n, &f[0], &d) != EOF) {
if(n == 0 && f[0] == 0 && d == 0) break;
for(int i = 1; i <= n; i++) scanf("%d%d%d%d", &p[i].d, &p[i].p, &p[i].r, &p[i].g);
sort(p + 1, p + n + 1, cmp);
++n;
p[n].d = d + 1;
p[n].g = p[n].p = 0;
for(int i = 1; i <= n; i++) f[i] = f[0];
cdq(0, n);
printf("Case %d: %I64d\n", ++cas, f[n]);
}
return 0;
}

91
HDOJ/3843_autoAC.cpp Normal file
View File

@ -0,0 +1,91 @@
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
const double pi = acos(-1.);
const double eps = 1e-8;
using namespace std;
int N;
int a[1000];
double ang[1000];
int dcmp( double x )
{
if( x < eps ) return -1;
if( x > eps ) return 1;
return 0;
}
bool chk( int s, int e, double R )
{
double t = 2*R*R;
double sA = 0, mA = -1e30;
int id;
for( int i = s; i < e; ++i )
{
ang[i] = acos((t-a[i]*a[i])/t);
sA += ang[i];
if( mA < ang[i] )
mA = ang[i], id = i;
}
if( dcmp(sA-mA-pi) >= 0 )
return dcmp(sA-2*pi) <= 0;
else
{
ang[id] *= -1;
return dcmp(sA-mA+2*pi-mA - 2*pi) >= 0;
}
}
int cal( int s, int e, double& S )
{
double ll = 0, rr = 1000000, R;
for( int i = s; i < e; ++i )
ll = max(ll, a[i]*0.5);
for( int T = 0; T < 80; ++T )
{
R = (ll+rr)/2;
if( chk(s, e, R) )
rr = R;
else
ll = R;
}
R = (ll+rr)/2;
S = 0;
for( int i = s; i < e; ++i )
S += R*R*sin(ang[i]);
S *= 0.5;
for( int i = s; i < e; ++i ) if( ang[i] < 0 )
return i;
return -1;
}
double f( int s, int e )
{
if( e-s < 3 ) return 0;
int sum = 0, mv = a[s];
int i, j, k, id = s;
for( i = s; i < e; ++i )
{
sum += a[i];
if( a[i] > mv )
mv = a[i], id = i;
}
if( sum <= mv*2 )
return f(s, id)+f(id+1, e);
else
{
double S;
k = cal(s, e, S);
if( k < 0 )
return S;
return max(S, f(s, k)+f(k+1, e));
}
}
int main()
{
int i, cases = 1;
while( scanf("%d", &N), N )
{
for( i = 0; i < N; ++i )
scanf("%d", &a[i]);
printf("Case %d: %.8lf\n", cases++, f(0, N));
}
return 0;
}

129
HDOJ/3844_autoAC.cpp Normal file
View File

@ -0,0 +1,129 @@
#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<vector>
#include<stack>
const int maxn=1e5+1000;
const int maxm=maxn*2;
using namespace std;
struct Edge
{
int u;
int v;
Edge(){}
Edge(int su,int sv)
{
u=su;
v=sv;
}
};
int e,n,m,nxt[maxm],head[maxn],pnt[maxm],dfn[maxn],low[maxn],iscut[maxn],bccno[maxn],dfs_clock,bcc_cnt;
vector<int> bcc[maxn];
stack<Edge> s;
int dfs(int u,int fa)
{
low[u]=dfn[u]=++dfs_clock;
int child=0;
for(int i=head[u];i!=-1;i=nxt[i])
{
if(!dfn[pnt[i]])
{
s.push(Edge(u,pnt[i]));
child++;
low[u]=min(low[u],dfs(pnt[i],u));
if(dfn[u]<=low[pnt[i]])
{
iscut[u]=true;
bcc_cnt++;
bcc[bcc_cnt].clear();
for(;;)
{
Edge x=s.top();
s.pop();
if(bccno[x.u]!=bcc_cnt)
{
bcc[bcc_cnt].push_back(x.u);
bccno[x.u]=bcc_cnt;
}
if(bccno[x.v]!=bcc_cnt)
{
bcc[bcc_cnt].push_back(x.v);
bccno[x.v]=bcc_cnt;
}
if(x.u==u&&x.v==pnt[i])
break;
}
}
}
else if(dfn[pnt[i]]<dfn[u]&&pnt[i]!=fa)
{
s.push(Edge(u,pnt[i]));
low[u]=min(low[u],dfn[pnt[i]]);
}
}
if(fa<0&&child==1)
iscut[u]=0;
return low[u];
}
void AddEdge(int u,int v)
{
pnt[e]=v;nxt[e]=head[u];head[u]=e++;
pnt[e]=u;nxt[e]=head[v];head[v]=e++;
}
void find_bcc(int n)
{
memset(dfn,0,sizeof(dfn));
memset(iscut,0,sizeof(iscut));
memset(bccno,0,sizeof(bccno));
dfs_clock=bcc_cnt=0;
for(int i=1;i<=n;i++)
if(!dfn[i])
dfs(i,-1);
}
void solve()
{
find_bcc(n);
long long ans1=0,ans2=1;
for(int i=1;i<=bcc_cnt;i++)
{
int cut_cnt=0;
for(int j=0;j<bcc[i].size();j++)
if(iscut[bcc[i][j]])
cut_cnt++;
if(cut_cnt==1)
{
ans1++;
ans2*=(long long)(bcc[i].size()-cut_cnt);
}
}
if(bcc_cnt==1)
{
ans1=2;
ans2=bcc[1].size()*(bcc[1].size()-1)/2;
}
printf("%I64d %I64d\n",ans1,ans2);
}
int main()
{
int cas=1;
while(scanf("%d",&m)&&m)
{
e=n=0;
memset(head,-1,sizeof(head));
while(!s.empty())
s.pop();
for(int i=0;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
n=max(n,u);
n=max(n,v);
AddEdge(u,v);
}
printf("Case %d: ",cas++);
solve();
}
return 0;
}

93
HDOJ/3845_autoAC.cpp Normal file
View File

@ -0,0 +1,93 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
#define PI pair<int, int>
#define MP make_pair
#define x first
#define y second
const int maxn = 100100;
using namespace std;
int N, m;
inline bool in( int x, int y )
{
return x >= -m && x <= m && y >= -m && y <= m;
}
PI p[maxn];
PI a[maxn], b[maxn], c[maxn], d[maxn];
int ea, eb, ec, ed;
bool chk()
{
int i, j, k;
ea = eb = ec = ed = -1;
for( i = 0; i < N; ++i )
{
if( in(p[i].x+m, p[i].y-m) )
{
while( ea >= 0 && a[ea].y >= p[i].y-m ) ea--;
if( ea >= 0 && a[ea].x==p[i].x+m ) continue;
a[++ea] = MP(p[i].x+m, p[i].y-m);
}
if( in(p[i].x+m, p[i].y+m) )
{
while( eb >= 0 && b[eb].y <= p[i].y+m ) eb--;
if( eb >= 0 && b[eb].x==p[i].x+m ) continue;
b[++eb] = MP(p[i].x+m, p[i].y+m);
}
}
for( i = N-1; i >= 0; --i )
{
if( in(p[i].x-m, p[i].y-m) )
{
while( ec >= 0 && c[ec].y >= p[i].y-m ) ec--;
if( ec >= 0 && c[ec].x==p[i].x-m ) continue;
c[++ec] = MP(p[i].x-m, p[i].y-m);
}
if( in(p[i].x-m, p[i].y+m) )
{
while( ed >= 0 && d[ed].y <= p[i].y+m ) ed--;
if( ed >= 0 && d[ed].x==p[i].x-m ) continue;
d[++ed] = MP(p[i].x-m, p[i].y+m);
}
}
int na = 0, nb = 0, nc = ec+1, nd = ed+1;
int v1, v2;
for( i = -m; i <= m; ++i )
{
if( nc > 0 && c[nc-1].x == i ) nc--;
if( nd > 0 && d[nd-1].x == i ) nd--;
v1 = m+1, v2 = -m-1;
if( na <= ea ) v1 = min(v1, a[na].y);
if( nb <= eb ) v2 = max(v2, b[nb].y);
if( nc <= ec ) v1 = min(v1, c[nc].y);
if( nd <= ed ) v2 = max(v2, d[nd].y);
if( v1-1 > v2 ) return 0;
if( na <= ea && a[na].x == i ) na++;
if( nb <= eb && b[nb].x == i ) nb++;
}
return 1;
}
int main()
{
int i, j, k, cases = 1;
int ll, rr;
while( scanf("%d", &N), N+1 )
{
for( i = 0; i < N; ++i )
scanf("%d %d", &p[i].x, &p[i].y);
sort(p, p+N);
ll = -1, rr = 1000000;
while( rr-ll > 1 )
{
m = (ll+rr)/2;
if( chk() )
rr = m;
else
ll = m;
}
m = rr;
printf("Case %d: ", cases++);
if( chk() ) printf("%d\n", rr);
else puts("never");
}
return 0;
}

90
HDOJ/3846_autoAC.cpp Normal file
View File

@ -0,0 +1,90 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct pnt
{
int n,h,f;
}p[1000];
const int maxn=1000000;
int cmp(pnt a,pnt b)
{
if(a.n==b.n) return a.f<b.f;
return a.n<b.n;
}
int c;
void init()
{
long long i,x;
for(i=2;;i++)
{
x=i*(i+1)*(2*i+1)/6;
if(x>maxn) break;
p[c].n=x,p[c].h=i,p[c++].f=1;
}
x=1;
for(i=3;;i+=2)
{
x+=i*i;
if(x>maxn) break;
p[c].n=x,p[c].h=i,p[c++].f=0;
}
x=4;
for(i=4;;i+=2)
{
x+=i*i;
if(x>maxn) break;
p[c].n=x,p[c].h=i,p[c++].f=0;
}
sort(p,p+c,cmp);
}
int dp[maxn+5][12],pre[maxn+5][7];
int sta[1000],top;
int main()
{
init();
int tot,i,j,tc=1;
for(i=0;i<=maxn;i++)
for(j=0;j<=8;j++)
dp[i][j]=21000;
dp[0][0]=-1;
int k;
for(i=0;i<=maxn;i++)
{
for(j=0;j<=6;j++)if(dp[i][j]<=10000)
for(k=dp[i][j]+1;k<c&&p[k].n+i<=maxn;k++)if(dp[i+p[k].n][j+1]>k)
dp[i+p[k].n][j+1]=k;
}
int ii=0;
while(scanf("%d",&tot),tot)
{
int ans=-1;
for(i=1;i<=6;i++)if(dp[tot][i]<=2100)
{
ans=i;
break;
}
printf("Case %d:",++ii);
if(ans<0)
{
puts(" impossible");
continue;
}
i=c;
while(ans--)
{
for(i--;i>=0;i--)if(tot>=p[i].n&&dp[tot-p[i].n][ans]<i)
{
printf(" %d",p[i].h);
if(p[i].f)
printf("H");
else
printf("L");
break;
}
tot-=p[i].n;
}
puts("");
}
}

98
HDOJ/3847_autoAC.cpp Normal file
View File

@ -0,0 +1,98 @@
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include <algorithm>
using namespace std;
const double eps = 1e-8;
const double pi = acos(-1.0);
int n;
struct point {
double x, y;
point operator - (const point& t) const {
point tmp;
tmp.x = x - t.x;
tmp.y = y - t.y;
return tmp;
}
point operator + (const point& t) const {
point tmp;
tmp.x = x + t.x;
tmp.y = y + t.y;
return tmp;
}
bool operator == (const point& t) const {
return fabs(x-t.x) < eps && fabs(y-t.y) < eps;
}
}p[220];
bool cmpxy(point a,point b)
{
if(a.y!=b.y) return a.y<b.y;
return a.x<b.x;
}
double xmult(point p1,point p2,point p0){
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
inline 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);
}
inline double dis(point a, point b) {
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void Grahamxy(point *p, int &n) {
if ( n < 3 )
return;
int i, m=0, top=1;
sort(p, p+n, cmpxy);
for (i=n; i < 2*n-1; i++)
p[i] = p[2*n-2-i];
for (i=2; i < 2*n-1; i++) {
while ( top > m && cross(p[top], p[i], p[top-1]) < eps )
top--;
p[++top] = p[i];
if ( i == n-1 ) m = top;
}
n = top;
}
inline double PLdis(point p,point l1,point l2){
return fabs(cross(p,l1,l2))/dis(l1,l2);
}
void judge(point a,point b,double &len)
{
double max=0;
for(int i=0;i<n;i++)
{
double tmp=PLdis(p[i],a,b);
if(tmp>max)
max=tmp;
}
len=max;
}
double solve()
{
int i,j;
double tmp;
double ans=((__int64)1<<62);
for(i=0;i<n;i++)
{
judge(p[i],p[i+1],tmp);
if(tmp<ans)
ans=tmp;
}
return ans;
}
int main()
{
int i,j;
int cases=1;
while(scanf("%d",&n),n)
{
for(i=0;i<n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
Grahamxy(p,n);
p[n]=p[0];
double ans=solve();
printf("Case %d: %.2lf\n",cases++,ans+0.005);
}
return 0;
}

66
HDOJ/3848_autoAC.cpp Normal file
View File

@ -0,0 +1,66 @@
#include <iostream>
#include <cstdio>
using namespace std;
const int inf=30000000;
const int maxx=10003;
struct Tree{
int v,we,next;
};
Tree tree[maxx*2];
int head[maxx*2],pt;
int dp_f[maxx],dp_s[maxx];
void adde(int x,int y,int we){
tree[pt].v=y;
tree[pt].we=we;
tree[pt].next=head[x];
head[x]=pt++;
}
int min(int a,int b){
return a<b?a:b;
}
void dfs(int root,int pre){
bool mark=false;
for(int i=head[root];i;i=tree[i].next){
int v=tree[i].v;
if(pre==v)continue;
mark=true;
dfs(v,root);
if(dp_f[root]>(dp_f[v]+tree[i].we)){
dp_s[root]=dp_f[root];
dp_f[root]=dp_f[v]+tree[i].we;
}else if(dp_f[root]==dp_f[v]+tree[i].we){
dp_s[root]=dp_f[root];
}else{
dp_s[root]=min(dp_s[root],dp_f[v]+tree[i].we);
}
}
if(!mark)dp_f[root]=dp_s[root]=0;
}
int main(){
int n;
while(scanf("%d",&n)&&n){
pt=1;
int i,x,y,we,cnt;
cnt=0;
for(i=1;i<=n;i++){
dp_f[i]=dp_s[i]=inf;
}
memset(head,0,sizeof(head));
memset(tree,0,sizeof(tree));
for(i=2;i<=n;i++){
scanf("%d%d%d",&x,&y,&we);
adde(x,y,we);
adde(y,x,we);
if(x==1 || y==1)cnt++;
}
dfs(1,0);
int ans=1<<30;
for(i=1;i<=n;i++){
if(dp_f[i]==0 || dp_s[i]==0 ||dp_f[i]>20000000 ||dp_s[i]>20000000)continue;
ans=min(ans,dp_f[i]+dp_s[i]);
}
if(cnt==1)ans=min(ans,dp_f[1]);
printf("%d\n",ans);
}
return 0;
}

135
HDOJ/3849_autoAC.cpp Normal file
View File

@ -0,0 +1,135 @@
#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
#include <string>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN=300000;
const int MAXM=600000;
struct node
{
int u,v;
};
node edge[MAXM];
int first[MAXN],ne[MAXM],cc;
int dfn[MAXN],low[MAXN],belong[MAXN];
int ti,id,top;
int stack[MAXN];
int arc[MAXM];
int arcid;
inline void add_edge(int u,int v)
{
edge[cc].u=u;
edge[cc].v=v;
ne[cc]=first[u];
first[u]=cc;
cc++;
}
void tardfs(int u,int p)
{
dfn[u]=low[u]=ti++;
stack[top++]=u;
int i;
int ff=1;
for(i=first[u];i!=-1;i=ne[i])
{
int v=edge[i].v;
if(v==p&&ff)
{
ff=0;
continue;
}
if(dfn[v]==-1)
{
tardfs(v,u);
if(low[u]>low[v])
low[u]=low[v];
else if(low[v]>dfn[u])
{
arc[arcid]=i;
arcid++;
id++;
for(stack[top]=-1;stack[top]!=v;)
{
top--;
belong[stack[top]]=id;
}
}
}
else if(low[u]>dfn[v])
low[u]=dfn[v];
}
}
int tarjan(int n)
{
ti=1;
id=0;
top=0;
arcid=0;
memset(dfn,-1,sizeof(dfn));
memset(low,0,sizeof(low));
memset(belong,0,sizeof(belong));
tardfs(1,-1);
return id;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(first,-1,sizeof(first));
memset(ne,-1,sizeof(ne));
cc=0;
int n,m;
scanf("%d%d",&n,&m);
map<string,int> mp1;
map<int,string> mp2;
int cnt=1;
int i;
char str1[150],str2[150];
for(i=0;i<m;i++)
{
scanf("%s%s",str1,str2);
if(mp1[str1]==0)
{
mp1[str1]=cnt;
mp2[cnt]=str1;
cnt++;
}
if(mp1[str2]==0)
{
mp1[str2]=cnt;
mp2[cnt]=str2;
cnt++;
}
int u=mp1[str1];
int v=mp1[str2];
add_edge(u,v);
add_edge(v,u);
}
tarjan(n);
int flag=0;
for(i=1;i<=n;i++)
{
if(dfn[i]==-1)
flag=1;
}
if(flag)
{
printf("0\n");
continue;
}
sort(arc,arc+arcid);
printf("%d\n",arcid);
for(i=0;i<arcid;i++)
{
int x=arc[i];
x=min(x,x^1);
printf("%s %s\n",mp2[edge[x].u].c_str(),mp2[edge[x].v].c_str());
}
}
return 0;
}

167
HDOJ/3850_autoAC.cpp Normal file
View File

@ -0,0 +1,167 @@
#include <stdio.h>
#include <string.h>
#define maxn 150
#define mod 1000000007
struct matrix{
int v[70][70];
void clear(){
memset(v,0,sizeof(v));
}
} a,b;
int st_num[maxn],stn;
int n,m;
int ans;
int f[10][10][maxn];
int q[2][maxn],qn[2];
void dfs(int p,int mask,int st){
if (p==n){
if ((mask&(1<<n))==0){
a.v[st_num[st]][st_num[mask]]++;
}
return;
}
int c1=((mask>>p)&1),c2=((mask>>p+1)&1);
if (c1 && c2){
dfs(p+1,mask^(1<<p)^(1<<p+1),st);
}else
if (c1 || c2){
dfs(p+1,mask,st);
dfs(p+1,mask^(1<<p)^(1<<p+1),st);
}else{
dfs(p+1,mask^(1<<p)^(1<<p+1),st);
}
}
matrix mat_mul(matrix a,matrix b){
matrix c;
for (int i=0;i<stn;i++)
for (int j=0;j<stn;j++){
c.v[i][j]=0;
for (int k=0;k<stn;k++)
c.v[i][j]=(c.v[i][j]+(long long)a.v[i][k]*b.v[k][j])%mod;
}
return c;
}
matrix Mul(int p){
matrix ret,ta=a;
ret.clear();
for (int i=0;i<stn;i++) ret.v[i][i]=1;
while (p){
if ((p&1)>0) ret=mat_mul(ret,ta);
ta=mat_mul(ta,ta);
p>>=1;
}
return ret;
}
inline int getBit(int mask,int p){
return (mask>>p)&1;
}
inline void Add(int &x,int v){
x=(x+v)%mod;
}
int cal(int st1,int st2,int c1,int c2){
int ret=0;
int now=0,next;
qn[0]=1;
q[0][0]=st1;
memset(f,0,sizeof(f));
for (int i=0;i<n;i++){
next=now^1;
qn[next]=0;
for (int p=0;p<qn[now];p++){
int st=q[now][p];
if (i==0 || getBit(st,n)==getBit(st2,i-1)){
if (i==0){
f[0][0][st<<1]=1;
q[next][qn[next]++]=st<<1;
}else{
int nst=st;
if (getBit(st2,i-1)) nst-=(1<<n);
Add(f[i][0][nst<<1],f[i-1][n][st]);
q[next][qn[next]++]=nst<<1;
}
}
}
now=next;
for (int j=0;j<n;j++){
next=now^1;
qn[next]=0;
for (int p=0;p<qn[now];p++){
int st=q[now][p],nst;
int c1=getBit(st,j),c2=getBit(st,j+1);
if (c1 && c2){
nst=st^(1<<j)^(1<<j+1);
if (f[i][j+1][nst]==0){
q[next][qn[next]++]=nst;
}
Add(f[i][j+1][nst],f[i][j][st]);
}else
if (c1 || c2){
nst=st;
if (f[i][j+1][nst]==0){
q[next][qn[next]++]=nst;
}
Add(f[i][j+1][nst],f[i][j][st]);
nst=st^(1<<j)^(1<<j+1);
if (f[i][j+1][nst]==0){
q[next][qn[next]++]=nst;
}
Add(f[i][j+1][nst],f[i][j][st]);
}else{
nst=st^(1<<j)^(1<<j+1);
if (f[i][j+1][nst]==0){
q[next][qn[next]++]=nst;
}
Add(f[i][j+1][nst],f[i][j][st]);
}
}
now=next;
}
}
for (int i=0;i<qn[now];i++){
int st=q[now][i];
if (getBit(st,n)==getBit(st2,n-1)){
int nst=st;
if (getBit(st,n)) nst-=(1<<n);
if (nst==0) Add(ret,f[n-1][n][st]);
}
}
long long tmp=ret;
tmp*=c1;
tmp%=mod;
tmp*=c2;
tmp%=mod;
ret=tmp;
return ret;
}
int main(){
int cp=0;
while (scanf("%d%d",&n,&m)!=EOF){
printf("Case %d: ",++cp);
if (n%2){
puts("0");
continue;
}
stn=0;
memset(st_num,-1,sizeof(st_num));
for (int i=0;i<(1<<n);i++){
int k=0;
for (int j=0;j<n;j++)
if ((i&(1<<j))>0) k++;
if (k%2==0){
st_num[i]=stn++;
}
}
a.clear();
for (int i=0;i<(1<<n);i++)
if (st_num[i]>-1) dfs(0,i<<1,i);
b=Mul(m-n);
ans=0;
for (int i=0;i<(1<<n);i++)
for (int j=0;j<(1<<n);j++){
int si=st_num[i],sj=st_num[j];
if (b.v[0][si] && b.v[0][sj]) ans=(ans+cal(i,j,b.v[0][si],b.v[0][sj]))%mod;
}
printf("%d\n",ans);
}
return 0;
}

53
HDOJ/3851_autoAC.cpp Normal file
View File

@ -0,0 +1,53 @@
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=700000;
int Q,N,T;
__int64 PD,PN;
__int64 must;
int n;
__int64 d[maxn];
__int64 f[maxn];
void solve(__int64 t,__int64 P)
{
__int64 k;
if (t>T*2) k=(t-T*2)/T;
else k=0;
must+=k*P;
for (int i=0;i<t-k*T;i++)
d[++n]=P;
}
__int64 ff(int i)
{
if (i<=0) return 0;
else return f[i];
}
__int64 max(__int64 a,__int64 b)
{
if (a>b) return a;
else return b;
}
void Dp()
{
memset(f,0,sizeof(f));
for (int i=1;i<=n;i++)
f[i]=max(ff(i-1),ff(i-T)+d[i]);
}
int main()
{
scanf("%d",&Q);
for (int ci=1;ci<=Q;ci++)
{
scanf("%d%d%I64d%I64d",&N,&T,&PD,&PN);
must=n=0;
for (int i=0;i<N;i++)
{
__int64 t1,t2;
scanf("%I64d%I64d",&t1,&t2);
solve(t1,PD);
solve(t2,PN);
}
Dp();
printf("Case %d: %I64d\n",ci,must+f[n]);
}
}

35
HDOJ/3853_autoAC.cpp Normal file
View File

@ -0,0 +1,35 @@
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
double p[3][1005][1005];
double f[1005][1005];
bool vs[1005][1005];
int R, C;
double DP(int x, int y)
{
if(x==R && y==C) return 0;
if(x>R || y>C) return 0;
if(vs[x][y]) return f[x][y];
if(abs(p[0][x][y]-1)<1e-6)
{
vs[x][y]=1;
return f[x][y]=0;
}
vs[x][y]=1;
return f[x][y]=(2+p[1][x][y]*DP(x, y+1)+p[2][x][y]*DP(x+1, y))/(1-p[0][x][y]);
}
int main()
{
while(scanf("%d%d", &R, &C)!=EOF)
{
for(int i=1; i<=R; i++)
for(int j=1; j<=C; j++)
scanf("%lf%lf%lf", &p[0][i][j], &p[1][i][j], &p[2][i][j]);
memset(f, 0, sizeof(f));
memset(vs, 0, sizeof(vs));
printf("%.3lf\n", DP(1, 1));
}
return 0;
}

80
HDOJ/3854_autoAC.cpp Normal file
View File

@ -0,0 +1,80 @@
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define typev int
#define N 200000
typev ar[N];
int lowb(int t) { return t & (-t) ; }
void add(int i, typev v)
{for ( ; i < N; ar[i] += v, i += lowb(i));}
typev sum(int i)
{
typev s = 0;
for ( ; i > 0; s += ar[i], i -= lowb(i));
return s;
}
int n, m, k;
long long nowans;
int x[N], L[N], R[N], sta[N];
void init()
{
memset(L, -1, sizeof(L)); memset(R, -1, sizeof(R));
memset(ar, 0, sizeof(ar));
for(int i = 1; i <= n; i++) if (x[i] < k)
for(int j = i + 1; j <= n && x[j] >= k; j++)
L[j] = i;
for(int i = n; i >= 1; i--) if (x[i] < k)
for(int j = i - 1; j >= 1 && x[j] >= k; j--)
R[j] = i;
}
int Get(int l, int r, int now)
{
if (l > r) return 0;
if (l == -1 || r == -1) return 0;
if (now == 0) return r - l + 1 - (sum(r) - sum(l - 1));
return sum(r) - sum(l - 1);
}
int main()
{
int t;scanf("%d", &t);
while(t--)
{
scanf("%d%d%d", &n, &m, &k);
for(int i = 1; i <= n; i++) scanf("%d", &x[i]);
init();
for(int i = 1; i <= n; i++)
{
scanf("%d", &sta[i]);
if (sta[i]) add(i, 1);
}
nowans = 0;
for(int i = 1; i <= n; i++)
if (x[i] < k) nowans += Get(i + 1, n, !sta[i]);
else nowans += Get(R[i], n, !sta[i]);
while(m--)
{
int tmp;scanf("%d", &tmp);
if (tmp == 1) cout << nowans << endl;
else
{
scanf("%d", &tmp);
int before = 0, after = 0;
int i = tmp;
if (x[tmp] < k)
{
before += Get(1, i - 1, !sta[i]) + Get(i + 1, n, !sta[i]);
after += Get(1, i - 1, sta[i]) + Get(i + 1, n, sta[i]);
}else
{
before += Get(1, L[i], !sta[i]) + Get(R[i], n, !sta[i]);
after += Get(1, L[i], sta[i]) + Get(R[i], n, sta[i]);
}
if (sta[i] == 0) add(i, 1), sta[i] = 1;
else add(i, -1), sta[i] = 0;
nowans += (after - before);
}
}
}
return 0;
}

25
HDOJ/3857_autoAC.cpp Normal file
View File

@ -0,0 +1,25 @@
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int n,i,a;
int out;
while (scanf("%d",&n)!=EOF)
{
out=0;
for (i=1;i<=n;i++)
{
scanf("%d",&a);
if (a==i)
{
if (out==0) printf("%d",a);
else printf(" %d",a);
out=1;
}
}
if (out==1) printf("\n");
else printf("No Mistake\n");
}
return 0;
}

21
HDOJ/3859_autoAC.cpp Normal file
View File

@ -0,0 +1,21 @@
#include <stdio.h>
__int64 Solve(__int64 n,__int64 m)
{
if (n%m==0) return n/m;
if (n%2==1 && m%2==0) return -1;
if (n>=3*m) return (n/m-2)+Solve(n%m+2*m,m);
if ((n%2)==(m%2)) return 3;
if (n>=2*m) return 4;
return Solve(n,n-m);
}
int main()
{
__int64 n,m,ret,q,r;
while(scanf("%I64d%I64d",&n,&m)!=EOF)
{
ret=Solve(n,m);
if (ret<0) printf("No Solution!\n");
else printf("%I64d\n",ret);
}
return 0;
}

215
HDOJ/3860_autoAC.cpp Normal file
View File

@ -0,0 +1,215 @@
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
#define SIZE 250
#define Max 0x7fffffff
struct edge{int to, next, val;}e[10000000];
int t, n, m, P, O, g[SIZE * SIZE][4];
pair<int, int> o[SIZE], p[SIZE];
int mo[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
int v[SIZE * SIZE], cnt, len[SIZE * SIZE], dis[SIZE * SIZE];
int que[10000000], vst[SIZE * SIZE], n2, m2, pst, ost;
int GetMove(int a, int b, int c, int d)
{
if (c < a) return 0;
if (c > a) return 1;
if (d < b) return 2;
return 3;
}
bool judge(int a, int b){return a >= 0 && a < n && b >= 0 && b < m;}
void insert(int from, int to, int val)
{
e[cnt].val = val; e[cnt].to = to; e[cnt].next = v[from];
v[from] = cnt++;
e[cnt].val = val; e[cnt].to = from; e[cnt].next = v[to];
v[to] = cnt++;
}
int spfa(int s, int t)
{
memset(dis, -1, sizeof(dis));
memset(vst, 0, sizeof(vst));
int head = 0, tail = 0;
dis[s] = 0;
que[tail++] = s; vst[s] = 0;
while(head < tail)
{
int id = que[head++];
vst[id] = 0;
for(int i = v[id]; i != -1; i = e[i].next)
{
if (dis[e[i].to] == -1 || dis[id] + e[i].val < dis[e[i].to])
{
dis[e[i].to] = dis[id] + e[i].val;
if (!vst[e[i].to])
{
vst[e[i].to] = 1;
que[tail++] = e[i].to;
}
}
}
}
return dis[t];
}
void build_graph(int mid, int s, int t)
{
memset(v, -1, sizeof(v)); cnt = 0;
for(int i = 0; i < P - 1; i++)
{
int pre, next;
if (i == 0) pre = s; else pre = pst + i - 1;
if (i == P - 2) next = t; else next = pst + i + 1;
insert(pre, pst + i, p[i].second);
insert(pst + i, next, p[i + 1].second);
int from = p[i].first, to = p[i + 1].first;
for(int j = from; j < to; j++)
{
int tmp = j * m2;
int val = g[j * m][1];
if (val == -1) val = 0;
val = min(val, len[mid]);
insert(pst + i, tmp, val);
}
}
for(int i = 0; i < O - 1; i++)
{
int pre, next;
if (i == 0) pre = s; else pre = ost + i - 1;
if (i == O - 2) next = t; else next = ost + i + 1;
insert(pre, ost + i, o[i].second);
insert(ost + i, next, o[i + 1].second);
int from = o[i].first, to = o[i + 1].first;
for(int j = from; j < to; j++)
{
int tmp = j * m2 + m2 - 1;
int val = g[j * m + m - 1][1];
if (val == -1) val = 0;
val = min(val, len[mid]);
insert(ost + i, tmp, val);
}
}
for(int i = 0; i < m2; i++)
{
int val = g[i][3];
if (val == -1) val = 0;
val = min(val, len[mid]);
insert(s, i, val);
val = g[(n - 1) * m + i][3];
if (val == -1) val = 0;
val = min(val, len[mid]);
insert(t, (n2 - 1) * m2 + i, val);
}
for(int i = 0; i < p[0].first; i++)
{
int val = g[i * m][1];
if (val == -1) val = 0;
val = min(val, len[mid]);
insert(s, i * m2, val);
}
for(int i = 0; i < o[0].first; i++)
{
int val = g[i * m + m - 1][1];
if (val == -1) val = 0;
val = min(val, len[mid]);
insert(s, i * m2 + m2 - 1, val);
}
for(int i = p[P - 1].first; i < n2; i++)
{
int val = g[i * m][1];
if (val == -1) val = 0;
val = min(val, len[mid]);
insert(t, i * m2, val);
}
for(int i = o[O - 1].first; i < n2; i++)
{
int val = g[i * m + m - 1][1];
if (val == -1) val = 0;
val = min(val, len[mid]);
insert(t, i * m2 + m2 - 1, val);
}
if (P == 1) insert(s, t, p[0].second);
if (O == 1) insert(s, t, o[0].second);
for(int i = 0; i < n2; i++)
for(int j = 0; j < m2; j++)
{
int val[4];
val[0] = g[i * m + j][3];
val[2] = g[i * m + j][1];
val[1] = g[(i + 1) * m + j + 1][2];
val[3] = g[(i + 1) * m + j + 1][0];
for(int k = 0; k < 4; k++)
{
if (val[k] == -1) val[k] = 0;
val[k] = min(val[k], len[mid]);
int tmpa = i + mo[k][0], tmpb = j + mo[k][1];
if (tmpa >= 0 && tmpa < n2 && tmpb >= 0 && tmpb < m2)
insert(i * m2 + j, tmpa * m2 + tmpb, val[k]);
}
}
}
int main()
{
scanf("%d", &t);
while(t--)
{
int sum = 0;
scanf("%d%d", &n, &m);
scanf("%d", &P);for(int i = 0; i < P; i++)
scanf("%d%d", &p[i].first, &p[i].second), p[i].first--;
scanf("%d", &O);for(int i = 0; i < O; i++)
scanf("%d%d", &o[i].first, &o[i].second), o[i].first--, sum += o[i].second;
sort(p, p + P); sort(o, o + O);
for(int i = 0; i < n; i++) for(int j = 0; j < m; j++)
for(int k = 0; k < 4; k++)
{
int tmpa = i + mo[k][0], tmpb = j + mo[k][1];
g[i * m + j][k] = -1;
if (judge(tmpa, tmpb)) g[i * m + j][k] = Max;
}
int tmp;scanf("%d", &tmp);while(tmp--)
{
int x1, y1, x2, y2, val;
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &val);
x1--; y1--; x2--; y2--;
g[x1 * m + y1][GetMove(x1, y1, x2, y2)] = val;
g[x2 * m + y2][GetMove(x2, y2, x1, y1)] = val;
}
scanf("%d", &tmp); while(tmp--)
{
int a, b;scanf("%d%d", &a, &b); a--; b--;
for(int i = 0; i < 4; i++)
{
int tmpa = a + mo[i][0], tmpb = b + mo[i][1];
if (judge(tmpa, tmpb))
g[a * m + b][GetMove(a, b, tmpa, tmpb)] = -1;
}
}
scanf("%d", &tmp);
for(int i = 0; i < tmp; i++) scanf("%d", &len[i]);
sort(len, len + tmp);
n2 = n - 1, m2 = m - 1;
int l = 0, r = tmp - 1, ans = -1;
int s = n2 * m2;
int t = s + 1;
pst = t + 1, ost = t + P;
while(l <= r)
{
int mid = (l + r) >> 1;
build_graph(mid, s, t);
int tmp = spfa(s, t);
if (tmp < sum)
l = mid + 1;
else
{
if (ans == -1 || mid < ans) ans = mid;
r = mid - 1;
}
}
if (ans == -1) printf("-1\n");
else
printf("%d\n", len[ans]);
}
return 0;
}

106
HDOJ/3861_autoAC.cpp Normal file
View File

@ -0,0 +1,106 @@
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn=55555;
int dfn[maxn], low[maxn], stack[maxn], belong[maxn], visit[maxn], match[maxn];
bool instack[maxn];
int top, scnt, Index, n, m, T;
vector<int>vt[maxn];
struct Node
{
int u, v;
}f[2*maxn];
void Init_tarjan()
{
top=scnt=Index=0;
for(int i=1; i<=n; i++) dfn[i]=low[i]=instack[i]=0;
}
void tarjan(int u)
{
stack[++top]=u;
dfn[u]=low[u]=++Index;
instack[u]=1;
for(int i=0; i<vt[u].size(); i++)
{
int v=vt[u][i];
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v])
{
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u])
{
int v;
scnt++;
do
{
v=stack[top--];
instack[v]=0;
belong[v]=scnt;
}
while(u!=v);
}
}
bool find(int u)
{
for(int i=0; i<vt[u].size(); i++)
{
int v=vt[u][i];
if(!visit[v])
{
visit[v]=1;
if(match[v]==-1||find(match[v]))
{
match[v]=u;
return true;
}
}
}
return false;
}
int hungary()
{
int cnt=0;
memset(match,-1,sizeof(match));
for(int i=1; i<=scnt; i++)
{
for(int j=1; j<=scnt; j++) visit[j]=0;
if(find(i)) cnt++;
}
return cnt;
}
int main()
{
cin >> T;
while(T--)
{
cin >> n >> m;
for(int i=0; i<=n; i++) vt[i].clear();
for(int i=0; i<m; i++)
{
scanf("%d%d",&f[i].u,&f[i].v);
vt[f[i].u].push_back(f[i].v);
}
Init_tarjan();
for(int i=1; i<=n; i++)
if(!dfn[i]) tarjan(i);
for(int i=0; i<=n; i++) vt[i].clear();
for(int i=0; i<m; i++)
{
int u=belong[f[i].u], v=belong[f[i].v];
if(u==v) continue;
vt[u].push_back(v);
}
int ans=hungary();
cout << scnt-ans <<endl;
}
return 0;
}

63
HDOJ/3862_autoAC.cpp Normal file
View File

@ -0,0 +1,63 @@
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
const double eps=1e-9;
const double pi=acos(-1.0);
using namespace std;
double l, r, ang;
bool mark, ok;
int an, bn, n;
void check(double a)
{
if((!mark)&&(fabs(a-180.0)>eps)&&(fabs(a)>eps))mark=true;
if(fabs(a-180.0)<eps)an++;
else if(fabs(a)<eps)bn++;
if(a<180.0&&a>l)l=a;
else if(a>180.0&&360.0-a>r)r=360.0-a;
if(l+r>180.0)ok=false;
}
int main()
{
int i, t;
double tmp;
scanf("%d", &t);
while(t-->0)
{
ang=0;
l=r=0;
mark=false;
an=0;bn=1;
ok=true;
scanf("%d", &n);
for(i=0; i<n; i++)
{
scanf("%lf", &tmp);
if(ok)
{
if(tmp<0)tmp=-tmp;
else tmp=360.0-tmp;
ang+=tmp;
if(ang>=360.0)ang-=360.0;
check(ang);
}
}
if(ok)
{
if(!mark)
{
if(an>0)ok=false;
}
else
{
if(an>0&&fabs(l)<eps)l=180.0;
else if(an>0&&fabs(r)<eps)r=180.0;
if(l+r-180.00>eps)ok=false;
}
}
if(ok)printf("No\n");
else printf("Yes\n");
}
return 0;
}

8
HDOJ/3863_autoAC.cpp Normal file
View File

@ -0,0 +1,8 @@
#include <stdio.h>
int main()
{
int n;
while(scanf("%d",&n),n+1)
puts("I bet on Oregon Maple~");
return 0;
}

128
HDOJ/3864_autoAC.cpp Normal file
View File

@ -0,0 +1,128 @@
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
using namespace std;
typedef __int64 LL;
const LL NUM=10;
LL t,f[100];
LL mul_mod(LL a,LL b,LL n)
{
a=a%n;
b=b%n;
LL s=0;
while(b)
{
if(b&1)
s=(s+a)%n;
a=(a<<1)%n;
b=b>>1;
}
return s;
}
LL pow_mod(LL a,LL b,LL n)
{
a=a%n;
LL s=1;
while(b)
{
if(b&1)
s=mul_mod(s,a,n);
a=mul_mod(a,a,n);
b=b>>1;
}
return s;
}
bool check(LL a,LL n,LL r,LL s)
{
LL ans,p,i;
ans=pow_mod(a,r,n);
p=ans;
for(i=1;i<=s;i++)
{
ans=mul_mod(ans,ans,n);
if(ans==1&&p!=1&&p!=n-1)return true;
p=ans;
}
if(ans!=1)return true;
return false;
}
bool Miller_Rabin(LL n)
{
if(n<2)return false;
if(n==2)return true;
if(!(n&1))return false;
LL i,r,s,a;
r=n-1;s=0;
while(!(r&1)){r=r>>1;s++;}
for(i=0;i<NUM;i++)
{
a=rand()%(n-1)+1;
if(check(a,n,r,s))
return false;
}
return true;
}
LL gcd(LL a,LL b)
{
return b==0?a:gcd(b,a%b);
}
LL Pollard_rho(LL n,LL c)
{
LL i=1,j,k=2,x,y,d,p;
x=rand()%n;
y=x;
while(true)
{
i++;
x=(mul_mod(x,x,n)+c)%n;
if(y==x)return n;
if(y>x)p=y-x;
else p=x-y;
d=gcd(p,n);
if(d!=1&&d!=n)return d;
if(i==k)
{
y=x;
k+=k;
}
}
}
void find(LL n)
{
if(Miller_Rabin(n))
{
f[t++]=n;
return;
}
LL p=n;
while(p>=n)p=Pollard_rho(p,rand()%(n-1)+1);
find(p);
find(n/p);
}
int main()
{
srand(time(NULL));
LL n;
while(cin>>n)
{
if(n==1){cout<<"is not a D_num"<<endl;continue;}
t=0;
find(n);
if(t!=2&&t!=3){cout<<"is not a D_num"<<endl;continue;}
sort(f,f+t);
if(t==2)
{
if(f[0]!=f[1])cout<<f[0]<<" "<<f[1]<<" "<<n<<endl;
else cout<<"is not a D_num"<<endl;
}
else
{
if(f[0]==f[1]&&f[1]==f[2])cout<<f[0]<<" "<<f[0]*f[0]<<" "<<n<<endl;
else cout<<"is not a D_num"<<endl;
}
}
return 0;
}

115
HDOJ/3865_autoAC.cpp Normal file
View File

@ -0,0 +1,115 @@
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
int x[20][20][15][15], n, m, p, q, need[20][20][2], small[20][20];
int dp[20][20][2], match[20], g[20][20];
bool vst[20], equ[20][20];
int find(int u, int m)
{
for(int i = 0; i < m; i++)
if (!vst[i] && g[u][i])
{
vst[i] = 1;
if (match[i] == -1 || find(match[i], m) == 1)
{
match[i] = u;
return 1;
}
}
return 0;
}
int solve(int n)
{
memset(match, -1, sizeof(match));
int ans = 0;
for(int i = 0; i < n; i++)
{
memset(vst, 0, sizeof(vst));
ans += find(i, n);
}
return ans;
}
int main()
{
while(scanf("%d%d%d%d", &n, &m, &p, &q) != EOF)
{
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
for(int i2 = 0; i2 < m; i2++)
for(int j2 = 0; j2 < m; j2++)
scanf("%d", &x[i][j][i2][j2]);
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
{
for(int i2 = 0; i2 < m; i2++)
for(int j2 = 0; j2 < m; j2++)
{
if (x[i][j][i2][j2]) g[i2][j2] = 1;
else g[i2][j2] = 0;
}
small[i][j] = solve(m) * p;
}
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
{
need[i][j][0] = need[i][j][1] = 0;
dp[i][j][0] = dp[i][j][1] = 0x7fffffff;
for(int i2 = 0; i2 < m; i2++)
{
bool flag = false;
for(int j2 = 0; j2 < m; j2++)
if (x[i][j][i2][j2])
{flag = true; break;}
need[i][j][0] += flag;
}
need[i][j][0] *= p;
for(int j2 = 0; j2 < m; j2++)
{
bool flag = false;
for(int i2 = 0; i2 < m; i2++)
if (x[i][j][i2][j2])
{flag = true; break;}
need[i][j][1] += flag;
}
need[i][j][1] *= p;
if (small[i][j] < need[i][j][0] && small[i][j] < need[i][j][1])
equ[i][j] = true;
else equ[i][j] = false;
}
dp[0][0][0] = min(need[0][0][0], need[0][0][1] + q);
if (equ[0][0]) dp[0][0][0] = min(dp[0][0][0], small[0][0] + q);
dp[0][0][1] = min(need[0][0][1], need[0][0][0] + q);
if (equ[0][0]) dp[0][0][1] = min(dp[0][0][1], small[0][0] + q);
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
{
int tmpa, tmpb;
tmpa = i - 1; tmpb = j;
if (tmpa >= 0 && tmpa < n && tmpb >= 0 && tmpb < n)
{
int val = dp[tmpa][tmpb][0];
int val1 = min(need[i][j][0], need[i][j][1] + q + q);
if (equ[i][j]) val1 = min(val1, small[i][j] + q + q);
int val2 = min(need[i][j][0] + q, need[i][j][1] + q);
if (equ[i][j]) val2 = min(val2, small[i][j] + q);
dp[i][j][0] = min(dp[i][j][0], val + val1);
dp[i][j][1] = min(dp[i][j][1], val + val2);
}
tmpa = i; tmpb = j - 1;
if (tmpa >= 0 && tmpa < n && tmpb >= 0 && tmpb < n)
{
int val = dp[tmpa][tmpb][1];
int val1 = min(need[i][j][1], need[i][j][0] + q + q);
if (equ[i][j]) val1 = min(val1, small[i][j] + q + q);
int val2 = min(need[i][j][1] + q, need[i][j][0] + q);
if (equ[i][j]) val2 = min(val2, small[i][j] + q);
dp[i][j][1] = min(dp[i][j][1], val + val1);
dp[i][j][0] = min(dp[i][j][0], val + val2);
}
}
printf("%d\n", min(dp[n - 1][n - 1][0], dp[n - 1][n - 1][1]));
}
return 0;
}

71
HDOJ/3866_autoAC.cpp Normal file
View File

@ -0,0 +1,71 @@
#include<iostream>
#include<algorithm>
#include<stdlib.h>
using namespace std;
struct Node
{
int value1,value,sub;
}node[10005],ex;
int T,P,N;
int i,sum;
bool cmp1(struct Node p,struct Node q)
{
if(p.value!=q.value)
return p.value>q.value;
else return p.sub<q.sub;
}
int cmp2(const void *p,const void *q)
{
int c = (*(struct Node*)p).sub - (*(struct Node*)q).sub;
if(c>0) return 1;
else return -1;
}
int main()
{
cin>>T;
while(T--)
{
cin>>P>>N;
sum=0;
for(i=0;i<N;i++)
{
scanf("%d",&node[i].value);
node[i].sub=i;
node[i].value1=node[i].value;
sum+=node[i].value;
}
if(sum<P)
{
printf("IMPOSSIBLE\n");
continue;
}
sort(node,node+N,cmp1);
int ln=N,lp=P,x;
while(lp>0)
{
x=node[ln-1].value;
if(lp/ln<x) x=lp/ln;
if(x>0)
{
int pre_ln=ln;
for(i=0;i<pre_ln;i++)
{
node[i].value-=x;
if(!node[i].value) ln--;
}
lp-=x*pre_ln;
}
else
{
for(i=0;i<lp;i++)
node[i].value--;
lp=0;
}
}
qsort(node,N,sizeof(struct Node),cmp2);
for(i=0;i<N-1;i++)
printf("%d ",node[i].value1-node[i].value);
printf("%d\n",node[N-1].value1-node[N-1].value);
}
return 0;
}

105
HDOJ/3867_autoAC.cpp Normal file
View File

@ -0,0 +1,105 @@
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <algorithm>
using namespace std;
#define EPS 1e-8
#define LS0(a) (a << 1)
#define LS1(a) ((a << 1) | 1)
const int MAXN = 20010;
struct Point {
double x, y;
Point(double _x = 0.0, double _y = 0.0): x(_x), y(_y) {}
Point operator + (const Point &b) const {
return Point(x + b.x, y + b.y);
}
Point operator - (const Point &b) const {
return Point(x - b.x, y - b.y);
}
double operator ^ (const Point &b) const {
return x * b.y - y * b.x;
}
bool operator < (const Point &b) const {
return x * b.y < y * b.x;
}
void input() {
scanf("%lf%lf", &x, &y);
}
double diso() {
return sqrt(x * x + y * y);
}
}cur,ps[MAXN];
Point lnlncross_pt(Point aa, Point ad, Point ba, Point bd) {
ad = ad - aa;
bd = bd - ba;
double tmp = bd ^ ad;
return Point(
(ad.x * bd.x * (ba.y - aa.y) + aa.x * bd.x * ad.y - ba.x * ad.x * bd.y) / tmp,
(ad.y * bd.y * (aa.x - ba.x) + ba.y * ad.y * bd.x - aa.y * bd.y * ad.x) / tmp);
}
struct Item {
Point *u, *v;
int type;
int sgid;
Item(Point *_u = NULL, Point *_v = NULL, int _ty = 0, int _id = 0)
: u(_u), v(_v), type(_ty), sgid(_id) {}
bool operator < (const Item &b) const {
if(u == b.u && v == b.v)
return false;
Point au = lnlncross_pt(Point(0.0, 0.0), cur, *u, *v);
Point bu = lnlncross_pt(Point(0.0, 0.0), cur, *b.u, *b.v);
return au.diso() < bu.diso();
}
}item[MAXN];
bool flag[MAXN];
set<Item> Scan;
bool cmp(const Item &a, const Item &b) {
return atan2(a.u->y, a.u->x) < atan2(b.u->y, b.u->x);
}
void inputps(int n) {
Point src, a, b;
src.input();
for(int i = 0; i < n; ++i) {
a.input(); a = a - src;
b.input(); b = b - src;
if(b < a) swap(a, b);
ps[LS0(i)] = a;
ps[LS1(i)] = b;
item[LS0(i)] = Item(&ps[LS0(i)], &ps[LS1(i)], 0, i);
item[LS1(i)] = Item(&ps[LS1(i)], &ps[LS0(i)], 1, i);
}
sort(item, item + 2 * n, cmp);
}
bool sgcross_with_ax(Item &a) {
Point tmp(-1.0, 0.0);
return (*a.u ^ *a.v) * (*a.u ^ tmp) > 0.0
&& (*a.u ^ tmp) * (tmp ^ *a.v) > 0.0;
}
int main() {
int n;
while(scanf("%d", &n) != EOF) {
inputps(n);
memset(flag,0,sizeof(flag));
Scan.clear();
for(int i = 0; i < 2 * n; ++i) {
cur = *item[i].u;
if(item[i].type == 1 && sgcross_with_ax(item[i]))
Scan.insert(item[i]);
}
for(int i = 0; i < 2 * n; ++i) {
cur = *item[i].u;
if(item[i].type == 1)
Scan.insert(item[i]);
else
Scan.erase(Item(item[i].v, item[i].u, 1, item[i].sgid));
if(!Scan.empty())
flag[Scan.begin()->sgid] = true;
}
int ans = 0;
for(int i = 0; i < n; ++i)
if(flag[i])ans ++;
printf("%d\n", ans);
}
return 0;
}

86
HDOJ/3868_autoAC.cpp Normal file
View File

@ -0,0 +1,86 @@
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define eps 1e-6
typedef struct
{
double x,y;
}point;
point a[20005];
double ret;
point tag[20005];
double Dist(point p,point q)
{
return sqrt((p.x-q.x)*(p.x-q.x)+(p.y-q.y)*(p.y-q.y));
}
bool cmpx(point p,point q)
{
if (p.x!=q.x) return p.x<q.x;
return p.y<q.y;
}
bool cmpy(point p,point q)
{
if (p.y!=q.y) return p.y<q.y;
return p.x<q.x;
}
void Merge(int l,int r)
{
int mid,x,y,i,j,k;
double m;
if (r-l+1<=6)
{
for (i=l;i<=r;i++)
{
for (j=i+1;j<=r;j++)
{
m=Dist(a[i],a[j]);
if (m>ret/2) continue;
for (k=j+1;k<=r;k++)
{
ret=min(ret,m+Dist(a[j],a[k])+Dist(a[i],a[k]));
}
}
}
return;
}
mid=(l+r)/2;
Merge(l,mid);
Merge(mid+1,r);
int up=0;
for (i=l;i<=r;i++)
{
if (fabs(a[mid].x-a[i].x)<ret/2) tag[up++]=a[i];
}
sort(tag,tag+up,cmpy);
for (i=0;i<up;i++)
{
for (j=i+1;j<up && j<=i+7;j++)
{
m=Dist(tag[i],tag[j]);
if (m>ret/2) continue;
for (k=j+1;k<up && k<=j+7;k++)
{
ret=min(ret,m+Dist(tag[j],tag[k])+Dist(tag[i],tag[k]));
}
}
}
}
int main()
{
int i,j,n,T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for (i=0;i<n;i++)
{
scanf("%lf%lf",&a[i].x,&a[i].y);
}
sort(a,a+n,cmpx);
ret=999999999;
Merge(0,n-1);
printf("%.3lf\n",ret);
}
return 0;
}

109
HDOJ/3869_autoAC.cpp Normal file
View File

@ -0,0 +1,109 @@
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#define CL(arr, val) memset(arr, val, sizeof(arr))
#define REP(i, n) for((i) = 0; (i) < (n); ++(i))
#define FOR(i, l, h) for((i) = (l); (i) <= (h); ++(i))
#define FORD(i, h, l) for((i) = (h); (i) >= (l); --(i))
#define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%lld\n", x)
typedef long long LL;
const double eps = 1e-6;
const double PI = acos(-1.0);
const int inf = 0x1F1F1F1F;
using namespace std;
const int N = 100010;
const int MOD = 1000000007;
int n;
int pre[N];
bool vis[2][N];
int v[N], e[N];
int vv[N<<1], ee[N<<1];
void get_next(int P[]) {
int i, j = -1;
pre[0] = -1;
for(i = 1; i < n; ++i) {
while(j > -1 && P[j+1] != P[i]) j = pre[j];
if(P[j + 1] == P[i]) ++j;
pre[i] = j;
}
}
void kmp(int T[], int P[], int f) {
get_next(P);
int i, k;
for(k = -1, i = 0; i < (n<<1); ++i) {
while(k > -1 && P[k+1] != T[i]) k = pre[k];
if(P[k+1] == T[i]) ++k;
if(k == n - 1) {
vis[f][i - n + 1] = true;
k = pre[k];
}
}
}
LL Pow(LL a, int b) {
LL res = 1;
while(b) {
if(b&1) res = (res*a)%MOD;
a = (a*a)%MOD;
b >>= 1;
}
return res;
}
int exp_gcd(int a, int b, int& x, int& y) {
if(b == 0) {
x = 1; y = 0;
return a;
}
int d = exp_gcd(b, a%b, y, x);
y -= a/b*x;
return d;
}
int Inv(int c) {
int x, y;
exp_gcd(c, MOD, x, y);
return x < 0 ? x + MOD : x;
}
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a%b);
}
int main() {
int T, c, cnt, i;
LL ans;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &c);
for(i = 0; i < n; ++i) {scanf("%d", &v[i]); vv[i] = vv[i+n] = v[i];}
for(i = 0; i < n; ++i) {scanf("%d", &e[i]); ee[i] = ee[i+n] = e[i];}
CL(vis, false);
kmp(vv, v, 0);
kmp(ee, e, 1);
ans = cnt = 0;
for(i = 1; i <= n; ++i) {
if(vis[0][i] && vis[1][i]) {
ans += Pow(c, gcd(i, n));
ans %= MOD;
cnt++;
}
}
ans = ans*Inv(cnt)%MOD;
cout << ans << endl;
}
return 0;
}

87
HDOJ/3870_autoAC.cpp Normal file
View File

@ -0,0 +1,87 @@
#include<cstdio>
#include<vector>
#include<queue>
#include<string.h>
#include<algorithm>
#define N 400*400
using namespace std;
int t,n,a[405][405];
int v,e,low[N],p[N],q[N],adj[N];
bool f[N];
struct Edge
{
int v,w,next;
}edge[4*N];
void insert(int u,int v,int w)
{
edge[e].v=v;edge[e].w=w;
edge[e].next=adj[u];
adj[u]=e++;
}
int spfa(int s)
{
memset(f,0,sizeof(f));
memset(low,0x7f,sizeof(low));
int i,j,k,x,h=0,t=1;
v=n*n+1;
q[t]=s;
low[s]=0;
while(h!=t)
{
h=(h+1)%(v+1);
x=q[h];
f[x]=0;
for(k=adj[x];k!=-1;k=edge[k].next)
{
i=edge[k].v;
j=edge[k].w;
if(j+low[x]<low[i])
{
low[i]=low[x]+j;
if(!f[i])
{
f[i]=1;
t=(t+1)%(v+1);
q[t]=i;
}
}
}
}
return low[n*n+1];
}
int main()
{
scanf("%d",&t);
while(t--)
{
memset(adj,-1,sizeof(adj));
int i,j;
e=0;
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
n--;
for(i=0;i<n;i++)
{
insert(0,i*n+1,a[i][0]);
insert(0,(n-1)*n+i+1,a[n][i]);
insert(i+1,n*n+1,a[0][i]);
insert(i*n+n,n*n+1,a[i][n]);
for(j=0;j<n;j++)
{
if(j+1<n)
{
insert(i*n+j+1,i*n+j+2,a[i][j+1]);
insert(i*n+j+2,i*n+j+1,a[i][j+1]);
}
if(i+1<n)
{
insert(i*n+j+1,(i+1)*n+j+1,a[i+1][j]);
insert((i+1)*n+j+1,i*n+j+1,a[i+1][j]);
}
}
}
printf("%d\n",spfa(0));
}
}

191
HDOJ/3871_autoAC.cpp Normal file
View File

@ -0,0 +1,191 @@
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
#define maxn 31
bool mat[maxn][maxn][maxn];
struct point
{
short x,y,z,rot,dir,step;
point(){}
point(int a,int b,int c,int d,int e):x(a),y(b),z(c),rot(d),dir(e){}
bool operator ==(const point q)
{
return x == q.x && y == q.y && z == q.z && rot == q.rot && dir == q.dir;
}
}ST,EN;
int n;
char opt[10] = {0,'F','L','R','U','D','B'};
point pre[maxn][maxn][maxn][7][7];
short step[maxn][maxn][maxn][7][7];
int dis[maxn][maxn][maxn][7][7];
int LEFT[7][7] =
{
0,0,0,0,0,0,0,
0,0,3,5,0,6,2,
0,6,0,1,3,0,4,
0,2,4,0,5,1,0,
0,0,6,2,0,3,5,
0,3,0,4,6,0,1,
0,5,1,0,2,4,0
};
int RIGHT[7][7] =
{
0,0,0,0,0,0,0,
0,0,6,2,0,3,5,
0,3,0,4,6,0,1,
0,5,1,0,2,4,0,
0,0,3,5,0,6,2,
0,6,0,1,3,0,4,
0,2,4,0,5,1,0
};
int dx[7] = {0,1,0,0,-1,0,0};
int dy[7] = {0,0,1,0,0,-1,0};
int dz[7] = {0,0,0,1,0,0,-1};
int back[7] = { 0,4,5,6,1,2,3 };
char record[maxn * maxn * maxn * 10 * 10];
bool check(point p)
{
int x = p.x,y = p.y,z = p.z;
return (1 <= x && x <= n) && (1 <= y && y <= n) && (1 <= z && z <= n) && !mat[x][y][z];
}
queue <point> Q;
void bfs()
{
while(!Q.empty())
Q.pop();
int x = ST.x,y = ST.y,z = ST.z,rot = ST.rot,dir = ST.dir;
if(!check(ST))
return ;
dis[x][y][z][rot][dir] = 0;
if(ST == EN)
return ;
pre[x][y][z][rot][dir] = ST;
ST.step = 0;
Q.push(ST);
while(!Q.empty())
{
point now = Q.front();
Q.pop();
for(int i = 1;i <= 6;i++)
{
if(i == 1)
{
rot = now.rot;
dir = now.dir;
x = now.x + dx[now.dir];
y = now.y + dy[now.dir];
z = now.z + dz[now.dir];
}
else
{
if(i == 2)
{
rot = now.rot;
dir = LEFT[now.rot][now.dir];
}
else if(i == 3)
{
rot = now.rot;
dir = RIGHT[now.rot][now.dir];
}
else if(i == 4)
{
rot = back[now.dir];
dir = now.rot;
}
else if(i == 5)
{
rot = now.dir;
dir = back[now.rot];
}
else
{
rot = now.rot;
dir = back[now.dir];
}
x = now.x;
y = now.y;
z = now.z;
}
if(check(point(x,y,z,rot,dir)) && dis[x][y][z][rot][dir] > now.step + 1)
{
dis[x][y][z][rot][dir] = now.step + 1;
pre[x][y][z][rot][dir] = now;
step[x][y][z][rot][dir] = i;
point haha(x,y,z,rot,dir);
haha.step = now.step + 1;
Q.push(haha);
}
}
}
}
void come(int temp)
{
record[temp + 1] = 0;
point rc = EN;
while(!(rc == ST))
{
int x = rc.x,y = rc.y,z = rc.z,rot = rc.rot,dir = rc.dir;
record[temp--] = opt[step[x][y][z][rot][dir]];
rc = pre[x][y][z][rot][dir];
}
}
void solve()
{
scanf("%d",&n);
int a,b,c,d,e;
memset(mat,0,sizeof(mat));
for(int i = 0;i < 2;i++)
{
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
if(i == 0)
ST = point(a,b,c,e,d);
else
EN = point(a,b,c,e,d);
}
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
char str[15];
str[0] = 'x';
scanf(" %s",&str[1]);
int l = strlen(str);
bool code[35];
memset(code,0,sizeof(code));
for(int q = l - 1;q >= 1;q--)
{
int fuck = str[q] - '0';
for(int k = 1;k <= 3;k++)
{
if(fuck & 1)
code[(l - q - 1) * 3 + k] = 1;
fuck >>= 1;
}
}
for(int k = n;k >= 1;k --)
mat[j][k][i] = code[k];
}
}
memset(dis,0x7f,sizeof(dis));
memset(pre,0,sizeof(pre));
memset(step,0,sizeof(step));
bfs();
if(dis[EN.x][EN.y][EN.z][EN.rot][EN.dir] == 0x7f7f7f7f)
puts("Sorry, I can't get there.");
else
{
int ans = dis[EN.x][EN.y][EN.z][EN.rot][EN.dir];
come(ans);
printf("%d\n",ans);
puts(&record[1]);
}
}
int main()
{
int t;
scanf("%d",&t);
for(int i = 0;i < t;i++)solve();
}

123
HDOJ/3872_autoAC.cpp Normal file
View File

@ -0,0 +1,123 @@
#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
typedef __int64 LL;
typedef unsigned __int64 ULL;
const int N=100010;
const int INF=0x3f3f3f3f;
const int MOD=95041567,STA=8000010;
const LL LNF=1LL<<60;
const double EPS=1e-8;
const double OO=1e15;
const int dx[4]={-1,0,1,0};
const int dy[4]={0,1,0,-1};
const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
LL low[N<<2][2],f[N];
int l[N],la[N],q[N][2],type[N],eng[N];
int T,n;
void update(int l,int r,int rt,int w,LL val,int flag)
{
if(l==r){
low[rt][flag]=val;
return;
}
int mid=(l+r)>>1;
if(w<=mid)update(lson,w,val,flag);
else update(rson,w,val,flag);
low[rt][flag]=Min(low[rt<<1][flag],low[rt<<1|1][flag]);
}
LL query(int l,int r,int rt,int L,int R,int flag)
{
if(L<=l && r<=R){
return low[rt][flag];
}
int mid=(l+r)>>1;
LL ret=LNF;
if(L<=mid)ret=Min(ret,query(lson,L,R,flag));
if(R>mid)ret=Min(ret,query(rson,L,R,flag));
return ret;
}
int binary(int l,int r,int tar)
{
int mid;
while(l<r){
mid=(l+r)>>1;
if(q[mid][0]<tar)l=mid+1;
else r=mid;
}
return l;
}
int main()
{
int i,j,t,L,R,top;
LL lowf;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
mem(la,0);
for(i=1;i<=n;i++){
scanf("%d",&type[i]);
l[i]=la[type[i]];
la[type[i]]=i;
}
for(i=1;i<=n;i++){
scanf("%d",&eng[i]);
}
mem(low,INF);mem(f,INF);f[0]=0;
update(0,n+1,1,0,0,1);
q[top=0][0]=0;q[top][1]=INF;
for(i=1;i<=n;i++){
while(q[top][1]<=eng[i]){
update(0,n+1,1,q[top-1][0],LNF,0);
top--;
}
q[++top][0]=i;q[top][1]=eng[i];
t=q[top-1][0];
lowf=query(0,n+1,1,t,i-1,1);
update(0,n+1,1,t,lowf+eng[i],0);
L=binary(0,top+1,l[i]);
f[i]=query(0,n+1,1,q[L][0],i,0);
if(l[i]<q[L][0]){
lowf=query(0,n+1,1,l[i],q[L][0]-1,1);
f[i]=Min(f[i],lowf+q[L][1]);
}
update(0,n+1,1,i,f[i],1);
}
printf("%I64d\n",f[n]);
}
return 0;
}

78
HDOJ/3873_autoAC.cpp Normal file
View File

@ -0,0 +1,78 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long LL;
typedef pair<LL,int>Pair;
#define inf (1ll)<<55
#define MAXN 3333
struct Node {
int v,w;
};
int In[MAXN];
LL dist[MAXN],pro[MAXN];
bool mark[MAXN];
vector<Node>Map[MAXN];
vector<int>vet[MAXN];
int n,m;
void Dijkstra(){
for(int i=1;i<=n;i++){ dist[i]=inf;pro[i]=0; }
dist[1]=0;
memset(mark,false,sizeof(mark));
priority_queue<Pair,vector<Pair>,greater<Pair> >Q;
Q.push(make_pair(dist[1],1));
while(!Q.empty()){
Pair pp=Q.top();
Q.pop();
int u=pp.second;
if(mark[u])continue;
mark[u]=true;
for(int i=0;i<vet[u].size();i++){
int v=vet[u][i];
In[v]--;
pro[v]=max(pro[v],dist[u]);
if(dist[v]!=inf&&In[v]==0){
dist[v]=max(dist[v],pro[v]);
Q.push(make_pair(dist[v],v));
}
}
for(int i=0;i<Map[u].size();i++){
int v=Map[u][i].v;
int w=Map[u][i].w;
if(dist[v]>dist[u]+w){
dist[v]=max(dist[u]+w,pro[v]);
if(In[v]==0){ Q.push(make_pair(dist[v],v)); }
}
}
}
}
int main() {
int _case,u,v,w,x;
scanf("%d",&_case);
while(_case--) {
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++) {
Map[i].clear();
vet[i].clear();
}
while(m--) {
scanf("%d%d%d",&u,&v,&w);
Node p;
p.v=v,p.w=w;
Map[u].push_back(p);
}
for(int i=1; i<=n; i++) {
scanf("%d",&In[i]);
for(int j=1; j<=In[i]; j++) {
scanf("%d",&x);
vet[x].push_back(i);
}
}
Dijkstra();
printf("%I64d\n",dist[n]);
}
return 0;
}

81
HDOJ/3874_autoAC.cpp Normal file
View File

@ -0,0 +1,81 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define mem(a) memset(a,0,sizeof(a))
#define MAX(a , b) ((a) > (b) ? (a) : (b))
using namespace std;
struct QUERY
{
int a,b;
int index;
}query[200005];
__int64 c[50005], ans[200005];
int last[1000005], value[50005], N, M, T;
int cmp(QUERY x, QUERY y)
{
return x.b < y.b;
}
int lowbit(int x)
{
return x & (-x);
}
__int64 getSum(int k)
{
__int64 sum = 0;
while(k>=1)
{
sum += c[k];
k -= lowbit(k);
}
return sum;
}
void edit(int k, int val)
{
while(k<=N)
{
c[k] += val;
k += lowbit(k);
}
}
int main()
{
while(~scanf("%d", &T))while(T--)
{
mem(query); mem(c);
mem(last); mem(ans);
mem(value);
scanf("%d", &N);
for(int i=1;i<=N;i++)
{
scanf("%d", &value[i]);
edit(i, value[i]);
if(!last[value[i]]) last[value[i]] = i;
}
scanf("%d", &M);
for(int i=1;i<=M;i++)
{
scanf("%d%d", &query[i].a, &query[i].b);
query[i].index = i;
}
sort(query+1, query+M+1, cmp);
int lastRight = 1;
for(int i = 1; i <= M; i ++)
{
for(int j = lastRight; j <= query[i].b; j ++)
{
if(last[value[j]] != j)
{
edit(last[value[j]], -value[j]);
last[value[j]] = j;
}
}
lastRight = query[i].b;
ans[query[i].index] = getSum(query[i].b) - getSum(query[i].a-1);
}
for(int i=1;i<=M;i++)
{
printf("%I64d\n", ans[i]);
}
}
return 0;
}

169
HDOJ/3875_autoAC.cpp Normal file
View File

@ -0,0 +1,169 @@
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
using namespace std;
typedef unsigned __int64 u64;
const int MAX=100;
u64 f0[100],f1[100],ff,n,tmp,ret,ret1,p,pp;
u64 myrandom()
{
u64 a;
a=rand();
a*=rand();
a*=rand();
a*=rand();
return a;
}
u64 mulmod(u64 a,u64 b,u64 c)
{
u64 ret=0;
while(b)
{
if(b&1)
{
ret+=a;
if(ret>c)
ret-=c;
}
a<<=1;
if(a>c)
a-=c;
b>>=1;
}
return ret;
}
u64 powmod(u64 a,u64 b,u64 c)
{
u64 ret=1;
while(b)
{
if(b&1)
ret=mulmod(ret,a,c);
a=mulmod(a,a,c);
b>>=1;
}
return ret;
}
int miller(u64 base,u64 n)
{
u64 m=n-1,k=0;
while(m%2==0)
{
m>>=1;
k++;
}
if(powmod(base,m,n)==1)
return 1;
for(int i=0;i<k;i++)
{
if(powmod(base,m<<i,n)==n-1)
return 1;
}
return 0;
}
int Miller_Rabin(u64 n)
{
for(int i=2;i<100;++i)
if(n%i==0)
if(n==i)
return 1;
else return 0;
for( int i=0;i<MAX;++i)
{
u64 base=myrandom()%(n-1)+1;
if(!miller(base,n))
return 0;
}
return 1;
}
u64 gcd(u64 a, u64 b)
{
if(b==0) return a;
return gcd(b,a%b);
}
u64 f(u64 a,u64 b)
{
return (mulmod(a,a,b)+1)%b;
}
u64 Pollard_Rho(u64 n)
{
if(n<=2) return 0;
for(u64 i=2;i<100;++i)
if(n%i==0)
return i;
u64 p,x,xx;
for( int i=1;i<MAX;i ++)
{
x=myrandom()%n;
xx=f(x,n);
p=gcd((xx+n-x)%n,n);
while(p==1)
{
x=f(x,n);
xx=f(f(xx,n),n);
p=gcd((xx+n-x)%n,n)%n;
}
if(p) return p;
}
return 0;
}
u64 Prime(u64 a)
{
if(Miller_Rabin(a)) return 0;
u64 t=Pollard_Rho(a);
u64 p=Prime(t);
if(p) return p;
return t;
}
int main()
{
int tt;
scanf("%d",&tt);
while(tt--)
{
scanf("%I64d %I64d",&n,&p);
pp=p*2;
u64 old=n%pp;
ret=1,ret1=1,ff=0;
while(n>1)
{
if(Miller_Rabin(n))
break;
tmp=Prime(n);
f0[ff]=tmp;
f1[ff]=1;
n/=tmp;
while(n%tmp==0)
{
n/=tmp;
f1[ff]++;
}
ff++;
}
if(n>0)
{
f0[ff]=n;
f1[ff++]=1;
}
for(int i=0;i<ff;++i)
{
u64 tmp=1;
for(int j=0;j<f1[i];++j)
tmp=tmp*f0[i];
ret1=ret1*(f1[i]*(tmp-tmp/f0[i])+tmp);
ret1=ret1%p;
tmp=1;
for(int j=0;j<2*f1[i]+1;++j)
tmp=tmp*f0[i];
ret=ret*((tmp+1)/(1+f0[i]));
ret=ret%pp;
}
ret=((old*(ret+1))%pp)/2;
if(ret==ret1)
printf("yes\n");
else
printf("no\n");
}
return 0;
}

32
HDOJ/3876_autoAC.cpp Normal file
View File

@ -0,0 +1,32 @@
#include<stdio.h>
#include<math.h>
int main()
{
int t;
int a,b,c;
double x1,x2,x,temp;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&a,&b,&c);
x=b*b-4*a*c;
if(x<0)
printf("NO\n");
else if(x==0)
{
x1=-1.0*b/(2*a);
printf("%.2f\n",x1);
}
else if(x>0)
{
x1=(-1.0*b-sqrt(x))/(2*a);
x2=(-1.0*b+sqrt(x))/(2*a);
if(x1>x2)
{
temp=x1;x1=x2;x2=temp;
}
printf("%.2f %.2f\n",x1,x2);
}
}
return 0;
}

46
HDOJ/3877_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
struct node{
__int64 a;
__int64 b;
__int64 c;
int temp;
}m[10002];
__int64 cmp(node x,node y)
{
if((x.c==y.c)&&((x.a-x.b)*(y.a-y.b)<=0))
return x.a>y.a;
if(x.c==y.c)
return x.temp<y.temp;
return x.c>y.c;
}
int main()
{
int t,n,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%I64d%I64d",&m[i].a,&m[i].b);
m[i].c=m[i].a+m[i].b;
m[i].temp=i;
}
sort(m,m+n,cmp);
for(i=0;i<n;i++)
{
if(m[i].a>m[i].b)
printf("%I64d+%I64d=[>%I64d]\n",m[i].a,m[i].b,m[i].c);
else if(m[i].a==m[i].b)
printf("%I64d+%I64d=[=%I64d]\n",m[i].a,m[i].b,m[i].c);
else
printf("%I64d+%I64d=[<%I64d]\n",m[i].a,m[i].b,m[i].c);
}
printf("\n");
}
return 0;
}

110
HDOJ/3879_autoAC.cpp Normal file
View File

@ -0,0 +1,110 @@
#include<iostream>
#include<cstring>
#include<cstdio>
#define MAXN 60000
#define INF 1e8
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
using namespace std;
struct edge
{
int u,v,w,next;
}E[400050];
int head[MAXN],ecnt;
int gap[MAXN],cur[MAXN],pre[MAXN],dis[MAXN];
int l,r,mid;
int N,M,scr,sink,vn,num;
void Insert(int u,int v,int w)
{
E[ecnt].u=u;
E[ecnt].v=v;
E[ecnt].w=w;
E[ecnt].next=head[u];
head[u]=ecnt++;
E[ecnt].u=v;
E[ecnt].v=u;
E[ecnt].w=0;
E[ecnt].next=head[v];
head[v]=ecnt++;
}
int Sap(int s,int t,int n)
{
int ans=0,aug=INF;
int i,v,u=pre[s]=s;
for(i=0;i<=n;i++)
{
cur[i]=head[i];
dis[i]=gap[i]=0;
}
gap[s]=n;
bool flag;
while(dis[s]<n)
{
flag=false;
for(int &j=cur[u];j!=-1;j=E[j].next)
{
v=E[j].v;
if(E[j].w>0&&dis[u]==dis[v]+1)
{
flag=true;
aug=min(aug,E[j].w);
pre[v]=u;
u=v;
if(u==t)
{
ans+=aug;
while(u!=s)
{
u=pre[u];
E[cur[u]].w-=aug;
E[cur[u]^1].w+=aug;
}
aug=INF;
}
break;
}
}
if(flag) continue;
int mindis=n;
for(i=head[u];i!=-1;i=E[i].next)
{
v=E[i].v;
if(E[i].w>0&&dis[v]<mindis)
{
mindis=dis[v];
cur[u]=i;
}
}
if((--gap[dis[u]])==0) break;
gap[dis[u]=mindis+1]++;
u=pre[u];
}
return ans;
}
int n,m;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(head,-1,sizeof(head));ecnt=0;
scr=0;sink=n+m+1;vn=sink+1;
for(int i=1;i<=n;i++)
{
int v;
scanf("%d",&v);
Insert(i,sink,v);
}
int sum=0;
for(int i=1;i<=m;i++)
{
int a,b,v;
scanf("%d%d%d",&a,&b,&v);
Insert(scr,i+n,v);
Insert(i+n,a,INF);
Insert(i+n,b,INF);
sum+=v;
}
printf("%d\n",sum-Sap(scr,sink,vn));
}
return 0;
}

26
HDOJ/3880_autoAC.cpp Normal file
View File

@ -0,0 +1,26 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
__int64 i, j, k;
__int64 a, b, M;
while(scanf("%I64d%I64d%I64d", &a, &b, &M) != EOF){
if (a > b){
k = a;
a = b;
b = k;
}
if (b == 1){
k = M;
}else{
k = 0;
j = 1;
for (i = b; M / i > 0; i = i * b){
k += j * M / i;
j = -j;
}
}
printf("%I64d\n", k);
}
return 0;
}

163
HDOJ/3881_autoAC.cpp Normal file
View File

@ -0,0 +1,163 @@
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
typedef double typev;
typedef pair<int, int> pii;
const int N = 100005;
const double eps = 1e-8;
const double DINF = 1e40;
const double pi = acos(-1.0);
template<typename T>
void getSNum(T& ans){
char ch;
int s;
while(true){
ch = getchar();
if((ch >= '0' && ch <= '9') || ch == '-') break;
}
if(ch == '-'){
s = -1;
ans = 0;
}else{
s = 1;
ans = ch -'0';
}
while(true){
ch = getchar();
if(!(ch >= '0' && ch <= '9')) break;
ans = ans*10+ch-'0';
}
ans *= s;
}
int sign(double a){
return a < -eps ? -1 : (a > eps);
}
struct point{
typev x, y;
int id;
void read(){
scanf("%lf%lf", &x, &y);
}
void print(){
printf("(%lf, %lf)\n", x, y);
}
point(typev _x=0, typev _y=0):x(_x), y(_y) {}
}ps[N], que[N];
int n;
point k;
typev xmul(point st, point ed1, point ed2){
return (ed1.x-st.x)*(ed2.y-st.y) - (ed1.y-st.y)*(ed2.x-st.x) ;
}
bool isLess(pii a, pii b){
return a.first < b.first || (a.first == b.first && a.second < b.second);
}
double maxSlope(point *ps, int n, pii& ans){
int l, r, i, k;
double s, tmp;
pii ti;
l = r = 0;
que[r++] = ps[0];
s = -DINF;
for(i = 1; i < n; i++){
while(r-l >= 2){
k = sign(xmul(que[l], que[l+1], ps[i]));
if(k < 0 || (k == 0 && que[l].id < que[l+1].id)) break;
l++;
}
tmp = (ps[i].y-que[l].y)/(ps[i].x-que[l].x);
ti.first = que[l].id; ti.second = ps[i].id;
if(sign(s-tmp) < 0 || (sign(s-tmp) == 0 && isLess(ti, ans))){
s = tmp;
ans = ti;
}
while(r-l >= 2){
k = sign(xmul(que[r-2], que[r-1], ps[i]));
if(k > 0 || (k == 0 && que[r-1].id < ps[i].id)) break;
r--;
}
que[r++] = ps[i];
}
return s;
}
bool cmpp(point a, point b){
return (a.x < b.x) || (sign(a.x-b.x) == 0 && a.y < b.y);
}
double maxSlopeAbs(point* ps, int n, pii& ans){
//棣ゆ圭x肩哥
sort(ps, ps+n, cmpp);
double s = -DINF;
int i, j, mi;
pii ti;
for(i = 0; i < n; i++){
for(j = i; j < n && sign(ps[j].x-ps[i].x) == 0; j++) ;
if(j-i > 1){
for(mi=ps[i].id, i++; i < j; i++){
ti.first = mi;
ti.second = ps[i].id;
if(s < 0 || (s > 0 && isLess(ti, ans))){
s = DINF;
ans = ti;
}
if(mi > ps[i].id){
mi = ps[i].id;
}
}
}
i = j-1;
}
if(s > 0){
return s;
}
s = maxSlope(ps, n, ans);
double tmp;
int l, r;
for(i = 0; i < n; i++){
ps[i].x = 0 - ps[i].x;
}
l = 0; r = n-1;
while(l < r){
swap(ps[l++], ps[r--]);
}
tmp = maxSlope(ps, n, ti);
if(sign(tmp-s) > 0 || (sign(tmp-s) == 0 && isLess(ti, ans))){
s = tmp;
ans = ti;
}
return s;
}
point rotate(point st, double Cos, double Sin){
return point(st.x*Sin- st.y*Cos, st.x*Cos+st.y*Sin);
}
bool input(){
if(scanf("%d", &n) == EOF) return false;
k.read();
int i, g;
double Cos, Sin, len;
len = sqrt(k.x*k.x+k.y*k.y);
Cos = k.x / len;
Sin = k.y / len;
for(i = 0; i < n; i++){
getSNum(g);
ps[i].x = g;
getSNum(g);
ps[i].y = g;
ps[i] = rotate(ps[i], Cos, Sin);
ps[i].id = i+1;
}
return true;
}
void solve(){
pii ans;
if(k.x == 0 && k.y == 0){
ans.first = 1;
ans.second = 2;
}else{
maxSlopeAbs(ps, n, ans);
}
printf("%d %d\n", ans.first, ans.second);
}
int main(){
while(input()) solve();
return 0;
}

100
HDOJ/3882_autoAC.cpp Normal file
View File

@ -0,0 +1,100 @@
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;
const double eps=1e-9;
int dcmp(double x){if(x<-eps) return -1;return x>eps;}
struct point3
{
double x,y,z;
point3(double x=0,double y=0,double z=0):x(x),y(y),z(z){}
void read(){int xt,yt,zt;scanf("%d %d %d",&xt,&yt,&zt);x=xt;y=yt;z=zt;}
};
point3 operator -(point3 a,point3 b){return point3(a.x-b.x,a.y-b.y,a.z-b.z);}
point3 operator *(point3 a,double b){return point3(a.x*b,a.y*b,a.z*b);}
bool operator <(point3 a,point3 b){return a.x<b.x||(dcmp(a.x-b.x)==0&&a.y<b.y)||(dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0&&a.z<b.z);}
bool operator ==(point3 a,point3 b){return !(a<b||b<a);}
double dot(point3 a,point3 b){return a.x*b.x+a.y*b.y+a.z*b.z;}
double cross(point3 a,point3 b){return a.x*b.y-a.y*b.x;}
double length(point3 a){return sqrt(dot(a,a));}
int convexhull(point3 *p,int n,point3 *ch)
{
sort(p,p+n);
int nt=0;
for(int i=0;i<n;i++)
{
if(!nt||!(p[nt-1]==p[i])) p[nt++]=p[i];
}
n=nt;
int m=0;
for(int i=0;i<n;i++)
{
while(m>1&&dcmp(cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]))<=0) --m;
ch[m++]=p[i];
}
int k=m;
for(int i=n-2;i>=0;i--)
{
while(m>k&&dcmp(cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]))<=0) --m;
ch[m++]=p[i];
}
if(n>1) --m;
return m;
}
int n,m;
double a,b,c,d;
point3 p[105],p0,q[105],ch[105];
int main()
{
int i,j;
int at,bt,ct,dt;
while(scanf("%d %d %d %d",&at,&bt,&ct,&dt)!=EOF&&(at||bt||ct||dt))
{
a=at,b=bt,c=ct,d=dt;
scanf("%d",&n);
for(i=0;i<n;i++) p[i].read();
p0.read();
for(i=0;i<n;i++) p[i]=p[i]-p0;
d=d-dot(point3(a,b,c),p0);
scanf("%d",&m);
for(i=0;i<m;i++) q[i].read(),q[i]=q[i]-p0;
int nt=0;
for(i=0;i<n;i++)
{
double te=dot(point3(a,b,c),p[i]);
if(dcmp(te)==0) continue;
if(dcmp(d/te)>0) p[i]=p[i]*(d/te),++nt;
}
if(nt==0) printf("ZERO\n");
else if(nt<n) printf("INF\n");
else
{
for(i=0;i<n;i++)
{
if(ct) p[i].z=0;
else if(bt) swap(p[i].y,p[i].z),p[i].z=0;
else swap(p[i].x,p[i].z),p[i].z=0;
}
nt=convexhull(p,n,ch);
int rt=0;
for(i=0;i<m;i++)
{
double te=dot(point3(a,b,c),q[i]);
if(dcmp(te)==0||dcmp(d/te)<0) continue;
q[i]=q[i]*(d/te);
if(ct) q[i].z=0;
else if(bt) swap(q[i].y,q[i].z),q[i].z=0;
else swap(q[i].x,q[i].z),q[i].z=0;
for(j=0;j<nt;j++)
{
if(dcmp(cross(q[i]-ch[j],ch[(j+1)%nt]-ch[j]))>=0) break;
}
if(j>=nt) {++rt;/*cout<<q[i].x<<" 1 "<<q[i].y<<endl;*/}
}
printf("%.2lf%%\n",100.0*rt/m);
}
}
return 0;
}

59
HDOJ/3883_autoAC.cpp Normal file
View File

@ -0,0 +1,59 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int n, m;
int map[101][101];
int hash[2][101][101][2];
bool vis[101];
int f[2];
int abs(int a){
if (a < 0) return -a;
return a;
}
int main(){
int i, j, k, t, ii, ans;
int *p;
while(scanf("%d%d", &n, &m) != EOF){
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
scanf("%d", &map[i][j]);
memset(vis, 0, sizeof(vis));
ans = 0x80000000;
ii = 0;
for (i = 1; i <= n; i++){
ii = 1 - ii;
for (j = 1; j <= m; j++){
f[0] = f[1] = 0x80000000;
for (k = 100; k >= 0; k--){
for (t = 0; t <= 1; t++){
p = & hash[ii][j][k][t];
*p = 0x80000000;
if (!vis[k]) continue;
if (i > 1 && *p < hash[1 - ii][j][k][t])
*p = hash[1 - ii][j][k][t];
if (j > 1 && *p < hash[ii][j - 1][k][t])
*p = hash[ii][j - 1][k][t];
if (k <= abs(map[i][j])) continue;
if (f[1 - t] < *p) f[1 - t] = *p;
}
}
if (f[0] == 0x80000000) f[0] = 0;
if (f[1] == 0x80000000) f[1] = 0;
f[0] = map[i][j] - f[0];
f[1] = map[i][j] - f[1];
if (ans < f[0]) ans = f[0];
k = abs(map[i][j]);
if (!vis[k] || hash[ii][j][k][0] < f[0]){
vis[k] = 1;
hash[ii][j][k][0] = f[0];
}
if (!vis[k] || hash[ii][j][k][1] < f[1]){
vis[k] = 1;
hash[ii][j][k][1] = f[1];
}
}
}
printf("%d\n", ans);
}
return 0;
}

94
HDOJ/3884_autoAC.cpp Normal file
View File

@ -0,0 +1,94 @@
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <memory.h>
#define MAXN 10006
#define INF 1e9
using namespace std;
struct Node
{
int x,p;
}node[MAXN];
int N,ans;
__int64 K;
int cmp(const void *p1,const void *p2)
{
return ((struct Node *)p1)->x-((struct Node *)p2)->x;
}
void Init()
{
int i;
for(i=1;i<=N;i++)
scanf("%d%d",&node[i].x,&node[i].p);
qsort(node+1,N,sizeof(node[0]),cmp);
}
void Solve()
{
int i,j,k;
__int64 M,d1,d2;
int now;
ans=0;
for(i=1;i<=N;i++)
{
k=i-1;j=i+1;
M=K;now=node[i].p;
while(true)
{
if(k==0&&j!=N+1)
{
d1=INF;
d2=node[j].x-node[i].x;
}
else if(k!=0&&j==N+1)
{
d1=node[i].x-node[k].x;
d2=INF;
}
else if(k!=0&&j!=N+1)
{
d1=node[i].x-node[k].x;
d2=node[j].x-node[i].x;
}
else break;
if(d1<d2)
{
if(d1*node[k].p<=M)
{
now+=node[k].p;
M-=d1*node[k].p;
k--;
}
else
{
now+=M/d1;
break;
}
}
else
{
if(d2*node[j].p<=M)
{
now+=node[j].p;
M-=d2*node[j].p;
j++;
}
else
{
now+=M/d2;
break;
}
}
}
if(now>ans) ans=now;
}
printf("%d\n",ans);
}
int main()
{
while(scanf("%d%I64d",&N,&K)!=EOF)
{
Init();
Solve();
}
return 0;
}

154
HDOJ/3885_autoAC.cpp Normal file
View File

@ -0,0 +1,154 @@
#include <iostream>
#include <cstdio>
#include <memory.h>
#include <algorithm>
#include <queue>
#define MAXN 55
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
using namespace std;
struct Rect
{
int x1,y1,x2,y2;
}rect[55*55];
struct Point
{
int x,y;
Point(int x1,int y1)
{x=x1;y=y1;}
Point(){}
};
int dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1}};
int map1[MAXN][MAXN],map2[MAXN][MAXN];
bool vis[MAXN][MAXN];
bool flag[55*55];
int cnt;
int N,M,cas;
queue<Point>Q;
int cmp(const void *p1,const void *p2)
{
if(((struct Rect *)p1)->x1!=((struct Rect *)p2)->x1)
return ((struct Rect *)p1)->x1-((struct Rect *)p2)->x1;
if(((struct Rect *)p1)->y1!=((struct Rect *)p2)->y1)
return ((struct Rect *)p1)->y1-((struct Rect *)p2)->y1;
if(((struct Rect *)p1)->x2!=((struct Rect *)p2)->x2)
return ((struct Rect *)p2)->x2-((struct Rect *)p1)->x2;
return ((struct Rect *)p2)->y2-((struct Rect *)p1)->y2;
}
void Init()
{
int i,j;
char s[300];
for(i=1;i<=N;i++)
{
for(j=1;j<=M;j++)
scanf("%d",&map1[i][j]);
}
for(i=1;i<=N;i++)
{
for(j=1;j<=M;j++)
scanf("%d",&map2[i][j]);
}
}
bool Judge(int x,int y)
{
if(x>=1&&x<=N&&y>=1&&y<=M)
return true;
return false;
}
void Run(int dx,int dy,Rect &now)
{
int i;
Q.push(Point(dx,dy));
if(dx<now.x1||dx>now.x2)
{
for(i=now.y1;i<=now.y2;i++)
Q.push(Point(dx,i));
}
if(dy<now.y1||dy>now.y2)
{
for(i=now.x1;i<=now.x2;i++)
Q.push(Point(i,dy));
}
now.x1=min(now.x1,dx);
now.y1=min(now.y1,dy);
now.x2=max(now.x2,dx);
now.y2=max(now.y2,dy);
}
void BFS(int sx,int sy)
{
int i,x,y;
while(!Q.empty()) Q.pop();
memset(vis,false,sizeof(vis));
vis[sx][sy]=true;
rect[cnt].x1=rect[cnt].x2=sx;
rect[cnt].y1=rect[cnt].y2=sy;
Q.push(Point(sx,sy));
while(!Q.empty())
{
x=Q.front().x;
y=Q.front().y;
Q.pop();
for(i=0;i<8;i++)
{
int dx=x+dir[i][0];
int dy=y+dir[i][1];
if(Judge(dx,dy)&&!vis[dx][dy])
{
vis[dx][dy]=true;
if(map1[dx][dy]!=map2[dx][dy])
{
Run(dx,dy,rect[cnt]);
}
}
}
}
cnt++;
}
void Solve()
{
int i,j;
cnt=0;
for(i=1;i<=N;i++)
{
for(j=1;j<=N;j++)
{
if(map1[i][j]!=map2[i][j])
{
BFS(i,j);
}
}
}
qsort(rect,cnt,sizeof(rect[0]),cmp);
memset(flag,true,sizeof(flag));
int num=0;
for(i=0;i<cnt;i++)
{
if(!flag[i]) continue;
for(j=i+1;j<cnt;j++)
{
if(!flag[j]) continue;
if(rect[i].x1<=rect[j].x1&&rect[i].y1<=rect[j].y1&&rect[i].x2>=rect[j].x2&&rect[i].y2>=rect[j].y2)
{
flag[j]=false;
}
}
}
for(i=0;i<cnt;i++)
num+=flag[i];
printf("%d\n",num);
for(i=0;i<cnt;i++)
{
if(flag[i])
printf("%d %d %d %d\n",rect[i].x1,rect[i].y1,rect[i].x2,rect[i].y2);
}
}
int main()
{
while(scanf("%d%d",&N,&M)!=EOF)
{
Init();
Solve();
}
return 0;
}

58
HDOJ/3886_autoAC.cpp Normal file
View File

@ -0,0 +1,58 @@
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define ll long long
#define M 100000000
using namespace std;
char str[101],a[101],b[101];
int len,bit[101],dp[101][101][10];
bool ok(int i,int u,int v)
{
if(str[i]=='/') return u<v;
if(str[i]=='-') return u==v;
if(str[i]=='\\') return u>v;
}
int dfs(int pos,int j,int pre,bool h,bool f)
{
if(pos==-1) return j==len;
if(!f&&dp[pos][j][pre]!=-1) return dp[pos][j][pre];
int ans=0;
int e=f?bit[pos]:9;
for(int i=0;i<=e;i++){
if(h) ans+=dfs(pos-1,j,i,h&&i==0,f&&i==e);
else if(j<len&&ok(j,pre,i)) ans+=dfs(pos-1,j+1,i,h,f&&i==e);
else if(j>0&&ok(j-1,pre,i)) ans+=dfs(pos-1,j,i,h,f&&i==e);
ans%=M;
}
if(!f) dp[pos][j][pre]=ans;
return ans;
}
int solve(char an[],bool f)
{
int m=0,i,j=0,le=strlen(an);
while(an[j]=='0') j++;
for(i=le-1;i>=j;i--) bit[m++]=an[i]-'0';
if(f&&m>0){
for(i=0;i<m;i++){
if(bit[i]){
bit[i]--;
break;
}
else bit[i]=9;
}
}
return dfs(m-1,0,0,1,1);
}
int main()
{
int i,j,k,m,n;
while(scanf("%s",str)!=EOF){
len=strlen(str);
scanf("%s%s",a,b);
memset(dp,-1,sizeof(dp));
printf("%08d\n",(solve(b,0)-solve(a,1)+M)%M);
}
return 0;
}

85
HDOJ/3887_autoAC.cpp Normal file
View File

@ -0,0 +1,85 @@
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
const int maxn=100005;
vector<int>vt[maxn];
int bit[2*maxn];
int que[2*maxn];
int st[maxn];
int sd[maxn];
int f[maxn];
int n, rt, num;
void dfs(int u, int fa)
{
que[++num]=u;
for(int i=0; i<vt[u].size(); i++)
{
int v=vt[u][i];
if(v==fa) continue;
dfs(v,u);
}
que[++num]=u;
}
int lowbit(int x)
{
return x&(-x);
}
void cal(int x, int val)
{
while(x<=num)
{
bit[x]+=val;
x+=lowbit(x);
}
}
int getsum(int x)
{
int ans=0;
while(x>0)
{
ans+=bit[x];
x-=lowbit(x);
}
return ans;
}
int main()
{
while(~scanf("%d%d",&n,&rt),n+rt)
{
for(int i=0; i<=n; i++)
vt[i].clear();
for(int i=1; i<n; i++)
{
int x, y;
scanf("%d%d",&x,&y);
vt[x].push_back(y);
vt[y].push_back(x);
}
fill(st+1,st+1+n,0);
num=0;
dfs(rt,-1);
for(int i=1; i<=num; i++)
{
if(!st[que[i]]) st[que[i]]=i;
else sd[que[i]]=i;
}
memset(bit,0,sizeof(bit));
for(int i=1; i<=num; i++)
cal(i,1);
for(int i=n; i>=1; i--)
{
f[i]=(getsum(sd[i]-1)-getsum(st[i]))/2;
cal(st[i],-1);
cal(sd[i],-1);
}
printf("%d",f[1]);
for(int i=2; i<=n; i++)
printf(" %d",f[i]);
puts("");
}
return 0;
}

83
HDOJ/3889_autoAC.cpp Normal file
View File

@ -0,0 +1,83 @@
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int main()
{
int p,n,i,k=0;
double Mx,My,SDx,SDy,SE,rxy,a[10002],b[10002];
while(scanf("%d%d",&p,&n)!=EOF)
{
double sum=0,sx=0,sy=0,Sx=0,Sy=0,s=0,t,SD=0,ka,kb;
if(p==1)
{
for(i = 0 ; i < n ; i ++ )
{
scanf("%lf",&a[i]);
sum+=a[i];
}
Mx = sum/n;
for( i = 0 ; i < n ; i ++ )
s += ((a[i]-Mx)*(a[i]-Mx));
SDx = sqrt(s/n);
printf("%.2lf",(a[0]-Mx)/SDx);
for(i=1;i<n;i++)
printf(" %.2lf",(a[i]-Mx)/SDx);
printf("\n");
}
else if(p==2)
{
for( i = 0 ; i <= n ; i ++ )
{
scanf("%lf",&a[i]);
if(i!=0)
sum+=a[i];
}
Mx=sum/n;
for( i = 1 ; i <= n ; i ++ )
s+=((a[i]-Mx)*(a[i]-Mx));
SDx=sqrt(s/(n-1));
SE=SDx/sqrt(n);
t=(Mx-a[0])/SE;
printf("%.2lf\n",t);
}
else
{
for( i = 0 ; i < n ; i ++ )
{
scanf("%lf",&a[i]);
sx += a[i];
}
for( i = 0 ; i < n ; i ++ )
{
scanf("%lf",&b[i]);
sy += b[i];
}
Mx = sx/n;
for( i = 0 ; i < n ; i ++ )
Sx += ((a[i]-Mx)*(a[i]-Mx));
SDx = sqrt(Sx/n);
My = sy/n;
for( i = 0 ; i < n ; i ++ )
Sy += ((b[i]-My)*(b[i]-My));
SDy = sqrt(Sy/n);
for( i = 0 ; i < n ; i ++)
{
SD += ((a[i]-Mx)*(b[i]-My));
}
rxy = SD/(n*SDx*SDy);
kb = rxy*SDy/SDx;
ka = My-kb*Mx;
if(p==3)
{
printf("%.2lf\n",rxy);
}
else
{
printf("%.2lf %.2lf\n",ka,kb);
}
}
}
return 0;
}

122
HDOJ/3890_autoAC.cpp Normal file
View File

@ -0,0 +1,122 @@
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct point
{
int x,y,flag,tag;
double c;
}p[141000];
struct tree
{
double c;
int num;
}T1[141000],T2[21000];
int x[141000],y[141000],xnum,ynum;
bool cmp(point a,point b)
{
if(a.x!=b.x) return a.x<b.x;
if(a.y!=b.y) return a.y<b.y;
return a.flag<b.flag;
}
void Add(int index,int x,int y,int flag,int tag)
{
p[index].x=x,p[index].y=y;
p[index].flag=flag,p[index].tag=tag;
}
int Lowbit(int t)
{
return t^(t&(t-1));
}
void AddNum(int index,double c)
{
int i;
for(i=index;i<=ynum;i+=Lowbit(i))
{
T1[i].num++;
T1[i].c+=c;
}
}
void Query(int index,int now)
{
int i;
for(i=index;i>0;i-=Lowbit(i))
{
T2[p[now].flag].c=T2[p[now].flag].c+T1[i].c*p[now].tag;
T2[p[now].flag].num=T2[p[now].flag].num+T1[i].num*p[now].tag;
}
}
int BiSearch(int now)
{
int l=0,r=ynum-1,mid;
while(l<=r)
{
mid=(l+r)/2;
if(y[mid]>now) r=mid-1;
else l=mid+1;
}
return l;
}
int main()
{
int n,m,i,j,x1,y1,x2,y2;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(xnum=0,ynum=0,i=0;i<n;i++)
{
scanf("%d%d%lf",&p[i].x,&p[i].y,&p[i].c);
p[i].flag=0;
x[xnum++]=p[i].x;
y[ynum++]=p[i].y;
}
for(i=0;i<m;i++)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x[xnum++]=x2,x[xnum++]=x1-1;
y[ynum++]=y2,y[ynum++]=y1-1;
Add(n+4*i,x1-1,y1-1,i+1,1);
Add(n+4*i+1,x1-1,y2,i+1,-1);
Add(n+4*i+2,x2,y1-1,i+1,-1);
Add(n+4*i+3,x2,y2,i+1,1);
T2[i+1].num=0;
T2[i+1].c=0;
}
sort(x,x+xnum);
sort(y,y+ynum);
for(j=1,i=1;i<xnum;i++)
if(x[i]!=x[i-1])
x[j++]=x[i];
xnum=j;
for(j=1,i=1;i<ynum;i++)
if(y[i]!=y[i-1])
y[j++]=y[i];
ynum=j;
for(i=0;i<=ynum+1;i++)
{
T1[i].c=0;
T1[i].num=0;
}
sort(p,p+n+4*m,cmp);
for(y1=0,i=0;i<n+4*m;i++)
{
if(!p[i].flag)
{
y1=BiSearch(p[i].y);
AddNum(y1,p[i].c);
}
else
{
y1=BiSearch(p[i].y);
Query(y1,i);
}
}
for(i=1;i<=m;i++)
{
if(!T2[i].num)
printf("0.00/0\n");
else
printf("%.2lf/%d\n",T2[i].c,T2[i].num);
}
}
return 0;
}

73
HDOJ/3891_autoAC.cpp Normal file
View File

@ -0,0 +1,73 @@
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
using namespace std;
const long long P=112233;
typedef long long ll;
int n;
long long c[P*10],pro,p[20],s[20],a[P*10],inv[P*10];
void exgcd(ll a,ll b,ll &d,ll &x,ll &y){
if ( b==0 ){
d=a; x=1; y=0;
}
else{
exgcd(b,a%b,d,x,y);
ll t=x; x=y; y=t-(a/b)*y;
}
}
void cnt(){
long long d,x,y;
for (int i=1;i<=1000001;i++){
long long a=i;
exgcd(a,P,d,x,y);
inv[a]=x;
}
}
void work(long long a,long long b,int k){
for (int i=1;i<=4;i++){
while ( a%p[i]==0 ){
s[i]++;
a/=p[i];
}
while ( b%p[i]==0 ){
s[i]--;
b/=p[i];
}
}
pro=(pro*inv[b]*a)%P;
c[k]=pro;
for (int i=1;i<=4;i++)
for (int j=1;j<=s[i];j++)
c[k]=c[k]*p[i]%P;
}
int main(){
cnt();
while ( scanf("%d",&n)!=EOF ){
for (int i=1;i<=n;i++) scanf("%I64d",&a[i]);
p[1]=3; p[2]=11; p[3]=19; p[4]=179;
pro=1;
memset(s,0,sizeof(s));
c[0]=1;
int e=1;
for (int i=1;i<=n;i++)
work(n-i+1,i,i);
a[0]=0;
for (int i=1;i<=n;i++){
a[0]=(a[0]+c[i]*a[i]*e)%P;
e=-e;
}
memset(s,0,sizeof(s));
pro=1;
e=1;
for (int i=1;i<=n+1;i++)
work(n-i+2,i,i);
long long ans=0;
for (int i=n;i>=0;i--){
ans=(ans+c[i]*a[i]*e)%P;
e=-e;
}
ans=(ans+P)%P;
cout<<ans<<endl;
}
}

94
HDOJ/3892_autoAC.cpp Normal file
View File

@ -0,0 +1,94 @@
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
typedef long long LL;
const LL MOD = 999983;
vector<LL> p[505];
int T;
LL quick_mod(LL a,LL b,LL m)
{
LL ans = 1;
a %= m;
while(b)
{
if(b&1)
{
ans = ans*a%m;
b--;
}
b>>=1;
a=a*a%m;
}
return ans;
}
vector<LL> poly_gcd(vector<LL> a,vector<LL> b)
{
if(b.size() == 0) return a;
int t = a.size() - b.size();
vector<LL> c;
for(LL i=0; i<=t; i++)
{
LL tmp = a[i] * quick_mod(b[0],MOD-2,MOD) % MOD;
for(LL j=0; j<b.size(); j++)
a[i+j] = (a[i+j] - tmp * b[j] % MOD + MOD) % MOD;
}
LL p = -1;
for(LL i=0; i<a.size(); i++)
{
if(a[i] != 0)
{
p=i;
break;
}
}
if(p >= 0)
for(LL i=p; i<a.size(); i++)
c.push_back(a[i]);
return poly_gcd(b,c);
}
bool Import()
{
LL n,t;
if(scanf("%d",&T) == 1)
{
for(LL i=0;i<T;i++)
{
p[i].clear();
scanf("%I64d",&n);
for(LL j=0;j<=n;j++)
{
scanf("%I64d",&t);
p[i].push_back(t);
}
}
return true;
}
return false;
}
void Work()
{
if(T==1)
{
if(p[0].size() > 1) puts("YES");
else puts("NO");
return;
}
vector<LL> v = poly_gcd(p[0],p[1]);
LL i = 2;
while(i < T && v.size() > 1)
{
v = poly_gcd(v,p[i]);
i++;
}
if(v.size() > 1) puts("YES");
else puts("NO");
}
int main()
{
while(Import())
Work();
return 0;
}

82
HDOJ/3893_autoAC.cpp Normal file
View File

@ -0,0 +1,82 @@
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL __int64
const LL mod=112233;
LL a[14][14]={
{ 0, 1, 1, 1, 1, 1,1,0,0,0,0,0,0,0},
{ 1, 0, 1, 1, 1, 1,0,0,0,0,0,0,0,0},
{ 1, 1, 0, 1, 1, 1,0,0,0,0,0,0,0,0},
{ 1, 1, 1, 0, 1, 1,0,0,0,0,0,0,0,0},
{ 1, 1, 1, 1, 0, 1,0,0,0,0,0,0,0,0},
{ 1, 1, 1, 1, 1, 0,0,0,0,0,1,0,0,0},
{ 0, 0, 0, 0, 0, 0,0,1,0,0,0,0,0,0},
{ 0, 0, 0, 0, 0, 0,0,0,1,0,0,0,0,0},
{ 0, 0, 0, 0, 0, 0,0,0,0,1,0,0,0,0},
{ 0, 0, 0, 0, 0,-1,0,0,0,0,0,0,0,0},
{ 0, 0, 0, 0, 0, 0,0,0,0,0,0,1,0,0},
{ 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,1,0},
{ 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,1},
{-1, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0}
};
struct matrix{
LL f[14][14];
};
matrix mul(matrix a,matrix b)
{
matrix c;
LL i,j,k;
memset(c.f,0,sizeof(c.f));
for(k=0;k<14;k++)
{
for(i=0;i<14;i++)
{
if(!a.f[i][k])continue;
for(j=0;j<14;j++)
{
if(!b.f[k][j])continue;
c.f[i][j]=(c.f[i][j]+a.f[i][k]*b.f[k][j])%mod;
}
}
}
return c;
}
matrix pow_mod(matrix a,LL b)
{
matrix s;
memset(s.f,0,sizeof(s.f));
for(LL i=0;i<14;i++)
s.f[i][i]=1;
while(b)
{
if(b&1)
s=mul(s,a);
a=mul(a,a);
b=b>>1;
}
return s;
}
int main()
{
LL n;
while(cin>>n)
{
if(n%2==0)
{
cout<<0<<endl;
continue;
}
matrix e;
LL i,j,ans=0;
memcpy(e.f,a,sizeof(a));
e=pow_mod(e,n/2);
for(i=0;i<6;i++)
for(j=0;j<6;j++)
ans=(ans+e.f[i][j])%mod;
cout<<(ans+mod)%mod<<endl;
}
return 0;
}

142
HDOJ/3894_autoAC.cpp Normal file
View File

@ -0,0 +1,142 @@
#include <iostream>
#include <stdio.h>
using namespace std;
const int N = 1000005;
struct e{
int v;
e* nxt;
}es[N<<1], *fir[N], stk[N];
int n, m, en, p;
int E, W;
bool hasFood[N];
int eastNum[N], westNum[N], par[N];
int minp[N], t[N], que[N], dep[N];
inline void add_e(int u, int v){
es[en].v = v;
es[en].nxt = fir[u];
fir[u] = &es[en++];
}
template <typename T>
void getNum(T& a){
a = 0;
char ch;
while(true){
ch = getchar();
if(ch >= '0' && ch <= '9') break;
}
a = ch - '0';
while(true){
ch = getchar();
if(ch < '0' || ch > '9') break;
a = a * 10 + ch - '0';
}
}
bool input(){
if(scanf("%d%d%d%d", &n, &m, &E, &W)== EOF) return false;
int i, u, v;
en = 0;
for(i = 1; i <= n; i++){
fir[i] = NULL;
hasFood[i] = false;
}
for(i = 1; i < n; i++){
getNum<int>(u);
getNum<int>(v);
add_e(u, v);
add_e(v, u);
}
scanf("%d", &p);
for(i = 1; i <= p; i++){
getNum(u);
hasFood[i] = true;
}
return true;
}
inline bool isEast(int u){ return u <= E; }
inline bool has(int u) { return hasFood[u]; }
inline bool isWest(int u){ return u >= n - W + 1; }
inline void push(int v, int p, int& top){
eastNum[v] = westNum[v] = 0;
if(isEast(v)){
eastNum[v]++;
}else if(isWest(v)){
westNum[v]++;
}
++top;
stk[top].v = v;
stk[top].nxt = fir[v];
par[v] = p;
}
typedef bool (*fun)(int u);
void bfs(int st, int* ar, fun check){
int l, r, u, v, num;
e* cur;
l = r = num = 0;
que[r++] = st; dep[st] = 0;
if(check(st)){
ar[num++] = 0;
}
while(l != r && num < p){
u = que[l++];
for(cur = fir[u]; cur; cur = cur->nxt){
if((v = cur->v) != par[u]){
que[r++] = v; par[v] = u; dep[v] = dep[u] + 1;
if(check(v)){
ar[num++] = dep[v];
if(num >= p) break;
}
}
}
}
}
void solve(){
int top = -1, u, v, est, wst, ans;
int i, maxt;
push(1, -1, top);
while(top >= 0){
u = stk[top].v;
if(stk[top].nxt == NULL){
if(eastNum[u] == 0 && westNum[u] == W){
wst = u;
est = par[u];
break;
}
if(eastNum[u] == E && westNum[u] == 0){
wst = par[u];
est = u;
break;
}
if(par[u] >= 1){
eastNum[par[u]] += eastNum[u];
westNum[par[u]] += westNum[u];
}
top--;
continue;
}else{
v = stk[top].nxt->v;
stk[top].nxt = stk[top].nxt->nxt;
if(v != par[u]){
push(v, u, top);
}
}
}
par[est] = wst;
par[wst] = est;
bfs(wst, minp, isWest);
bfs(est, t, has);
ans = 0;
maxt = -1;
for(i = 0; i < p; i++){
if(t[i] < maxt + 1) t[i] = maxt + 1;
maxt = t[i];
if(ans < t[i] + minp[p - i - 1]){
ans = t[i] + minp[p - i - 1];
}
}
ans++;
cout<<ans<<endl;
}
int main() {
while(input()) solve();
return 0;
}

76
HDOJ/3895_autoAC.cpp Normal file
View File

@ -0,0 +1,76 @@
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
const double eps=1e-4;
using namespace std;
inline double sqr(double a)
{
return a*a;
}
double ax, x2, x3, ay, y2, y3, r1, r2, r3;
int m1, m2, m3;
bool check(double x, double y, double r)
{
if(fabs(sqr(x-ax)+sqr(y-ay)-sqr(r+m1*r1))>eps)return false;
if(fabs(sqr(x-x2)+sqr(y-y2)-sqr(r+m2*r2))>eps)return false;
if(fabs(sqr(x-x3)+sqr(y-y3)-sqr(r+m3*r3))>eps)return false;
return true;
}
int main()
{
int t;
scanf("%d", &t);
while(t-->0)
{
scanf("%lf%lf%lf%d%lf%lf%lf%d%lf%lf%lf%d", &ax, &ay, &r1, &m1, &x2, &y2, &r2, &m2, &x3, &y3, &r3, &m3);
if(m1==0)m1=-1;
if(m2==0)m2=-1;
if(m3==0)m3=-1;
double a, b, c, d, aa, bb, cc, dd;
a=2*(ax-x2);
b=2*(ay-y2);
c=2*(r2*m2-r1*m1);
d=m2*m2*r2*r2-m1*m1*r1*r1-x2*x2+ax*ax-y2*y2+ay*ay;
aa=2*(ax-x3);
bb=2*(ay-y3);
cc=2*(r3*m3-r1*m1);
dd=m3*m3*r3*r3-m1*m1*r1*r1-x3*x3+ax*ax-y3*y3+ay*ay;
double a1, b1, a2, b2;
if(fabs(bb*a-aa*b)<eps){printf("NO SOLUTION!\n");continue;}
a1=(a*cc-c*aa)/(bb*a-aa*b);
b1=(dd*a-d*aa)/(bb*a-aa*b);
if(fabs(b*aa-bb*a)<eps){printf("NO SOLUTION!\n");continue;}
a2=(cc*b-c*bb)/(b*aa-bb*a);
b2=(b*dd-bb*d)/(b*aa-bb*a);
double A, B, C;
A=a2*a2+a1*a1-1;
B=2*a2*b2-2*ax*a2+2*a1*b1-2*ay*a1-2*m1*r1;
C=b2*b2-2*ax*b2+ax*ax+b1*b1-2*ay*b1+ay*ay-r1*r1;
double rr;
if(B*B-4*A*C<0){printf("NO SOLUTION!\n");continue;}
if(fabs(A)<eps){printf("NO SOLUTION!\n");continue;}
rr=(sqrt(B*B-4*A*C)-B)/2/A;
double rx=a2*rr+b2;
double ry=a1*rr+b1;
if(rr>=-eps&&check(rx, ry, rr))
{
printf("%.4lf %.4lf", rx, ry);
if(fabs(rr)>=eps)printf(" %.4lf", rr);
printf("\n");
continue;
}
rr=(-B-sqrt(B*B-4*A*C))/2/A;
rx=a2*rr+b2;
ry=a1*rr+b1;
if(rr>=-eps&&check(rx, ry, rr))
{
printf("%.4lf %.4lf", rx, ry);
if(fabs(rr)>=eps)printf(" %.4lf", rr);
printf("\n");
continue;
}
printf("NO SOLUTION!\n");
}
return 0;
}

152
HDOJ/3896_autoAC.cpp Normal file
View File

@ -0,0 +1,152 @@
#include <cstdio>
#include <cstring>
#include <cstdlib>
#define len1 100001
#define len2 1000001
#define min(x1,x2) ((x1)<(x2)?(x1):(x2))
#define swap(x1,x2) {int _t = x1 ; x1 = x2 ; x2 = _t;}
int dfn[len1] , low[len1] , final[len1] , hash[len1] , sm = 0 , timel = 0 , deep[len1] ;
int n , m , lim = 0 ;
int fa[20][len1] , ed[20] ;
struct edge
{
int tot ;
int now[len1] , next[len2] , g[len2] ;
void add( int st, int ed )
{
++tot;
g[tot] = ed ;
next[tot] = now[st];
now[st] = tot ;
}
}e;
void dfs( int x , int ff , int dep )
{
++timel ;
dfn[x] = low[x] = timel ;
hash[x] = sm ;
deep[x] = dep ;
for ( int i = e.now[x] ; i ; i = e.next[i] )
if ( e.g[i] != ff )
{
if ( hash[ e.g[i] ] != sm )
{
dfs( e.g[i] , x , dep+1 );
low[x] = min( low[x] , low[ e.g[i] ] );
fa[1][ e.g[i] ] = x ;
}
else
low[x] = min( low[x] , dfn[ e.g[i] ] );
}
final[x] = ++timel ;
}
bool ini( int a , int b )
{
if ( dfn[a] >= dfn[b] && final[a] <= final[b] )
return true ;
return false;
}
bool judge( int sc , int sv , int g1 , int g2 )
{
int fl = 0 ;
if ( deep[g1] < deep[g2] )
swap(g1,g2);
if ( ini( sc , g1 ) && ini( sv , g1 ) )
fl = 1 ;
else if ( !ini( sc , g1 ) && !ini( sv , g1 ) )
fl = 1 ;
else if ( low[g1] <= dfn[g2] )
fl = 1 ;
return fl ;
}
int move( int x , int step )
{
int t = x , p = 1;
if ( step < 0 )
return -1 ;
while ( step )
{
if ( step & 1 )
t = fa[p][t];
step >>= 1 ;
++p;
}
return t ;
}
int main()
{
FILE *in , *out;
int sc , sv , g1 , g2 , q , t , t1 , t2 ;
int fl = 1 ;
scanf("%d%d",&n,&m);
ed[1] = 1 ;
for ( int i = 2 ; ; ++i )
{
ed[i] = (ed[i-1]<<1);
lim = i ;
if ( ed[i] >= n )
break;
}
for ( int i = 1 ; i <= m ; ++i )
{
scanf("%d%d",&sc,&sv);
e.add( sc , sv );
e.add( sv , sc );
}
++sm ;
dfs( 1 , 1 , 1 );
for ( int i = 2 ; i <= lim ; ++i )
for ( int j = 1 ; j <= n ; ++j )
fa[i][j] = fa[i-1][ fa[i-1][j] ];
scanf("%d",&q);
for ( int i = 1 ; i <= q ; ++i )
{
if ( i == 27 )
i = 27 ;
scanf("%d",&t);
fl = 0 ;
if ( t == 1 )
{
scanf("%d%d%d%d",&sc,&sv,&g1,&g2);
fl = judge( sc , sv , g1 , g2 );
}
else
{
scanf("%d%d%d",&sc,&sv,&g1);
if ( !ini( sc , g1 ) && !ini( sv , g1 ) )
fl = 1 ;
else if ( ini( sc , g1 )^ini( sv , g1 ) )
{
if ( ini( sc , g1 ) )
{
t = move( sc , deep[sc] - deep[g1] - 1 );
if ( low[t] < dfn[g1] )
fl = 1 ;
}
else
{
t = move( sv , deep[sv] - deep[g1] - 1 );
if ( low[t] < dfn[g1] )
fl = 1 ;
}
}
else
{
t1 = move( sc , deep[sc] - deep[g1] - 1 );
t2 = move( sv , deep[sv] - deep[g1] - 1 );
if ( t1 == t2 )
fl = 1 ;
else
{
if ( low[t1] < dfn[g1] && low[t2] < dfn[g1] )
fl = 1;
}
}
}
if ( fl )
printf("yes\n");
else
printf("no\n");
}
return 0;
}

109
HDOJ/3899_autoAC.cpp Normal file
View File

@ -0,0 +1,109 @@
#include <stdio.h>
#include <memory.h>
const int maxn = 100005;
struct Graph {
int hed[maxn], nxt[maxn*2], pnt[maxn*2], val[maxn*2];
int idx;
void init(int n) {
memset(hed + 1, -1, n * 4);
idx = 0;
}
void addedge(int x, int y, int v) {
pnt[idx] = y; val[idx] = v; nxt[idx] = hed[x]; hed[x] = idx++;
pnt[idx] = x; val[idx] = v; nxt[idx] = hed[y]; hed[y] = idx++;
}
} gra;
struct Node {
int r, x, p;
} sta[maxn];
int teams[maxn], total;
__int64 cunt[maxn], cost[maxn];
void dfs_1() {
int x, y, r, p, top = 0;
sta[0].x = 1; sta[0].r = 0; sta[0].p = gra.hed[1];
cunt[1] = teams[1];
cost[1] = 0;
while (true) {
p = sta[top].p;
if (p == -1) {
top--;
if (top >= 0) {
p = sta[top].p;
x = sta[top].x;
y = gra.pnt[p];
cunt[x] += cunt[y];
cost[x] += cunt[y] * gra.val[p] + cost[y];
sta[top].p = gra.nxt[p];
} else {
break;
}
} else {
x = sta[top].x;
r = sta[top].r;
y = gra.pnt[p];
if (y != r) {
++top;
cunt[y] = teams[y];
cost[y] = 0;
sta[top].r = x;
sta[top].x = y;
sta[top].p = gra.hed[y];
} else {
sta[top].p = gra.nxt[p];
}
}
}
}
void dfs_2() {
int x, y, r, p, top = 0;
sta[0].x = 1; sta[0].r = 0; sta[0].p = gra.hed[1];
while (true) {
p = sta[top].p;
if (p == -1) {
top--;
if (top >= 0) {
p = sta[top].p;
sta[top].p = gra.nxt[p];
} else {
break;
}
} else {
x = sta[top].x;
r = sta[top].r;
y = gra.pnt[p];
if (y != r) {
++top;
cost[y] = cost[x] + (total - 2 * cunt[y]) * gra.val[p];
sta[top].r = x;
sta[top].x = y;
sta[top].p = gra.hed[y];
} else {
sta[top].p = gra.nxt[p];
}
}
}
}
int main() {
int n, x, y, v;
while (scanf("%d", &n) != EOF) {
total = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", teams + i);
total += teams[i];
}
gra.init(n);
for (int i = 1; i < n; i++) {
scanf("%d %d %d", &x, &y, &v);
gra.addedge(x, y, v);
}
dfs_1();
dfs_2();
__int64 ans = cost[1];
for (int i = 2; i <= n; i++) {
if (cost[i] < ans)
ans = cost[i];
}
printf("%I64d\n", ans);
}
return 0;
}