Powered By HC TECH : AutoACer Engine

2600-2699
This commit is contained in:
KiritoTRw 2016-08-24 18:19:33 +08:00 committed by GitHub
parent 357541214f
commit 7a2a9109f5
65 changed files with 3363 additions and 0 deletions

52
HDOJ/2600_autoAC.cpp Normal file
View File

@ -0,0 +1,52 @@
#include<stdio.h>
#include<string.h>
char hash[12000005];
int main()
{
int N,i,A,B,j,p,q,temp;
char s[100];
while(scanf("%d",&N)!=EOF)
{
memset(hash,0,sizeof(hash));
scanf("%d %d",&p,&q);
if(p>q)
{
temp=p;
p=q;
q=temp;
}
for(i=0;i<N;i++)
{
scanf("%d %d",&A,&B);
if(A>B)
{
temp=A;
A=B;
B=A;
}
for(j=A;j<=B;j++)
{
if(j<0)
hash[-j]++;
else
hash[j]++;
}
gets(s);
}
for(i=q;i>=p;i--)
{
if(i>=0&&hash[i]==0)
{
printf("%d\n",i);
break;
}
if(i<0&&hash[-i]==0)
{
printf("%d\n",i);
break;
}
}
if(i<p)
printf("Badly!\n");
}
}

22
HDOJ/2601_autoAC.cpp Normal file
View File

@ -0,0 +1,22 @@
#include<stdio.h>
#include<math.h>
int main()
{
int n,k;
__int64 i,j,m;
scanf("%d",&n);
while(n--)
{
scanf("%I64d",&m);
m=m+1;
j=sqrt(m);
k=0;
for(i=2;i<=j;i++)
{
if(m%i==0)
k++;
}
printf("%d\n",k);
}
return 0;
}

43
HDOJ/2602_autoAC.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int Max = 1000 + 10;
struct node
{
int value;
int volum;
}a[Max];
int f[Max];
int main()
{
int t;
int n,v;
int i,j;
while(scanf("%d",&t) != EOF)
{
while(t--)
{
scanf("%d %d",&n,&v);
memset(f, 0, sizeof(f));
for(i=1; i<=n; i++)
{
scanf("%d",&a[i].value);
}
for(i=1; i<=n; i++)
{
scanf("%d",&a[i].volum);
}
for(i=1; i<=n; i++)
{
for(j=v; j>=a[i].volum; j--)
{
f[j] = max(f[j], f[j-a[i].volum] + a[i].value);
}
}
cout<<f[v]<<endl;
}
}
return 0;
}

30
HDOJ/2603_autoAC.cpp Normal file
View File

@ -0,0 +1,30 @@
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define PI acos(-1.0)
#define G 9.87
#define H1 3
#define H2 0.5
int main()
{
double v,m,a;
while(~scanf("%lf%lf%lf",&v,&m,&a))
{
double vx=v*cos(a*PI/180);
double vy=v*sin(a*PI/180);
double x,t1,t2,t;
if(vy*vy>G)
{
double t1=2*(vy-sqrt(vy*vy-2*H2*G))/G;
double t2=(sqrt(vy*vy+2*G*H1)-vy)/G;
t=t1+t2;
}
else
{
t=2*vy/G+(sqrt(vy*vy+2*G*H1)-vy)/G;
}
x=t*vx;
printf("%.3lf\n",x);
}
return 0;
}

64
HDOJ/2604_autoAC.cpp Normal file
View File

@ -0,0 +1,64 @@
#include <stdio.h>
#include <string.h>
#define ll long long
#define Max 4
int mod;
struct Matrix
{
int mm[Max][Max];
}A,I;
Matrix matrixmul(Matrix a,Matrix b)
{
int i,j,k;
Matrix c;
for (i = 0 ; i <= 3; i++)
for (j = 0; j <= 3;j++)
{
c.mm[i][j] = 0;
for (k = 0; k <= 3; k++)
c.mm[i][j] += (a.mm[i][k] * b.mm[k][j])%mod;
c.mm[i][j]%=mod;
}
return c;
}
Matrix quickpagow(int n)
{
Matrix m = A, b = I;
while (n >= 1)
{
if (n & 1)
b = matrixmul(b,m);
n = n >> 1;
m = matrixmul(m,m);
}
return b;
}
int main ()
{
int n;
A.mm[0][0]=1,A.mm[0][1]=0,A.mm[0][2]=1,A.mm[0][3]=1;
A.mm[1][0]=1,A.mm[1][1]=0,A.mm[1][2]=0,A.mm[1][3]=0;
A.mm[2][0]=0,A.mm[2][1]=1,A.mm[2][2]=0,A.mm[2][3]=0;
A.mm[3][0]=0,A.mm[3][1]=0,A.mm[3][2]=1,A.mm[3][3]=0;
I.mm[0][0]=1,I.mm[0][1]=0,I.mm[0][2]=0,I.mm[0][3]=0;
I.mm[1][0]=0,I.mm[1][1]=1,I.mm[1][2]=0,I.mm[1][3]=0;
I.mm[2][0]=0,I.mm[2][1]=0,I.mm[2][2]=1,I.mm[2][3]=0;
I.mm[3][0]=0,I.mm[3][1]=0,I.mm[3][2]=0,I.mm[3][3]=1;
while(scanf("%d %d",&n,&mod)!=EOF)
{
if(n==1)
printf("%d\n",2%mod);
else if(n==2)
printf("%d\n",4%mod);
else if(n==3)
printf("%d\n",6%mod);
else if(n==4)
printf("%d\n",9%mod);
else
{
Matrix c=quickpagow(n-4);
printf("%d\n",((c.mm[0][0]*9)%mod+(c.mm[0][1]*6)%mod+(c.mm[0][2]*4)%mod+(c.mm[0][3]*2)%mod)%mod);
}
}
return 0;
}

30
HDOJ/2606_autoAC.cpp Normal file
View File

@ -0,0 +1,30 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int ans[111];
void init()
{
int i,l;
ans[0]=1;
ans[1]=1;
ans[2]=ans[1]+4*ans[0];
ans[3]=ans[2]+4*ans[1]+2*ans[0]+2*ans[0];
for(i=4;i<=100;i++)
{
ans[i]=(ans[i-1]+4*ans[i-2]+2*ans[i-3]+ans[i-4])%19890907;
for(l=3;l<=i;l++) ans[i]=(ans[i]+2*ans[i-l])%19890907;
}
return ;
}
int main()
{
init();
int T,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
printf("%d\n",ans[n]);
}
return 0;
}

38
HDOJ/2607_autoAC.cpp Normal file
View File

@ -0,0 +1,38 @@
#include <stdio.h>
#include <string.h>
#define maxn 100002
int X[maxn], Y[maxn], Z[maxn];
char buf[40];
int main() {
// freopen("data.in", "r", stdin);
int i, id = 0, cnt = 0, ret = 0; // id璁板版涓℃, ret璁板缁缁
while (gets(buf)) {
if (*buf == NULL) {
if (id)
if (ret == 0) puts("None.");
else {
while (id--) {
if (ret >= X[id] && ret <= Y[id] && (ret - X[id]) % Z[id] == 0)
++cnt;
}
printf("%d %d\n", ret, cnt);
}
id = cnt = ret = 0;
continue;
}
sscanf(buf, "%d%d%d", &X[id], &Y[id], &Z[id]);
for (i = X[id]; i <= Y[id]; i += Z[id])
ret ^= i;
++id;
}
if (id)
if (ret == 0) puts("None.");
else {
while (id--) {
if (ret >= X[id] && ret <= Y[id] && (ret - X[id]) % Z[id] == 0)
++cnt;
}
printf("%d %d\n", ret, cnt);
}
return 0;
}

27
HDOJ/2608_autoAC.cpp Normal file
View File

@ -0,0 +1,27 @@
#include <stdio.h>
int main()
{
int n,k,i,num;
scanf("%d",&n);
while(n--)
{
num=0;
scanf("%d",&k);
for(i=1;i<=k;i++)
{
if(i*i<=k)
num++;
else
break;
}
for(i=1;i<=k;i++)
{
if(2*i*i<=k)
num++;
else
break;
}
printf("%d\n",num%2);
}
return 0;
}

99
HDOJ/2609_autoAC.cpp Normal file
View File

@ -0,0 +1,99 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
typedef struct TNode{
struct TNode *left,*right;
int shit;
}Node;
Node p;
Node *root=&p;
Node *newnode()
{
Node *u=(Node*) malloc(sizeof(Node));
u->left=NULL;
u->right=NULL;
u->shit=0;
return u;
}
int addnode(char *s,int n)
{
int fuck=0,i;
Node *u=root;
for(i=0;i<n;i++)
{
if(s[i]=='0')
{
if(u->left==NULL)
u->left=newnode();
u=u->left;
if(u->shit==0) {fuck=1;u->shit=1;}
}
else if(s[i]=='1')
{
if(u->right==NULL)
u->right=newnode();
u=u->right;
if(u->shit==0) {fuck=1;u->shit=1;}
}
}
return fuck;
}
int min(int x,int y)
{
if(x<=y)
return x;
return y;
}
int huiliyi(char *s,int n)
{
int i,j,k,t;
i=0;j=1;k=0;
while(i<n&&j<n&&k<n)
{
t=s[(i+k)%n]-s[(j+k)%n];
if(t==0) ++k;
else
{
if(t<0) j=j+k+1;
else
i=i+k+1;
k=0;
if(i==j) ++j;
}
}
return min(i,j);
}
void remove(Node *u)
{
if(u==NULL) return;
remove(u->left);
remove(u->right);
u->shit=0;
}
int main()
{
int t,sum,n,x,i,j;
char s[105],c[105];
while(scanf("%d",&t)==1)
{
getchar();
sum=0;
while(t--)
{
scanf("%s",s);
n=strlen(s);
x=huiliyi(s,n);
j=0;
for(i=x;i<n;i++)
c[j++]=s[i];
for(i=0;i<x;i++)
c[j++]=s[i];
sum=sum+addnode(c,n);
}
printf("%d\n",sum);
remove(root->left);
remove(root->right);
}
return 0;
}

68
HDOJ/2610_autoAC.cpp Normal file
View File

@ -0,0 +1,68 @@
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
int n,p,len,count_num;
int num[1001];
bool flag;
typedef struct
{
int n,pos;
}Tem;
Tem tem[1001];
bool check(int s,int e)
{
for(int i = s+1; i < e; i++)
if(num[i]==num[e])return false;
return true;
}
void print_sequence(int length)
{
for(int i = 0; i < length-1;i++)
cout<<tem[i].n<<" ";
cout<<tem[length-1].n<<endl;
}
void dfs(int dep,int pos)
{
if(count_num >= p)return;
if(dep==len)
{
count_num++;
flag = true;
print_sequence(len);
return;
}
for(int i=pos;i<n;i++)
{
if((dep!=0&&tem[dep-1].n<=num[i])||dep==0)
{
if(dep==0&&!check(-1,i))
continue;
if(dep!=0&&!check(tem[dep-1].pos,i))
continue;
tem[dep].n = num[i];
tem[dep].pos = i;
dfs(dep+1,i+1);
}
}
return;
}
int main()
{
while(cin>>n>>p)
{
for(int i=0;i<n;i++)
cin>>num[i];
count_num = 0;
for(int i = 1;i < n;i++)
{
flag=false;
len = i;
dfs(0,0);
if(count_num>=p||!flag)break;
}
cout<<endl;
}
return 0;
}

84
HDOJ/2611_autoAC.cpp Normal file
View File

@ -0,0 +1,84 @@
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
int a[1001];
int n,p,len,tmp;
struct node {
int num;
int pos;
};
node tree[1001];
bool cmp(const node& x,const node& y)
{
if(x.num!=y.num)
return x.num<y.num;
else
return x.pos<y.pos;
}
void print(int x)
{
int i;
for(i=0;i<x-1;i++)
printf("%d ",a[i]);
printf("%d\n",a[i]);
}
bool dfs(int deep,int pos,int pp)
{
int pre,i;
bool flag=false;
if(deep == len)
{
tmp++;
print(deep);
if(tmp == p)
return true;
return false;
}
if(pos>n)
return false;
for(i=pos;i<n;i++)
{
if(tree[i].pos > pp )
{
if(!flag)
{
pre=tree[i].num;
flag=true;
}
else if(pre == tree[i].num)
continue;
pre=tree[i].num;
a[deep]=tree[i].num;
if(dfs(deep+1,i+1,tree[i].pos))
return true;
}
}
return false;
}
int main()
{
while(~scanf("%d%d",&n,&p))
{
int tc;
for(int i=0;i<n;i++)
{
scanf("%d",&tc);
tree[i].num=tc;
tree[i].pos=i;
}
sort(tree,tree+n,cmp);
tmp=0;
for(int i=1;i<n;i++)
{
len=i;
if(dfs(0,0,-1))
break;
}
printf("\n");
}
return 0;
}

113
HDOJ/2612_autoAC.cpp Normal file
View File

@ -0,0 +1,113 @@
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
struct node
{
int x,y;
int time;
};
int n,m;
queue<node> qq;
char tm[205][205];
char vis[205][205];
int kfc[2005][2];
int ans1[505][505];
int ans2[505][505];
int ans[505][505][3];
int dir[][2]={0,-1, 0,1, -1,0, 1,0};
int y11,y2;
int m1,m2;
int bfs(int f,int c)
{
while(!qq.empty()) qq.pop();
int i;
node tmp;
tmp.x=f;
tmp.y=c;
tmp.time=0;
qq.push(tmp);
vis[f][c]=1;
while(!qq.empty())
{
node t=qq.front();
qq.pop();
node k=t;
for (i=0;i<4;i++)
{
t=k;
t.x+=dir[i][0];
t.y+=dir[i][1];
if ((tm[t.x][t.y]=='.'||tm[t.x][t.y]=='@')&&vis[t.x][t.y]==0&& t.x>=1 &&t.y<=m&&t.y>=1&&t.x<=n)
{
vis[t.x][t.y]=1;
t.time++;
{
if ( f==y11 && c==y2 )
{
ans[t.x][t.y][0]=t.time;
}
else
{
ans[t.x][t.y][1]=t.time;
}
}
qq.push(t);
}
}
}
return 0;
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
getchar();
int ok=0;
for (i=1;i<=n;i++)
{
for (j=1;j<=m;j++)
{
scanf("%c",&tm[i][j]);
if (tm[i][j]=='Y')
{
y11=i;
y2=j;
}
if (tm[i][j]=='M')
{
m1=i;
m2=j;
}
if (tm[i][j]=='@')
{
kfc[++ok][0]=i;
kfc[ok][1]=j;
}
}
getchar();
}
int max=0x7f7f7f7f;
int h;
memset(vis,0,sizeof(vis));
bfs(y11,y2);
memset(vis,0,sizeof(vis));
bfs(m1,m2);
for (h=1;h<=ok;h++)
{
int q1=kfc[h][0];
int q2=kfc[h][1];
max=__min(ans[q1][q2][0]+ans[q1][q2][1],max);
}
__int64 ans=max*11;
printf("%I64d\n",ans);
}
return 0;
}

40
HDOJ/2614_autoAC.cpp Normal file
View File

@ -0,0 +1,40 @@
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
int flag[16];
int map[16][16],sum,n;
int max(int a,int b)
{
return a>b?a:b;
}
void dfs(int th,int num,int time)
{
for(int i=0;i<n;i++)
{
if(flag[i] || map[th][i]<time) continue;
flag[i]=1;
dfs(i,num+1,map[th][i]);
flag[i]=0;
}
sum=max(sum,num);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
scanf("%d",&map[i][j]);
}
}
memset(flag,0,sizeof(flag));
sum=0;
flag[0]=1;
dfs(0,1,0);
printf("%d\n",sum);
}
return 0;
}

36
HDOJ/2616_autoAC.cpp Normal file
View File

@ -0,0 +1,36 @@
#include<stdio.h>
int n,m,min;
int ai[10],mi[10],used[10];
int dfs(int dep,int hp)
{
int i;
if(hp<=0)
{
if(dep-1<min) min=dep-1;
return 0;
}
if(dep>=min) return 0;
for(i=1;i<=n;i++)
if(used[i]==0)
{
used[i]=1;
if(hp<=mi[i])
dfs(dep+1,hp-2*ai[i]);
else
dfs(dep+1,hp-ai[i]);
used[i]=0;
}
}
int main()
{
int i;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=1;i<=n;i++) scanf("%d%d",&ai[i],&mi[i]);
for(i=1;i<=n;i++) used[i]=0;
min=n+1;
dfs(1,m);
if(min==n+1) min=-1;
printf("%d\n",min);
}
}

50
HDOJ/2617_autoAC.cpp Normal file
View File

@ -0,0 +1,50 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<stack>
#include <iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define M 100005
char a[M];
int main()
{
int i,l,t,k;
int ji[30];
while(gets(a))
{
memset(ji,0,sizeof(ji));
l=strlen(a);t=0;
for(i=0;i<l;i++)
{
if(a[i]=='h')
t++;
k=a[i]-'a';
if(k==0)
{
if(ji[k]<t)
ji[k]++;
}
if(k==15)
{
if(ji[k]<2*t&&ji[k]<ji[0]*2)
ji[k]++;
}
if(k==24)
{
if(ji[k]<t&&ji[k]<ji[0]&&ji[k]<ji[15]/2)
ji[k]++;
}
}
if(ji[0]<t)
t=ji[0];
if(ji[15]/2<t)
t=ji[15]/2;
if(ji[24]<t)
t=ji[24];
printf("%d\n",t);
}
return 0;
}

61
HDOJ/2619_autoAC.cpp Normal file
View File

@ -0,0 +1,61 @@
#include <stack>
#include <cstdio>
#include <list>
#include <cassert>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <cmath>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define X first
#define Y second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int MAXN = 1e7 + 2;
const int MOD = 1e9 + 7;
const int dir[][2] = { {-1, 0}, {0, -1}, { 1, 0 }, { 0, 1 } };
int cases = 0;
typedef pair<int, int> pii;
LL get_phi(int n)
{
int m = (int)sqrt(n+0.5);
int ans = n;
for (int i = 2; i <= m; i++) if (n % i == 0)
{
ans = ans / i * (i-1);
while (n % i == 0) n /= i;
}
if (n > 1) ans = ans / n * (n-1);
return ans;
}
int main()
{
int n;
while (~scanf("%d", &n))
{
printf("%d\n", get_phi(get_phi(n)));
}
return 0;
}

20
HDOJ/2620_autoAC.cpp Normal file
View File

@ -0,0 +1,20 @@
#include <cstdio>
using namespace std;
typedef long long LL;
int main()
{
LL n,k;
while(~scanf("%I64d%I64d",&n,&k)){
LL ans=n*k;
if(n>k) n=k;
for(LL i=1;i<=n;){
LL d=k/i;
LL j=k/d;
if(j>n) j=n;
ans-=d*(i+j)*(j-i+1)/2;
i=j+1;
}
printf("%I64d\n",ans);
}
return 0;
}

24
HDOJ/2621_autoAC.cpp Normal file
View File

@ -0,0 +1,24 @@
#include<stdio.h>
#include<string.h>
__int64 C(__int64 n, __int64 m)
{
__int64 i, j;
if (m > n/2) m = n - m;
for (i = 1, j = 1;i <= m; i++)
{
j = j * (n + 1 - i) / i;
}
return j;
}
int main ()
{
int i,j,t;
__int64 k1,k2,k;
__int64 m,n;
while(scanf("%I64d%I64d",&n,&m)!=EOF&&n||m)
{
t=m>n?n:m;
printf("%I64d\n",C(m+n,t));
}
return 1;
}

35
HDOJ/2626_autoAC.cpp Normal file
View File

@ -0,0 +1,35 @@
#include<stdio.h>
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
printf("Samuel will ");
switch(n)
{
case 13:printf("take test.\n");break;
case 9:printf("buy food.\n");break;
case 5:printf("meet friends.\n");break;
case 1:printf("decorate.\n");break;
case -3:printf("visit relative.\n");break;
case 12:printf("go home.\n");break;
case 8:printf("stay at home.\n");break;
case 4:printf("go to movies.\n");break;
case 0:printf("go out.\n");break;
case -4:printf("visit relative.\n");break;
case 11:printf("stay at home.\n");break;
case 7:printf("visit teachers.\n");break;
case 3:printf("stay at home.\n");break;
case -1:printf("be in the park.\n");break;
case -5:printf("stay at home.\n");break;
case 10:printf("buy clothes.\n");break;
case 6:printf("go to KTV.\n");break;
case 2:printf("decorate.\n");break;
case -2:printf("stay at home.\n");break;
case -6:printf("prepare dinner.\n");break;
}
}
return 0;
}

37
HDOJ/2627_autoAC.cpp Normal file
View File

@ -0,0 +1,37 @@
#include<stdio.h>
int main()
{
int t,n,i;
char a[100];
int b[100];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
getchar();
scanf("%s",a);
int sum=0,p=0;
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
if(a[i]=='1')
{
p++;
sum+=b[i];
}
}
printf("Samuel was accused with %d case(s),",p);
if(sum==0)printf("and he will stay in the prison for 0 year(s).\n");
else if(sum>0&&sum<20)printf("and he will stay in the prison for 3 year(s).\n");
else if(sum>=20&&sum<30)printf("and he will stay in the prison for 5 year(s).\n");
else if(sum>=30&&sum<40)printf("and he will stay in the prison for 10 year(s).\n");
else if(sum>=40&&sum<50)printf("and he will stay in the prison for 15 year(s).\n");
else if(sum>=50&&sum<60)printf("and he will stay in the prison for 20 year(s).\n");
else if(sum>=60&&sum<70)printf("and he will stay in the prison for 25 year(s).\n");
else if(sum>=70&&sum<80)printf("and he will stay in the prison for 30 year(s).\n");
else if(sum>=80&&sum<90)printf("and he will stay in the prison for 35 year(s).\n");
else
printf("and he will stay in the prison for 40 year(s).\n");
}
return 0;
}

16
HDOJ/2628_autoAC.cpp Normal file
View File

@ -0,0 +1,16 @@
#include<stdio.h>
int main()
{
int t;
double n,m;
scanf("%d",&t);
while(t--)
{
scanf("%lf%lf",&n,&m);
if(n*52>=m)
printf("Oh,Samuel's hope will come true,thanks to the DcSLs.\n");
else
printf("Unfortunately,Samuel's hope will not come true,more tourists are welcomed!\n");
}
return 0;
}

36
HDOJ/2629_autoAC.cpp Normal file
View File

@ -0,0 +1,36 @@
#include <stdio.h>
#include <string.h>
int main()
{
int n;
int s[20],i,len;
char str[20];
scanf("%d%*c",&n);
while(n--)
{
gets(str);
len = strlen(str);
for(i = 0;i<len;i++)
{
s[i+1] = str[i]-48;
}
printf("He/She is from ");
if(s[1] == 3 && s[2] == 3)
printf("Zhejiang");
else if(s[1] == 1 && s[2] == 1)
printf("Beijing");
else if(s[1] == 7 && s[2] == 1)
printf("Taiwan");
else if(s[1] == 8 && s[2] == 1)
printf("Hong Kong");
else if(s[1] == 8 && s[2] == 2)
printf("Macao");
else if(s[1] == 2 && s[2] == 1)
printf("Liaoning");
else if(s[1] == 5 && s[2] == 4)
printf("Tibet");
else if(s[1] == 3 && s[2] == 1)
printf("Shanghai");
printf(",and his/her birthday is on %d%d,%d%d,%d%d%d%d based on the table.\n",s[11],s[12],s[13],s[14],s[7],s[8],s[9],s[10]);
}
}

20
HDOJ/2630_autoAC.cpp Normal file
View File

@ -0,0 +1,20 @@
#include<stdio.h>
#include<string.h>
char name[6][20]={"S.H.E","Jay","Elva","Jolin","Aska","Yoga"},temp[20];
int main(){
int T,n,s,h,m,h1,h2;scanf("%d",&T);
while(T--){
scanf("%d",&n);
s=m=0;
while(n--)
{
scanf("%s %d:%d",temp,&h1,&h2);
for(int i=0;i<6;i++)
if(strcmp(temp,name[i])==0) m+=h1,s+=h2;
}
m+=s/60;s%=60;
h=m/60;m%=60;
printf("Samuel will watch Channel[V] for %d hour(s),%d minute(s),%d second(s).\n",h,m,s);
}
return 0;
}

24
HDOJ/2631_autoAC.cpp Normal file
View File

@ -0,0 +1,24 @@
#include<stdio.h>
int main()
{
int t,h1,m1,h2,m2,n,t1,dh,flag;
char c;
scanf("%d",&t);
while(t--&&scanf("%d:%d-%d:%d",&h1,&m1,&h2,&m2))
{
flag=1;
if(h1-12<0)flag=0;
if(h2<h1)h2+=24;
dh=h2-h1;
t1=60*dh+m2-m1;
if(t1<420||t1>480)flag=0;
scanf("%d%*c",&n);
if(n<8)flag=0;
scanf("%c%*c",&c);
if(c!='T')flag=0;
scanf("%c%*c",&c);
if(c!='T')flag=0;
if(flag)printf("Health life,NeverGone you should keep doing!\n");
else printf("NeverGone should pay attention to his health!\n");
}
}

24
HDOJ/2632_autoAC.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#define N 1000010
#define esp 1e-10
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
double x,a,b;
scanf("%lf%lf%lf",&x,&a,&b);
int ans=0;
while(x*(1-b)>=a+esp)
{
ans++;
x*=(1-b);
}
printf("Samuel will change the appearance for %d time(s),before he decide to sell it.\n",ans);
}
return 0;
}

17
HDOJ/2634_autoAC.cpp Normal file
View File

@ -0,0 +1,17 @@
#include <stdio.h>
int main()
{
double a,sum;
int n, i,ca;
scanf("%d", &ca);
while (ca--) {
scanf("%d", &n);
sum = 0;
for (i = 0; i < n; ++i) {
scanf("%lf", &a);
sum += a;
}
printf("The average M = %.10lf.\n", sum / n);
}
return 0;
}

39
HDOJ/2635_autoAC.cpp Normal file
View File

@ -0,0 +1,39 @@
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct ss
{
char a[100];
int r,x,y,flag;
}s[10000];
int main()
{
int t,n,i,k1,k2,k3,max;
scanf("%d",&t);
while(t--&&scanf("%d",&n))
{
for(i=0;i<n;i++)
{
scanf("%s %d.%d.%d",s[i].a,&s[i].r,&s[i].x,&s[i].y);
if(s[i].x&1) s[i].flag=0;
else s[i].flag=1;
}
max=0;
for(i=0;i<n;i++)
{
if(s[i].flag&&s[i].r>max){max=s[i].r;}
}
k1=max;max=0;
for(i=0;i<n;i++)
{
if(s[i].flag&&s[i].r==k1&&max<s[i].x){max=s[i].x;}
}
k2=max;max=0;
for(i=0;i<n;i++)
{
if(s[i].r==k1&&s[i].x==k2&&s[i].y>max){k3=i;max=s[i].y;}
}
printf("The latest distribution Samuel will choose is %s.\n",s[k3].a);
}
return 0;
}

15
HDOJ/2637_autoAC.cpp Normal file
View File

@ -0,0 +1,15 @@
#include <stdio.h>
int main()
{
int i,t,k,a,b,n;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&a,&b);
k=1;
for(i=0;i<n;i++)
k*=(b-i);
printf("%d\n",a*k);
}
return 0;
}

166
HDOJ/2638_autoAC.cpp Normal file
View File

@ -0,0 +1,166 @@
#include<stdio.h>
#include<string.h>
int main()
{
char nek[1000],bf[1000];
int i,j,l,k;
int as,bs,c1,c2;
int ap,bp;
int max;
int t;
while(scanf("%d",&t)!=EOF)
{
while(t--)
{
max=0;
as=0;
bs=1;
scanf("%s",nek);
strcpy(bf,nek);
l=strlen(nek);
while(bs<l)
{
ap=bp=0;
c1=0;
c2=0;
for(i=as;;i--)
{
if(i<0)
{
i=l-1;
}
if(nek[i]=='a')
break;
if(nek[i]=='w')
{
nek[i]='a';
c1++;
}
if(nek[i]=='p')
{
if(ap==0||ap==1)
{
nek[i]='a';
ap=1;
c1++;
}else
break;
}
if(nek[i]=='r')
{
if(ap==0||ap==2)
{
nek[i]='a';
ap=2;
c1++;
}else
break;
}
}
for(j=bs;;j++)
{
if(j==l)
{
j=0;
}
if(nek[j]=='a')
break;
if(nek[j]=='w')
{
nek[j]=='a';
c2++;
}
if(nek[j]=='p')
{
if(bp==0||bp==1)
{nek[j]=='a';
bp=1;
c2++;
}
else
break;
}
if(nek[j]=='r')
{
if(bp==0||bp==2)
{nek[j]=='a';
bp=2;
c2++;
}
else
break;
}
}
if(max<c1+c2)
{
max=c1+c2;
}
strcpy(nek,bf);
c1=c2=0;
ap=bp=0;
as++;
bs++;
}
c1=c2=0;
ap=bp=0;
as=bs=1;
for(i=l-1,j=0;i>j;i--,j++)
{
if(nek[i]=='w'&&as)
{
c1++;
}
if(nek[j]=='w'&&bs)
{
c2++;
}
if(nek[i]=='p'&&as)
{
if(ap==0||ap==1)
{
ap=1;
c1++;
}else
{as=0;
}
}
if(nek[j]=='p'&&bs)
{
if(bp==0||bp==1)
{
bp=1;
c2++;
}else
{
bs=0;
}
}
if(nek[i]=='r'&&as)
{
if(ap==0||ap==2)
{
ap=2;
c1++;
}else
as=0;
}
if(nek[j]=='r'&&bs)
{
if(bp==0||bp==2)
{
bp=2;
c2++;
}else
{
bs=0;
}
}
}
if(max<c1+c2)
{
max=c1+c2;
}
printf("%d\n",max);
}
}
}

65
HDOJ/2639_autoAC.cpp Normal file
View File

@ -0,0 +1,65 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAXN 105
#define MAXV 1005
int MyMax(int a,int b)
{
return (a>b?a:b);
}
int main()
{
int t,n,v,m;
while(scanf("%d",&t)!=EOF)
{
while(t--)
{
int i,j,k,tag;
int val[MAXN],vol[MAXN],dp[MAXV][35];
int a[35],b[35];
scanf("%d%d%d",&n,&v,&m);
for(i=1;i<=n;i++)
{
scanf("%d",&val[i]);
}
for(i=1;i<=n;i++)
{
scanf("%d",&vol[i]);
}
memset(dp,0,sizeof(dp));
tag=1;
for(i=1;i<=n;i++)
{
for(j=v;j>=vol[i];j--)
{
for(k=1;k<=m;k++)
{
a[k]=dp[j-vol[i]][k]+val[i];
b[k]=dp[j][k];
}
int x,y,z;
x=y=z=1;
a[k]=b[k]=-1;
while(z<=m&&(y<=m||x<=m))
{
if(a[x]>b[y])
{
dp[j][z]=a[x++];
}
else
{
dp[j][z]=b[y++];
}
if(dp[j][z]!=dp[j][z-1])
{
z++;
}
}
}
}
printf("%d\n",dp[v][m]);
}
}
return (0);
}

3
HDOJ/2640_autoAC.cpp Normal file
View File

@ -0,0 +1,3 @@
@
@@@
@

81
HDOJ/2642_autoAC.cpp Normal file
View File

@ -0,0 +1,81 @@
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
const int N=1010;
int c[N][N];
bool vis[N][N];
int lowbit(int x)
{
return x&(-x);
}
void add(int x,int y,int val)
{
int i=y;
while(x<=N)
{
y=i;
while(y<=N)
{
c[x][y]+=val;
y+=lowbit(y);
}
x+=lowbit(x);
}
}
int sum(int x,int y)
{
int i,sum=0;
i=y;
while(x)
{
y=i;
while(y)
{
sum+=c[x][y];
y-=lowbit(y);
}
x-=lowbit(x);
}
return sum;
}
int main()
{
int n,x1,x2,y1,y2;
scanf("%d",&n);
memset(c,0,sizeof(c));
memset(vis,0,sizeof(vis));
while(n--)
{
getchar();
char c;
scanf("%c ",&c);
if(c=='Q')
{
scanf("%d%d%d%d",&x1,&x2,&y1,&y2);
x1++,x2++,y1++,y2++;
if(x1>x2)swap(x1,x2);
if(y1>y2)swap(y1,y2);
printf("%d\n",(sum(x2,y2)+sum(x1-1,y1-1)-sum(x1-1,y2)-sum(x2,y1-1)));
}
else if(c=='B')
{
scanf("%d%d",&x1,&y1);
if(vis[x1+1][y1+1]==1)
continue;
else
add(x1+1,y1+1,1);
vis[x1+1][y1+1]=1;
}
else if(c=='D')
{
scanf("%d%d",&x1,&y1);
if(vis[x1+1][y1+1]==0)
continue;
else
add(x1+1,y1+1,-1);
vis[x1+1][y1+1]=0;
}
}
return 0;
}

46
HDOJ/2643_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include<iostream>
using namespace std;
const int N=110;
__int64 dp[N][N],fact[N];
__int64 bell[N];
const int mod=20090126;
void init()
{
int i,j;
fact[0]=1;
for(i=1;i<=100;i++)
{
fact[i]=fact[i-1]*i%mod;
}
for(i=0;i<=100;i++) dp[i][1]=dp[i][i]=1;
for(i=2;i<=100;i++) //绗浜绫
{
for(j=1;j<i;j++)
{
dp[i][j]=(dp[i-1][j-1]+j*dp[i-1][j])%mod;
}
}
__int64 s;
for(i=1;i<=100;i++)
{
s=0;
for(j=1;j<=i;j++)
{
s+=dp[i][j]*fact[j];
s%=mod;
}
bell[i]=s;
}
}
int main()
{
init();
int t,n;
cin>>t;
while(t--)
{
cin>>n;
printf("%I64d\n",bell[n]);
}
return 0;
}

65
HDOJ/2645_autoAC.cpp Normal file
View File

@ -0,0 +1,65 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#define N 333
using namespace std;
int n,m;
char map[N][N];
int dis[N][N],flag[N][N];
int dir[4][2]={1,0, -1,0, 0,1, 0,-1};
struct node
{
int x,y;
};
void solve()
{
queue<node>q;
node now,next;
memset(dis,0,sizeof(dis));
memset(flag,0,sizeof(flag));
int i,l;
for(i=0;i<n;i++)
{
scanf("%s",map[i]);
for(l=0;l<m;l++)
{
if(map[i][l]=='1')
{
now.x=i,now.y=l;
q.push(now);
flag[i][l]=1;
}
}
}
while(!q.empty())
{
now=q.front();
q.pop();
for(i=0;i<4;i++)
{
next.x=now.x+dir[i][0];
next.y=now.y+dir[i][1];
if(next.x<0 || next.x>=n || next.y<0 || next.y>=m) continue;
if(flag[next.x][next.y]) continue;
q.push(next);
flag[next.x][next.y]=1;
dis[next.x][next.y]=dis[now.x][now.y]+1;
}
}
}
int main()
{
int i,l;
while(scanf("%d%d",&n,&m)!=-1)
{
solve();
for(i=0;i<n;i++)
{
printf("%d",dis[i][0]);
for(l=1;l<m;l++) printf(" %d",dis[i][l]);
printf("\n");
}
}
return 0;
}

66
HDOJ/2647_autoAC.cpp Normal file
View File

@ -0,0 +1,66 @@
#include<stdio.h>
#include<string.h>
struct A
{
int ts;
int outdegree;
int total;
int mem[15];
}E[10011];
int max(int a,int b)
{
return a>b?a:b;
}
int topsort(int n)
{
int k,i,j;
int flag;
int temp;
k=0;
while(k<n)
{
flag=0;
for(i=1;i<=n;i++)
{
if(E[i].outdegree==0)
{
E[i].outdegree--;
temp=E[i].ts+1;
for(j=0;j<E[i].total;j++)
{
E[E[i].mem[j]].outdegree--;
E[E[i].mem[j]].ts=max(temp,E[E[i].mem[j]].ts);
}
flag=1;
k++;
}
}
if(flag==0) return 1;
}
return 0;
}
int main()
{
int n,m;
int i;
int a,b;
int flag;
int ans;
while(scanf("%d%d",&n,&m)!=-1)
{
for(i=1;i<=n;i++) E[i].ts=E[i].total=E[i].outdegree=0;
while(m--)
{
scanf("%d%d",&a,&b);
E[a].outdegree++;
E[b].mem[E[b].total++]=a;
}
flag=topsort(n);
if(flag) {printf("-1\n");continue;}
ans=0;
for(i=1;i<=n;i++) ans+=E[i].ts;
ans+=888*n;
printf("%d\n",ans);
}
return 0;
}

69
HDOJ/2648_autoAC.cpp Normal file
View File

@ -0,0 +1,69 @@
#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
using namespace std;
typedef struct shop
{
int price;
char name[35];
}shop;
shop a[10005];
shop M;
bool cmp(shop b,shop c)
{
return strcmp(b.name,c.name)>0;
}
int erfen(int n,char s[])
{
int low=1,high=n,t;
while(low<=high)
{
t=(low+high)/2;
if(strcmp(s,a[t].name)==0)
break;
else if(strcmp(s,a[t].name)>0)
high=t-1;
else
low=t+1;
}
return t;
}
int main()
{
int n,m,count;
int i,j;
char s[35];
int price,tem;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
{
scanf("%s",a[i].name);
a[i].price=0;
}
M.price=0;
scanf("%d",&m);
sort(a+1,a+n+1,cmp);
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d%s",&price,s);
if(strcmp(s,"memory")==0)
{
M.price+=price;
}
tem=erfen(n,s);
a[tem].price+=price;
}
count=0;
for(j=1;j<=n;j++)
{
if(a[j].price>M.price)
count++;
}
printf("%d\n",count+1);
}
}
}

82
HDOJ/2650_autoAC.cpp Normal file
View File

@ -0,0 +1,82 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
const int Times=10;
using namespace std;
typedef long long LL;
LL multi(LL a,LL b,LL m)
{
LL ans=0;
while(b)
{
if(b&1)
{
ans=(ans+a)%m;
b--;
}
b>>=1;
a=(a+a)%m;
}
return ans;
}
LL quick_mod(LL a,LL b,LL m)
{
LL ans=1;
a%=m;
while(b)
{
if(b&1)
{
ans=multi(ans,a,m);
b--;
}
b>>=1;
a=multi(a,a,m);
}
return ans;
}
bool Miller_Rabin(LL n)
{
if(n==2) return true;
if(n<2||!(n&1)) return false;
LL a,m=n-1,x,y;
int k=0;
while((m&1)==0)
{
k++;
m>>=1;
}
for(int i=0;i<Times;i++)
{
a=rand()%(n-1)+1;
x=quick_mod(a,m,n);
for(int j=0;j<k;j++)
{
y=multi(x,x,n);
if(y==1&&x!=1&&x!=n-1) return false;
x=y;
}
if(y!=1) return false;
}
return true;
}
int main()
{
LL a,b;
while(~scanf("%I64d%I64d",&a,&b))
{
if(a==0)
{
if(b%4==3&&Miller_Rabin(b)) puts("Yes");
else puts("No");
}
else
{
LL t=a*a+2*b*b;
if(Miller_Rabin(t)) puts("Yes");
else puts("No");
}
}
return 0;
}

71
HDOJ/2651_autoAC.cpp Normal file
View File

@ -0,0 +1,71 @@
#include <iostream>
#include <string.h>
using namespace std;
int sum=0;
struct TireTree
{
int num;
TireTree *next[9];
};
int Change(int a[],char str[])
{
int i,k=0,len=strlen(str),t=1;
for(i=1;i<len;++i)
{
if(str[i]!=str[i-1])
{ a[k++]=t; t=1;}
else if(str[i]==str[i-1])
t++;
}
a[k]=t;
return k;
}
void Insert(TireTree *root,int a[],int k)
{
TireTree *current=root;
int i=0;
while(i<=k)
{
if(current->next[a[i]]!=NULL)
current=current->next[a[i]];
else
{
TireTree *NewNode=new TireTree;
NewNode->num=0;
for(int j=0;j<9;j++) NewNode->next[j]=NULL;
current->next[a[i]]=NewNode;
current=NewNode;
}
i++;
}
current->num++;
}
void GetSum(TireTree *root)
{
int temp=root->num;
sum+=(temp)*(temp-1)/2;
for(int k=0;k<9;++k)
if(root->next[k])
GetSum(root->next[k]);
}
int main()
{
int a[8],n,i,k;
char str[8];
while(cin>>n)
{
TireTree *root=new TireTree;
root->num=0;
for(k=0;k<9;++k) root->next[k]=NULL;
sum=0;
for(i=0;i<n;++i)
{
cin>>str;
k=Change(a,str);
Insert(root,a,k);
}
GetSum(root);
cout<<sum<<endl;
}
return 0;
}

43
HDOJ/2652_autoAC.cpp Normal file
View File

@ -0,0 +1,43 @@
#include<iostream>
#include<algorithm>
using namespace std;
struct Node{
int a,b,c;
};
bool com(Node &a,Node &b){
return a.a<b.a;
}
Node nodes[100010];
int kan[100010];
int no[100010];
int main(){
int number;
while(scanf("%d",&number)!=EOF){
int i;
for(i=1;i<=number;i++){
scanf("%d%d%d",&nodes[i].a,&nodes[i].b,&nodes[i].c);
}
sort(&nodes[1],&nodes[number+1],com);
kan[number+1]=no[number+1]=0;
for(i=number;i>=1;i--){
kan[i]=no[i]=0;
int begin=i;
int end=number;
while(begin<end){
int mid=(begin+end)/2;
if(nodes[mid].a>nodes[i].b){
end=mid;
}else{
begin=mid+1;
}
}
if(nodes[end].a>nodes[i].b){
kan[i]=nodes[i].c+(kan[end]>no[end]?kan[end]:no[end]);
}else{
kan[i]=nodes[i].c;
}
no[i]=(kan[i+1]>no[i+1]?kan[i+1]:no[i+1]);
}
printf("%d\n",kan[1]>no[1]?kan[1]:no[1]);
}
}

77
HDOJ/2653_autoAC.cpp Normal file
View File

@ -0,0 +1,77 @@
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
char g[100][100];
bool vis[88][88][88];
int n,m,p,t,si,sj,ans;
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
struct node
{
int step,p,x,y;
node(int a=0,int b=0,int c=0,int d=0):x(a),y(b),p(c),step(d){}
bool friend operator <(const node a,const node b)
{
return a.step>b.step;
}
};
void BFS()
{
priority_queue<node> Q;
node f=node(si,sj,p,0);
Q.push(f);
memset(vis,false,sizeof(vis));
vis[si][sj][p]=true;
node temp;
while(!Q.empty())
{
temp=Q.top();
Q.pop();
if(temp.step>t)
return ;
if(g[temp.x][temp.y]=='L')
{
ans=temp.step;
return ;
}
for(int k=0;k<4;k++)
{
int i=dir[k][0]+temp.x;
int j=dir[k][1]+temp.y;
if(i<0||i>n-1 || j<0 || j>m-1||g[i][j]=='#')
continue;
if(temp.p!=0 && !vis[i][j][temp.p-1])
{
vis[i][j][temp.p-1]=true;
Q.push(node(i,j,temp.p-1,temp.step+1));
}
if(g[temp.x][temp.y]!='@' && g[i][j]!='@'&&!vis[i][j][temp.p])
{
vis[i][j][temp.p]=true;
Q.push(node(i,j,temp.p,temp.step+2));
}
}
}
return ;
}
int main()
{
int cas=0;
while(scanf("%d %d %d %d",&n,&m,&t,&p)==4)
{
for(int i=0;i<n;i++)
{
scanf("%s",g[i]);
for(int j=0;j<m;j++)
if(g[i][j]=='Y')
si=i,sj=j;
}
ans=100001;
BFS();
printf("Case %d:\n",++cas);
if(ans>t)
printf("Poor Yifenfei, he has to wait another ten thousand years.\n");
else printf("Yes, Yifenfei will kill Lemon at %d sec.\n",ans);
}
return 0;
}

59
HDOJ/2656_autoAC.cpp Normal file
View File

@ -0,0 +1,59 @@
#include<stdio.h>
#include<string.h>
int X, K;
struct Integer
{
int a[32];
void init(int x)
{
int i;
for(i = 0; i < 32; i ++)
{
a[i] = x % 2;
x /= 2;
}
}
void getnext()
{
int i, j, cnt = 0;
for(i = 0;; i ++)
if(a[i] == 1)
{
if(a[i + 1] == 0)
{
a[i + 1] = 1;
for(j = 0; j <= i; j ++)
{
if(cnt)
-- cnt, a[j] = 1;
else
a[j] = 0;
}
break;
}
++ cnt;
}
}
}integer;
void solve()
{
int i;
long long int ans = 0;
integer.init(X);
for(i = 0; i < K; i ++)
integer.getnext();
for(i = 31; i >= 0; i --)
ans = ans * 2 + integer.a[i];
printf("%I64d\n", ans);
}
int main()
{
for(;;)
{
scanf("%d%d", &X, &K);
if(!X && !K)
break;
solve();
}
return 0;
}

45
HDOJ/2660_autoAC.cpp Normal file
View File

@ -0,0 +1,45 @@
#include <stdio.h>
#include <string.h>
struct node
{
int v,w;
}p[50];
int maxn,n,k,weight,vis[50];
void dfs(int wei,int val,int step,int sum)
{
if(sum == k || wei == weight)
{
if(maxn<val)
maxn = val;
return ;
}
for(int i = step;i<=n;i++)
{
if(!vis[i] && sum+1<=k && wei+p[i].w<=weight)
{
vis[i] = 1;
dfs(wei+p[i].w,val+p[i].v,i+1,sum+1);
vis[i] = 0;
}
}
return ;
}
int main()
{
int t,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
for(i = 1;i<=n;i++)
{
scanf("%d%d",&p[i].v,&p[i].w);
vis[i] = 0;
}
scanf("%d",&weight);
maxn = 0;
dfs(0,0,0,0);
printf("%d\n",maxn);
}
return 0;
}

17
HDOJ/2662_autoAC.cpp Normal file
View File

@ -0,0 +1,17 @@
#include <iostream>
using namespace std;
typedef unsigned long long int longint;
int main()
{
int t;
cin >> t;
while (t--)
{
longint n, m;
cin >> n >> m;
longint ans;
ans = (n * m) - n - m;
cout << ans << endl;
}
return 0;
}

73
HDOJ/2665_autoAC.cpp Normal file
View File

@ -0,0 +1,73 @@
#include<stdio.h>
#include<algorithm>
#define N 100010
int T[N];
int num[N];
int san[N];
int ls[N*20];
int rs[N*20];
int sum[N*20];
int tot,rt;
int n, m;
void Build(int l,int r,int &rt)
{
rt=++tot;
sum[rt]=0;
if(l==r) return ;
int m=(l+r)>>1;
Build(l,m,ls[rt]);
Build(m+1,r,rs[rt]);
}
void Update(int last,int p,int l,int r,int &rt)
{
rt=++tot;
ls[rt]=ls[last];
rs[rt]=rs[last];
sum[rt]=sum[last]+1;
if(l==r) return ;
int m=(l+r)>>1;
if(p<=m) Update(ls[last],p,l,m,ls[rt]);
else Update(rs[last],p,m+1,r,rs[rt]);
}
int Query(int ss,int tt,int l,int r,int k)
{
if(l==r) return l;
int m=(l+r)>>1;
int cnt=sum[ls[tt]]-sum[ls[ss]];
if(k<=cnt)
return Query(ls[ss],ls[tt],l,m,k);
else
return Query(rs[ss],rs[tt],m+1,r,k-cnt);
}
void gogogo()
{
int l,r,k;
for(int i=1;i<=n;i++)
{
scanf("%d",&num[i]);
san[i]=num[i];
}
tot=0;
std::sort(san+1,san+n+1);
int cnt=std::unique(san+1,san+n+1)-san-1;
Build(1,cnt,T[0]);
for(int i=1;i<=n;i++) num[i]=std::lower_bound(san+1,san+cnt+1,num[i])-san;
for(int i=1;i<=n;i++) Update(T[i-1],num[i],1,cnt,T[i]);
while(m--)
{
scanf("%d%d%d",&l,&r,&k);
int id=Query(T[l-1],T[r],1,cnt,k);
printf("%d\n",san[id]);
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
gogogo();
}
return 0;
}

32
HDOJ/2668_autoAC.cpp Normal file
View File

@ -0,0 +1,32 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std;
const int MAX=10000000+10;
char s[MAX];
int pos[500];
int main(){
int n;
while(scanf("%d",&n)!=EOF){
scanf("%s",s);
memset(pos,-1,sizeof pos);
int st=0,ed=0,len=0,temp=0;
for(int i=0;i<n;++i){
if(pos[s[i]]>=temp){
if(i-temp>len){len=i-temp,st=temp,ed=i-1;}
temp=pos[s[i]]+1;
}
pos[s[i]]=i;
}
if(n-temp>len){len=n-temp,st=temp,ed=n-1;}
printf("%d %d %d\n",len,st,ed);
}
return 0;
}

42
HDOJ/2669_autoAC.cpp Normal file
View File

@ -0,0 +1,42 @@
#include <iostream>
using namespace std;
typedef long long LL;
LL gcd(LL a, LL b)
{
return b ? gcd(b, a%b) : a;
}
LL extend_gcd(LL a, LL b, LL &x, LL &y)
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
LL d = extend_gcd(b, a%b, x, y);
LL t = x;
x = y;
y = t- a/b *y;
return d;
}
int main()
{
LL a, b;
while (cin >> a >> b)
{
if (gcd(a, b) != 1)
cout << "sorry" << endl;
else
{
LL x, y;
extend_gcd(a, b, x, y);
while (x <= 0)
{
x+= b;
y-= a;
}
cout << x << " "<< y<< endl;
}
}
return 0;
}

32
HDOJ/2670_autoAC.cpp Normal file
View File

@ -0,0 +1,32 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
#define max(a,b) ( a>b ? a:b )
using namespace std;
struct ha
{
int a, b;
} s[1111];
int haha ( ha a, ha b )
{
return a.b > b.b;
}
int main ()
{
int i, j, k;
int n, m, v;
int dp[1111];
while ( ~scanf ("%d %d", &n, &m) )
{
memset ( dp, 0, sizeof (dp));
for ( i=0 ; i<n ; i++ )
scanf ("%d", &s[i].a);
for ( i=0 ; i<n ; i++ )
scanf ("%d", &s[i].b);
sort (s, s+n, haha);
for ( i=0 ; i<n ; i++ )
for ( j=m ; j>0 ; j-- )
dp[j] = max (dp[j], dp[j-1]+s[i].a-(j-1)*s[i].b);
printf ("%d\n" ,dp[m]);
}
}

39
HDOJ/2671_autoAC.cpp Normal file
View File

@ -0,0 +1,39 @@
#include <stdio.h>
#include <cmath>
#include <vector>
#include <map>
#include <time.h>
#include <cstring>
#include <set>
#include<iostream>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define inf 0x6f6f6f6f
double liangdianzhijiandejuli(double ax,double ay,double x1,double y1)
{
return sqrt((ax-x1)*(ax-x1)+(ay-y1)*(ay-y1));
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
double ax,ay,bx,by,cx,cy,k,l;
double x1,y1,ans;
scanf("%lf",&k);
scanf("%lf%lf%lf%lf%lf%lf",&ax,&ay,&bx,&by,&cx,&cy);
if((k*ax-ay-k*cx+cy)*(k*bx-by-k*cx+cy)>0)
{
y1=(2*bx*k+by*k*k-by-2*k*cx+2*cy)/(k*k+1);
x1=bx-(y1-by)*k;
ans=liangdianzhijiandejuli(ax,ay,x1,y1);
}
else
ans=liangdianzhijiandejuli(ax,ay,bx,by);
printf("%.2lf\n",ans);
}
return 0;
}

31
HDOJ/2672_autoAC.cpp Normal file
View File

@ -0,0 +1,31 @@
#include<iostream>
#include<string>
#include<ctype.h>
#include<cstdio>
using namespace std;
int main()
{
int n[11111]={0};
n[2]=1,n[1]=1;
for(int i=3;i<11111;i++)
{
n[i]=(n[i-1]+n[i-2])%26;
}
char c[11111];
while(gets(c))
{
int num=0;
for(int i=0;c[i];i++)
{
if(isupper(c[i]))
{
num++;
printf("%c",isupper(c[i]+n[num])?c[i]+n[num]:c[i]+n[num]-26);
}
else
printf("%c",c[i]);
}
cout<<endl;
}
return 0;
}

32
HDOJ/2673_autoAC.cpp Normal file
View File

@ -0,0 +1,32 @@
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
vector<int>v(n),vv(n);
int i,j;
for(i=0;i<n;i++)
{
cin>>v[i];
}
sort(v.begin(),v.end());
int k=0;
for(i=0,j=n-1;i<j;)
{
vv[k++]=v[j--];
vv[k++]=v[i++];
}
if(i==j)
vv[k]=v[i];
for(i=0;i<n;i++)
{
if(i!=n-1)cout<<vv[i]<<" ";
else cout<<vv[i]<<endl;
}
}
return 0;
}

27
HDOJ/2674_autoAC.cpp Normal file
View File

@ -0,0 +1,27 @@
#include <stdio.h>
#include <stdlib.h>
int arr[41];
void init()
{
int i;
arr[0] = 1;
for(i = 1; i<41; i++)
arr[i] = (i*arr[i-1])%2009;
}
int main(int argc, char* argv[])
{
int t;
init();
while(1)
{
if(scanf("%d", &t)==EOF)
break;
if(t>=41)
{
printf("0\n");
continue;
}
printf("%d\n", arr[t]);
}
return 0;
}

40
HDOJ/2675_autoAC.cpp Normal file
View File

@ -0,0 +1,40 @@
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
const double ee=2.718281828459;
const double eps=1e-7;
int main()
{
double x,y;
while(scanf("%lf",&y)!=EOF)
{
double tmp=(1+log(y))/(y*ee);
double low=1+eps,hei=ee-eps,mid;
if(tmp*ee-1>eps){puts("Happy to Womens day!");continue;}
while(hei-low>eps)
{
mid=(hei+low)/2;
if(log(mid)*(y*ee)>(1+log(y))*mid)
hei=mid;
else
low=mid;
}
if(y==1)printf("%.5lf\n",(hei+low)/2);
else if(y>1)
{
double ans=(hei+low)/2;
low=ee+eps,hei=2000000000+eps;
while(hei-low>eps)
{
mid=(hei+low)/2;
if(log(mid)*(y*ee)<(1+log(y))*mid)
hei=mid;
else
low=mid;
}
printf("%.5lf %.5lf\n",ans,(hei+low)/2);
}
}
return 0;
}

91
HDOJ/2677_autoAC.cpp Normal file
View File

@ -0,0 +1,91 @@
#include <cstdio>
#include <map>
#include <string>
using namespace std;
map<string,int>mp,has;
map<string,string>he;
char a[10010];
int n,m,k,u;
int dfs(string s){
if(mp.count(s))return mp[s];
string l=he[s];
int price=0;
for(int i=0;l[i]!='\0';i++){
if(l[i]==' '||l[i]=='+')continue;
string g="";
for(;l[i]!='\0'&&l[i]!=' ';i++)
g+=l[i];
if(mp.count(g))
price+=mp[g];
else{
mp.insert(make_pair(g,dfs(g)));
price+=mp[g];
}
}
return price;
}
void dfs(string s,int t){
if(!he.count(s)){
has[s]-=t;
return;
}
if(has[s]>=t){
has[s]-=t;return;
}
t=t-has[s];
has[s]=0;
string l=he[s];
for(int i=0;l[i]!='\0';i++){
if(l[i]==' '||l[i]=='+')continue;
string g="";
for(;l[i]!='\0'&&l[i]!=' ';i++)
g+=l[i];
dfs(g,t);
}
}
void make_map(){
map<string,string>::iterator i;
for(i=he.begin();i!=he.end();i++)
mp.insert(make_pair(i->first,dfs(i->first)));
}
int main(void){
int t;
while(~scanf("%d",&n)){
mp.clear();has.clear();he.clear();
for(int i=1;i<=n;i++){
scanf("%s %d",a,&t);
mp.insert(make_pair(a,t));
}
scanf("%d",&m);
for(int i=0;i<m;i++){
scanf("%s %d",a,&t);
has.insert(make_pair(a,t));
}
scanf("%d",&k);
getchar();
for(int j=0;j<k;j++){
gets(a);
string s="",g="";
int i;
for(i=0;a[i]!='=';i++)
s+=a[i];
for(i+=2;a[i]!='\0';i++)
g+=a[i];
he.insert(make_pair(g,s));
}
make_map();
scanf("%d",&u);
while(u--){
scanf("%s %d",a,&t);
dfs(a,t);
}
int ans=0;
map<string,int>::iterator it;
for(it=has.begin();it!=has.end();it++){
if(it->second<0)
ans+=-(it->second)*mp[it->first];
}
printf("%d\n",ans);
}
return 0;
}

64
HDOJ/2680_autoAC.cpp Normal file
View File

@ -0,0 +1,64 @@
#include<stdio.h>
#define max 1001
#define INF 100000000
int N;
int v[max];
int dis[max];
int g[max][max];
void dijk()
{
int i,j,mark,mindis;
for(i=0;i<=N;i++)
{
v[i] = 0;
dis[i] = INF;
}
dis[0] = 0;
v[0] = 1;
for(i=0;i<=N;i++)
{
mindis = INF;
mark = 0;
for(j=0;j<=N;j++)
if(!v[j] && dis[j] < mindis)
{
mindis = dis[j];
mark = j;
}
v[mark] = j;
for(j=0;j<=N;j++)
if(!v[j] && dis[j] > dis[mark] + g[mark][j])
dis[j] = dis[mark] + g[mark][j];
}
}
int main()
{
int W,M,Q,i,j,d,a,b;
while(~scanf("%d%d%d",&N,&M,&Q))
{
for(i=0;i<max;i++)
for(j=0;j<max;j++)
{
if(i==j)g[i][j] = 0;
else
g[i][j] = INF;
}
while(M--)
{
scanf("%d%d%d",&a,&b,&d);
if(d < g[a][b])
g[a][b] = d;
}
scanf("%d",&W);
while(W--)
{
scanf("%d",&a);
g[0][a] = 0;
}
dijk();
if(dis[Q] == INF)
printf("-1\n");
else
printf("%d\n",dis[Q]);
}
}

50
HDOJ/2681_autoAC.cpp Normal file
View File

@ -0,0 +1,50 @@
#include<iostream>
#include<cmath>
#include<algorithm>
#define maxn 200+10
using namespace std;
class Girl
{
public:
int s,t;
bool operator <(const class Girl g)
{
if(s==g.s)
return t<g.t;
return s<g.s;
}
};
Girl girl[maxn];
int tmp[maxn];
bool cmp(const int & a,const int & b)
{
return a>b;
}
int main()
{
int n,k,MAXK;
int sum,ans;
int i,j,cnt;
while(scanf("%d%d%d",&n,&k,&MAXK)!=EOF)
{
ans=0;
for(i=0;i<n;i++)
scanf("%d%d",&girl[i].s,&girl[i].t);
sort(girl,girl+n);
for(i=0;i<n;i++)
{
tmp[0]=girl[i].t;
for(j=i+1,cnt=1;j<n;j++)
if(girl[j].s-girl[i].s<=MAXK)
tmp[cnt++]=girl[j].t;
if(cnt<k)
continue;
sort(tmp,tmp+cnt,cmp);
for(sum=j=0;j<cnt&&j<k;j++)
sum+=tmp[j];
if(j==k && sum>ans)
ans=sum;
}
printf("%d\n",ans==0?-1:ans);
}
}

102
HDOJ/2682_autoAC.cpp Normal file
View File

@ -0,0 +1,102 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define INF 2000002
int hash[2000002];
int lowcost[601];
int e[601][601];
int used[601];
int w[601];
int n,i,j;
void sushubiao()
{
int i;
memset(hash,0,sizeof(hash));
int num=0;
hash[1]=1;
hash[0]=1;
int M=2000002;
for(i=2; i<=M; i++)
{
if(!hash[i])
{
for(j=i+i;j<M;j+=i)
{
hash[j]=1;
}
}
}
}
int yxc(int a,int b)
{
if(a>=b)return b;
else return a;
}
void prim()
{
int sum=0;
int min;
int v;
for(i=0; i<n; i++)
{
lowcost[i]=e[0][i];
used[i]=0;
}
used[0]=-1;
for(i=1; i<n; i++)
{
min=INF;
for(j=0; j<n; j++)
{
if(used[j]==0&&lowcost[j]<min)
{
min=lowcost[j];
v=j;
}
}
if(min==INF) break;
sum+=min;
used[v]=-1;
for(j=0; j<n; j++)
{
if(used[j]==0&&e[v][j]<lowcost[j])
{
lowcost[j]=e[v][j];
}
}
}
int flag=0;
for(j=0; j<n; j++)
{
if(used[j]==0)
{
flag=1;
break;
}
}
if(flag) printf("-1\n");
else
printf("%d\n",sum);
}
int main()
{
int T;
sushubiao();
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=0; i<n; i++)
scanf("%d",&w[i]);
for(i=0; i<n-1; i++)
for(j=i+1; j<n; j++)
{
if(hash[w[i]]==0||hash[w[j]]==0||hash[w[i]+w[j]]==0)
e[i][j]=e[j][i]=yxc(yxc(w[i],w[j]),abs(w[i]-w[j]));
else e[i][j]=e[j][i]=INF;
}
prim();
}
return 0;
}

42
HDOJ/2685_autoAC.cpp Normal file
View File

@ -0,0 +1,42 @@
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
long long gcd(long long a,long long b)
{
if(b==0)
return a;
return gcd(b,a%b);
}
long long Pow(long long a,long long b,long long mod)
{
long long ans=1;
while(b)
{
if(b&1)
{
b--;
ans=(ans*a)%mod;
}
else
{
b/=2;
a=(a*a)%mod;
}
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
long long a,m,n,k;
scanf("%I64d%I64d%I64d%I64d",&a,&m,&n,&k);
printf("%I64d\n",(Pow(a,gcd(m,n),k)+k-1)%k);
}
return 0;
}

96
HDOJ/2686_autoAC.cpp Normal file
View File

@ -0,0 +1,96 @@
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <queue>
using namespace std;
const int MAXN = 2005;
const int MAXEDGE = 1e5 + 5;
const int INF = 0x3f3f3f3f;
int n;
struct CEdge
{
int from, to, cap, flow, cost, next;
}edge[MAXEDGE];
struct CMCMF
{
int s, t, pp;
int head[MAXN], a[MAXN], d[MAXN], p[MAXN];
bool inq[MAXN];
CMCMF(int ss, int tt)
{
s = ss, t = tt;
pp = 0;
memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int cap, int cost)
{
edge[pp] = (CEdge){u, v, cap, 0, cost, head[u]};
head[u] = pp++;
edge[pp] = (CEdge){v, u, 0, 0, -cost, head[v]};
head[v] = pp++;
}
bool bellmanFord(int &flow, int &cost)
{
memset(d, INF, sizeof(d));
memset(inq, false, sizeof(inq));
queue <int> q;
q.push(s), inq[s] = true;
a[s] = INF, d[s] = 0, p[s] = -1;
while(!q.empty())
{
int u = q.front();
q.pop(), inq[u] = false;
int next = head[u];
while(next != -1)
{
CEdge &e = edge[next];
if(e.cap > e.flow && d[e.to] > d[u] + e.cost)
{
d[e.to] = d[u] + e.cost;
a[e.to] = min(a[u], e.cap - e.flow);
p[e.to] = next;
if(!inq[e.to]) inq[e.to] = true, q.push(e.to);
}
next = e.next;
}
}
if(d[t] == INF) return false;
cost += d[t] * a[t];
flow += a[t];
int u = t;
while(u != s)
{
edge[p[u]].flow += a[t];
edge[p[u]^1].flow -= a[t];
u = edge[p[u]].from;
}
return true;
}
};
int a[33][33];
int main()
{
while(scanf("%d",&n) != EOF)
{
int s = 1, t = n*n;
CMCMF mcmf(s, t);
int tmp;
for(int i = 0;i < n; i++)
{
for(int j = 1;j <= n; j++)
{
scanf("%d",&tmp);
a[i][j] = tmp;
if(j != n) mcmf.addEdge(i*n+j, i*n+j+1 + n*n, 1, -tmp);
if(i != n-1) mcmf.addEdge(i*n+j, (i+1)*n+j+n*n, 1, -tmp);
mcmf.addEdge(i*n+j+n*n, i*n+j, 1, 0);
}
}
mcmf.addEdge(n*n + n*n, n*n, 1, 0);
int cost = 0, flow = 0;
while(mcmf.bellmanFord(flow, cost));
cout<<-cost - a[0][1] + a[n-1][n]<<endl;
}
return 0;
}

37
HDOJ/2687_autoAC.cpp Normal file
View File

@ -0,0 +1,37 @@
#include<stdio.h>
int main()
{
int n,i,k,j,a[11][11],b[11][11];
while(scanf("%d",&n)!=EOF){
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
scanf("%d",&k);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
b[i][j]=(a[i][j]+a[j][n+1-i]+a[n+1-i][n+1-j]+a[n+1-j][i])*(k/4);
if(k%4==0)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
b[i][j]+=a[i][j];
else if(k%4==1)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
b[i][j]+=a[i][j]+a[n+1-j][i];
else if(k%4==2)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
b[i][j]+=a[i][j]+a[n+1-j][i]+a[n+1-i][n+1-j];
else
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
b[i][j]+=a[i][j]+a[n+1-j][i]+a[n+1-i][n+1-j]+a[j][n+1-i];
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
printf("%d",b[i][j]);
if(j<n)
printf(" ");}
printf("\n");
}
}
}

65
HDOJ/2688_autoAC.cpp Normal file
View File

@ -0,0 +1,65 @@
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn=10005;
const int maxm=3000005;
int c[maxn],a[maxm],n;
int sum(int x)
{
int s=0;
while(x>=1)
{
s+=c[x];
x-=x&(-x);
}
return s;
}
void updata(int i)
{
while(i<=maxn-1)
{
c[i]++;
i+=i&(-i);
}
}
int main()
{
int i,m,x,y;
__int64 ans;
char str[2];
while(scanf("%d",&n)>0)
{
memset(c,0,sizeof(c));
ans=0;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
updata(a[i]);
ans+=sum(a[i]-1);
}
scanf("%d",&m);
while(m--)
{
scanf("%s",str);
if(str[0]=='Q')
printf("%I64d\n",ans);
else
if(str[0]=='R')
{
scanf("%d %d",&x,&y);
int count=a[x];
for(i=x;i<y;i++)
{
a[i]=a[i+1];
if(a[i]>count)
ans--;
else
if(a[i]<count)
ans++;
}
a[y]=count;
}
}
}
return 0;
}

42
HDOJ/2689_autoAC.cpp Normal file
View File

@ -0,0 +1,42 @@
#include<stdio.h>
#include<string.h>
int a[1002];
int lowbit(int x)
{
return x&(-x);
}
void add(int x)
{
while(x<=1000)
{
++a[x];
x=x+lowbit(x);
}
}
int getsum(int x)
{
int sum=0;
while(x>0)
{
sum=sum+a[x];
x=x-lowbit(x);
}
return sum;
}
int main()
{
int i,j,n,x,sum;
while(scanf("%d",&n)==1)
{
sum=0;
memset(a,0,sizeof(a));
for(i=1;i<=n;i++)
{
scanf("%d",&x);
sum=sum+(i-1-getsum(x));
add(x);
}
printf("%d\n",sum);
}
return 0;
}

40
HDOJ/2690_autoAC.cpp Normal file
View File

@ -0,0 +1,40 @@
#include<stdio.h>
int main()
{
int max,min,n,i,x,t,a[10001],sum1,sum2,s;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
max=0;
min=10001;
s=0;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]>max)
max=a[i];
if(a[i]<min)
min=a[i];
if(a[i]>=n&&a[i]<0)s=1;
}
if(s==1||n==1&&max==0){ printf("Impossible!\n"); continue;}
sum1=sum2=0;
if(max==min+1){
for(i=0;i<n;i++){
if(max==a[i])sum1++;
else sum2++;
}
if(sum2==max)printf("%d\n",max);
else printf("Impossible!\n");
}
else if(max==min)
{ if(max==0)printf("0\n");
else {
if(max==n-1)printf("%d\n",n);
else printf("Impossible!\n"); }
}
else
printf("Impossible!\n");
}
}

150
HDOJ/2691_autoAC.cpp Normal file
View File

@ -0,0 +1,150 @@
#include <stdio.h>
#include <string.h>
const int Mod=199997;
const int maxn=1000000;
char names[]="BOWRYG";
int s,t;
int a[24],b[24];
int cnt,box[Mod];
struct edge{
long long to;
int next,step;
bool flag;
} e[maxn];
struct point{
int step;
bool flag;
char a[24];
} qu[maxn];
int tr[12][4]={{0,1,3,2},
{4,5,13,12},
{6,7,15,14},
{8,9,17,16},
{10,11,19,18},
{20,21,23,22}};
int netr[12][8]={{11,10,9,8,7,6,5,4},
{0,2,6,14,20,22,19,11},
{2,3,8,16,21,20,13,5},
{3,1,10,18,23,21,15,7},
{17,9,1,0,4,12,22,23},
{12,13,14,15,16,17,18,19}};
void pre()
{
for(int i=6;i<12;i++)
{
for(int j=0;j<4;j++)
tr[i][j]=tr[i-6][3-j];
for(int j=0;j<8;j++)
netr[i][j]=netr[i-6][7-j];
}
}
void hash(int &p1,long long &p2)
{
p2=0;
for(int i=0;i<24;i++)
p2=p2*6+a[i];
p1=p2%Mod;
}
void add(int p1,long long p2,int step,bool flag)
{
e[cnt].to=p2;e[cnt].step=step;e[cnt].flag=flag;
e[cnt].next=box[p1];box[p1]=cnt++;
t++;
qu[t].step=step;
qu[t].flag=flag;
for(int i=0;i<24;i++) qu[t].a[i]=a[i];
}
void finds(int p1,long long p2,bool &flag,int &step)
{
for(int p=box[p1];p!=-1;p=e[p].next)
if (e[p].to==p2)
{
flag=e[p].flag;
step=e[p].step;
return ;
}
step=-1;
}
int makeit()
{
bool flag=1,nowflag;
int step,q1,q2,q3,nowstep;
for(int i=0;i<24;i++)
flag=flag&&(a[i]==b[i]);
if (flag) return 0;
cnt=0;
memset(box,-1,sizeof(box));
int p1;
long long p2;
s=0;t=0;
hash(p1,p2);
add(p1,p2,0,0);
for(int i=0;i<24;i++) a[i]=b[i];
hash(p1,p2);
add(p1,p2,0,1);
while(s!=t)
{
s++;
step=qu[s].step;
flag=qu[s].flag;
for(int i=0;i<24;i++)
a[i]=qu[s].a[i];
for(int i=0;i<12;i++)
{
q1=a[tr[i][0]];
q2=a[netr[i][0]];
q3=a[netr[i][1]];
for(int j=0;j<3;j++)
a[tr[i][j]]=a[tr[i][j+1]];
a[tr[i][3]]=q1;
for(int j=0;j<6;j++)
a[netr[i][j]]=a[netr[i][j+2]];
a[netr[i][6]]=q2;a[netr[i][7]]=q3;
hash(p1,p2);
finds(p1,p2,nowflag,nowstep);
if (nowstep==-1)
add(p1,p2,step+1,flag);
else
if (nowflag!=flag) return nowstep+step+1;
int ii=(i+6);
if (ii>=12) ii-=12;
q1=a[tr[ii][0]];
q2=a[netr[ii][0]];
q3=a[netr[ii][1]];
for(int j=0;j<3;j++)
a[tr[ii][j]]=a[tr[ii][j+1]];
a[tr[ii][3]]=q1;
for(int j=0;j<6;j++)
a[netr[ii][j]]=a[netr[ii][j+2]];
a[netr[ii][6]]=q2;a[netr[ii][7]]=q3;
}
}
}
int trans(char ch)
{
for(int i=0;i<6;i++)
if (names[i]==ch) return i;
return -1;
}
int main()
{
int cas;
char s[10];
pre();
scanf("%d",&cas);
while(cas--)
{
for(int i=0;i<24;i++)
{
scanf("%s",s);
a[i]=trans(s[0]);
}
for(int i=0;i<24;i++)
{
scanf("%s",s);
b[i]=trans(s[0]);
}
printf("%d\n",makeit());
}
return 0;
}

34
HDOJ/2697_autoAC.cpp Normal file
View File

@ -0,0 +1,34 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N =110;
int a[N];
int dp[N][N];
int main(){
int t;
int n,m;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++){
for(int j=0;j<=m;j++){
int cnt=0;
int sum=0;
dp[i][j]=dp[i-1][j];
for(int h=i;h>=1;h--){
cnt++;
sum+=a[h];
if(sum>j) break;
dp[i][j]=max(dp[i][j],dp[h-1][j-sum]+cnt*cnt);
}
}
}
printf("%d\n",dp[n][m]);
}
return 0;
}

83
HDOJ/2698_autoAC.cpp Normal file
View File

@ -0,0 +1,83 @@
#include<stdio.h>
#include<string>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<math.h>
using namespace std;
typedef long long int64;
typedef pair<int64,int64> PII;
#define MP(a,b) make_pair((a),(b))
const int maxn = 115;
const int inf = 0x7fffffff;
const double pi=acos(-1.0);
const double eps = 1e-8;
struct Node{
char s[ maxn ];
int cnt;
}a[ maxn ],q[ maxn ];
int Find( char s[],int n ){
for( int i=1;i<n;i++ ){
if( strcmp( s,a[i].s)==0 )
return i;
}
return -1;
}
bool cmp( Node a,Node b ){
if( a.cnt!=b.cnt ) return a.cnt>b.cnt;
else {
if( strcmp( a.s,b.s )<=0 ) return true;
else return false;
}
}
int main(){
int T;
scanf("%d",&T);
while( T-- ){
int n;
scanf("%d",&n);
char ID[ maxn ],Web[ maxn ];
int Cnt = 1;
int cc;
while( n-- ){
scanf("%s%s",ID,Web);
cc = Find( Web,Cnt );
if( ID[0]=='V' ){
if( cc==-1 ){
strcpy( a[Cnt].s,Web );
a[ Cnt ].cnt = 1;
Cnt++;
}
else{
a[ cc ].cnt++;
}
}
else{
int pp = 0;
for( int k=1;k<Cnt;k++ ){
bool f = false;
for( int i=0;Web[i]!='\0';i++ ){
if( Web[i]!=a[k].s[i] ){
f = true;
break;
}
}
if( f==false ){
q[ pp++ ] = a[ k ];
}
}
sort( q,q+pp,cmp );
for( int i=0;i<pp;i++ ){
printf("%s\n",q[i].s);
}
printf("\n");
}
}
}
return 0;
}