Powered By HC TECH : AutoACer Engine

2200-2299
This commit is contained in:
KiritoTRw 2016-08-22 00:09:53 +08:00 committed by GitHub
parent 26a72e347d
commit 46f6aa3fc4
68 changed files with 4838 additions and 0 deletions

22
HDOJ/2200_autoAC.cpp Normal file
View File

@ -0,0 +1,22 @@
#include<iostream>
using namespace std;
long long C(int m,int n)
{
long long s=1;
for(int i=n,j=1;j<=m;--i,++j)
s=s*i/j;
return s;
}
int main()
{
int n;
long long ans;
while(cin>>n)
{
ans=0;
for(int i=2;i<=n;++i)
ans+=C(i,n)*(i-1);
cout<<ans<<endl;
}
return 0;
}

10
HDOJ/2201_autoAC.cpp Normal file
View File

@ -0,0 +1,10 @@
#include<stdio.h>
int main()
{
double n,m;
while(scanf("%lf %lf",&n,&m)!=EOF)
{
printf("%0.2f\n",(1.0/n));
}
return 0;
}

64
HDOJ/2202_autoAC.cpp Normal file
View File

@ -0,0 +1,64 @@
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define eps 1e-8
struct node
{
int x,y;
}
;
node p[50005];
node res[50005];
int cross(node p0,node p1,node p2)
{
return (p0.x-p2.x)*(p1.y-p2.y)-(p1.x-p2.x)*(p0.y-p2.y);
}
bool cmp(node a,node b)
{
if(a.x==b.x)
return a.y<b.y;
else
return a.x<b.x;
}
int Graham(int n)
{
int len;
int top=0;
sort(p,p+n,cmp);
for(int i=0; i<n; i++)
{
while(top>1&&cross(res[top-1],p[i],res[top-2])<=0)
top--;
res[top++]=p[i];
}
len=top;
for(int i=n-2; i>=0; i--)
{
while(top>len&&cross(res[top-1],p[i],res[top-2])<=0)
top--;
res[top++]=p[i];
}
if(n>1)
top--;
return top;
}
int main()
{
int n;
while(cin>>n)
{
for(int i=0; i<n; i++)
cin>>p[i].x>>p[i].y;
int dian=Graham(n);
int ans=-22;
for(int i=0; i<dian; i++)
for(int j=i+1; j<dian; j++)
for(int k=j+1; k<dian; k++)
ans=max(ans,cross(res[j],res[k],res[i]));
printf("%.2lf\n",0.5*ans);
}
return 0;
}

12
HDOJ/2203_autoAC.cpp Normal file
View File

@ -0,0 +1,12 @@
#include<stdio.h>
#include<string.h>
int main()
{
char s1[200001],s2[110000];
while(~scanf("%s %s",s1,s2))
{
sprintf(s1,"%s%s",s1,s1);
if(strstr(s1,s2))puts("yes");
else puts("no");
}
}

60
HDOJ/2204_autoAC.cpp Normal file
View File

@ -0,0 +1,60 @@
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#define EPS 1e-8
#define MAXN 65
typedef long long LL;
using namespace std;
bool p[MAXN];
vector<int> prime;
void Init() {
int i, j;
memset(p, true, sizeof(p));
for (i = 2; i < 9; i++) {
if (p[i]) {
for (j = i * i; j < MAXN; j += i)
p[j] = false;
}
}
prime.clear();
for (i = 2; i < MAXN; i++) {
if (p[i])
prime.push_back(i);
}
}
int main() {
LL n, tmp;
int i, j, k, ans;
Init();
while (~scanf("%I64d", &n)) {
ans = 1;
for (i = 0; i < (int) prime.size(); i++) {
tmp = (LL) (pow((double) n, 1.0 / prime[i]) + EPS);
if (tmp == 1)
break;
ans += tmp - 1;
}
for (i = 0; i < (int) prime.size(); i++) {
for (j = i + 1; j < (int) prime.size(); j++) {
tmp = (LL) (pow((double) n, 1.0 / (prime[i] * prime[j])) + EPS);
if (tmp == 1)
break;
ans -= tmp - 1;
}
}
for (i = 0; i < (int) prime.size(); i++) {
for (j = i + 1; j < (int) prime.size(); j++) {
for (k = j + 1; k < (int) prime.size(); k++) {
tmp = (LL) (pow((double) n,
1.0 / (prime[i] * prime[j] * prime[k])) + EPS);
if (tmp == 1)
break;
ans += tmp - 1;
}
}
}
printf("%d\n", ans);
}
return 0;
}

34
HDOJ/2205_autoAC.cpp Normal file
View File

@ -0,0 +1,34 @@
#include<iostream>
#include<cstring>
using namespace std;
char str[51],st[51];
bool ar[51][51][51][51];
int main()
{
int max,i,j,len1,len2,l1,l2;
while (gets(str)&&gets(st))
{
len1=strlen(str);
len2=strlen(st);
memset(ar,0,sizeof(ar));
max=0;
for (i=0;i<=len1;i++)
for (j=0;j<=len2;j++)
ar[i][1][j][0]=ar[i][0][j][1]=ar[i][0][j][0]=true;
for (l1=0;l1<=len1;l1++)
for (l2=0;l2<=len2;l2++)
for (i=0;i<=len1-l1;i++)
for (j=0;j<=len2-l2;j++)
{
if (!ar[i][l1][j][l2])
ar[i][l1][j][l2]=
l1>1&&ar[i+1][l1-2][j][l2]&&str[i]==str[i+l1-1]||
l2>1&&ar[i][l1][j+1][l2-2]&&st[j]==st[j+l2-1]||
l1>0&&l2>0&&ar[i][l1-1][j+1][l2-1]&&str[i+l1-1]==st[j]||
l1>0&&l2>0&&ar[i+1][l1-1][j][l2-1]&&str[i]==st[j+l2-1];
if (ar[i][l1][j][l2]&&l1+l2>max) max=l1+l2;
}
cout<<max<<endl;
}
return 0;
}

35
HDOJ/2206_autoAC.cpp Normal file
View File

@ -0,0 +1,35 @@
#include <stdio.h>
char str[102];
int ok;
int check(char ch){
if(ch >= '0' && ch <= '9') return 1;
if(ch == '.') return 2;
return 0;
}
int main(){
int dot, dignum, a, dotsum;
while(gets(str)){
dotsum = a = dot = dignum = 0;
for(int i = 0; str[i]; ++i){
ok = check(str[i]);
if(!ok) break;
if(ok == 1){
dot = 0;
++dignum;
a = a * 10 + str[i] - '0';
if(dignum > 3 || a > 255){
ok = 0; break;
}
}else{
dignum = a = 0;
++dot; ++dotsum;
if(dot > 1 || dotsum > 3){
ok = 0; break;
}
}
}
if(dot || dotsum != 3) ok = 0;
printf(ok ? "YES\n" : "NO\n");
}
return 0;
}

22
HDOJ/2207_autoAC.cpp Normal file
View File

@ -0,0 +1,22 @@
#include <stdio.h>
#include <math.h>
double LogAN(double a, double N)
{
return log(N) / log(a);
}
int main()
{
int N = 0;
while (scanf("%d", &N) != EOF)
{
double fBitNum = LogAN(2, N+3);
int nBitNum = ceil(fBitNum);
unsigned int nBitIP = ~((1<<nBitNum) - 1);
printf("%u.%u.%u.%u\n",
(nBitIP >> 24),
((nBitIP >> 16) & 0xFF),
((nBitIP >> 8) & 0xFF),
nBitIP & 0xFF);
}
return 0;
}

51
HDOJ/2208_autoAC.cpp Normal file
View File

@ -0,0 +1,51 @@
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,map[20][20];
int root[20],flag;
void DFS(int x,int y){
if(flag)
return ;
if(y>m)
return ;
if(x==n){
flag=1;
return ;
}
for(int i=0;i<x;i++){
if(root[i]!=i)
continue;
int tag=1;
for(int j=i;j<x && tag;j++)
if(root[j]==i)
tag=map[j][x];
if(tag){
root[x]=i;
DFS(x+1,y);
root[x]=x;
}
}
DFS(x+1,y+1);
}
int main(){
while(~scanf("%d%d",&n,&m)){
memset(map,0,sizeof(map));
flag=0;
int k,x;
for(int i=0;i<n;i++){
scanf("%d",&k);
root[i]=i;
while(k--){
scanf("%d",&x);
map[i][x]=1;
}
}
DFS(0,0);
if(m>=n || flag)
puts("YES");
else
puts("NO");
}
return 0;
}

47
HDOJ/2209_autoAC.cpp Normal file
View File

@ -0,0 +1,47 @@
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 99999999
int num[25];
char s[25];
int dfs(int n,int len,int step)
{
if(n==len)
{
return num[len-1]?INF:step;
}
if(num[n-1])
{
num[n-1]=0;
num[n]=!num[n];
num[n+1]=!num[n+1];
step++;
}
return dfs(n+1,len,step);
}
int main()
{
while(gets(s))
{
int len=strlen(s);
int step=INF,t;
for(int i=0;i<len;i++)
{
num[i]=s[i]-'0';
}
num[0]=!num[0];
num[1]=!num[1];
step=min(step,dfs(1,len,1));
for(int i=0;i<len;i++)
{
num[i]=s[i]-'0';
}
step=min(step,dfs(1,len,0));
if(step==INF)
cout<<"NO"<<endl;
else
cout<<step<<endl;
}
return 0;
}

24
HDOJ/2211_autoAC.cpp Normal file
View File

@ -0,0 +1,24 @@
#include<stdio.h>
int main()
{
int dfs(int n,int k);
int t;
int n,k;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
printf("%d\n",dfs(n,k));
}
return 0;
}
int dfs(int n,int k)
{
int num;
if(n==k)
{
return k;
}
num=dfs(n-n/k,k);
return ((num-1)/(k-1)+num);
}

32
HDOJ/2212_autoAC.cpp Normal file
View File

@ -0,0 +1,32 @@
#include <stdio.h>
int jieceng(int a)
{
int s,i;
s=1;
for(i=2;i<=a;i++)
s*=i;
return s;
}
int weishu(int a)
{
int sum=0;
int b;
b=a;
while(a)
{
sum+=jieceng(a%10);
a/=10;
}
if(sum==b)
return 1;
return 0;
}
int main()
{
int n,i;
for(i=1;i<=50000;i++)
{
if(weishu(i))
printf("%d\n",i);
}
}

64
HDOJ/2213_autoAC.cpp Normal file
View File

@ -0,0 +1,64 @@
#include <iostream>
#include <string.h>
using namespace std;
char map[102][103];
long long dp[102][103][30];
int N,M,T;
int sx,sy,ex,ey;
int dir[5][2] = {1,0,-1,0,0,1,0,-1,0,0};
int main()
{
while(scanf("%d%d%d",&N,&M,&T)==3)
{
memset(map,0,sizeof(map));
memset(dp,0,sizeof(dp));
for(int i=1;i<=N;i++)
scanf("%s",&map[i][1]);
for(int i=1;i<=N;i++)
{
for(int j=1;j<=M;j++)
{
if(map[i][j]=='S')
{
sx = i;
sy = j;
dp[i][j][0] = 1;
}
else if(map[i][j]=='E')
{
ex = i;
ey = j;
}
}
}
for(int t=1;t<=T;t++)
{
for(int i=1;i<=N;i++)
{
for(int j=1;j<=M;j++)
{
if(map[i][j]==0||map[i][j]=='X') continue;
dp[i][j][t] = 0;
for(int d=0;d<5;d++)
{
int tempi = i+dir[d][0];
int tempj = j+dir[d][1];
dp[i][j][t] += dp[tempi][tempj][t-1];
}
}
}
}
if(dp[ex][ey][T-1]>0)
{
printf("%I64d\n",dp[ex][ey][T-1]);
}
else if(dp[ex][ey][T]>0)
{
puts("Oh, my god, bad luck!");
}
else
{
puts("God will bless XX and WisKey!");
}
}
}

86
HDOJ/2215_autoAC.cpp Normal file
View File

@ -0,0 +1,86 @@
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
struct aa
{
int x,y;
}a1[105],a2[105];
double maxx(double x,double y)
{
return x>y?x:y;
}
double abss(double x)
{
return x>0?x:-x;
}
double x_y(aa a,aa b)
{
return sqrt((a.x-b.x)*(a.x-b.x)*1.0+(a.y-b.y)*(a.y-b.y)*1.0);
}
double sss(aa A,aa B,aa C)
{
return (B.x - A.x)*(C.y - B.y) - (C.x - B.x)*(B.y - A.y);
}
bool camp(aa a,aa b)
{
return a.y<b.y||a.y==b.y&&a.x<b.x;
}
bool fx(aa A,aa B,aa C)
{
return (A.x - B.x)*(C.y - B.y) > (C.x - B.x)*(A.y - B.y);
}
int main ()
{
int i,j,k,n,t,tt;
double a,b,c,r,s,maxr;
while(~scanf("%d",&n)&&n)
{
for(i=1;i<=n;i++)
scanf("%d%d",&a1[i].x,&a1[i].y);
if(n==1)
{
printf("0.50\n");
continue;
}
if(n==2)
{
printf("%.2f\n",x_y(a1[1],a1[2])/2+0.5);
continue;
}
sort(a1+1,a1+n+1,camp);
a2[1]=a1[1];
a2[2]=a1[2];
for(t=2,i=3;i<=n;i++)
{
while(t!=1&&!fx(a2[t],a2[t-1],a1[i]))
t--;
a2[++t]=a1[i];
}
a2[t+1]=a1[n-1];
for(tt=t+1,i=n-2;i>=1;i--)
{
while(t!=tt&&!fx(a2[tt],a2[tt-1],a1[i]))
tt--;
a2[++tt]=a1[i];
}
for(maxr=0,i=1;i<tt;i++)
for(j=i+1;j<tt;j++)
for(k=j+1;k<tt;k++)
{
a=x_y(a2[i],a2[j]);
b=x_y(a2[i],a2[k]);
c=x_y(a2[j],a2[k]);
if(a*a+b*b<c*c||a*a+c*c<b*b||b*b+c*c<a*a)
r=max(max(a,b),c)/2.0;
else
{
s=abss(sss(a2[i],a2[j],a2[k]))/2.0;
r=a*b*c/4.0/s;
}
maxr=max(r,maxr);
}
printf("%.2f\n",maxr+0.5);
}
return 0;
}

103
HDOJ/2216_autoAC.cpp Normal file
View File

@ -0,0 +1,103 @@
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
int n,m;
char map[50][50];
int dir[4][2]={1,0,-1,0,0,1,0,-1};
bool used[30][30][30][30];
int ab(int x)
{
if(x>0)
return x;
return -1*x;
}
struct node
{
int sx,sy;
int step;
int zx,zy;
friend bool operator < (node a,node b)
{
return a.step>b.step;
}
};
bool inmap(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<m&&map[x][y]!='X')
return true;
return false;
}
bool meet(int zx,int zy,int sx,int sy)
{
int distance=ab(zx-sx)+ab(zy-sy);
if(distance==0||distance==1)
return true;
return false;
}
int bfs(int zx,int zy,int sx,int sy)
{
node first,next;
priority_queue<node> q;
first.sx=sx;
first.sy=sy;
first.zx=zx;
first.zy=zy;
first.step=0;
q.push(first);
used[zx][zy][sx][sy]=true;
while(!q.empty())
{
first=q.top();
q.pop();
for(int i=0;i<4;i++)
{
next.zx=first.zx+dir[i][0];
next.zy=first.zy+dir[i][1];
int ssx=first.sx-dir[i][0];
int ssy=first.sy-dir[i][1];
if(!inmap(next.zx,next.zy))
continue;
if(inmap(ssx,ssy))
{
next.sx=ssx;
next.sy=ssy;
}
else
next.sx=first.sx,next.sy=first.sy;
next.step=first.step+1;
if(!used[next.zx][next.zy][next.sx][next.sy])
{
if(meet(next.zx,next.zy,next.sx,next.sy))
return next.step;
used[next.zx][next.zy][next.sx][next.sy]=true;
q.push(next);
}
}
}
return -1;
}
int main()
{
while(cin>>n>>m)
{
int zx,zy,sx,sy;
for(int k=0;k<n;k++)
scanf("%s",map[k]);
memset(used,0,sizeof used);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(map[i][j]=='Z')
zx=i,zy=j;
if(map[i][j]=='S')
sx=i,sy=j;
}
int ans=bfs(zx,zy,sx,sy);
if(ans!=-1)
cout<<ans<<endl;
else
cout<<"Bad Luck!"<<endl;
}
return 0;
}

71
HDOJ/2217_autoAC.cpp Normal file
View File

@ -0,0 +1,71 @@
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
using namespace std;
int place[200010] ;
int main()
{
int n, m;
while(scanf("%d%d", &n, &m)!=EOF)
{
memset(place, 0, sizeof(place) );
bool flag1 ,flag2;
flag1 = flag2 = 0;
for(int i=0; i<n; i++)
{
scanf("%d", &place[i]);
if(place[i] < 0 ) flag1 = 1;
else flag2 = 1;
}
sort(place, place+n);
if(flag1 && !flag2)
{
int max = 0, t_m = m;
for(int i=0; i<n; i++)
{
if(abs(place[i]) <= m)
{max = n -i; break;}
}
printf("%d\n", max);
}
else if(!flag1 && flag2)
{
int max = n, t_m = m;
for(int i=0; i<n; i++)
{
if((place[i]) > m)
{max = i; break;}
}
printf("%d\n", max);
}
else if(flag1 && flag2)
{
int max = 0, t_m = m;
int turn;
for(int i=0; i<n; i++)
if(place[i] >= 0) {turn = i; break;}
for(int i=0; i<turn; i++)
{
int t_max = 0;
for(int j=turn; j<n; j++)
{
if(2*abs(place[i]) + place[j] <= m)
t_max = j-i+1;
if(t_max > max) max = t_max;
}
}
for(int i=0; i<turn; i++)
{
int t_max = 0;
for(int j=turn; j<n; j++)
{
if(abs(place[i]) + 2*place[j] <= m)
t_max = j-i+1;
if(t_max > max) max = t_max;
}
}
printf("%d\n", max);
}
}
}

17
HDOJ/2218_autoAC.cpp Normal file
View File

@ -0,0 +1,17 @@
#include <iostream>
using namespace std;
int main()
{
int a[6],i,n;
cin>>n;
while(n--)
{
for(i=0;i<6;i++)
cin>>a[i];
if(a[0]-a[1]+a[2]-a[3]+a[4]-a[5]==0)
cout<<"YES\n";
else
cout<<"NO\n";
}
return 0;
}

105
HDOJ/2222_autoAC.cpp Normal file
View File

@ -0,0 +1,105 @@
#include<iostream>
#include<string>
#include<queue>
using namespace std;
typedef struct node
{
int count;
node* fail;
node* next[26];
node()
{
count=0;
fail=NULL;
for(int i=0;i<26;++i)
next[i]=NULL;
}
}*Pnode;
Pnode insert(Pnode root,string s)
{
Pnode p=root;
int len=s.size();
for(int i=0;i<len;++i)
{
if(!p->next[s[i]-'a'])
p->next[s[i]-'a']=new node();
p=p->next[s[i]-'a'];
}
p->count++;
return root;
}
void build_AC(Pnode root)
{
Pnode p,temp;
queue<Pnode> q;
q.push(root);
while(!q.empty())
{
p=q.front();
q.pop();
for(int i=0;i<26;++i)
if(p->next[i])
{
if(p==root)
p->next[i]->fail=root;
else
{
temp=p->fail;
while(temp)
{
if(temp->next[i])
{
p->next[i]->fail=temp->next[i];
break;
}
temp=temp->fail;
}
if(!temp)
p->next[i]->fail=root;
}
q.push(p->next[i]);
}
}
}
int match(Pnode root,string s)
{
int result=0,len=s.size();
Pnode p=root,temp;
for(int i=0;i<len;++i)
{
while(!p->next[s[i]-'a']&&p!=root)
p=p->fail;
p=p->next[s[i]-'a'];
if(!p)
p=root;
temp=p;
while(temp!=root&&temp->count!=-1)
{
result+=temp->count;
temp->count=-1;
temp=temp->fail;
}
}
return result;
}
int main()
{
int t,n;
string keyword,s;
Pnode root;
cin>>t;
while(t--)
{
root=new node();
cin>>n;
while(n--)
{
cin>>keyword;
root=insert(root,keyword);
}
build_AC(root);
cin>>s;
cout<<match(root,s)<<endl;
}
return 0;
}

48
HDOJ/2224_autoAC.cpp Normal file
View File

@ -0,0 +1,48 @@
#include <stdio.h>
#include <math.h>
#define INF 0x7fffffff
#define N 201
struct point{
double x, y;
}point[N];
int n;
double dis[N][N];
double distant(int i, int j)
{
return sqrt((point[i].x - point[j].x)*(point[i].x - point[j].x) + (point[i].y - point[j].y)*(point[i].y - point[j].y));
}
double dp()
{
int i, j, k;
double temp, b[N][N];
b[1][2] = dis[1][2];
for (j=3; j<=n; j++)
{
for (i=1; i<=j-2; i++)
b[i][j] = b[i][j-1] + dis[j-1][j];
b[j-1][j] = INF;
for (k=1; k<=j-2; k++)
{
temp = b[k][j-1] + dis[k][j];
if (temp < b[j-1][j])
b[j-1][j] = temp;
}
}
b[n][n] = b[n-1][n] + dis[n-1][n];
return b[n][n];
}
int main()
{
int i, j;
double ans;
while (scanf("%d", &n) > 0)
{
for (i=1; i<=n; i++)
scanf("%lf %lf", &point[i].x, &point[i].y);
for (j=2; j<=n; j++)
for (i=1; i<j; i++)
dis[i][j] = distant(i,j);
ans = dp();
printf("%.2lf\n", dp());
}
}

24
HDOJ/2225_autoAC.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
typedef long long LL;
template<class T> T sqr(T x) { return x * x;}
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a;}
int main() {
LL n, m;
while (cin >> n >> m) {
LL bx = 1, by = 1;
for (LL i = 1; i <= m; i++) {
LL t = (int) sqrt((double) sqr(i) * n);
for (int d = 0; d < 2; d++) {
if (abs(sqr(t + d) * sqr(by) - n * sqr(by) * sqr(i)) < abs(sqr(bx) * sqr(i) - n * sqr(by) * sqr(i))) bx = t + d, by = i;
}
}
LL GCD = gcd(bx, by);
cout << bx / GCD << "/" << by / GCD << endl;
}
return 0;
}

63
HDOJ/2227_autoAC.cpp Normal file
View File

@ -0,0 +1,63 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn = 100000+10;
const int mod = 1000000007;
int c[maxn];
int n;
struct Node{
int value;
int index;
}node[maxn];
bool cmp(Node a, Node b)
{
if(a.value == b.value) return a.index < b.index;
else return a.value < b.value;
}
int lowbit(int x)
{
return x&(-x);
}
void add(int index, int value)
{
while(index <= n)
{
c[index] = (c[index]+value) % mod;
index += lowbit(index);
}
}
int sum(int index)
{
int ret = 0;
while(index > 0)
{
ret = (ret+c[index])%mod;
index -= lowbit(index);
}
return ret;
}
int main()
{
while(scanf("%d", &n) != EOF)
{
memset(node, 0, sizeof(node));
memset(c, 0, sizeof(c));
for(int i = 1; i <= n; i++)
{
scanf("%d", &node[i].value);
node[i].index = i;
}
sort(node+1, node+n+1, cmp);
int tmp = 0;
for(int i = 1; i <= n; i++)
{
tmp = sum(node[i].index)+1;
tmp %= mod;
add(node[i].index, tmp);
}
printf("%d\n", sum(n));
}
return 0;
}

48
HDOJ/2228_autoAC.cpp Normal file
View File

@ -0,0 +1,48 @@
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
string ord[2];
string dfs(string ord)
{
int i;
if(ord == "01")return "01";
ord = ord.substr(1,ord.length()-2);
vector<string> subtree;
int flag = 0,start = 0 ,end;
for(i = 0 ; i < ord.length() ; i++ )
{
flag += ord[i] == '0' ? 1 : -1;
if(!flag){
end = i;
subtree.push_back( dfs(ord.substr(start , end - start + 1)) );
start = end + 1;
}
}
sort(subtree.begin() , subtree.end() );
string ret = "0";
for(i = 0 ; i < subtree.size(); i ++)ret += subtree[i] ;
ret += "1";
return ret;
}
bool solve()
{
if(dfs(ord[0]) == dfs(ord[1]))return true;
else return false;
}
int main()
{
int cas;
char t[2][4000];
scanf("%d",&cas);
while(cas--)
{
t[0][0] = t[1][0] = '0';
scanf("%s%s",t[0]+1,t[1]+1);
ord[0] = string(t[0]) + "1" ; ord[1] = string(t[1]) + "1";
if(solve())printf("True\n");
else printf("False\n");
}
return 0;
}

78
HDOJ/2232_autoAC.cpp Normal file
View File

@ -0,0 +1,78 @@
#include <iostream>
using namespace std;
int B[4][4]={
{1,1,0,1},
{1,1,1,0},
{0,1,1,1},
{1,0,1,1}
};
struct In{
int A[4][4];
};
In a,b;
void f(int n)
{
int i,j;
if(n==0)
{
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
b.A[i][j]=B[i][j];
}
}
}
else
{
f(n-1);
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
a.A[i][j]=b.A[i][j];
a.A[i][j]%=9937;
}
}
if(n==1)
return ;
for(i=0; i<4; i++)
{
b.A[i][0]=a.A[i][0]+a.A[i][1]+a.A[i][3];
b.A[i][1]=a.A[i][0]+a.A[i][2]+a.A[i][1];
b.A[i][2]=a.A[i][1]+a.A[i][2]+a.A[i][3];
b.A[i][3]=a.A[i][0]+a.A[i][2]+a.A[i][3];
}
}
}
void Solve()
{
int i,j,e,f,sum=0;
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
for(e=0; e<4; e++)
{
for(f=0; f<4; f++)
{
if(i==j || i==e || i==f || j==e || j==f || e==f)
continue;
sum+=((((((b.A[0][i]*b.A[1][j])%9937)*b.A[2][e])%9937)*b.A[3][f])%9937)%9937;
sum%=9937;
}
}
}
}
cout<<sum<<endl;
}
int main()
{
int n;
while(cin>>n)
{
f(n);
Solve();
}
return 0;
}

85
HDOJ/2233_autoAC.cpp Normal file
View File

@ -0,0 +1,85 @@
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define maxt(a,b) (a>b?a:b)
int a[100001];
int b[100001];
bool cmp(int x,int y)
{
return x>y;
}
int main()
{
int sum1,sum2,sum3,sum4,i,j,n,t,ji,s1,s2,max;
while(~scanf("%d %d",&n,&t))
{
sum1=sum2=sum3=sum4=0;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n,cmp);
for(i=0;i<n;i++)
{
if(a[i]<0)break;
}
ji=i;
for(j=0;i<n;j++,i++)
{
b[j]=abs(a[i]);
}
sort(a,a+ji);
s1=ji;
s2=n-ji;
for(i=0;i<s1;i++)
{
if(a[i]<=t)sum1++;
else break;
}
for(i=0;i<s2;i++)
{
if(b[i]<=t)sum2++;
else break;
}
max=0;
ji=0;
for(i=0;i<=t/2;i++)
{
ji=0;
for(j=0;j<=s1;j++)
{
if(a[j]<=i)ji++;
else break;
}
for(j=0;j<s2;j++)
{
if(b[j]<=t-2*i)ji++;
else break;
}
if(ji>max)max=ji;
}
sum3=max;
max=0;
ji=0;
for(i=0;i<=t/2;i++)
{
ji=0;
for(j=0;j<=s2;j++)
{
if(b[j]<=i)ji++;
else break;
}
for(j=0;j<s1;j++)
{
if(a[j]<=t-2*i)ji++;
else break;
}
if(ji>max)max=ji;
}
sum4=max;
printf("%d\n",maxt(maxt(sum1,sum2),maxt(sum3,sum4)));
}
return 0;
}

140
HDOJ/2234_autoAC.cpp Normal file
View File

@ -0,0 +1,140 @@
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
int map[5][5];
int deep;
int check()
{
int i,j;
if(map[1][1]!=map[1][2])
{
for(i = 1; i<=4; i++)
for(j = 1; j<=4; j++)
if(map[j][i]!=map[i][i])
return 0;
}
else
{
for(i = 1; i<=4; i++)
for(j = 1; j<=4; j++)
if(map[i][j]!=map[i][i])
return 0;
}
return 1;
}
int get_h()
{
int s1 = 0,s2 = 0,i,j;
for(i = 1; i<=4; i++)
{
int a[10] = {0},ans = 0;
for(j = 1; j<=4; j++)
a[map[i][j]]++;
for(j = 1; j<=4; j++)
ans+=a[j];
s1 = max(s1,4-ans);
}
for(j = 1; j<=4; j++)
{
int a[10] = {0},ans = 0;
for(i = 1; i<=4; i++)
a[map[i][j]]++;
for(i = 1; i<=4; i++)
ans+=a[j];
s2 = max(s2,4-ans);
}
return min(s1,s2);
}
void mov_l(int i)
{
int j;
int t = map[i][1];
for(j = 2; j<=4; j++)
map[i][j-1] = map[i][j];
map[i][4] = t;
}
void mov_r(int i)
{
int j;
int t = map[i][4];
for(j = 4; j>=2; j--)
map[i][j] = map[i][j-1];
map[i][1] = t;
}
void mov_u(int j)
{
int i;
int t = map[1][j];
for(i = 2; i<=4; i++)
map[i-1][j] = map[i][j];
map[4][j] = t;
}
void mov_d(int j)
{
int i;
int t = map[4][j];
for(i = 4; i>=2; i--)
map[i][j] = map[i-1][j];
map[1][j] = t;
}
int IDA(int step)
{
if(step == deep)
return check();
if(step+get_h()>deep)
return 0;
int i;
for(i = 1; i<=4; i++)
{
mov_l(i);
if(IDA(step+1))
return 1;
mov_r(i);
mov_r(i);
if(IDA(step+1))
return 1;
mov_l(i);
}
for(i = 1; i<=4; i++)
{
mov_u(i);
if(IDA(step+1))
return 1;
mov_d(i);
mov_d(i);
if(IDA(step+1))
return 1;
mov_u(i);
}
return 0;
}
int main()
{
int t,i,j;
cin >> t;
while(t--)
{
for(i = 1; i<=4; i++)
for(j = 1; j<=4; j++)
cin>>map[i][j];
if(check())
{
cout<<"0"<<endl;
continue;
}
deep = 1;
while(deep<=5)
{
if(IDA(0))
break;
deep++;
}
if(deep<=5)
cout<<deep<<endl;
else
cout<<"-1"<<endl;
}
return 0;
}

99
HDOJ/2235_autoAC.cpp Normal file
View File

@ -0,0 +1,99 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N_n 505
#define N_N 250050
using namespace std;
typedef struct
{
int x ,y;
int high;
int key;
}NODE;
NODE node[N_N];
int H[N_n][N_n];
int mark[N_n][N_n];
int now_mksum;
int dir[4][2] = {0 ,1 ,0 ,-1 ,1 ,0 ,-1 ,0};
int n ,m;
bool camp(NODE a ,NODE b)
{
return a.high < b.high || a.high == b.high && a.key > b.key;
}
bool ok(int x ,int y ,int now_h)
{
if(x >= 1 && x <= n && y >= 1 && y <= m && !mark[x][y] && H[x][y] <= now_h)
return 1;
return 0;
}
void MK(int x ,int y ,int now_h)
{
for(int i = 0 ;i < 4 ;i ++)
{
int xx = x + dir[i][0];
int yy = y + dir[i][1];
if(ok(xx ,yy ,now_h))
{
mark[xx][yy] = 1;
now_mksum ++;
MK(xx ,yy ,now_h);
}
}
return;
}
void jude(int x ,int y ,int now_h)
{
for(int i = 0 ;i < 4 ;i ++)
{
int xx = x + dir[i][0];
int yy = y + dir[i][1];
if(xx == 0 || xx == n + 1 || yy == 0 || yy == m + 1)
continue;
if(mark[xx][yy])
{
mark[x][y] = 1;
now_mksum ++;
MK(x ,y ,now_h);
break;
}
}
}
int main ()
{
int t ,i ,j ,ans;
scanf("%d" ,&t);
while(t--)
{
scanf("%d %d" ,&n ,&m);
for(i = 1 ;i <= n ;i ++)
for(j = 1 ;j <= m ;j ++)
{
scanf("%d" ,&H[i][j]);
int now = (i - 1) * m + j;
node[now].x = i;
node[now].y = j;
node[now].high = H[i][j];
node[now].key = (i == 1 || i == n || j == 1 || j == m);
}
sort(node + 1 ,node + n * m + 1 ,camp);
memset(mark ,0 ,sizeof(mark));
for(ans = now_mksum = 0 ,i = 1 ;i <= n * m ;i ++)
{
if(node[i].high == node[n*m].high) break;
if(!mark[node[i].x][node[i].y])
{
if(node[i].key)
{
mark[node[i].x][node[i].y] = 1;
now_mksum ++;
MK(node[i].x ,node[i].y ,node[i].high);
}
else jude(node[i].x ,node[i].y ,node[i].high);
}
if(node[i].high < node[i+1].high)
ans += ((node[i+1].high - node[i].high) * (i - now_mksum));
}
printf("%d\n" ,ans);
}
return 0;
}

78
HDOJ/2236_autoAC.cpp Normal file
View File

@ -0,0 +1,78 @@
#include<stdio.h>
#include<string.h>
#define INF 1000000000
int tot,list[10005],mark_dfs[10005],mark_gx[10005],n,map[105][105];
int max,min,left,right,kk,mid;
struct dian
{
int date,next,value;
}cun[10005];
void add(int a,int b,int c)
{
cun[tot].date=b;
cun[tot].value=c;
cun[tot].next=list[a];
list[a]=tot++;
}
int dfs(int x)
{
for(int i=list[x];i;i=cun[i].next)
{
int date=cun[i].date;
int w=cun[i].value;
if(mark_dfs[date]||w<kk||w>kk+mid) continue;
mark_dfs[date]=1;
if(mark_gx[date]==-1||dfs(mark_gx[date]))
{
mark_gx[date]=x;
return 1;
}
}
return 0;
}
int finish()
{
memset(mark_gx,255,sizeof(mark_gx));
for(int i=1;i<=n;i++)
{
memset(mark_dfs,0,sizeof(mark_dfs));
if(!dfs(i)) return 0;
}
return 1;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
tot=1;
min=INF;
max=-INF;
memset(list,0,sizeof(list));
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
scanf("%d",&map[i][j]);
if(min>map[i][j]) min=map[i][j];
if(max<map[i][j]) max=map[i][j];
add(i,j,map[i][j]);
}
right=max-min;
left=0;
while(left<=right)
{
if(left==right) break;
mid=(left+right-1)/2;
int flag=0;
for(kk=min;kk+mid<=max;kk++)
{
if(finish()) {flag=1;break;}
}
if(flag) {right=mid;}
if(!flag) {left=mid+1;}
}
printf("%d\n",right);
}
}

98
HDOJ/2238_autoAC.cpp Normal file
View File

@ -0,0 +1,98 @@
#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
struct sta{
int s[4];
};
struct node{
int m[8][8];
}res,temp,mod;
int getnum( sta a )
{
int m,n,index;
m=n=0;
for( int i=0;i<4;i++ )
if( m<a.s[i] )
m=a.s[i],index=i;
m=0;
for( int i=0;i<4;i++ )
m=m*10+a.s[(index+i)%4],
n=n*10+a.s[(4+index-i)%4];
return m>n?m:n;
}
int h[4444];
void dfs( sta a,sta b,int at )
{
if( at>3 )
{
int n1=getnum(a);
int n2=getnum(b);
temp.m[h[n1]][h[n2]]++;
return ;
}
for( int i=0;i<=a.s[at];i++ )//1->2
for( int j=0;i+j<=a.s[at];j++ )//1->0
{
sta x=b;
x.s[at]=x.s[at]-i-j;
x.s[(at+1)%4]+=i;
x.s[(at+3)%4]+=j;
dfs( a,x,at+1 );
}
}
node matriXmult( node a,node b )
{
node c;
memset( c.m,0,sizeof(c.m) );
for( int i=0;i<8;i++ )
for( int k=0;k<8;k++ )
if( a.m[i][k] )
for( int j=0;j<8;j++ )
c.m[i][j]+=a.m[i][k]*b.m[k][j];
for( int i=0;i<8;i++ )
for( int j=0;j<8;j++ )
c.m[i][j]%=9937;
return c;
}
void matrixPower( int n )
{
memset( res.m,0,sizeof(res.m) );
for( int i=0;i<8;i++ )
{
res.m[i][i]=1;
for( int j=0;j<8;j++ )
mod.m[i][j]=temp.m[i][j];
}
for( int i=0;i<32;i++ )
{
if( n&(1<<i) )
res=matriXmult( res,mod );
mod=matriXmult( mod,mod );
}
}
int main()
{
memset( temp.m,0,sizeof(temp.m) );
for( int i=0;i<8;i++ )
res.m[i][i]=1;
int a[8][4]={ 1,1,1,1,2,0,2,0,2,1,0,1,
2,1,1,0,2,2,0,0,3,0,1,0,
3,1,0,0,4,0,0,0 };
h[1111]=0;h[2020]=1;h[2101]=2;
h[2110]=3;h[2200]=4;h[3010]=5;
h[3100]=6;h[4000]=7;
sta s;
for( int i=0;i<8;i++ ){
for( int j=0;j<4;j++ )
s.s[j]=a[i][j];
dfs( s,s,0 );
}
int n;
while( scanf( "%d",&n )!=EOF )
{
matrixPower(n);
printf( "%d\n",res.m[0][0] );
}
return 0;
}

66
HDOJ/2239_autoAC.cpp Normal file
View File

@ -0,0 +1,66 @@
#include <iostream>
#include <math.h>
using namespace std;
#define M 66000
#define LL __int64
int p[6600], k, vis[M] = {0}, mod = 9937;
int Euler (int n)
{
int i, res = n;
for (i = 0; i < k && (LL)p[i]*p[i] <= n; i++)
{
if (n % p[i] == 0)
{
do
n /= p[i];
while (n % p[i] == 0);
res = res - res/p[i];
}
}
if (n > 1) res = res - res/n;
return res % mod;
}
int qmod (int a, int b)
{
a %= mod;
int res = 1;
for ( ; b; b >>= 1)
{
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
int main()
{
int n, m, i, j, ms, ans;
k = 0;
for (i = 2; i < M; i++)
{
if (!vis[i])
{
p[k++] = i;
for (j = i+i; j < M; j+=i)
vis[j] = 1;
}
}
while (~scanf ("%d%d", &n, &m))
{
ms = (int)sqrt (n+0.5);
ans = 0;
for (i = 1; i <= ms; i++)
{
if (n % i == 0)
{
ans = (ans + Euler (n/i)*qmod (m, i)%mod) % mod;
if (i != n/i) ans = (ans + Euler (i)*qmod (m, n/i)%mod) % mod;
}
}
int tot = ans;
for (ans = 0; ans < mod; ans++)
if ((LL)ans*n % mod == tot % mod)
break;
printf ("%d\n", ans);
}
return 0;
}

53
HDOJ/2241_autoAC.cpp Normal file
View File

@ -0,0 +1,53 @@
#include <iostream>
#include <math.h>
using namespace std;
#define N 10005
#define EPS 1e-10
double MIN;
double n;
double x[N],y[N];
double yyy(double xx,int id)
{
double yy,k;
k=(y[id+1]-y[id])/(x[id+1]-x[id]);
yy=y[id]+k*(xx-x[id]);
return yy;
}
void san_fen(int m)
{
int i;
double x1,y1,x2,y2,midx1,midx2,d,elem1,elem2;
for(i=0;i<m-1;i++)
{
x1=x[i];y1=y[i];
x2=x[i+1];y2=y[i+1];
d=(y2-y1)/(x2-x1);
do
{
midx1=(x1+x2)/2;
midx2=(midx1+x2)/2;
elem1=n/(midx1*midx1)+yyy(midx1,i);
elem2=n/(midx2*midx2)+yyy(midx2,i);
if(elem1>elem2)
x1=midx1;
else if(elem1<elem2)
x2=midx2;
}
while(fabs(elem1-elem2)>EPS);
if(MIN>elem1)
MIN=elem1;
}
}
int main()
{
int m,i;
while(scanf("%d%lf",&m,&n)!=EOF)
{
MIN=999999.0;
for(i=0;i<m;i++)
scanf("%lf%lf",&x[i],&y[i]);
san_fen(m);
printf("%.3lf\n",MIN);
}
return 0;
}

100
HDOJ/2242_autoAC.cpp Normal file
View File

@ -0,0 +1,100 @@
#include<stdio.h>
#include<string.h>
#include<stack>
#include<math.h>
using namespace std;
#define N 10005
#define M 20005
struct node{
int from,to,next;
}edge[2*M],edge1[2*M];
int head[N],tol,n,m,cnt,dfn[N],low[N],visit[N],Belong[N],tol1,head1[N],val[N],val1[N],SUM,Min,count;
stack<int>S;
void add(int a,int b)
{
edge[tol].from=a;edge[tol].to=b;edge[tol].next=head[a];head[a]=tol++;
}
void add1(int a,int b)
{
edge1[tol1].from=a;edge1[tol1].to=b;edge1[tol1].next=head1[a];head1[a]=tol1++;
}
int min(int a,int b)
{
return a<b?a:b;
}
void tarjan(int u,int father)
{
int j,v,flag;
dfn[u]=low[u]=cnt++;
visit[u]=1;
S.push(u);
flag=0;
for(j=head[u];j!=-1;j=edge[j].next)
{
v=edge[j].to;
if(v==father && !flag) {flag=1;continue;}
if(!visit[v]) tarjan(v,u);
low[u]=min(low[u],low[v]);
}
if(dfn[u]==low[u])
{
count++;
do{
v=S.top();
S.pop();
Belong[v]=count;
val[count]+=val1[v];
}while(v!=u);
}
}
int dfs(int u,int father)
{
int j,v,sum;
sum=val[u];
for(j=head1[u];j!=-1;j=edge1[j].next)
{
v=edge1[j].to;
if(v==father) continue;
sum+=dfs(v,u);
}
Min=min(Min,abs(SUM-2*sum));
return sum;
}
int main()
{
int i,a,b;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(head,-1,sizeof(head));
tol=cnt=0;
SUM=0;
for(i=0;i<n;i++)
{
scanf("%d",&val1[i]);
SUM+=val1[i];
}
for(i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
memset(visit,0,sizeof(visit));
memset(val,0,sizeof(val));
count=0;
tarjan(0,0);
if(count==1) {printf("impossible\n");continue;}
tol1=0;
memset(head1,-1,sizeof(head1));
for(i=0;i<tol;i++)
{
a=edge[i].from;
b=edge[i].to;
if(Belong[a]!=Belong[b]) add1(Belong[a],Belong[b]);
}
Min=0xfffffff;
dfs(1,0);
printf("%d\n",Min);
}
return 0;
}

212
HDOJ/2243_autoAC.cpp Normal file
View File

@ -0,0 +1,212 @@
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
#define LL unsigned __int64
struct node
{
int cnt;
struct node *fail;
struct node *next[26];
struct node *jump[26];
};
node root[33];
int num;
char str[10];
LL mod=10330176681277348905;
void insert(char word[])
{
int i=0;
node *tmp=root;
while(word[i])
{
int b=word[i]-'a';
if(tmp->next[b]==NULL)
{
tmp->next[b]=root+num;
memset(root+num,0,sizeof(struct node));
num++;
}
tmp=tmp->next[b];
i++;
}
tmp->cnt=1;
}
node *q[33];
int head,tail;
void add_fail()
{
head=tail=0;
q[tail++]=root;
while(head<tail)
{
node *x=q[head++];
for(int i=0;i<26;i++)
{
node *t=x->fail;
while(t!=NULL && t->next[i]==NULL)
t=t->fail;
if(x->next[i]!=NULL)
{
q[tail++]=x->next[i];
if(t==NULL)
x->next[i]->fail=root;
else
{
x->next[i]->fail=t->next[i];
if(t->next[i]->cnt)
x->next[i]->cnt=1;
}
x->jump[i]=x->next[i];
}
else
{
if(t==NULL)
x->jump[i]=root;
else
x->jump[i]=t->next[i];
}
}
}
}
int m;
int n;
struct Mat
{
LL a[2*33][2*33];
void init()
{
int i,j;
for(i=0;i<2*33;i++)
for(j=0;j<2*33;j++)
a[i][j]=0;
}
};
int len;
Mat e;
Mat mul(Mat a,Mat b)
{
Mat res;
int i,j,k;
for(i=0;i<2*len;i++)
{
for(j=0;j<2*len;j++)
{
res.a[i][j]=0;
for(k=0;k<2*len;k++)
{
if(a.a[i][k]>0 && b.a[k][j]>0)
{
res.a[i][j]+=a.a[i][k]*b.a[k][j];
}
}
}
}
return res;
}
LL solve(Mat a,int k)
{
Mat b,ans=e;
int i,j;
b.init();
for(i=0;i<len;i++)
for(j=0;j<len;j++)
b.a[i][j]=a.a[i][j];
for(i=0;i<len;i++)
{
for(j=len;j<2*len;j++)
b.a[i][j]=(i==j-len);
}
for(i=len;i<2*len;i++)
{
for(j=len;j<2*len;j++)
b.a[i][j]=(i==j);
}
k++;
while(k)
{
if(k&1)
{
ans=mul(ans,b);
}
b=mul(b,b);
k/=2;
}
LL res=0;
for(i=0;i<len;i++)
{
res+=ans.a[i][len];
}
return res-1;
}
LL sum(LL a,int k)
{
Mat b;
Mat ans=e;
b.init();
b.a[0][0]=26;b.a[0][1]=1;
b.a[1][0]=0; b.a[1][1]=1;
k++;
while(k)
{
if(k&1)
{
ans=mul(ans,b);
}
b=mul(b,b);
k/=2;
}
return ans.a[0][1]-1;
}
int main()
{
int i,j,k;
for(i=0;i<2*33;i++)
for(j=0;j<2*33;j++)
e.a[i][j]=(i==j);
while(scanf("%d%d",&n,&m)==2)
{
num=1;
memset(root,0,sizeof(struct node));
for(i=0;i<n;i++)
{
scanf("%*c%s",str);
insert(str);
}
add_fail();
int id=0,id1;
Mat res;
for(i=0;i<num;i++)
{
if(root[i].cnt==0)
{
id1=0;
for(j=0;j<num;j++)
{
if(root[j].cnt==0)
{
LL count=0;
for(k=0;k<26;k++)
{
if(root[j].jump[k]==root+i)
{
count++;
}
}
res.a[id][id1]=count;
id1++;
}
}
id++;
}
}
len=id;
LL all=sum(26,m);
LL left=solve(res,m);
all-=left;
if(all<0)
all+=mod;
printf("%I64u\n",all);
}
return 0;
}

244
HDOJ/2248_autoAC.cpp Normal file
View File

@ -0,0 +1,244 @@
#include <functional>
#include <algorithm>
#include <iostream>
#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 FOR(i,a) for((i)=0;i<(a);(i)++)
#define MEM(a) (memset((a),0,sizeof(a)))
#define LL long long
const int N=6500010;
const int M=20010;
const int MOD=1000000007ll;
const int INF=0x7fffffff;
const int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
const double eps=1e-8;
const double PI=acos(-1.0);
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));}
char str[12];
bool f[N];
int tot,prime[N];
int a[M],r[M];
void fun()
{
int i,j,s;
memset(f,true,sizeof(f));
f[1]=false;
tot=0;
prime[tot++]=2;
for(i=4;i<=N;i+=2)
f[i]=false;
for(i=3;i*i<=N;i+=2)
{
if(!f[i])
continue;
prime[tot++]=i;
for(s=2*i,j=i*i;j<=N;j+=s)
f[j]=false;
}
for(;i<=N;i++)
if(f[i])
prime[tot++]=i;
}
void Swords()
{
char ss[110];
scanf("%s",ss);
int len=(int)strlen(ss);
if(f[len])
printf("Yes.\n");
else
printf("No.\n");
}
void Stones()
{
int n,sign,p=0,len=INF;
memset(r,-1,sizeof(r));
r[0]=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
p=(p+a[i])%n;
if(r[p] != -1)
{
if(i-r[p] < len)
{
len=i-r[p];
sign=r[p]+1;
}
}
r[p]=i;
}
if(len == INF)
printf("浜绾疯!\n");
else
{
for(int i=sign;i<sign+len;i++)
{
if(i!=sign)
printf(" ");
printf("%d",i);
}
printf("\n");
}
}
void Search()
{
int x,k,i=0;
scanf("%d%d",&x,&k);
while(x > 1)
{
while(x%prime[i] == 0)
{
x/=prime[i];
k--;
if(k == 0)
{
printf("%d\n",prime[i]);
return ;
}
}
i++;
}
printf("no\n");
}
#define MAXN 9999
#define MAXSIZE 10
#define DLEN 4
class BigNum
{
private:
int a[1510];
int len;
public:
BigNum(){ len = 1;memset(a,0,sizeof(a)); }
BigNum(const int);
BigNum operator*(const BigNum &) const;
BigNum operator^(const int &) const;
void print();
};
BigNum::BigNum(const int b)
{
int c,d = b;
len = 0;
memset(a,0,sizeof(a));
while(d > MAXN)
{
c = d - (d / (MAXN + 1)) * (MAXN + 1);
d = d / (MAXN + 1);
a[len++] = c;
}
a[len++] = d;
}
BigNum BigNum::operator*(const BigNum & T) const
{
BigNum ret;
int i,j,up;
int temp,temp1;
for(i = 0 ; i < len ; i++)
{
up = 0;
for(j = 0 ; j < T.len ; j++)
{
temp = a[i] * T.a[j] + ret.a[i + j] + up;
if(temp > MAXN)
{
temp1 = temp - temp / (MAXN + 1) * (MAXN + 1);
up = temp / (MAXN + 1);
ret.a[i + j] = temp1;
}
else
{
up = 0;
ret.a[i + j] = temp;
}
}
if(up != 0)
ret.a[i + j] = up;
}
ret.len = i + j;
while(ret.a[ret.len - 1] == 0 && ret.len > 1)
ret.len--;
return ret;
}
BigNum BigNum::operator^(const int & n) const
{
BigNum t,ret(1);
int i;
if(n<0)
exit(-1);
if(n==0)
return 1;
if(n==1)
return *this;
int m=n;
while(m>1)
{
t=*this;
for(i=1;i<<1<=m;i<<=1)
t=t*t;
m-=i;
ret=ret*t;
if(m==1)
ret=ret*(*this);
}
return ret;
}
void BigNum::print()
{
int i;
printf("%d",a[len-1]);
for(i = len - 2 ; i >= 0 ; i--)
printf("%04d",a[i]);
printf("\n");
}
void See()
{
int n;
scanf("%d",&n);
BigNum t(2);
BigNum ans=t^n;
ans.print();
}
void solve()
{
if(!strcmp(str,"Swords"))
Swords();
else if(!strcmp(str,"Stones"))
Stones();
else if(!strcmp(str,"Search"))
Search();
else
See();
}
int main()
{
fun();
while(scanf("%s",str)!=EOF)
solve();
return 0;
}

70
HDOJ/2253_autoAC.cpp Normal file
View File

@ -0,0 +1,70 @@
#include <stdio.h>
#include <string.h>
#define M 30005
#define SIZE 128
#define WORDMAX 3200
#define BIT 32
char s1[M], s2[M];
int nword;
unsigned int str[SIZE][WORDMAX];
unsigned int tmp1[WORDMAX], tmp2[WORDMAX];
void pre(int len)
{
int i, j;
memset(str, 0, sizeof(str));
for(i = 0; i < len; i ++)
str[s1[i]][i / BIT] |= 1 << (i % BIT);
}
void cal(unsigned int *a, unsigned int *b, char ch)
{
int i, bottom = 1, top;
unsigned int x, y;
for(i = 0; i < nword; i ++)
{
y = a[i];
x = y | str[ch][i];
top = (y >> (BIT - 1)) & 1;
y = (y << 1) | bottom;
if(x < y) top = 1;
b[i] = x & ((x - y) ^ x);
bottom = top;
}
}
int bitcnt(unsigned int *a)
{
int i, j, res = 0, t;
unsigned int b[5] = {0x55555555, 0x33333333, 0x0f0f0f0f, 0x00ff00ff, 0x0000ffff}, x;
for(i = 0; i < nword; i ++)
{
x = a[i];
t = 1;
for(j = 0; j < 5; j ++, t <<= 1)
x = (x & b[j]) + ((x >> t) & b[j]);
res += x;
}
return res;
}
void process()
{
int i, j, len1, len2;
unsigned int *a, *b, *t;
len1 = strlen(s1);
len2 = strlen(s2);
nword = (len1 + BIT - 1) / BIT;
pre(len1);
memset(tmp1, 0, sizeof(tmp1));
a = &tmp1[0];
b = &tmp2[0];
for(i = 0; i < len2; i ++)
{
cal(a, b, s2[i]);
t = a; a = b; b = t;
}
printf("%d\n", bitcnt(a));
}
int main()
{
while(scanf("%s%s", s1, s2) != EOF)
process();
return 0;
}

76
HDOJ/2254_autoAC.cpp Normal file
View File

@ -0,0 +1,76 @@
#include<iostream>
#include<map>
#include<string.h>
using namespace std;
#define N 35
#define FF(i,n) for(int i=0;i<n;i++)
int ans[N];
int init[N][N];
void matrixMul(int a[][N],int b[][N],int n,int mod){
int i,j,k;
int buf[N][N]={0};
FF(i,n)FF(k,n) if(a[i][k])FF(j,n)if(b[k][j])
buf[i][j]=(buf[i][j]+a[i][k]*b[k][j])%mod;
FF(i,n)FF(j,n) a[i][j]=buf[i][j];
}
void matrixMul(int a[],int b[][N],int n,int mod){
int i,j;
int buf[N]={0};
FF(i,n)FF(j,n)
buf[i]=(buf[i]+a[j]*b[i][j])%mod;
FF(i,n) a[i]=buf[i];
}
void matrixMul(int ans[],int init[][N],int n,int m,int mod){
for(;m;m>>=1){
if(m&1) matrixMul(ans,init,n,mod);
matrixMul(init,init,n,mod);
}
}
int main(void){
int t;
int i,j,k,n,mod;
int Map[N][N];
n=30;
mod=2008;
while(~scanf("%d",&t)){
int a,b;
int ind=0;
map<int,int> M;
memset(Map,0,sizeof(Map));
while(t--){
scanf("%d%d",&a,&b);
if(M.find(a)==M.end())
M[a]=ind++;
if(M.find(b)==M.end())
M[b]=ind++;
Map[M[b]][M[a]]++;
}
scanf("%d",&t);
while(t--){
int v1,v2;
int t1,t2;
scanf("%d%d%d%d",&v1,&v2,&t1,&t2);
if(M.find(v1)==M.end()||M.find(v2)==M.end()||(t1==0&&t2==0)){
printf("0\n");
continue;
}
memset(ans,0,sizeof(ans));
ans[M[v1]]=1;
int ret;
if(t1==0)
ret=0;
else{
FF(j,M.size())FF(k,M.size()) init[j][k]=Map[j][k];
matrixMul(ans,init,M.size(),t1,mod);
ret=ans[M[v2]]%mod;
}
t1++;
for(;t1<=t2;t1++){
FF(i,M.size())FF(j,M.size()) init[i][j]=Map[i][j];
matrixMul(ans,init,M.size(),1,mod);
ret=(ret+ans[M[v2]])%mod;
}
printf("%d\n",ret);
}
}
}

87
HDOJ/2255_autoAC.cpp Normal file
View File

@ -0,0 +1,87 @@
#include<cstdio>
#define Max 456
#include<cstring>
using namespace std;
int g[Max][Max];
int lx[Max],ly[Max];
int visx[Max],visy[Max];
int match[Max];
int n;
int findpath(int u);
int KM();
int main()
{
//int T;
//scanf("%d",&T);
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&g[i][j]);
printf("%d\n",KM());
}
return 0;
}
int findpath(int u)
{
visx[u]=1;
for(int v=1;v<=n;v++)
{
if(!visy[v]&&lx[u]+ly[v]==g[u][v])
{
int t=match[v];visy[v]=1;
if(!t||findpath(t))
{
match[v]=u;return 1;
}
}
}
return 0;
}
int KM()
{
for(int i=1;i<=n;i++)
{
lx[i]=0x80000000;
ly[i]=0;
for(int j=1;j<=n;j++)
if(lx[i]<g[i][j])
lx[i]=g[i][j];
}
memset(match,0,sizeof(match));
for(int u=1;u<=n;u++)
{
while(1)
{
memset(visx,0,sizeof(visx));
memset(visy,0,sizeof(visy));
if(findpath(u))
break;
int d=0x7fffffff;
for(int i=1;i<=n;i++)
{
if(visx[i])
{
for(int j=1;j<=n;j++)
if(!visy[j]&&d>lx[i]+ly[j]-g[i][j])
d=lx[i]+ly[j]-g[i][j];
}
}
for(int k=1;k<=n;k++)
{
if(visx[k])
lx[k]-=d;
if(visy[k])
ly[k]+=d;
}
}
}
int sum=0;
for(int i=1;i<=n;i++)
{
int t=match[i];
if(t)
sum+=g[t][i];
}
return sum;
}

53
HDOJ/2256_autoAC.cpp Normal file
View File

@ -0,0 +1,53 @@
#include<iostream>
#include<cstdio>
#include<memory.h>
using namespace std;
struct node
{
int matrix[3][3];
}ma,e;
node operator *(node x,node y)
{
node temp;
for(int i=1;i<=2;i++)
for(int j=1;j<=2;j++)
{
temp.matrix[i][j]=0;
for(int k=1;k<=2;k++)
temp.matrix[i][j]+=(x.matrix[i][k]*y.matrix[k][j])%1024;
temp.matrix[i][j]%=1024;
}
return temp;
}
node operator ^ (node ma,int k)
{
node ans=e,p=ma;
if(k==0)
return e;
while(k)
{
if(k%2==1)
{
ans=ans*p;
}
k=k/2;
p=p*p;
}
return ans;
}
int main()
{
int T,n;
memset(e.matrix,0,sizeof(e.matrix));
e.matrix[1][1]=e.matrix[2][2]=1;
scanf("%d",&T);
ma.matrix[1][1]=5,ma.matrix[1][2]=12,ma.matrix[2][1]=2,ma.matrix[2][2]=5;
while(T--)
{
scanf("%d",&n);
node temp=ma^(n-1);
int ans=(2*(temp.matrix[1][1]*5+temp.matrix[1][2]*2)-1)%1024;
printf("%d\n",ans);
}
return 0;
}

107
HDOJ/2258_autoAC.cpp Normal file
View File

@ -0,0 +1,107 @@
#include <stdio.h>
int n,m,total,nxt[4][2]={{0,-1},{-1,0},{1,0},{0,1}};
bool vis[20][20];
char d[20][21];
void dfs(int x,int y,int num)
{
for(int i=0;i<4;i++)
{
x+=nxt[i][0];
y+=nxt[i][1];
if(x>=0 && x<n && y>=0 && y<m && !vis[x][y] && d[x][y]==num)
{
vis[x][y]=1;
total++;
dfs(x,y,num);
}
x-=nxt[i][0];
y-=nxt[i][1];
}
}
void tran(int x,int y,int num)
{
for(int i=0;i<4;i++)
{
x+=nxt[i][0];
y+=nxt[i][1];
if(x>=0 && x<n && y>=0 && y<m && d[x][y]==num)
{
d[x][y]='0';
tran(x,y,num);
}
x-=nxt[i][0];
y-=nxt[i][1];
}
}
int main()
{
int i,j,k,mx,x,y,ans,remain,t;
while(~scanf("%d%d",&n,&m))
{
for(i=0;i<n;i++) scanf("%s",d[i]);
ans=0;
remain=n*m;
while(remain)
{
mx=0;
for(i=0;i<n;i++) for(j=0;j<m;j++) vis[i][j]=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(d[i][j]>'0' && !vis[i][j])
{
vis[i][j]=1;
total=1;
dfs(i,j,d[i][j]);
if(total>mx)
{
mx=total;
x=i;
y=j;
}
}
}
}
remain-=mx;
ans+=mx*(mx-1);
tran(x,y,d[x][y]);
d[x][y]='0';
for(i=n-1;i>=0;i--)
{
for(j=0;j<m;j++)
{
if(d[i][j]=='0')
{
for(k=i-1;k>=0;k--)
{
if(d[k][j]>'0')
{
d[i][j]=d[k][j];
d[k][j]='0';
break;
}
}
}
}
}
t=m-1;
while(t--)
{
for(j=0;j<m-1;j++)
{
for(i=0;i<n;i++) if(d[i][j]>'0') break;
if(i==n)
{
for(i=0;i<n;i++)
{
d[i][j]=d[i][j+1];
d[i][j+1]='0';
}
}
}
}
}
printf("%d\n",ans);
}
}

150
HDOJ/2259_autoAC.cpp Normal file
View File

@ -0,0 +1,150 @@
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
struct S{
char mp[20][21];
int step,ans,x[50],y[50],val;
int num[20][20];
bool operator<(const S &p) const
{
return val<p.val;
}
}t,ans,tt;
int n,m,nxt[4][2]={{1,0},{0,1},{-1,0},{0,-1}},cnt;
char temp[20][21];
bool vis[20][20];
void dfs(int x,int y,char c)
{
int i,j;
for(i=0;i<4;i++)
{
x+=nxt[i][0];
y+=nxt[i][1];
if(x>=0 && x<n && y>=0 && y<m && !vis[x][y] && temp[x][y]==c)
{
vis[x][y]=1;
cnt++;
dfs(x,y,c);
}
x-=nxt[i][0];
y-=nxt[i][1];
}
}
void tran(int x,int y,char num,char (*d)[21])
{
for(int i=0;i<4;i++)
{
x+=nxt[i][0];
y+=nxt[i][1];
if(x>=0 && x<n && y>=0 && y<m && d[x][y]==num)
{
d[x][y]='0';
tran(x,y,num,d);
}
x-=nxt[i][0];
y-=nxt[i][1];
}
}
void move(char (*d)[21])
{
int i,j,k;
for(i=n-1;i>=0;i--)
{
for(j=0;j<m;j++)
{
if(d[i][j]=='0')
{
for(k=i-1;k>=0;k--)
{
if(d[k][j]>'0')
{
d[i][j]=d[k][j];
d[k][j]='0';
break;
}
}
}
}
}
int t=m-1;
while(t--)
{
for(j=0;j<m-1;j++)
{
for(i=0;i<n;i++) if(d[i][j]>'0') break;
if(i==n)
{
for(i=0;i<n;i++)
{
d[i][j]=d[i][j+1];
d[i][j+1]='0';
}
}
}
}
}
void eval(S &node)
{
int i,j;
for(i=0;i<n;i++) for(j=0;j<m;j++) vis[i][j]=0,temp[i][j]=node.mp[i][j];
node.val=node.ans;
for(i=0;i<n;i++) for(j=0;j<m;j++)
{
node.num[i][j]=0;
if(temp[i][j]>'0' && !vis[i][j])
{
vis[i][j]=1;
cnt=1;
dfs(i,j,temp[i][j]);
if(cnt>1)
{
node.num[i][j]=cnt;
node.val+=cnt*(cnt-1);
}
}
}
}
int main()
{
int i,j,p,q,tot;
while(~scanf("%d%d",&n,&m))
{
for(i=0;i<n;i++) scanf("%s",t.mp[i]);
t.step=0;
t.ans=0;
t.val=0;
ans.ans=0;
priority_queue<S>que;
eval(t);
que.push(t);
tot=0;
while(!que.empty())
{
t=que.top();
if(t.ans>ans.ans) ans=t;
tot++;
if(tot>24) break;
que.pop();
for(i=0;i<n;i++) for(j=0;j<m;j++)
{
if(t.num[i][j])
{
tt=t;
tran(i,j,t.mp[i][j],t.mp);
t.mp[i][j]='0';
move(t.mp);
t.x[t.step]=i;
t.y[t.step]=j;
t.step++;
t.ans+=t.num[i][j]*(t.num[i][j]-1);
eval(t);
que.push(t);
t=tt;
}
}
}
printf("%d\n",ans.step);
for(i=0;i<ans.step;i++) printf("%d %d\n",ans.x[i],ans.y[i]);
}
}

141
HDOJ/2262_autoAC.cpp Normal file
View File

@ -0,0 +1,141 @@
#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=230;
const int INF=0x3f3f3f3f;
const LL MOD=1000000007,STA=8000010;
const LL LNF=1LL<<55;
const double EPS=1e-9;
const double OO=1e30;
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));}
int vis[20][20],cnt[20][20],e[20][20];
char g[20][20];
int n,m,tot,sx,sy;
double A[N][N];
int gauss(int n)
{
int i,j,k,r;
for(i=0;i<n;i++){
r=i;
for(j=i+1;j<n;j++)
if(fabs(A[j][i]) > fabs(A[r][i]))r=j;
if(r!=i)for(j=0;j<=n;j++)swap(A[r][j],A[i][j]);
for(j=n;j>=i;j--){
for(k=i+1;k<n;k++)
A[k][j]-=A[k][i]/A[i][i]*A[i][j];
}
}
for(i=0;i<n;i++)if(sign(A[i][i])==0)return 0;
for(i=n-1;i>=0;i--){
for(j=i+1;j<n;j++)
A[i][n]-=A[j][n]*A[i][j];
A[i][n]/=A[i][i];
}
return 1;
}
int bfs()
{
int i,j,x,y,nx,ny,t;
queue<int> q;
q.push(sx*m+sy);
mem(vis,-1);mem(cnt,0);
vis[sx][sy]=tot=0;
tot++;
while(!q.empty()){
t=q.front();q.pop();
x=t/m;y=t%m;
for(i=0;i<4;i++){
nx=x+dx[i];
ny=y+dy[i];
if(nx>=0&&nx<n && ny>=0&&ny<m && g[nx][ny]!='#'){
cnt[x][y]++;
if(vis[nx][ny]!=-1)continue;
vis[nx][ny]=tot++;
q.push(nx*m+ny);
}
}
}
for(i=0;i<n;i++){
for(j=0;j<m;j++)
if(vis[i][j]!=-1 && e[i][j])return 1;
}
return 0;
}
int main(){
int i,j,k;
while(~scanf("%d%d",&n,&m))
{
mem(e,0);
for(i=0;i<n;i++){
scanf("%s",g[i]);
for(j=0;j<m;j++){
if(g[i][j]=='@')sx=i,sy=j;
else if(g[i][j]=='$')e[i][j]=1;
}
}
if(!bfs()){
printf("-1\n");
continue;
}
mem(A,0);
for(i=0;i<n;i++){
for(j=0;j<m;j++){
if(vis[i][j]==-1)continue;
int u=vis[i][j];
double p=1.0/cnt[i][j];
if(e[i][j]){
A[u][u]=1;
A[u][tot]=0;
continue;
}
A[u][u]=A[u][tot]=1;
for(k=0;k<4;k++){
int x=i+dx[k],y=j+dy[k];
if(x>=0&&x<n && y>=0&&y<m && vis[x][y]!=-1){
A[u][vis[x][y]]=-p;
}
}
}
}
gauss(tot);
printf("%.6lf\n",A[vis[sx][sy]][tot]);
}
return 0;
}

39
HDOJ/2265_autoAC.cpp Normal file
View File

@ -0,0 +1,39 @@
#include<stdio.h>
#include<string.h>
int main()
{
int a,b,j,m;
char c[210],d[210];
while(fgets(c,210,stdin))
{
int e[210]={0};
m=0;
a=strlen(c);
for(j=0;j<a-1;j++)
if(c[j]!=' ')
d[m++]=c[j];
d[m]='\0';
b=strlen(d);
for(j=2;j<b;)
{
printf("%c",d[j]);
e[j]=1;
j=j+3;
}
for(j=1;j<b;j=j+2)
{
if(e[j]==1)
continue;
else
{
printf("%c",d[j]);
e[j]=1;
}
}
for(j=0;j<b;j++)
if(e[j]!=1)
printf("%c",d[j]);
printf("\n");
}
return 0;
}

55
HDOJ/2266_autoAC.cpp Normal file
View File

@ -0,0 +1,55 @@
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
__int64 sum,ji;
void dfs(char s[],__int64 now,int ci)
{
int i,j,len;
char str[15];
len=strlen(s);
if(len == 0)
{
if(now == sum)ji++;
return ;
}
if(ci == 0)
{
for(i=0;i<len;i++)
{
for(j=0;j<=i;j++)
{
str[j]=s[j];
}
str[j]=0;
dfs(s+i+1,(__int64)atoi(str),ci+1);
}
}
else
{
for(i=0;i<len;i++)
{
for(j=0;j<=i;j++)
{
str[j]=s[j];
}
str[j]=0;
dfs(s+1+i,now+atoi(str),ci+1);
dfs(s+1+i,now-atoi(str),ci+1);
}
}
}
int main()
{
int i,j;
char s[15];
memset(s,0,sizeof(s));
while(~scanf("%s %I64d",s,&sum))
{
ji=0;
dfs(s,0,0);
printf("%I64d\n",ji);
memset(s,0,sizeof(s));
}
return 0;
}

102
HDOJ/2267_autoAC.cpp Normal file
View File

@ -0,0 +1,102 @@
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<deque>
using namespace std;
struct PT {
int x;
int y;
};
void Fill(int n, int m, char Graph[][305]) {
int i, j;
for (i = 0; i <= n + 1; i++)
Graph[i][0] = Graph[i][m + 1] = '*';
for (i = 0; i <= m + 1; i++)
Graph[0][i] = Graph[n + 1][i] = '*';
}
void BFS(int n, int m, char Graph[][305], int &O, int &V) {
int i, j;
int numO, numV, sumO = 0, sumV = 0;
int plusX[] = {0, 1, 0, -1};
int plusY[] = {1, 0, -1, 0};
bool IsSurvive;
deque<PT> Queue;
int top = 0;
PT pt, get;
bool IsReturn;
RETURN:
IsReturn = false;
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
if (Graph[i][j] != '#' && Graph[i][j] != '-') {
pt.x = i;
pt.y = j;
Queue.push_back(pt);
IsReturn = true;
goto BFSSTART;
}
BFSSTART:
IsSurvive = true;
numO = 0;
numV = 0;
while (!Queue.empty()) {
get = Queue.front();
Queue.pop_front();
if (Graph[get.x][get.y] == 'v')
numV++;
if (Graph[get.x][get.y] == 'o')
numO++;
Graph[get.x][get.y] = '-';
for (i = 0; i < 4; i++) {
if (Graph[get.x + plusX[i]][get.y + plusY[i]] == '*')
IsSurvive = false;
if (Graph[get.x + plusX[i]][get.y + plusY[i]] == 'v') {
pt.x = get.x + plusX[i];
pt.y = get.y + plusY[i];
Queue.push_back(pt);
numV++;
Graph[get.x + plusX[i]][get.y + plusY[i]] = '-';
}
if (Graph[get.x + plusX[i]][get.y + plusY[i]] == 'o') {
pt.x = get.x + plusX[i];
pt.y = get.y + plusY[i];
Queue.push_back(pt);
numO++;
Graph[get.x + plusX[i]][get.y + plusY[i]] = '-';
}
if (Graph[get.x + plusX[i]][get.y + plusY[i]] == '.') {
pt.x = get.x + plusX[i];
pt.y = get.y + plusY[i];
Queue.push_back(pt);
Graph[get.x + plusX[i]][get.y + plusY[i]] = '-';
}
}
}
if (IsSurvive) {
if (numV > numO)
sumV += numV;
else if (numV < numO)
sumO += numO;
}
if (IsReturn)
goto RETURN;
ANS:
O = sumO;
V = sumV;
}
int main() {
char Graph[305][305];
int n, m;
int i, j;
int O, V;
while (scanf("%d%d", &n, &m) != EOF) {
for (i = 1; i <= n; i++)
scanf("%s", &Graph[i][1]);
Fill(n, m, Graph);
BFS(n, m, Graph, O, V);
printf("%d %d\n", O, V);
}
return 0;
}

18
HDOJ/2268_autoAC.cpp Normal file
View File

@ -0,0 +1,18 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
double time,a,b,c;
while(scanf("%lf%lf%lf",&a,&b,&c)!=EOF)
{
if(a>=b)time=c*1.0/a;
else {
double L=2*a*c/(3*a+b);
time=(c-L)/b+L/a;
}
printf("%.3lf\n",time);
}
return 0;
}

86
HDOJ/2269_autoAC.cpp Normal file
View File

@ -0,0 +1,86 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#define MAXD 15
#define ST 2048
#define INF 0x3f3f3f3f
char name[MAXD][25];
int N, M, T, f[2][ST], vis[2][ST], danger[ST];
char b[1010];
void init()
{
int i, j, k, n, st;
for(i = 0; i < N; i ++) scanf("%s", name[i]);
memset(danger, 0, sizeof(danger));
gets(b);
for(i = 0; i < M; i ++)
{
gets(b);
st = 0;
char *p = strtok(b, " ");
for(j = 0; j < N; j ++) if(strcmp(name[j], p) == 0) st |= 1 << j;
while((p = strtok(NULL, " ")) != NULL)
for(j = 0; j < N; j ++) if(strcmp(name[j], p) == 0) st |= 1 << j;
danger[st] = 1;
}
}
int can(int st)
{
int i, n = 0;
for(i = 0; i < N; i ++)
if(st & 1 << i) ++ n;
return n <= T;
}
int check(int st)
{
for(int i = st; i > 0; i = i - 1 & st)
if(danger[i]) return 0;
return 1;
}
void solve()
{
int i, j, k, cur, st, ans = -1, D = (1 << N) - 1;
std::queue <int> q;
memset(vis, 0, sizeof(vis));
f[0][0] = 0, vis[0][0] = 1;
q.push(0);
while(!q.empty())
{
cur = q.front() & 1, st = q.front() >> 1, q.pop();
if(cur == 1 && st == D)
{
ans = f[cur][st];
break;
}
if(cur == 1 && check(st) && !vis[0][st])
f[0][st] = f[1][st] + 1, vis[0][st] = 1, q.push(st << 1);
if(cur == 0) st = ~st & D;
for(i = st; i > 0; i = i - 1 & st)
if(can(i) && check(st - i))
{
if(cur == 0)
{
int t = ~(st - i) & D;
if(!vis[1][t])
f[1][t] = f[0][~st & D] + 1, vis[1][t] = 1, q.push(t << 1 | 1);
}
else
{
int t = st - i;
if(!vis[0][t])
f[0][t] = f[1][st] + 1, vis[0][t] = 1, q.push(t << 1);
}
}
}
printf("%d\n", ans);
}
int main()
{
while(scanf("%d%d%d", &N, &M, &T) == 3)
{
init();
solve();
}
return 0;
}

54
HDOJ/2270_autoAC.cpp Normal file
View File

@ -0,0 +1,54 @@
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<algorithm>
#include<queue>
using namespace std;
#define MP make_pair
#define PB push_back
#define foreach(e,x) for(__typedef(x.begin()) e=x.begin(); e!=x.end(); ++e)
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> mapII;
const int MAX_N = 1000000 + 10;
const double PI = acos(-1.);
const double EPS = 1e-8;
int N;
int a[MAX_N];
void read(int &x)
{
x = 0;
char ch;
for( ; ch = getchar(); ) {
if (isdigit(ch)) break;
}
x = ch - '0';
for( ; ; ) {
ch = getchar();
if (! isdigit(ch)) break;
x = x * 10 + ch - '0';
}
}
void solve()
{
for(int i = 1; i <= N; ++ i) a[i] = i;
int u;
for(int i = 1; i <= N; ++ i) {
read(u);
a[u] = a[i];
}
int ret = 0;
for(int i = 1; i <= N; ++ i)
ret += a[i] == a[1];
printf("%d\n", ret - 1);
}
int main()
{
for( ; cin >> N; )
solve();
return 0;
}

35
HDOJ/2272_autoAC.cpp Normal file
View File

@ -0,0 +1,35 @@
#include<stdio.h>
#include<math.h>
int main()
{
int n,i,j,rank[100],s,t;
char str[20];
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
{
getchar();
for(j=0;j<20;j++)
{
scanf("%c",&str[j]);
if(str[j]==' ')
break;
}
scanf("%d",&rank[i]);
getchar();
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(rank[j]<rank[i])
{
t=rank[j];rank[j]=rank[i];rank[i]=t;
}
}
s=0;
for(i=0;i<n;i++)
s+=abs(i+1-rank[i]);
printf("%d\n",s);
}
return 0;
}

17
HDOJ/2273_autoAC.cpp Normal file
View File

@ -0,0 +1,17 @@
#include<stdio.h>
#include<algorithm>
using namespace std;
int main(){
int n;
while(~scanf("%d",&n)){
int as=0,a,b;
int v=111;
for(int i=0;i<n;++i){
scanf("%d%d",&a,&b);
as+=a;
v=min(v,b);
}
printf("%.2lf\n",1.0*as/v);
}
return 0;
}

22
HDOJ/2274_autoAC.cpp Normal file
View File

@ -0,0 +1,22 @@
#include<stdio.h>
#include<string.h>
int a[6],s[100005],sum;
int main()
{
for(int i=0;i<=99999;i++) {
int t=i,k=0,sum;
while(t>0) {
a[k++]=t%10;
t/=10;
}
int tmp=0;
for(int j=0;j<k;j++)
tmp+=a[j];
s[i]=tmp*24*11111-i;
}
while(~scanf("%d",&sum)) {
for(int i=0;i<=99999;i++)
if(sum==s[i]) printf("%05d\n",i);
}
return 0;
}

46
HDOJ/2275_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include <iostream>
#include <set>
#include <cstdio>
using namespace std;
multiset<int>mset;
multiset<int>::iterator iter1,iter2;
int main()
{
int n,x;
char str[10];
while(scanf("%d",&n)!=EOF)
{
mset.clear();
while(n--)
{
scanf("%s %d",str,&x);
if(str[1]=='u')
mset.insert(x);
else
{
if (x<*(mset.begin()))
{
printf("No Element!\n");
continue;
}
iter1=mset.find(x);
if(iter1!=mset.end())
{
printf("%d\n",*iter1);
mset.erase(iter1);
}
else
{
mset.insert(x);
iter1=iter2=mset.find(x);
iter1--;
printf("%d\n",*iter1);
mset.erase(iter1);
mset.erase(iter2);
}
}
}
printf("\n");
}
return 0;
}

71
HDOJ/2276_autoAC.cpp Normal file
View File

@ -0,0 +1,71 @@
#include <stdio.h>
#include <string.h>
int a[105][105];
int ans[105];
int tag[105];
char str[105];
int tag1[105][105];
int main()
{
int i,j,m,n,k;
while(scanf("%d",&m)!=EOF)
{
scanf("%s",str);
n=strlen(str);
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
if (j==i || j==(i+n-1)%n) a[i][j]=1;
else a[i][j]=0;
}
}
for (i=0;i<n;i++)
{
ans[i]=str[i]-'0';
}
while(m!=0)
{
if (m%2==1)
{
for (i=0;i<n;i++)
{
tag[i]=0;
for (j=0;j<n;j++)
{
tag[i]=(tag[i]+a[i][j]*ans[j])%2;
}
}
for (i=0;i<n;i++)
{
ans[i]=tag[i];
}
}
m=m/2;
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
tag1[i][j]=0;
for (k=0;k<n;k++)
{
tag1[i][j]=(tag1[i][j]+a[i][k]*a[k][j])%2;
}
}
}
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
a[i][j]=tag1[i][j];
}
}
}
for (i=0;i<n;i++)
{
printf("%d",ans[i]);
}
printf("\n");
}
return 0;
}

44
HDOJ/2277_autoAC.cpp Normal file
View File

@ -0,0 +1,44 @@
#include <iostream>
#define INF 1<<30
#define MAX(a,b) a>b?a:b
#define MIN(a,b) a<b?a:b
#include <math.h>
using namespace std;
int f(int a,int b,int c)
{
int mark=0;
int sum1,sum2,sum3;
sum1=sum2=sum3=INF;
if((a-b)%3==0)
{
mark=1;
sum1=MAX(a,b);
}
if((a-c)%3==0)
{
mark=1;
sum2=MAX(a,c);
}
if((b-c)%3==0)
{
mark=1;
sum3=MAX(b,c);
}
if(mark==0)
return 0;
else
return MIN(MIN(sum1,sum2),sum3);
}
int main()
{
int y,b,r,sum;
while(cin>>y>>b>>r)
{
sum=f(y,b,r);
if(sum==0)
cout<<"):"<<endl;
else
cout<<sum<<endl;
}
return 0;
}

125
HDOJ/2279_autoAC.cpp Normal file
View File

@ -0,0 +1,125 @@
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
struct node
{
node *next[26];
int count;
bool dangours;
void clear()
{
dangours = false;
memset(next,0,sizeof(next));
count = 0;
}
}Q[200001];
queue<node *> sq;
int current,sum,len;
char s[21];
node *newnode()
{
Q[current].clear();
return &Q[current++];
}
void insert(node *root,char s[])
{
int i = 0,index;
node *temp = root;
while(s[i])
{
index = s[i] - 'a';
if(temp -> next[index] == NULL)
temp -> next[index] = newnode();
temp = temp -> next[index];
i++;
}
temp -> count++;
}
void dfs(int n,node *temp)
{
int i,index;
if(n == len)
{
if(!temp ->dangours)
{
temp -> dangours = true;
sum += (temp -> count);
sq.push(temp);
}
}
if(s[n] >= 'a' &&s[n] <= 'z')
{
if(temp -> next[s[n] - 'a'] != NULL)
{
dfs(n + 1,temp -> next[s[n] - 'a']);
}
}
else if(s[n] == '?')
{
for(i = 0;i < 26;i++)
{
if(temp -> next[i] != NULL)
{
dfs(n + 1,temp -> next[i]);
}
}
}
else if(s[n] == '*')
{
dfs(n + 1,temp);
for(i = 0;i < 26;i++)
{
if(temp ->next[i] != NULL)
{
dfs(n,temp -> next[i]);
}
}
}
}
void orz(char s[])
{
int i;
for(i = 0;s[i];i++)
{
while(s[i] == '*'&&s[i + 1] == '*')
{
for(int j = i;s[j];j++)
{
s[j] = s[j + 1];
}
}
}
}
int main()
{
node *root,*temp;
int n,m;
while(scanf("%d%d",&n,&m) != EOF)
{
current = 0;
root = newnode();
for(int i = 0;i < n;i++)
{
scanf("%s",s);
insert(root,s);
}
for(int i = 0;i < m;i++)
{
sum = 0;
scanf("%s",s);
orz(s);
len = strlen(s);
dfs(0,root);
if(sum)
printf("%d\n",sum);
else
printf("Not match\n");
while(!sq.empty())
{
temp = sq.front();sq.pop();
temp -> dangours = false;
}
}
}
}

74
HDOJ/2280_autoAC.cpp Normal file
View File

@ -0,0 +1,74 @@
#include <cstdio>
using namespace std;
const int INF=110;
int n,m=5;
int d[1010][40];
int mz[1010];
void dfs(int p,int s,int t,int c,int r)
{
if(p>=m)
{
if(c<d[r][t]) d[r][t]=c;
return;
}
if(p<4 && ((s>>p)&1)==0 && ((t>>p)&3)==0)
dfs(p+1,s|(1<<p),t|(3<<p),c,r);
if(p<4 && ((s>>p)&3)==0 && ((t>>p)&3)==0)
dfs(p+1,s|(3<<p),t|(3<<p),c,r);
if(p<4 && ((s>>p)&3)==0)
dfs(p+1,s|(3<<p),t,c,r);
if(((s>>p)&1)==0 && ((t>>p)&1)==0)
dfs(p+1,s|(1<<p),t|(1<<p),c,r);
if(p>0 && (((s>>p)&1)==0) && (t>>(p-1)&3)==0)
dfs(p+1,s|(1<<p),t|(3<<(p-1)),c,r);
if(p<4 && ((s>>p)&3)==0 && ((t>>p)&1)==0)
dfs(p+1,s|(3<<p),t|(1<<p),c,r);
if(p<4 && ((s>>p)&3)==0 && ((t>>p)&2)==0)
dfs(p+1,s|(3<<p),t|(2<<p),c,r);
if(((s>>p)&1)==1) dfs(p+1,s,t,c,r);
else dfs(p+1,s|(1<<p),t,c+1,r);
}
int f(int x)
{
int cnt=0;
x+=32;
for(int i=0;i<5;i++)
{
if((x&1)==1) x>>=1;
else
{
x>>=1;
if((x&1)==1) cnt++;
x>>=1;
i++;
}
}
return cnt;
}
int main()
{
int c;
while(~scanf("%d%d",&n,&c))
{
for(int i=0;i<n;i++)
{
char str[10];
scanf("%s",str);
int s=0;
for(int j=0;j<5;j++) s=(s<<1)+str[j]-'0';
mz[i]=s;
}
for(int i=0;i<n;i++)
for(int j=0;j<32;j++) d[i][j]=INF;
d[0][mz[0]]=0;
for(int i=0;i<n-1;i++)
for(int j=0;j<32;j++) if(d[i][j]<=c)
dfs(0,j,mz[i+1],d[i][j],i+1);
bool flag=false;
for(int i=0;i<32;i++)
if(d[n-1][i]+f(i)<=c) flag=true;
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}

34
HDOJ/2281_autoAC.cpp Normal file
View File

@ -0,0 +1,34 @@
#include<cstdio>
#include<vector>
using namespace std;
typedef __int64 ll;
const ll maxn = 1e18;
ll n[20], x[20];
vector<ll> nn, xx;
int main()
{
int i;
long long N;
n[0] = 7LL, x[0] = 1LL;
nn.push_back(1LL), xx.push_back(1LL);
for (i = 1;; ++i)
{
n[i] = 7LL * n[i - 1] + 48LL * x[i - 1];
x[i] = n[i - 1] + 7LL * x[i - 1];
if (n[i] < 0) break;
if ((n[i] - 3) % 4 == 0) nn.push_back((n[i] - 3) / 4), xx.push_back(x[i]);
}
nn.push_back(maxn + 5);
while (scanf("%I64d", &N), N)
{
for (i = 0; i < nn.size(); ++i)
{
if (N < nn[i])
{
printf("%I64d %I64d\n", nn[i - 1], xx[i - 1]);
break;
}
}
}
return 0;
}

97
HDOJ/2282_autoAC.cpp Normal file
View File

@ -0,0 +1,97 @@
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int maxn = 505;
const int INF = (1<<30)-1;
int g[maxn][maxn];
int lx[maxn],ly[maxn];
int match[maxn];
bool visx[maxn],visy[maxn];
int slack[maxn];
int a[maxn];
int n,m,rn;
bool dfs(int cur){
visx[cur] = true;
for(int y=1;y<=rn;y++){
if(visy[y]) continue;
int t=lx[cur]+ly[y]-g[cur][y];
if(t==0){
visy[y] = true;
if(match[y]==-1||dfs(match[y])){
match[y] = cur;
return true;
}
}
else if(slack[y]>t){
slack[y]=t;
}
}
return false;
}
int KM(){
memset(match,-1,sizeof(match));
memset(ly,0,sizeof(ly));
for(int i=1 ;i<=m;i++){
lx[i]=-INF;
for(int j=1;j<=rn;j++)
if(g[i][j]>lx[i]) lx[i]=g[i][j];
}
for(int x=1;x<=m;x++){
for(int i=1;i<=rn;i++) slack[i]=INF;
while(true){
memset(visx,false,sizeof(visx));
memset(visy,false,sizeof(visy));
if(dfs(x)) break;
int d=INF;
for(int i=1;i<=rn;i++){
if(!visy[i]&&d>slack[i]) d=slack[i];
}
for(int i=1;i<=m;i++){
if(visx[i]) lx[i]-=d;
}
for(int i=1;i<=rn;i++){
if(visy[i]) ly[i]+=d;
else slack[i]-=d;
}
}
}
int result = 0;
for(int i = 1; i <= rn; i++){
if(match[i]>-1){
result += g[match[i]][i];
}
}
return result;
}
int abs(int x){
if(x<0)return -x;
return x;
}
int main(){
int index[maxn];
while(scanf("%d",&n)!=EOF){
if(n==0) break;
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
int i,j,k;
m=0; rn=0;
for(int i=1;i<=n;i++) if(!a[i]){
index[++rn]=i;
}
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++)
g[i][j]=0;
for( i=1;i<=n;i++){
if(a[i]>1){
for( k=1;k<a[i];k++){
m++;
for(j=1;j<=rn;j++)
g[m][j]=-min(abs(index[j]-i),n-abs(index[j]-i));
}
}
}
int ans=-KM();
printf("%d\n",ans);
}
return 0;
}

28
HDOJ/2284_autoAC.cpp Normal file
View File

@ -0,0 +1,28 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int n,m,p;
int t,ans;
while(scanf("%d%d",&n,&p)!=-1)
{
if(p>n) {printf("Where is hero from?\n");continue;}
int count=0;
ans=0;
for(m=1;m<=n;m++)
{
if(m>n/2) break;
t=n-m+1;
while(!(t%p)) {count++;t/=p;}
t=m;
while(!(t%p)) {count--;t/=p;}
if(count<=0) continue;
if(2*m==n) ans++;
else ans+=2;
}
if(!ans) printf("Where is hero from?\n");
else printf("%d\n",ans);
}
return 0;
}

36
HDOJ/2289_autoAC.cpp Normal file
View File

@ -0,0 +1,36 @@
#include<stdio.h>
#include<math.h>
double r, R, H, V;
double volume( double h )
{
double u = r + ( R- r ) * h / H;
return ( 3.1415926535897932384626433832795* h * (u* u + r * u + r * r) / 3.0 );
}
double erfen(double bottom,double top)
{
double mid;
while(top-bottom>1e-10)
{
mid=(top+bottom)/2;
if(volume(mid)>V)
{
top=mid-1e-11;
}
else
{
bottom=mid+1e-11;
}
}
return (bottom+top)/2;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%lf %lf %lf %lf", &r, &R, &H, &V);
printf("%.6lf\n",erfen(0,H));
}
return 0;
}

123
HDOJ/2290_autoAC.cpp Normal file
View File

@ -0,0 +1,123 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <ctime>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <bitset>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <functional>
using namespace std;
typedef long long ll;
const double Pi = acos(-1.0);
const double eps = 1e-6;
const int IntMAX = 0x7fffffff;
const double DoubleMAX = 1e307;
#define TMIN(x,y) ( x<y?x:y )
const ll Inf = 40000000;
int dp[210][210][210];
int mp[210][210];
int dx[210];
int N,M,Q;
struct node
{
int C;
int dx;
friend bool operator<(node A,node B)
{
return A.C<B.C;
}
};
node cops[210];
int T;
int main()
{
int a,b,c;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&N,&M);
for(int i=0;i<N;i++)
{
scanf("%d",&cops[i].C);
cops[i].dx = i;
}
sort(cops,cops+N);
for(int i=0;i<N;i++)
{
dx[cops[i].dx] = i+1;
}
for(int i=1;i<=N;i++)
{
for(int j=1;j<=N;j++)
{
mp[i][j] = (i==j?0:Inf);
}
}
for(int i=0;i<M;i++)
{
scanf("%d %d %d",&a,&b,&c);
mp[dx[a]][dx[b]] = mp[dx[b]][dx[a]] = min(mp[dx[a]][dx[b]],c);
}
for(int i=1;i<=N;i++)
{
for(int j=1;j<=N;j++)
{
dp[i][j][0] = mp[i][j];
}
}
for(int k=1;k<=N;k++)
{
for(int i=1;i<=N;i++)
{
for(int j=1;j<=N;j++)
{
dp[i][j][k] = dp[i][j][k-1];
}
}
for(int i=1;i<=N;i++)
{
for(int j=1;j<=N;j++)
{
if( dp[i][j][k]>dp[i][k][k-1]+dp[k][j][k-1] )
{
dp[i][j][k] = dp[i][k][k-1]+dp[k][j][k-1];
}
}
}
}
scanf("%d",&Q);
while(Q--)
{
scanf("%d %d %d",&a,&b,&c);
a = dx[a];
b = dx[b];
int ans = Inf;
ans = min(ans,dp[a][b][0]);
for(int i=N-1;i>=0;i--)
{
if( cops[i].C<=c )
{
ans = min(ans,dp[a][b][i+1]);
break;
}
}
if( ans==Inf )
printf("-1\n");
else
printf("%d\n",ans);
}
printf("\n");
}
return 0;
}

46
HDOJ/2291_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
int dp[1024 * 8][140];
int w[20][20];
int e[20][20];
int s[20];
int n;
int dfs(int fs,int x)
{
if(dp[fs][x] != -1) return dp[fs][x];
dp[fs][x] = 0;
for(int i = 0;i < n-1;i++)
if( (fs & (1 << i)) == 0)
{
int ss = (fs | (1 << i));
if(x > s[i+1])
dp[fs][x] = max(dp[fs][x],dfs(ss,x+w[0][i+1]) + e[0][i+1]);
else dp[fs][x] = max(dp[fs][x],dfs(ss,x+w[0][i+1]));
}
return dp[fs][x];
}
int main()
{
int t;
cin >> t;
while(t--)
{
cin >> n;
for(int i = 0;i < n;i++)
for(int j = 0;j < n;j++)
cin >> w[i][j];
for(int i = 0;i < n;i++)
for(int j = 0;j < n;j++)
cin >> e[i][j];
for(int i = 0;i < n;i++)
cin >> s[i];
memset(dp,-1,sizeof(dp));
dfs(0,s[0]);
int ma = 0;
cout << dp[0][s[0]] << "\n";
}
return 0;
}

53
HDOJ/2292_autoAC.cpp Normal file
View File

@ -0,0 +1,53 @@
#include <iostream>
using namespace std;
const int maxn=1005;
__int64 n,m;
__int64 cnt[maxn];
int c[maxn][maxn];
int son(int n)
{
int t = 1;
while (t <= n) t=t<<1|1;
t = (t - 1) / 2;
t = (t - 1) / 2;
int sum = n - 1 - t;
if (sum > 2 * t + 1)
{
sum = 2 * t + 1;
}
return sum;
}
void calc_c() {
for (int i = 0; i < maxn; i++)
{
c[i][0] = c[i][i] = 1;
for (int j = 1; j < i; j++)
{
c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % m;
}
}
}
__int64 slove(int n)
{
if(cnt[n])
return cnt[n];
if(n==0||n==1)
return 1;
int left=son(n);
int right=(n-1)-left;
return cnt[n]=( (slove(left)*slove(right) )%m )*(__int64)c[n-1][left]%m;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(cnt,0,sizeof(cnt));
scanf("%I64d%I64d",&n,&m);
calc_c();
__int64 ans=slove(n);
printf("%I64d\n",ans);
}
return 0;
}

60
HDOJ/2293_autoAC.cpp Normal file
View File

@ -0,0 +1,60 @@
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
void solve(int &a,int &b,int &c,int l,string s)
{
int i,j;
a=0,b=1,c=0;
for(i=0;i<l;i++){
a=(a+(83-s[i])*(83-s[i]))%97;
b=(b*s[i])%79;
for(j=i+1;j<l;j++){
c=(c+s[i]*s[j])%11;
}
}
a=300-(a%97);
b=22+(b%79);
c=c%11;
}
int main()
{
int t;
string a,b;
int ahp,bhp;
int astr,bstr;
int aspd,bspd;
scanf("%d",&t);
while(t--)
{
cin>>a>>b;
ahp=bhp;
astr=bstr;
aspd=bspd;
int al=a.length();
int bl=b.length();
solve(ahp,astr,aspd,al,a);
solve(bhp,bstr,bspd,bl,b);
int da,db;
da=ahp/bstr;
if(ahp%bstr!=0)
{
da++;
}
db=bhp/astr;
if(bhp%astr!=0)
{
db++;
}
da *= (20-bspd);
db *= (20-aspd);
if(da==db){
printf("tie\n");
}else if(da<db){
printf("lose\n");
}else{
printf("win\n");
}
}
}

138
HDOJ/2294_autoAC.cpp Normal file
View File

@ -0,0 +1,138 @@
#include <stdio.h>
#include <string.h>
#include <iostream>
#define MAX_DIMENSION 35
using namespace std;
typedef __int64 MATRIX_TYPE;
typedef __int64 MAX_INT_TYPE;
typedef MATRIX_TYPE Matrix[MAX_DIMENSION][MAX_DIMENSION];
int ndim=MAX_DIMENSION;
int mod=1234567891;
void m_zero(Matrix x)
{
memset(x, 0, sizeof(MATRIX_TYPE)*MAX_DIMENSION*MAX_DIMENSION);
}
void m_one(Matrix x)
{
int i;
m_zero(x);
for(i=0;i<ndim;++i)x[i][i]=1;
}
void m_copy(Matrix src,Matrix dest)
{
memcpy(dest,src, sizeof(MATRIX_TYPE)*MAX_DIMENSION*MAX_DIMENSION);
}
void m_add(Matrix x,Matrix y,Matrix z)
{
int i,j;
for(i=0;i<ndim;i++)
for(j=0;j<ndim;j++)
if(mod<=1)z[i][j]=x[i][j]+y[i][j];
else z[i][j]=(x[i][j]+(MAX_INT_TYPE)y[i][j])%mod;//module
}
void m_multiple(Matrix a,Matrix b,Matrix c)
{
int i,j,k;
MAX_INT_ t;
for(i=0;i<ndim;i++)
for(j=0;j<ndim;j++)
{
t=0;
if(mod<=1)
for(k=0;k<ndim;k++) t+=a[i][k]*b[k][j];
else
for(k=0;k<ndim;k++){
t+=(a[i][k]*(MAX_INT_TYPE)b[k][j])%mod;
t%=mod;
}
c[i][j]=t;
}
}
void m_pow_with_saved(Matrix x_p[],unsigned int n, Matrix y)
{
Matrix temp;
m_one(y);
for(int k=1;n;++k,n>>=1)
{
if ((n & 1) != 0)
{
m_multiple(x_p[k],y,temp);
m_copy(temp,y);
}
}
}
void m_pow_sum1(Matrix x_p[],unsigned int n, Matrix y)
{
m_zero(y);
if(n==0)return;
if(n==1) m_copy(x_p[1],y);
else
{
Matrix temp;
m_pow_sum1(x_p,n>>1,temp);
m_add(temp,y,y);
Matrix temp2;
m_pow_with_saved(x_p,n>>1,temp2);
Matrix temp3;
m_multiple(temp,temp2,temp3);
m_add(temp3,y,y);
if(n&1)
{
m_multiple(temp2,temp2,temp3);
m_multiple(temp3,x_p[1],temp2);
m_add(temp2,y,y);
}
}
}
void m_pow_sum(Matrix x, unsigned int n, Matrix y)
{
unsigned int i=0,logn,n2=n;
for(logn=0,n2=n;n2;logn++,n2 >>= 1);
Matrix *pow_arry=new Matrix[logn==0?2:(logn+1)];
m_one(pow_arry[0]);
m_copy(x,pow_arry[1]);
for(i=1;i<logn;i++)
{
m_multiple(pow_arry[i],pow_arry[i],pow_arry[i+1]);
}
m_pow_sum1(pow_arry,n,y);
delete []pow_arry;
}
void view_mat(Matrix a, int n)
{
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
cout << a[i][j] << " ";
cout << endl;
}
}
int main(void)
{
int cas;
scanf("%d", &cas);
while(cas--)
{
int n, k;
scanf("%d %d", &n, &k);
Matrix a;
ndim = k + 1;
m_zero(a);
for(int i = 0; i <= k; i++)
{
if(i == 0)
continue;
a[i][i] = i;
a[i - 1][i] = k - i + 1;
}
Matrix asum, res, addsum, f1, ans;
m_one(res);
m_pow_sum(a, n - 1, asum);
m_add(res, asum, addsum);
m_zero(f1);
f1[0][1] = k;
m_multiple(f1, addsum, ans);
printf("%d\n", ((ans[0][k] % mod) + (__int64)mod) % mod);
}
return 0;
}

124
HDOJ/2295_autoAC.cpp Normal file
View File

@ -0,0 +1,124 @@
# include<stdio.h>
# include<math.h>
# include<string.h>
# define eps 1e-8
# define N 55
# define V 3600
int n,m,K;
int L[V],R[V];
int D[V],U[V];
int C[V];
int S[N],H[N];
int ak,size;
double dis(double x1,double y1,double x2,double y2)
{
return sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
}
void Link(int r,int c)
{
S[c]++;C[size]=c;
U[size]=U[c];D[U[c]]=size;
D[size]=c;U[c]=size;
if(H[r]==-1) H[r]=L[size]=R[size]=size;
else
{
L[size]=L[H[r]];R[L[H[r]]]=size;
R[size]=H[r];L[H[r]]=size;
}
size++;
}
void remove(int c)
{
int i;
for(i=D[c];i!=c;i=D[i])
L[R[i]]=L[i],R[L[i]]=R[i];
}
void resume(int c)
{
int i;
for(i=U[c];i!=c;i=U[i])
L[R[i]]=R[L[i]]=i;
}
int h()
{
int i,j,k,count=0;
bool visit[N];
memset(visit,0,sizeof(visit));
for(i=R[0];i;i=R[i])
{
if(visit[i]) continue;
count++;
visit[i]=1;
for(j=D[i];j!=i;j=D[j])
{
for(k=R[j];k!=j;k=R[k])
visit[C[k]]=1;
}
}
return count;
}
void Dance(int k)
{
int i,j,c,Min,ans;
ans=h();
if(k+ans>K || k+ans>=ak) return;
if(!R[0])
{
if(k<ak) ak=k;
return;
}
for(Min=N,i=R[0];i;i=R[i])
if(S[i]<Min) Min=S[i],c=i;
for(i=D[c];i!=c;i=D[i])
{
remove(i);
for(j=R[i];j!=i;j=R[j])
remove(j);
Dance(k+1);
for(j=L[i];j!=i;j=L[j])
resume(j);
resume(i);
}
return;
}
int main()
{
int i,j,ncase;
double x[N],y[N],x1[N],y1[N];
double left,right,ans,mid;
scanf("%d",&ncase);
while(ncase--)
{
scanf("%d%d%d",&n,&m,&K);
for(i=1;i<=n;i++)
scanf("%lf%lf",&x[i],&y[i]);
for(i=1;i<=m;i++)
scanf("%lf%lf",&x1[i],&y1[i]);
left=0;
right=1416.0;
ans=right;
while(right>=left)
{
for(i=0;i<=n;i++)
{
S[i]=0;
U[i]=D[i]=i;
L[i+1]=i;R[i]=i+1;
}R[n]=0;
memset(H,-1,sizeof(H));
size=n+1;
mid=(left+right)/2;
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
if(mid>=dis(x1[i],y1[i],x[j],y[j])) Link(i,j);
}
ak=N;
Dance(0);
if(ak<=K) {ans=mid<ans?mid:ans;right=mid-eps;}
else left=mid+eps;
}
printf("%.6lf\n",ans);
}
return 0;
}

148
HDOJ/2296_autoAC.cpp Normal file
View File

@ -0,0 +1,148 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<queue>
using namespace std;
struct node{
int next[26];
int fail;
int val;
void init()
{
memset(next,0,sizeof(next));
fail=0;
val=0;
}
}a[1200];
int tot,len;
int dp[55][1200];
char str[55][1200][55];
void chushihua()
{
memset(dp,-1,sizeof(dp));
tot=0;
a[0].init();
}
void insert(char *str,int v)
{
int index,p=0;
for(;*str!='\0';str++)
{
index=*str-'a';
if(a[p].next[index]==0)
{
a[++tot].init();
a[p].next[index]=tot;
}
p=a[p].next[index];
}
a[p].val=v;
}
void build_ac()
{
queue<int>Q;
int p,cur,son,i;
Q.push(0);
while(!Q.empty())
{
p=Q.front();
Q.pop();
for(i=0;i<26;i++)
{
if(a[p].next[i]!=0)
{
son=a[p].next[i];
cur=a[p].fail;
if(p==0)
a[son].fail=0;
else
{
while(cur&&a[cur].next[i]==0)
cur=a[cur].fail;
a[son].fail=a[cur].next[i];
}
if(a[a[son].fail].val)
a[son].val+=a[a[son].fail].val;
Q.push(son);
}
else
a[p].next[i]=a[a[p].fail].next[i];
}
}
}
void solve()
{
int i,j,k,son,max=0;
char shang[55],temp,res[55];
dp[0][0]=0;
strcpy(str[0][0],"\0");
strcpy(res,"\0");
for(i=1;i<=len;i++)
for(j=0;j<=tot;j++)
{
if(dp[i-1][j]==-1)
continue;
strcpy(shang,str[i-1][j]);
for(k=0;k<26;k++)
{
son=a[j].next[k];
temp=k+'a';
shang[i-1]=temp;
shang[i]='\0';
if(dp[i][son]==-1)
{
dp[i][son]=dp[i-1][j]+a[son].val;
strcpy(str[i][son],shang);
}
else
{
if(dp[i][son]<dp[i-1][j]+a[son].val)
{
dp[i][son]=dp[i-1][j]+a[son].val;
strcpy(str[i][son],shang);
}
else if(dp[i][son]==dp[i-1][j]+a[son].val)
{
if(strcmp(str[i][son],shang)>0)
strcpy(str[i][son],shang);
}
}
}
}
for(i=1;i<=len;i++)
for(j=0;j<=tot;j++)
{
if(dp[i][j]>max)
{
max=dp[i][j];
strcpy(res,str[i][j]);
}
else if(dp[i][j]==max&&strcmp(res,str[i][j])>0&&strlen(res)>=strlen(str[i][j]))
strcpy(res,str[i][j]);
}
printf("%s\n",res);
}
int main()
{
char keyword[105][15];
int n,T,val[105],i;
while(scanf("%d",&T)!=EOF)
{
while(T--)
{
chushihua();
scanf("%d%d",&len,&n);
getchar();
for(i=0;i<n;i++)
scanf("%s",keyword[i]);
for(i=0;i<n;i++)
scanf("%d",&val[i]);
for(i=0;i<n;i++)
insert(keyword[i],val[i]);
build_ac();
solve();
}
}
return 0;
}

63
HDOJ/2297_autoAC.cpp Normal file
View File

@ -0,0 +1,63 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
typedef __int64 LL;
const int maxn = 111111;
typedef struct{LL x, y;}Point;
inline LL det(LL x1, LL y1, LL x2, LL y2)
{return x1 * y2 - x2 * y1;}
LL cross(Point a, Point b, Point c)
{return det(b.x -a.x, b.y - a.y, c.x - a.x, c.y - a.y);}
int CompG(const void *a, const void *b)
{
if((*(Point*)a).y - (*(Point*)b).y)
return (*(Point*)a).x - (*(Point*)b).x;
return (*(Point*)a).y - (*(Point*)b).y;
}
LL Graham(Point p[], Point res[], LL n, LL &top)
{
LL len, i;
top = 1;
qsort(p, n, sizeof(Point), CompG);
res[0] = p[0], res[1] = p[1];
for(i = 2; i < n; ++ i)
{
while(top && cross(res[top], res[top - 1], p[i]) <= 0)
-- top;
res[++ top] = p[i];
}
len = top;
res[++ top] = p[n - 2];
for(i = n - 3; i >= 0; -- i)
{
while(top != len && cross(res[top], res[top - 1], p[i]) <= 0)
-- top;
res[++ top] = p[i];
}
return top;
}
Point p[maxn], res[maxn];
LL n, m;
int main()
{
int t, i, j, k, cnt;
LL maxx, maxy;
for(scanf("%d", &t); t -- ; )
{
scanf("%I64d", &n);
for(i = 0; i < n; ++ i)
scanf("%I64d%I64d", &p[i].x, &p[i].y);
p[n] = p[0];
Graham(p, res, n, m);
for(i = maxx = maxy = 0; i < m; ++ i)
{
if(res[i].x > res[maxx].x) maxx = i;
if(res[i].y > res[maxy].y) maxy = i;
}
printf("%I64d\n", (maxx - maxy + n) % n + 1);
}
return 0;
}

61
HDOJ/2298_autoAC.cpp Normal file
View File

@ -0,0 +1,61 @@
#include<stdio.h>
#include<cmath>
double x,y,v;
const double g=9.8;
double f(double a)
{
return x*tan(a)-x*x*g/(2*v*v*cos(a)*cos(a));
}
double sanf(double bottom,double top)
{
double rightthir,leftthir;
while(top-bottom>1e-9)
{
rightthir=(bottom+top*2)/3;
leftthir=(bottom*2+top)/3;
if(f(rightthir)>f(leftthir))
{
bottom=leftthir+1e-10;
}
else
{
top=rightthir-1e-10;
}
}
return (top+bottom)/2;
}
double erfen(double bottom,double top)
{
double mid;
while(top-bottom>1e-9)
{
mid=(top+bottom)/2;
if(f(mid)>y)
{
top=mid-1e-10;
}
else
{
bottom=mid+1e-10;
}
}
return (bottom+top)/2;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%lf%lf%lf",&x,&y,&v);
double max=sanf(0,3.1415926535897932384626433832795/2.0);
if(f(max)<y)
{
printf("-1\n");
}
else
{
printf("%.6lf\n",erfen(0,max));
}
}
}

60
HDOJ/2299_autoAC.cpp Normal file
View File

@ -0,0 +1,60 @@
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
#define PF(x) (scanf("%d",&x))
#define PT(x,y) (scanf("%d%d",&x,&y))
#define PR(x) (printf("%d\n",x))
#define PRT(x,y)(printf("%d %d\n",x,y))
#define M 1000
int n;
struct P
{
int x,y;
void in()
{
PT(x,y);
}
};
P ar[1000];
int mul(P a,P b,P c)
{
return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
double area(P a,P b,P c)
{
return fabs((double)mul(a,b,c))/2.0;
}
void init()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
ar[i].in();
double areas = 0.0;
for(int i=0;i<n;i++)
for(int j = i+1;j<n;j++)
for(int k = j+1;k<n;k++)
{
if(areas<area(ar[i],ar[j],ar[k]))
areas = area(ar[i],ar[j],ar[k]);
}
printf("%.1lf\n",areas);
}
return ;
}
int main()
{
init();
return 0;
}