Powered By HC TECH : AutoACer Engine

2500-2599
This commit is contained in:
KiritoTRw 2016-08-22 00:30:27 +08:00 committed by GitHub
parent 55762be649
commit 34806a93cd
81 changed files with 3534 additions and 0 deletions

20
HDOJ/2500_autoAC.cpp Normal file
View File

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

22
HDOJ/2501_autoAC.cpp Normal file
View File

@ -0,0 +1,22 @@
#include<iostream>
using namespace std;
int f[31];
void xx()
{
f[1]=1;
f[2]=3;
for(int i=3;i<=30;++i)
f[i]=f[i-1]+2*f[i-2];
}
int main()
{
xx();
int t,n;
cin>>t;
while(t--)
{
cin>>n;
cout<<f[n]<<endl;
}
return 0;
}

20
HDOJ/2502_autoAC.cpp Normal file
View File

@ -0,0 +1,20 @@
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
int a;
scanf("%d",&a);
int sum=0;
for(int i=pow(2.0,a-1);i<pow(2.0,a);i++)
sum+=__builtin_popcount(i);
printf("%d\n",sum);
}
return 0;
}

26
HDOJ/2503_autoAC.cpp Normal file
View File

@ -0,0 +1,26 @@
#include<stdio.h>
int gcd(int x,int y)
{
if(x==y)
return x;
if(x>y)
return gcd(x-y,y);
if(x<y)
return gcd(x,y-x);
}
int main()
{
int a,b,c,d,e,f,g;
int n;
while(scanf("%d",&n)!=EOF)
{
while(n--)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
e=a*d+b*c;
f=b*d;
g=gcd(e,f);
printf("%d %d\n",e/g,f/g);
}
}
}

18
HDOJ/2504_autoAC.cpp Normal file
View File

@ -0,0 +1,18 @@
#include<stdio.h>
int gcd(int x,int y)
{
return y==0?x:gcd(y,x%y);
}
int main()
{
int a,b,c,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&a,&b);
c=b+b;
while(gcd(a,c)!=b)
c+=b;
printf("%d\n",c);
}
}

32
HDOJ/2509_autoAC.cpp Normal file
View File

@ -0,0 +1,32 @@
#include<iostream>
using namespace std;
int main()
{
int N,x,i,s,a;
while(cin>>N)
{
s=a=0;
for (i=0;i<N;i++)
{
cin>>x;
s^=x;
if(x>1)
a=1;
}
if(a==0)
{
if(N%2)
cout<<"No"<<endl;
else
cout<<"Yes"<<endl;
}
else
{
if(s==0)
cout<<"No"<<endl;
else
cout<<"Yes"<<endl;
}
}
return 0;
}

8
HDOJ/2510_autoAC.cpp Normal file
View File

@ -0,0 +1,8 @@
#include <iostream>
using namespace std;
int main()
{
int n,a[25]={0,0,0,4,6,0,0,12,40,0,0,171,410,0,0,1896,5160,0,0,32757,59984,0,0,431095,822229};
while (cin >> n && n!=0) cout << n << " " << a[n] << endl;
return 0;
}

26
HDOJ/2511_autoAC.cpp Normal file
View File

@ -0,0 +1,26 @@
#include<cstdio>
#define maxn 63
using namespace std;
long long f[maxn+10];
void dfs(int s,int z,int e,long long n,long long m)
{
if(m==f[n-1]+1){printf("%I64d %d %d\n",n,s,e);return;}
if(m>f[n-1])dfs(z,s,e,n-1,m-f[n-1]-1);
if(m<=f[n-1])dfs(s,e,z,n-1,m);
}
void work()
{
int t,i,j,k; long long n,m;
for(f[0]=0,i=1;i<=maxn;i++)f[i]=f[i-1]*2+1;
while(scanf("%d",&t)==1)
for(i=1;i<=t;i++)
{
scanf("%I64d%I64d",&n,&m);
dfs(1,2,3,n,m);
}
}
int main()
{
work();
return 0;
}

37
HDOJ/2512_autoAC.cpp Normal file
View File

@ -0,0 +1,37 @@
#include<iostream>
#include<string.h>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
int a[2001][2001];
int main()
{
int i,t,j,n,sum;
memset(a,0,sizeof(a));
a[1][1]=1;
for(i=2;i<2001;i++)
{
for(j=1;j<=i;j++)
{
a[i][j]=a[i-1][j]*j+a[i-1][j-1];
a[i][j]%=1000;
}
}
while(~scanf("%d",&t))
{
while(t--)
{
scanf("%d",&n);
sum=0;
for(i=1;i<=n;i++)
{
sum+=a[n][i];
sum%=1000;
}
printf("%d\n",sum);
}
}
return 0;
}

69
HDOJ/2514_autoAC.cpp Normal file
View File

@ -0,0 +1,69 @@
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<cmath>
#include<iomanip>
#define INF 99999999
using namespace std;
const int MAX=10+10;
bool mark[MAX];
int s[MAX],p[MAX],pos[MAX],num;
bool panduan(int Pos,int x){
if(Pos == 0)if(abs(p[1]-x) == 1 || abs(p[2]-x) == 1 || abs(p[3]-x) == 1)return false;
if(Pos == 1)if(abs(p[0]-x) == 1 || abs(p[2]-x) == 1 || abs(p[4]-x) == 1 || abs(p[5]-x) == 1)return false;
if(Pos == 2)if(abs(p[0]-x) == 1 || abs(p[1]-x) == 1 || abs(p[3]-x) == 1 || abs(p[4]-x) == 1 || abs(p[5]-x) == 1 || abs(p[6]-x) == 1)return false;
if(Pos == 3)if(abs(p[0]-x) == 1 || abs(p[2]-x) == 1 || abs(p[5]-x) == 1 || abs(p[6]-x) == 1)return false;
if(Pos == 4)if(abs(p[1]-x) == 1 || abs(p[2]-x) == 1 || abs(p[5]-x) == 1 || abs(p[7]-x) == 1)return false;
if(Pos == 5)if(abs(p[1]-x) == 1 || abs(p[2]-x) == 1 || abs(p[3]-x) == 1 || abs(p[4]-x) == 1 || abs(p[6]-x) == 1 || abs(p[7]-x) == 1)return false;
if(Pos == 6)if(abs(p[2]-x) == 1 || abs(p[3]-x) == 1 || abs(p[5]-x) == 1 || abs(p[7]-x) == 1)return false;
if(Pos == 7)if(abs(p[4]-x) == 1 || abs(p[5]-x) == 1 || abs(p[6]-x) == 1)return false;
return true;
}
void dfs(int Pos){
if(Pos>pos[0]){
++num;
if(num == 1){
for(int i=0;i<8;++i)pos[pos[0]+1+i]=p[i];
}
return;
}
for(int i=1;i<=s[0];++i){
if(mark[s[i]])continue;
if(panduan(pos[Pos],s[i])){
p[pos[Pos]]=s[i];
mark[s[i]]=true;
dfs(Pos+1);
p[pos[Pos]]=10;
mark[s[i]]=false;
if(num>1)return;
}
}
}
int main(){
int t,Case=0;
cin>>t;
while(t--){
memset(mark,false,sizeof mark);
num=pos[0]=s[0]=0;
for(int i=0;i<8;++i){
cin>>p[i];
if(p[i] == 0)pos[++pos[0]]=i,p[i]=10;
mark[p[i]]=true;
}
for(int i=1;i<=8;++i)if(!mark[i])s[++s[0]]=i;
if(pos[0]>0)dfs(1);
cout<<"Case "<<++Case<<':';
if(!num)cout<<" No answer"<<endl;
if(num>1)cout<<" Not unique"<<endl;
if(num == 1){
for(int i=0;i<8;++i)cout<<' '<<pos[pos[0]+1+i];
cout<<endl;
}
}
return 0;
}

28
HDOJ/2515_autoAC.cpp Normal file
View File

@ -0,0 +1,28 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<stack>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;
#define M 2000000000
#define N 100005
int a[N],b[N];
int main()
{
int n,i,j,g;
while(~scanf("%d",&n))
{
for(i=1;i<=(n-1)*n/2;i++)
{
scanf("%d",&a[i]);
}
g=(a[1]+a[2]-a[n])/2;
printf("%d\n",g);
for(i=1;i<n;i++)
printf("%d\n",a[i]-g);
}
return 0;
}

31
HDOJ/2516_autoAC.cpp Normal file
View File

@ -0,0 +1,31 @@
#include <iostream>
using namespace std;
int main()
{
int a[50];
__int64 i;
long long int n;
while(1)
{
cin>>n;
if(n==1)
continue;
if(n==0)
break;
a[0]=1;
a[1]=1;
for(i=2;i<46;i++)
{
a[i]=a[i-1]+a[i-2];
}
for(i=0;i<46;i++)
if(a[i]==n)
{
cout<<"Second win"<<endl;
break;
}
if(i==46)
cout<<"First win"<<endl;
}
return 0;
}

46
HDOJ/2517_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include<stdio.h>
#include<math.h>
#include <string.h>
#define INF 1<<29
int map[9][9],sum[9][9];
int d[15][9][9][9][9];
inline int Min(int a,int b){return a>b?b:a;}
int s(int x1,int y1,int x2,int y2){
int temp=sum[x2][y2]-sum[x1-1][y2]-sum[x2][y1-1]+sum[x1-1][y1-1]; return temp*temp;
}
int dp(int k,int x1,int y1,int x2,int y2){
if(d[k][x1][y1][x2][y2]!=-1)return d[k][x1][y1][x2][y2];
if(k==1)return s(x1,y1,x2,y2);
int ans=INF,i;
for(i=x1;i<x2;i++)
{
ans=Min(dp(k-1,x1,y1,i,y2)+s(i+1,y1,x2,y2),ans);
ans=Min(dp(k-1,i+1,y1,x2,y2)+s(x1,y1,i,y2),ans);
}
for(i=y1;i<y2;i++)
{
ans=Min(dp(k-1,x1,y1,x2,i)+s(x1,i+1,x2,y2),ans);
ans=Min(dp(k-1,x1,i+1,x2,y2)+s(x1,y1,x2,i),ans);
}
return d[k][x1][y1][x2][y2]=ans;
}
int main()
{
int n,i,j,k;
while(~scanf("%d",&n)){
memset(map,0,sizeof(map));
for(i=1;i<=8;i++)
for(j=1;j<=8;j++)
scanf("%d",&map[i][j]);
memset(sum,0,sizeof(sum));
for(i=1;i<=8;i++)
for(j=1;j<=8;j++)
sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+map[i][j];
memset(d,-1,sizeof(d));
int ans=dp(n,1,1,8,8);
double aver=(double)sum[8][8]/(double)n;
double last=sqrt((double)ans/(double)n-aver*aver);
printf("%.3lf\n",last);
}
return 0;
}

14
HDOJ/2518_autoAC.cpp Normal file
View File

@ -0,0 +1,14 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
int a[] = {0, 0, 0, 2, 368, 1010, 2339};
int main()
{
int x, y;
while(scanf("%d%d", &x, &y) == 2)
{
if(x > y) std::swap(x, y);
printf("%d\n", a[x]);
}
return 0;
}

16
HDOJ/2519_autoAC.cpp Normal file
View File

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

11
HDOJ/2520_autoAC.cpp Normal file
View File

@ -0,0 +1,11 @@
#include<stdio.h>
int main ()
{
int T, n;
for(scanf("%d", &T); T; T--)
{
scanf("%d", &n);
printf("%d\n", n * n %10000);
}
return 0;
}

22
HDOJ/2521_autoAC.cpp Normal file
View File

@ -0,0 +1,22 @@
#include <stdio.h>
int cal(int n){
int s=0;
for(int i=1;i<=n;i++){
if(n%i==0) s++;}
return s;
}
int main(){
int n,a,b;
scanf("%d",&n);
while(n--){
scanf("%d%d",&a,&b);
int max=a;
int m=cal(a);
for(int i=a;i<=b;i++)
if(m<cal(i)){
m=cal(i);
max=i;}
printf("%d\n",max);
}
return 0;
}

43
HDOJ/2522_autoAC.cpp Normal file
View File

@ -0,0 +1,43 @@
#include<iostream>
using namespace std;
int main()
{
int a[100005],b[100005],t;
scanf("%d",&t);
while(t--)
{
int n,w=0;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
scanf("%d",&n);
if(n<0)
{
n=-n;
w=1;
}
int k=1;
b[k]=1;
int j=0;
while(k&&n!=1)
{
k*=10;
a[j++]=k/n;
k%=n;
if(b[k])
break;
b[k]=1;
}
if(w==1)
printf("-");
if(n==1)
printf("1\n");
else
{
printf("0.");
for(int i=0;i<j;i++)
printf("%d",a[i]);
printf("\n");
}
}
return 0;
}

29
HDOJ/2523_autoAC.cpp Normal file
View File

@ -0,0 +1,29 @@
#include<stdio.h>
int abs(int a)
{
return a<0 ? -a:a;
}
void main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,k,i,j;
scanf("%d %d",&n,&k);
int x[2001]={0};
int inf[1000];
for(i=0;i<n;i++)
{
scanf("%d",&inf[i]);
for(j=0;j<i;j++)
x[abs(inf[j]-inf[i])]=1;
}
for(i=0;k;i++)
{
if(x[i]==1)
k--;
}
printf("%d\n",i-1);
}
}

15
HDOJ/2524_autoAC.cpp Normal file
View File

@ -0,0 +1,15 @@
#include <cstdio>
int main ()
{
int i;
int n, m;
int a[101] = {0};
for ( i=1 ; i<101 ; i++ )
a[i] = a[i - 1] + i;
scanf ("%d", &i);
while ( i-- )
{
scanf ("%d %d", &n, &m);
printf ("%d\n", a[n] * a[m]);
}
}

44
HDOJ/2525_autoAC.cpp Normal file
View File

@ -0,0 +1,44 @@
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
int N, D, A, K, X;
long long P[105], W[105];
void init() {
memset(P, 0, sizeof (P));
memset(W, 0, sizeof (W));
P[0] = N;
}
void updateday() {
for (int i = D; i >= 0; --i) {
P[i+1] = P[i];
}
for (int i = K; i >= 0; --i) {
W[i+1] = W[i];
}
P[0] = W[K];
}
void collect() {
W[0] = 0;
for (int i = 1; i <= A; ++i) {
W[0] += P[i];
}
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
long long cnt = 0;
scanf("%d %d %d %d %d", &N, &D, &A, &K, &X);
init();
for (int i = 1; i <= X; ++i) {
updateday();
collect();
for (int i = 1; i <= D; ++i) {
cnt += P[i];
}
}
printf("%I64d\n", cnt * 5);
}
return 0;
}

78
HDOJ/2526_autoAC.cpp Normal file
View File

@ -0,0 +1,78 @@
#include<iostream>
#include<string.h>
const int MAX=1000;
int s[MAX];
int x[MAX];
int y[MAX];
char z[MAX];
char v[MAX];
char k[MAX];
using namespace std;
int main()
{
int t,n,m,i,j,q,p,sum;
cin>>t;
while(t--)
{
cin>>m;
for(i=0;i<8;i++)
{
cin>>k>>n;
sum=0;
for(j=0;j<3;j++)
{
if(k[j]=='1'&&j==0)
sum+=4;
if(k[j]=='1'&&j==1)
sum+=2;
if(k[j]=='1'&&j==2)
sum+=1;
}
x[i]=sum;
y[i]=n;
}
cin>>z;
n=strlen(z);
cout<<z<<endl;
m-=1;
while(m--)
{
strcpy(v,z);
for(i=n;i>=1;i--)
{
v[i]=v[i-1];
}
v[0]='0';
v[n+1]='0';
for(i=1;i<n+1;i++)
{
if(v[i-1]=='0'&&v[i]=='0'&&v[i+1]=='0')
sum=0;
if(v[i-1]=='0'&&v[i]=='0'&&v[i+1]=='1')
sum=1;
if(v[i-1]=='0'&&v[i]=='1'&&v[i+1]=='0')
sum=2;
if(v[i-1]=='0'&&v[i]=='1'&&v[i+1]=='1')
sum=3;
if(v[i-1]=='1'&&v[i]=='0'&&v[i+1]=='0')
sum=4;
if(v[i-1]=='1'&&v[i]=='0'&&v[i+1]=='1')
sum=5;
if(v[i-1]=='1'&&v[i]=='1'&&v[i+1]=='0')
sum=6;
if(v[i-1]=='1'&&v[i]=='1'&&v[i+1]=='1')
sum=7;
for(j=0;j<8;j++)
{
if(x[j]==sum)
{
z[i-1]=y[j]+'0';
break;
}
}
}
cout<<z<<endl;
}
}
return 0;
}

82
HDOJ/2527_autoAC.cpp Normal file
View File

@ -0,0 +1,82 @@
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<iomanip>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
const int MAX=1000000;
char s[MAX];
char k[MAX];
using namespace std;
struct min_max
{
int value;
friend bool operator<(const min_ a,const min_ b)
{
if(a.value>=b.value)
return true;
else
return false;
}
};
int main()
{
int t;
int n,m,f,i,j,sum;
cin>>t;
while(t--)
{
cin>>n>>s;
map<char,int>str;
priority_queue<min_max>que;
m=strlen(s);
sum=0;
f=0;
for(i=0;i<m;i++)
{
str[s[i]]++;
if(str[s[i]]==1)
k[f++]=s[i];
}
for(i=0;i<f;i++)
{
min_ p;
p.value=str[k[i]];
que.push(p);
}
if(que.size()==1)
{
if(m>n)
cout<<"no"<<endl;
else
cout<<"yes"<<endl;
}
else
{
while(que.size()!=1)
{
int temp;
min_max aa,bb,p;
aa=que.top();
que.pop();
bb=que.top();
que.pop();
temp=aa.value+bb.value;
p.value=temp;
sum+=temp;
que.push(p);
}
if(sum<=n)
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
}
return 0;
}

70
HDOJ/2528_autoAC.cpp Normal file
View File

@ -0,0 +1,70 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;
const double eps=1e-8;
const int maxn = 505;
struct point{
double x,y;
};
struct ploy{
point node[ maxn ];
int n;
};
point left,right,s;
ploy p,p1,p2;
double cross( point a,point b,point c ){
return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
double ploy_area( ploy res ){
double ans=0;
res.node[ res.n ]=res.node[0];
for( int i=0;i<res.n;i++ ){
ans+=cross( s,res.node[i],res.node[i+1] );
}
return ans;
}
int dblcmp(double a) {return a<-eps?-1:a>eps?1:0;}
ploy cut( ploy p,point s,point e ){
point tmp;
ploy bb;
int cnt=0;
for( int i=0;i<p.n;i++ ){
int d1,d2;
double s1,s2;
d1=dblcmp(s1=cross( p.node[i],s,e ));//璺ㄧ
d2=dblcmp(s2=cross( p.node[i+1],s,e ));//璺ㄧ
if( d1>=0 ){
bb.node[ cnt ]=p.node[ i ];
cnt++;
}
if( d1*d2<0 ){
tmp.x=(s2*p.node[i].x-s1*p.node[i+1].x)/(s2-s1);
tmp.y=(s2*p.node[i].y-s1*p.node[i+1].y)/(s2-s1);
bb.node[ cnt ]=tmp;
cnt++;
}
}
bb.n=cnt;
bb.node[ cnt ]=bb.node[ 0 ];
return bb;
}
int main(){
while( scanf("%d",&p.n),p.n ){
for( int i=0;i<p.n;i++ ){
scanf("%lf%lf",&p.node[ i ].x,&p.node[ i ].y);
}
p.node[ p.n ]=p.node[ 0 ];
scanf("%lf%lf%lf%lf",&left.x,&left.y,&right.x,&right.y);
s.x=s.y=0;
p1=cut( p,left,right );
p2=cut( p,right,left );
int res1,res2;
res1=int(fabs(ploy_area( p1 ))/2+0.5);
res2=int(fabs(ploy_area( p2 ))/2+0.5);
printf("%d %d\n",res1>res2?res1:res2,res1>res2?res2:res1);
}
return 0;
}

13
HDOJ/2529_autoAC.cpp Normal file
View File

@ -0,0 +1,13 @@
#include <stdio.h>
#define g 9.8
int main()
{
double h, l, v;
while (scanf("%lf%lf%lf", &h, &l, &v))
{
if(h == 0 && l == 0 && v == 0)
break;
printf("%.2lf\n", 0.5 * v * v / g - 0.5 * g * l * l / (v*v) + h);
}
return 0;
}

71
HDOJ/2531_autoAC.cpp Normal file
View File

@ -0,0 +1,71 @@
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <queue>
using namespace std;
const int inf = 0x7FFFFFFF;
const int maxn = 10000;
struct node{
int x[21],y[21];
int time;
}rt,ne;
int n,m,k;
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
char f[105][105];
int dis[105][105];
int Is_Ok(node& ne,int u){ ///娉: '&'涓灏
int q=1;
for(int i=0;i<k;++i){
ne.x[i]+=dx[u];
ne.y[i]+=dy[u];
if(ne.x[i]<0||ne.y[i]<0||ne.x[i]>=n||ne.y[i]>=m||f[ne.x[i]][ne.y[i]]=='O')
return 0;
if(f[ne.x[i]][ne.y[i]]=='Q') q=2;
}
return q;
}
int BFS(){
queue<node>M;
rt.time=0;
M.push(rt);
while(!M.empty()){
rt=M.front(); M.pop();
for(int i=0;i<4;++i){
ne=rt;
int q=Is_Ok(ne,i);
if(!q) continue;
ne.time++;
if(q==2) return ne.time;
if(dis[ne.x[0]][ne.y[0]]>ne.time){
dis[ne.x[0]][ne.y[0]]=ne.time;
M.push(ne);
}
}
}
return -1;
}
int main(){
while(~scanf("%d%d",&n,&m),n+m){
k=0;
for(int i=0;i<n;++i){
scanf("%s",f[i]);
for(int j=0;j<m;++j){
if(f[i][j]=='D')
rt.x[k]=i, rt.y[k++]=j;
dis[i][j]=inf;
}
}
for(int i=0;i<k;++i)
f[rt.x[i]][rt.y[i]]='.';
int ans=BFS();
if(ans==-1) puts("Impossible");
else printf("%d\n",ans);
}
return 0;
}

136
HDOJ/2532_autoAC.cpp Normal file
View File

@ -0,0 +1,136 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct node
{
char name[300];
int cnt;
int id;
int len;
} s[1005];
char ss[300];
struct kode
{
char name[30];
int len;
} word[15];
struct qode
{
char name[30];
int len;
} word2[15];
int cmp(node x,node y)
{
if(x.cnt==y.cnt)
return x.id<y.id;
return x.cnt>y.cnt;
}
int main()
{
int n,i,j,k,x,y,z,m,len;
int ll,l_word,flag;
int ll2,l_word2;
while(~scanf("%d",&n),n)
{
for(i = 0; i<n; i++)
{
getchar();
gets(s[i].name);
scanf("%d",&s[i].cnt);
s[i].id = i;
s[i].len = strlen(s[i].name);
s[i].name[s[i].len++] = ' ';
s[i].name[s[i].len] = '\0';
}
sort(s,s+n,cmp);
scanf("%d",&m);
getchar();
for(i = 0; i<m; i++)
{
gets(ss);
ll = 0;
l_word = 0;
len = strlen(ss);
ss[len++] = ' ';
ss[len] = '\0';
for(j = 0; j<len; j++)
{
if(ss[j]>='A' && ss[j]<='Z')
ss[j]+=32;
}
memset(word,'\0',sizeof(word));
for(j = 0; j<len; j++)
{
if(ss[j]>='a' && ss[j]<='z')
{
word[l_word].name[ll++] = ss[j];
}
else if(j>0 && ss[j] == ' ' && ss[j-1]>='a' && ss[j-1]<='z')
{
word[l_word].name[ll] = '\0';
word[l_word].len = ll;
l_word++;
ll = 0;
}
}
for(j = 0; j<n; j++)
{
l_word2 = ll2 = 0;
memset(word2,'\0',sizeof(word2));
for(k = 0; k<s[j].len; k++)
{
if(s[j].name[k]>='a' && s[j].name[k]<='z')
{
word2[l_word2].name[ll2++] = s[j].name[k];
}
else if(s[j].name[k]>='A' && s[j].name[k]<='Z')
{
word2[l_word2].name[ll2++] = s[j].name[k]+32;
}
else if(s[j].name[k] == ' ' && s[j].name[k-1]>='a' && s[j].name[k-1]<='z')
{
word2[l_word2].name[ll2] = '\0';
word2[l_word2].len = ll2;
l_word2++;
ll2 = 0;
}
else if(s[j].name[k] == ' ' && s[j].name[k-1]>='A' && s[j].name[k-1]<='Z')
{
word2[l_word2].name[ll2] = '\0';
word2[l_word2].len = ll2;
l_word2++;
ll2 = 0;
}
}
y = 0;
x = 0;
flag = 0;
while(x<l_word && y<l_word2)
{
if(!strcmp(word[x].name,word2[y].name))
{
x++;
y++;
flag++;
}
else
{
x = 0;
y++;
flag = 0;
}
}
if(flag == l_word)
{
for(z = 0;z<s[j].len-1;z++)
printf("%c",s[j].name[z]);
printf("\n");
}
}
printf("***\n");
}
printf("---\n");
}
return 0;
}

41
HDOJ/2534_autoAC.cpp Normal file
View File

@ -0,0 +1,41 @@
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<iomanip>
#include<string>
#include<cstring>
using namespace std;
int Gcd(__int64 x,__int64 y)
{
if(y==0)
return x;
else
return Gcd(y,x%y);
}
int main()
{
__int64 x,y,t,m;
while(cin>>x>>y&&(x||y))
{
if(x<y)
{
t=x;
x=y;
y=t;
}
if(Gcd(x,y)==1)
{
m=x*y-(x+y);
cout<<m<<endl;
}
else
cout<<"Inf"<<endl;
}
return 0;
}

30
HDOJ/2535_autoAC.cpp Normal file
View File

@ -0,0 +1,30 @@
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int p[111];
int cmp(int x,int y)
{
if(x>y)
return 0;
else return 1;
}
int main()
{
int n;
while(cin>>n&&n)
{
for(int i=0;i<n;i++)
{
cin>>p[i];
}
int sum=0;
sort(p,p+n,cmp);
for(int i=0;i<(n/2+1);i++)
{
sum+=p[i]/2+1;
}
cout<<sum<<endl;
}
return 0;
}

24
HDOJ/2537_autoAC.cpp Normal file
View File

@ -0,0 +1,24 @@
#include<stdio.h>
int main ()
{
char str[20];
int r, y, n;
int i;
while(~scanf("%d", &n) && n)
{
r = 7;
y = 7;
getchar();
gets(str);
for(i = 0 ; i < n; i++)
switch(str[i])
{
case 'R': r--; break;
case 'Y': y--; break;
case 'B': if(r) printf("Yellow\n"); else printf("Red\n"); break;
case 'L': if(y) printf("Red\n"); else printf("Yellow\n"); break;
default:break;
}
}
return 0;
}

56
HDOJ/2539_autoAC.cpp Normal file
View File

@ -0,0 +1,56 @@
#include<iostream>
#include<string>
#include<vector>
#include<sstream>
using namespace std;
int main()
{
int n;
while(cin>>n&&n!=0)
{
cin.get();
for(int i=1;i<=(n+1)/2;++i)
cout<<i<<" ";
cout<<"Score\n";
vector<char> v1;
vector<char> v2;
int sum1=0,sum2=0;
int i=0;
int m=n;
while(n--)
{
string temp;
vector<string> v;
getline(cin,temp);
istringstream is(temp);
string str;
while(is>>str)
v.push_back(str);
str=v[v.size()-2];
if(i%2==0&&str=="no")
v1.push_back('X');
else if(i%2==0&&str!="no")
{
v1.push_back('O');
++sum1;
}
else if(i%2==1&&str=="no")
v2.push_back('X');
else
{
v2.push_back('O');
++sum2;
}
++i;
}
for(int j=0;j!=v1.size();++j)
cout<<v1[j]<<" ";
cout<<sum1<<endl;
for(int j=0;j!=v2.size();++j)
cout<<v2[j]<<" ";
if(m%2==1)
cout<<"- ";
cout<<sum2<<endl;
}
return 0;
}

36
HDOJ/2540_autoAC.cpp Normal file
View File

@ -0,0 +1,36 @@
#include<stdio.h>
#include<stdlib.h>
typedef struct line
{
double x,h;
}node;
int cmp(const void *a,const void *b)
{
return (*(node *)a).x-(*(node *)b).x;
}
node s[100010];
int main()
{
long n,i,count;
double a,b,temp,max;
while(scanf("%ld",&n)>0,n)
{
count=0;
for(i=0;i<n;i++)
scanf("%lf%lf",&s[i].x,&s[i].h);
scanf("%lf/%lf",&a,&b);
qsort(s,n,sizeof(node),cmp);
max=0;
for(i=0;i<n;i++)
{
temp=s[i].h*b*1.0/a+s[i].x;
if(max<temp)
{
max=temp;
count++;
}
}
printf("%ld\n",count);
}
return 0;
}

80
HDOJ/2542_autoAC.cpp Normal file
View File

@ -0,0 +1,80 @@
#include<stdio.h>
int k;
struct stu
{
int x;
int mx;
}bu[110];
int f(int j)
{
int i,m=0,mxx=0;
for(i=1;i<=100;i++)
if(bu[i].mx)
m=m+(j/i)*bu[i].x;
return m;
}
int main()
{
int i,j,x,t,m,n,P,H,l,mxx;
double s,x1,t1,n1,y1;
while(scanf("%d",&n)&&n)
{
for(i=0;i<=100;i++)
bu[i].mx=bu[i].x=0;
s=0;
for(i=1;i<n;i++)
{
scanf("%d%d",&x,&t);
bu[t].x=bu[t].x+x;
if(x>bu[t].mx)bu[t].mx=x;
x1=x;
t1=t;
s=s+x1/t1;
}
scanf("%d%d",&P,&H);
x1=H;
y1=P;
n1=(x1-y1)/s;
n=n1;
if(n==0)n=1;
l=0;
for (i=n;i<=100+n;i++)
{ m=0;mxx=0;
for(j=1;j<=100;j++)
{
if(bu[j].mx)
{
m=m+(i/j)*bu[j].x;
if(i%j==0&&bu[j].mx>mxx)
mxx=bu[j].x;
}
}
if(m+P>=H&&mxx<=P){l=i;break;}
else if(m+P>=H)
break;
}
if(l==0)
{
printf("Impossible\n");
continue;
}
printf("%d",l);
n1=x1/s;
n=n1;
for(j=n+100;j>=l;j--)
{
m=0; mxx=0;
for(i=1;i<=100;i++)
{
if(bu[i].mx)
{
m=m+(j/i)*bu[i].x;
if(j%i==0&&bu[i].mx>mxx)
mxx=bu[i].mx;
}
}
if(m+P>=H&&mxx<=P&&f(j-1)<H){printf(" %d\n",j);break;}
}
}
return 0;
}

58
HDOJ/2544_autoAC.cpp Normal file
View File

@ -0,0 +1,58 @@
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define INF 1000000
using namespace std;
int city,road;
int mapp[205][205];
int spand[205];
int select[205];
void dij(int city)
{
int minn,k,i,j;
memset(select,0,sizeof(select));
for(i=1;i<=city;i++)
spand[i]=mapp[1][i];
spand[1]=0;select[1]=1;
for(i=2;i<=city;i++)
{
minn=INF;k=-1;
for(j=1;j<=city;j++)
{
if(!select[j]&&spand[j]<minn)
{
k=j;minn=spand[j];
}
}
if(k==-1)break;
select[k]=1;
for(j=1;j<=city;j++)
{
if(spand[j]>spand[k]+mapp[k][j]&&!select[j])
spand[j]=spand[k]+mapp[k][j];
}
}
printf("%d\n",spand[city]);
}
int main()
{
int i;int x,y,z;
while(scanf("%d%d",&city,&road)!=EOF)
{
if(city==0||road==0)
break;
memset(mapp,INF,sizeof(mapp));
memset(spand,INF,sizeof(spand));
for(i=1;i<=road;i++)
{
scanf("%d%d%d",&x,&y,&z);
if(mapp[x][y]>z||mapp[y][x]>z)
{
mapp[x][y]=z;mapp[y][x]=z;
}
}
dij(city);
}
return 0;
}

50
HDOJ/2545_autoAC.cpp Normal file
View File

@ -0,0 +1,50 @@
#include <iostream>
#include <stdio.h>
using namespace std;
#define Max 100001
int father[Max];
void Init()
{
int i;
for(i=0;i<Max;++i)
father[i]=i;
}
int Findfather(int x)
{
int count=0;
while (x!=father[x])
{
count++;
x=father[x];
}
return count;
}
void Union(int x,int y)
{
father[y]=x;
}
int main()
{
int n,m,x,y,i,t1,t2;
while (scanf("%d%d",&n,&m),n&&m)
{
Init();
for(i=0;i<n-1;++i)
{
scanf("%d%d",&x,&y);
Union(x,y);
}
for(i=0;i<m;++i)
{
scanf("%d%d",&x,&y);
t1=Findfather(x);
t2=Findfather(y);
if(t1<=t2)
printf("lxh\n");
else
{
printf("pfz\n");
}
}
}
}

44
HDOJ/2546_autoAC.cpp Normal file
View File

@ -0,0 +1,44 @@
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
#define max(a,b) (a>b?a:b)
int b[2000];
int a[2000];
int main()
{
int i,n,j,m,tm;
while(~scanf("%d",&n))
{
if(n==0)break;
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
scanf("%d",&m);
tm=m-5;
if(tm<0){printf("%d\n",m);continue;}
sort(b,b+n);
memset(a,0,sizeof(a));
for(i=0;i<=tm;i++)
{
if(b[0]<=i)
{
a[i]=b[0];
}
}
for(i=1;i<n-1;i++)
{
for(j=tm;j>=0;j--)
{
if(j>=b[i])
{
a[j]=max(a[j],a[j-b[i]]+b[i]);
}
}
}
m=m-a[tm]-b[n-1];
printf("%d\n",m);
}
return 0;
}

16
HDOJ/2547_autoAC.cpp Normal file
View File

@ -0,0 +1,16 @@
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main()
{
int t;
double a,b,c,d;
scanf("%d",&t);
while(t--)
{
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
printf("%.1lf\n",sqrt((a-c)*(a-c)+(b-d)*(b-d)));
}
return 0;
}

13
HDOJ/2548_autoAC.cpp Normal file
View File

@ -0,0 +1,13 @@
#include<stdio.h>
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
double u,v,w,l;
scanf("%lf %lf %lf %lf",&u,&v,&w,&l);
printf("%.3f\n",(l/(u+v))*w);
}
return 0;
}

23
HDOJ/2549_autoAC.cpp Normal file
View File

@ -0,0 +1,23 @@
#include<stdio.h>
#include<string.h>
int main()
{
char a[100005];
int length_a,i,j,n,t,d;
scanf("%d",&n);
while(n--)
{
scanf("%s%d",&a,&t);
length_a=strlen(a);
for(i=0;a[i]!='.';i++)
{
}
j=i+1;
d=length_a-j;
if(t>d)
printf("0\n");
else
printf("%c\n",a[i+t]);
}
return 0;
}

35
HDOJ/2550_autoAC.cpp Normal file
View File

@ -0,0 +1,35 @@
#include<iostream>
#include<map>
using namespace std;
int main()
{
int t;
while(cin>>t)
{
while(t--)
{
int n;
cin>>n;
map<int,int> m;
while(n--)
{
int a,b;
cin>>a>>b;
m.insert({a,b});
}
auto b=m.cbegin();
for(;b!=m.cend();++b)
{
for(int i=0;i!=b->second;++i)
{
cout<<">+";
for(int j=0;j!=b->first-2;++j)
cout<<"-";
cout<<"+>\n";
}
cout<<endl;
}
}
}
return 0;
}

19
HDOJ/2551_autoAC.cpp Normal file
View File

@ -0,0 +1,19 @@
#include <iostream>
using namespace std;
int main()
{
int t,x;
cin>>t;
while(t--)
{
cin>>x;
long long s=0,i=1;
while(s<x)
{
s+=i*i*i;
i++;
}
cout<<i-1<<endl;
}
return 0;
}

18
HDOJ/2552_autoAC.cpp Normal file
View File

@ -0,0 +1,18 @@
#include<stdio.h>
#include<math.h>
int main()
{
int n;
double u,v,s;
while(scanf("%d",&n)!=EOF)
{
while(n--)
{
scanf("%lf%lf",&s,&u);
v=tan(atan(1/s)-atan(1/u));
v=1.0/v;
printf("%.0lf\n", v*u-s*u-s*v);
}
}
return 0;
}

60
HDOJ/2553_autoAC.cpp Normal file
View File

@ -0,0 +1,60 @@
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
int n,vis[15][15],a[15][15],ok,b[15];
void dfs(int num,int x)
{
if(num==n)
ok++;
if(x==n+1)
return;
for(int i=1;i<=n;i++)
{
int he=1;
if(!vis[x][i])
{
for(int l=1;l<x;l++)
{
if(vis[l][i])
{
he=0;
break;
}
for(int j=1;j<=n;j++)
{
int k1=abs(x-l),k2=abs(i-j);
if(k1==k2 && vis[l][j])
{
he=0;
break;
}
if(he==0)
break;
}
}
if(he)
{
vis[x][i]=1;
dfs(num+1,x+1);
vis[x][i]=0;
}
}
}
}
int main()
{
for(n=1;n<=10;n++)
{
memset(vis,0,sizeof(vis));
ok=0;
dfs(0,1);
b[n]=ok;
}
int z;
while(cin>>z,z)
{
cout<<b[z]<<endl;
}
return 0;
}

7
HDOJ/2554_autoAC.cpp Normal file
View File

@ -0,0 +1,7 @@
#include <stdio.h>
int main()
{
int n;
while(scanf("%d",&n)&&n)
n*(n+1)/2&1?puts("N"):puts("Y");
}

49
HDOJ/2555_autoAC.cpp Normal file
View File

@ -0,0 +1,49 @@
#include<iostream>
#include<vector>
#include<algorithm>
struct point
{
int x;
int y;
};
using namespace std;
int main()
{
int m,n;
while(cin>>m>>n)
{
vector<point> v1;
vector<int> v2;
while(m--)
{
point temp;
cin>>temp.x>>temp.y;
v1.push_back(temp);
}
int **p=new int*[n];
for(int i=0;i!=n;++i)
p[i]=new int[4];
for(int i=0;i!=n;++i)
for(int j=0;j!=4;++j)
cin>>p[i][j];
for(auto b=v1.cbegin();b!=v1.cend();++b)
{
int temp,i;
for(i=0;i!=n;++i)
{
if(b->x>=p[i][0]&&b->x<=p[i][2]&&b->y>=p[i][1]&&b->y<=p[i][3])
{
temp=2*(p[i][2]-p[i][0]+p[i][3]-p[i][1]);
break;
}
}
if(i==n)
temp=0;
v2.push_back(temp);
}
sort(v2.begin(),v2.end());
for(int i=v2.size()-1;i>=0;--i)
cout<<v2[i]<<endl;
}
return 0;
}

222
HDOJ/2556_autoAC.cpp Normal file
View File

@ -0,0 +1,222 @@
#include<iostream>
#include<cstring>
#include<cctype>
#include<stack>
using std::stack;
const int L=1001;
stack<double> OPND;
stack<char> OPTR;
bool IsLegalChar(char c)
{
if(isdigit(c))
return true;
else if(c=='+' || c=='-' || c=='*' || c=='/' || c=='%' || c=='(' || c==')' || c=='.')
return true;
else
return false;
}
double ToOperand(char t[],int length,int count,int point)
{
if(count>1 || t[0]=='.' || t[length-1]=='.')
return -1.0;
double result=0.0,v=1.0;
if(count==0)
{
for(int i=length-1;i>=0;i--)
{
result+=(double)(t[i]-'0')*v;
v*=10;
}
return result;
}
else
{
for(int i=point-1;i>=0;i--)
{
result+=(double)(t[i]-'0')*v;
v*=10;
}
v=0.1;
for(int i=point+1;i<length;i++)
{
result+=(double)(t[i]-'0')*v;
v/=10.0;
}
return result;
}
}
char OperatorJudgement(char optr1,char optr2)
{
if(optr1=='+' || optr1=='-')
{
if(optr2=='+' || optr2=='-' || optr2==')' || optr2=='#')
return '>';
if(optr2=='*' || optr2=='/' || optr2=='%' || optr2=='(')
return '<';
}
if(optr1=='*' || optr1=='/' || optr1=='%')
{
if(optr2=='+' || optr2=='-' || optr2=='*' || optr2=='/' || optr2=='%' || optr2==')' || optr2=='#')
return '>';
if(optr2=='(')
return '<';
}
if(optr1=='(')
{
if(optr2==')')
return '=';
if(optr2=='#')
return 'E';
if(optr2=='+' || optr2=='-' || optr2=='*' || optr2=='/' || optr2=='%' || optr2=='(')
return '<';
}
if(optr1==')')
{
if(optr2=='(')
return 'E';
else
return '>';
}
if(optr1=='#')
{
if(optr2==')')
return 'E';
if(optr2=='#')
return '=';
else
return '<';
}
}
bool IsLegalMod(double opnd1,double opnd2)
{
if(opnd1-(int)opnd1!=0.0 || opnd2-(int)opnd2!=0.0 || opnd2==0.0)
return false;
return true;
}
double Calculate(double opnd1,char optr,double opnd2)
{
if(optr=='+')
return opnd1+opnd2;
if(optr=='-')
return opnd1-opnd2;
if(optr=='*')
return opnd1*opnd2;
if(optr=='/')
return opnd1/opnd2;
if(optr=='%')
return (double)((int)opnd1%(int)opnd2);
}
int main()
{
int i,j;
bool isLegalInfixNonation;
char s[L],t[L];
while(gets(s))
{
isLegalInfixNonation=true;
for(i=0;s[i]!='\0';i++)
{
if(IsLegalChar(s[i])==false)
{
isLegalInfixNonation=false;
break;
}
}
strcat(s,"#");
while(OPND.empty()==false)
OPND.pop();
while(OPTR.empty()==false)
OPTR.pop();
i=0;
OPTR.push('#');
while((s[i]!='#' || OPTR.top()!='#') && isLegalInfixNonation==true)
{
if(isdigit(s[i]) || s[i]=='.')
{
int point=0,count=0;
for(j=0;isdigit(s[i]) || s[i]=='.';i++,j++)
{
t[j]=s[i];
if(s[i]=='.')
count++,point=j;
}
double opnd=ToOperand(t,j,count,point);
if(opnd==-1.0)
{
isLegalInfixNonation=false;
break;
}
else
{
OPND.push(opnd);
}
}
else
{
char optr1=OPTR.top(),optr2=s[i];
char judgement=OperatorJudgement(optr1,optr2);
if(judgement=='E')
isLegalInfixNonation=false;
else
{
if(judgement=='<')
{
OPTR.push(optr2);
i++;
}
else if(judgement=='=')
{
OPTR.pop();
i++;
}
else if(judgement=='>')
{
OPTR.pop();
if(OPND.empty()==true)
{
isLegalInfixNonation=false;
break;
}
double opnd2=OPND.top();
OPND.pop();
if(OPND.empty()==true)
{
isLegalInfixNonation=false;
break;
}
double opnd1=OPND.top();
OPND.pop();
if(optr1=='%')
{
if(IsLegalMod(opnd1,opnd2)==false)
{
isLegalInfixNonation=false;
break;
}
}
if(optr1=='/')
{
if(opnd2==0.0)
{
isLegalInfixNonation=false;
break;
}
}
double result=Calculate(opnd1,optr1,opnd2);
OPND.push(result);
}
}
}
}
if(isLegalInfixNonation==false)
puts("ERROR IN INFIX NOTATION");
else
{
if(OPND.size()!=1)
puts("ERROR IN INFIX NOTATION");
else
printf("%.2lf\n",OPND.top());
}
}
return 0;
}

18
HDOJ/2560_autoAC.cpp Normal file
View File

@ -0,0 +1,18 @@
#include <stdio.h>
int main()
{
int i,c,s,m,n,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
s = 0;
for(i=0;i<m*n;i++)
{
scanf("%d",&c);
if(c) s += c;
}
printf("%d\n",s);
}
return 0;
}

32
HDOJ/2561_autoAC.cpp Normal file
View File

@ -0,0 +1,32 @@
#include<stdio.h>
int main ()
{
int min, temp;
int t, n, a;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
scanf("%d", &min);
scanf("%d", &temp);
if(min > temp)
{
min += temp;
temp = min - temp;
min = min -temp;
}
for(n -= 2; n; n--)
{
scanf("%d",&a);
if(a <= min)
{
temp = min;
min = a;
}
else if(a < temp)
temp = a;
}
printf("%d\n", temp);
}
return 0;
}

22
HDOJ/2562_autoAC.cpp Normal file
View File

@ -0,0 +1,22 @@
#include<stdio.h>
#include<string.h>
int main()
{
int n,i;
scanf("%d",&n);
while(n--)
{
char str[50],z,c;
scanf("%s",str);
z=strlen(str);
for(i=0;i<z;)
{
c=str[i];
str[i]=str[i+1];
str[i+1]=c;
i+=2;
}
printf("%s\n",str);
}
return 0;
}

12
HDOJ/2563_autoAC.cpp Normal file
View File

@ -0,0 +1,12 @@
#include<stdio.h>
int main(){
int m,i,n,a[21];
a[0]=0;a[1]=3;a[2]=7;
scanf("%d",&m);
while(m--){
scanf("%d",&n);
for(i=3;i<=n;i++){
a[i]=2*a[i-1]+a[i-2];}
printf("%d\n",a[n]);}
return 0;
}

22
HDOJ/2564_autoAC.cpp Normal file
View File

@ -0,0 +1,22 @@
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main(void)
{
int n;
char src2[15][15],src[150],*p;
scanf("%d",&n);
getchar();
while(n--)
{
gets(src);
p=strtok(src," ");
while(p!=NULL)
{
printf("%c",toupper(p[0]));
p=strtok(NULL," ");
}
printf("\n");
}
}

9
HDOJ/2565_autoAC.cpp Normal file
View File

@ -0,0 +1,9 @@
X X
X
X X
5*5X
X X
X X
X
X X
X X

22
HDOJ/2566_autoAC.cpp Normal file
View File

@ -0,0 +1,22 @@
#include<stdio.h>
int main()
{
int m,n,x,s,c,i,j;
scanf("%d",&x);
while(x--)
{
c=0;
scanf("%d%d",&n,&m);
s=5*n-m;
for(i=0;i<=n;i++)
{
for(j=0;j<=n;j++)
{
if(4*i+3*j==s && n-i-j>=0)
c++;
}
}
printf("%d\n",c);
}
return 0;
}

25
HDOJ/2567_autoAC.cpp Normal file
View File

@ -0,0 +1,25 @@
#include<stdio.h>
#include<string.h>
int main()
{
int n,a,b,m,i;
char x[100],y[100];
while(scanf("%d",&n)!=EOF)
{
while(n--)
{
scanf("%s%s",x,y);
a=strlen(x);
m=a/2;
b=strlen(y);
for(i=0;i<m;i++)
printf("%c",x[i]);
for(i=0;i<b;i++)
printf("%c",y[i]);
for(i=m;i<a;i++)
printf("%c",x[i]);
printf("\n");
}
}
return 0;
}

25
HDOJ/2568_autoAC.cpp Normal file
View File

@ -0,0 +1,25 @@
#include<stdio.h>
int main()
{
int C;
scanf("%d",&C);
while(C--)
{
int n,k=0;
scanf("%d",&n);
while(n!=0)
{
while(n%2==0)
{
n=n/2;
}
while(n%2!=0)
{
n--;
k++;
}
}
printf("%d\n",k);
}
return 0;
}

16
HDOJ/2569_autoAC.cpp Normal file
View File

@ -0,0 +1,16 @@
#include <iostream>
using namespace std;
int main()
{
int a[41],t,n;
a[1]=3;a[2]=9;
for(int i=3;i<=40;i++)
a[i]=2*a[i-1]+a[i-2];
cin>>t;
while(t--)
{
cin>>n;
cout<<a[n]<<endl;
}
return 0;
}

35
HDOJ/2570_autoAC.cpp Normal file
View File

@ -0,0 +1,35 @@
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
int c,n,v,w,i,V,j;
double pi[101],p;
scanf("%d",&c);
while(c--)
{
scanf("%d%d%d",&n,&v,&w);
for(i=0;i<n;i++)
{
scanf("%lf",&pi[i]);
}
sort(pi,pi+n);
j=n;
while(1)
{
p=0;
for(i=0;i<j;i++)
p+=pi[i];
p/=j;
V=j*v;
j--;
if(j==0||p<=w)
break;
}
if(j==0&&p>w)
printf("0 0.00\n");
else
printf("%d %.2lf\n",V,p/100);
}
return 0;
}

66
HDOJ/2571_autoAC.cpp Normal file
View File

@ -0,0 +1,66 @@
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define max(a,b) (a>b?a:b)
#define smax(a,b,c) (max(max(a,b),c))
int a[22][1010];
int main()
{
int t,i,j,han,lie,maxq,k;
while(~scanf("%d",&t))
{
while(t--)
{
memset(a,0,sizeof(a));
scanf("%d %d",&han,&lie);
for(i=1;i<=han;i++)
{
for(j=1;j<=lie;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=1;i<=han;i++)
{
for(j=1;j<=lie;j++)
{
if(i==1)
{
if(j==1);
else
{
maxq=-200;
for(k=1;k<j;k++)
{
if(j%k==0)
{
if(a[i][k]>maxq)maxq=a[i][k];
}
}
a[i][j]+=max(a[i][j-1],maxq);
}
}
else
{
if(j==1)a[i][j]+=a[i-1][j];
else
{
maxq=-200;
for(k=1;k<j;k++)
{
if(j%k==0)
{
if(a[i][k]>maxq)maxq=a[i][k];
}
}
a[i][j]+=smax(a[i-1][j],a[i][j-1],maxq);
}
}
}
}
printf("%d\n",a[han][lie]);
}
}
return 0;
}

38
HDOJ/2572_autoAC.cpp Normal file
View File

@ -0,0 +1,38 @@
#include<cstring>
#include<iostream>
using namespace std;
int main()
{
int T,i,j,n;
cin>>T;
while(T--)
{
string s,s1,s2,s3,str;
cin>>s1>>s2>>s3;
n=s1.length();
str=s1+"#";
for(i=0; i<n; i++)
{
for(j=i+1; j<=n; j++)
{
s=s1.substr(i,j-i);
if(s.find(s2)!=-1&&s.find(s3)!=-1)
{
if(s.length()<str.length())
{
str=s;
}
else if(s.length()==str.length())
{
if(s<str)
str=s;
}
}
}
}
if(str.length()==n+1) cout<<"No"<<endl;
else
cout<<str<<endl;
}
return 0;
}

59
HDOJ/2573_autoAC.cpp Normal file
View File

@ -0,0 +1,59 @@
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
const int MAX=101;
char s[MAX];
char k[MAX];
using namespace std;
int main()
{
int t,n,m,i,j,q,p;
cin>>t;
while(t--)
{
cin>>n;
p=1;
q=0;
getchar();
while(n--)
{
gets(s);
m=strlen(s);
if(strcmp(s,"Caps")==0)
{
p=-p;
continue;
}
if(m>1)
{
for(i=0;i<m;i++)
{
if(s[i]==' ')
break;
}
if(p==1)
k[q++]=s[i+1]-32;
else
k[q++]=s[i+1];
continue;
}
if(p==1)
k[q++]=s[0];
else
k[q++]=s[0]-32;
}
for(j=0;j<q;j++)
cout<<k[j];
cout<<endl;
}
return 0;
}

69
HDOJ/2574_autoAC.cpp Normal file
View File

@ -0,0 +1,69 @@
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<iomanip>
const int MAX=1001;
using namespace std;
typedef struct Student
{
char name[21];
__int64 vote;
__int64 count;
}student;
student s[MAX];
bool cmp(student a,student b)
{
if(a.count==b.count)
{
if(strcmp(a.name,b.name)<0)
return true;
else
return false;
}
else
return a.count>b.count;
}
int main()
{
__int64 t,n,m,i,j;
cin>>t;
while(t--)
{
cin>>n;
getchar();
for(i=0;i<n;i++)
{
cin>>s[i].name>>s[i].vote;
if(s[i].vote==0)
{
s[i].count=0;
continue;
}
int sum=0;
m=s[i].vote;
for(j=2;j*j<=m;j++)
{
if(m%j==0)
{
sum+=1;
while(m%j==0)
m/=j;
}
}
if(m!=1)
sum+=1;
s[i].count=sum;
}
sort(s,s+n,cmp);
cout<<s[0].name<<endl;
}
return 0;
}

19
HDOJ/2575_autoAC.cpp Normal file
View File

@ -0,0 +1,19 @@
#include <stdio.h>
int main()
{
int num,d,t;
int a[10]={0,1,2,2,4,4,6,6,10,10};
scanf("%d",&t);
while(t--)
{
d=0;
scanf("%d",&num);
while(num)
{
d=num%10;
num/=10;
}
printf("%d\n",a[d]);
}
return 0;
}

51
HDOJ/2576_autoAC.cpp Normal file
View File

@ -0,0 +1,51 @@
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std;
const int MAX=4;
const int mod=20090524;
__int64 sum[MAX][MAX],array[MAX][MAX];
void MatrixMult(__int64 a[MAX][MAX],__int64 b[MAX][MAX]){
__int64 c[MAX][MAX]={0};
for(int i=0;i<MAX;++i){
for(int j=0;j<MAX;++j){
for(int k=0;k<MAX;++k){
c[i][j]+=a[i][k]*b[k][j];
}
}
}
for(int i=0;i<MAX;++i){
for(int j=0;j<MAX;++j)a[i][j]=c[i][j]%mod;
}
}
__int64 MatrixPow(int k){
for(int i=0;i<MAX;++i){
for(int j=0;j<MAX;++j)sum[i][j]=(i == j);
}
array[0][0]=array[0][1]=array[0][2]=1,array[0][3]=0;
array[1][1]=array[1][2]=1,array[1][0]=array[1][3]=0;
array[2][2]=array[2][3]=1,array[2][0]=array[2][1]=0;
array[3][3]=1,array[3][0]=array[3][1]=array[3][0]=0;
while(k){
if(k&1)MatrixMult(sum,array);
MatrixMult(array,array);
k>>=1;
}
return (sum[0][2]+sum[0][3])%mod;
}
int main(){
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
printf("%I64d\n",MatrixPow(n));
}
return 0;
}

36
HDOJ/2577_autoAC.cpp Normal file
View File

@ -0,0 +1,36 @@
#include<iostream>
using namespace std;
int main()
{
int T;
scanf("%d", &T);
getchar();
while(T--){
char s[2000];
gets(s);
int result = strlen(s), i, len;
len = result;
int lian_xu = 0;
for(i = 0; i < len; ++i){
if(s[i] >= 'A' && s[i] <= 'Z'){
if(lian_xu < 2){
result++;
}
lian_xu++;
}
else{
if(i+1 < len && lian_xu >= 2 ){
if(s[i+1] >= 'a' && s[i+1] <= 'z'){
lian_xu = 0;
}
else{
result++;
}
}
else lian_xu = 0;
}
}
printf("%d\n", result);
}
return 0;
}

44
HDOJ/2578_autoAC.cpp Normal file
View File

@ -0,0 +1,44 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int a[200010],n;
int find(int x)
{
int i=1,j=n;
while(i<=j)
{
int mid=(i+j)/2;
if(a[mid]==x||a[mid+1]==x)
return 1;
else if(a[mid]<x&&a[mid+1]>x)
return 0;
else if(a[mid]>x)
j=mid-1;
else
i=mid+1;
}
return 0;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int k;
cin>>n>>k;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1);
__int64 sum=0;
a[0]=-1;
for(int i=1;i<=n;i++)
{
if(a[i]!=a[i-1]&&a[i]<=k&&find(k-a[i]))
sum++;
}
printf("%I64d\n",sum);
}
}

95
HDOJ/2579_autoAC.cpp Normal file
View File

@ -0,0 +1,95 @@
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<bitset>
#include<vector>
#include<cmath>
#define INF 0x7fffffff
#define eps 1e-8
#define LL long long
#define PI 3.141592654
#define CLR(a,b) memset(a,b,sizeof(a))
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define FOR_(i,a,b) for(int i=a;i>=b;i--)
#define sf scanf
#define pf printf
#define all(v) (v).begin(),(v).end()
#define acfun std::ios::sync_with_stdio(false)
#define SIZE (100 +2)
#define MOD 1000000007
using namespace std;
char g[SIZE][SIZE];
int t,n,m;
int xx[]={0,0,-1,1};
int yy[]={-1,1,0,0};
struct node
{
int x,y,s;
void init(int _x,int _y,int _s=0)
{
x=_x,y=_y,s=_s;
}
};
node Y,G;
void bfs()
{
bool vis[SIZE][SIZE][11];
CLR(vis,0);
queue<node>q;
q.push(Y);
vis[Y.x][Y.y][0]=1;
while(!q.empty())
{
node tmp=q.front();
q.pop();
//pf("%d %d=%d\n",tmp.x+1,tmp.y+1,tmp.s);
if(tmp.x==G.x&&tmp.y==G.y)
{
pf("%d\n",tmp.s);
return;
}
FOR(k,0,4)
{
int x=tmp.x+xx[k];
int y=tmp.y+yy[k];
int s=tmp.s+1;
if(x<0||x>=n||y<0||y>=m||vis[x][y][s%t])
continue;
if(g[x][y]=='#'&&s%t!=0)continue;
node now;
vis[x][y][s%t]=1;
now.init(x,y,s);
q.push(now);
}
}
puts("Please give me another chance!");
}
int main()
{
int T;
sf("%d",&T);
while(T--)
{
sf("%d%d%d",&n,&m,&t);
FOR(i,0,n)
{
sf("%s",g[i]);
FOR(j,0,m)
{
if(g[i][j]=='Y')
Y.init(i,j);
else if(g[i][j]=='G')
G.init(i,j);
}
}
bfs();
}
return 0;
}

46
HDOJ/2580_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 2000000
using namespace std;
int no[N],maxok[N];
int solve(int n,int k){
if(n<=k+1)return 0;
int x=0,y=0;
no[0]=maxok[0]=1;
while(no[x]<n)
{
x++;
no[x]=maxok[x-1]+1;
while(no[y+1]*k<no[x])
y++;
if(no[y]*k<no[x])
maxok[x]=no[x]+maxok[y];
else
maxok[x]=no[x];
}
if(no[x]==n) return 0;
int ans;
while(n)
{
if(n>=no[x])
{
ans=no[x];
n-=no[x];
}
x--;
}
return ans;
}
int main(){
int t,n,k,Case=0;
scanf("%d",&t);
while(++Case<=t){
scanf("%d%d",&n,&k);
printf("Case %d: ",Case);
int tmp=solve(n,k);
if(tmp)printf("%d\n",tmp);
else printf("lose\n");
}
return 0;
}

154
HDOJ/2581_autoAC.cpp Normal file
View File

@ -0,0 +1,154 @@
#include<iostream>
#include<math.h>
using namespace std;
int t, w, l, b;
struct POINT
{
double x, y, r;
} node[12];
int parent[12];
int bomb[12][12];
int visit[12];
int isbomb[2][12];
double dist(POINT a, POINT b)
{
return sqrt(1.0 * ((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)));
}
void UFset()
{
int i;
for(i = 0; i < b; i++)
parent[i] = -1;
}
int Find(int x)
{
int s;
for(s = x; parent[s] >= 0; s = parent[s]);
while(s != x)
{
int tmp = parent[x];
parent[x] = s;
x = tmp;
}
return s;
}
void Union(int r1, int r2)
{
if(parent[r1] > parent[r2])
{
parent[r2] += parent[r1];
parent[r1] = r2;
}
else
{
parent[r1] += parent[r2];
parent[r2] = r1;
}
}
void kruskal()
{
int i, j, k;
double s;
UFset();
for(i = 0; i < b; i++)
{
for(j = 0; j < b; j++)
{
if(i != j)
{
s = dist(node[i], node[j]) - node[i].r - node[j].r;
if(s <= 0 && Find(i) != Find(j))
Union(Find(i), Find(j));
}
}
}
}
int main()
{
int i, j, k, r, wa, wb;
double ab;
scanf("%d", &t);
while(t--)
{
scanf("%d%d%d", &w, &l, &b);
for(i = 0; i < b; i++)
scanf("%lf%lf%lf", &node[i].x, &node[i].y, &node[i].r);
kruskal();
r = k = 0;
memset(bomb, -1, 12 * 12 * sizeof(int));
memset(isbomb, -1, 2 * 12 * sizeof(int));
memset(visit, 0, sizeof(visit));
for(i = 0; i < b; i++)
{
if(visit[i]) continue;
visit[i] = 1;
bomb[k][r++] = i;
for(j = 0; j < b; j++)
{
if(i == j) continue;
if(Find(i) == Find(j))
{
bomb[k][r++] = j;
visit[j] = 1;
}
}
k++;
r = 0;
}
wa = wb = 0;
for(i = 0; i < k; i++)
{
r = 0;
for(j = 0; bomb[i][j] >= 0; j++)
{
if(node[bomb[i][j]].r - node[bomb[i][j]].x >= 0)
{
r = 1;
break;
}
}
if(r == 1)
{
for(j = 0; bomb[i][j] >= 0; wa++, j++)
isbomb[0][wa] = bomb[i][j];
}
r = 0;
for(j = 0; bomb[i][j] >= 0; j++)
{
if(w - node[bomb[i][j]].r - node[bomb[i][j]].x <= 0)
{
r = 1;
break;
}
}
if(r == 1)
{
for(j = 0; bomb[i][j] >= 0; wb++, j++)
isbomb[1][wb] = bomb[i][j];
}
}
ab = w;
for(i = 0; isbomb[0][i] >= 0; i++)
{
if(w - node[isbomb[0][i]].r - node[isbomb[0][i]].x < ab)
ab = w - node[isbomb[0][i]].r - node[isbomb[0][i]].x;
for(j = 0; isbomb[1][j] >= 0; j++)
{
double s = dist(node[isbomb[0][i]], node[isbomb[1][j]]) - node[isbomb[0][i]].r
- node[isbomb[1][j]].r;
if(s < ab)
ab = s;
}
}
for(i = 0; isbomb[1][i] >= 0; i++)
{
if(node[isbomb[1][i]].x - node[isbomb[1][i]].r < ab)
ab = node[isbomb[1][i]].x - node[isbomb[1][i]].r;
}
ab = 0.5 * ab;
if(ab < 0) ab = 0;
r = ceil(ab);
printf("%d\n", r);
}
return 0;
}

58
HDOJ/2582_autoAC.cpp Normal file
View File

@ -0,0 +1,58 @@
#include<stdio.h>
__int64 prime[100000];
bool flag[1000001];
__int64 sum[1000001];
void init()
{
__int64 i,j,num=0;
for(i=2;i<=1000000;i++)
{
if(!flag[i])
{
prime[num++]=i;
for(j=i*i;j<=1000000;j=j+i)
flag[j]=true;
}
}
}
__int64 solve(__int64 n)
{
__int64 i,k,ret=0;
for(i=0;prime[i]*prime[i]<=n;i++)
{
if(n%prime[i]==0)
{
n=n/prime[i];
while(n%prime[i]==0)
n=n/prime[i];
k=prime[i];
ret=ret+1;
}
if(ret>=2)
return 1;
}
if(n>1)
{
ret=ret+1;
k=n;
}
if(ret>=2)
return 1;
else
return k;
}
int main()
{
__int64 n,i;
init();
for(i=3;i<=1000000;i++)
{
if(flag[i])
sum[i]=sum[i-1]+solve(i);
else
sum[i]=sum[i-1]+i;
}
while(scanf("%d",&n)!=EOF)
printf("%I64d\n",sum[n]);
return 0;
}

26
HDOJ/2583_autoAC.cpp Normal file
View File

@ -0,0 +1,26 @@
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int dp[110][110];
int main()
{
memset(dp, 0, sizeof(dp));
for(int i=1; i<=100; i++)
{
dp[i][0] = dp[i][i-1] = 1;
}
for(int i=1; i<=100; i++)
{
for(int j=1; j<=100; j++)
{
dp[i][j] = (dp[i-1][j]*(j+1) + dp[i-1][j-1]*(i-j)) % 2009;
}
}
int n,k;
while(scanf("%d %d",&n,&k) != EOF)
{
cout<<dp[n][k]<<endl;
}
return 0;
}

74
HDOJ/2584_autoAC.cpp Normal file
View File

@ -0,0 +1,74 @@
#include<iostream>
using namespace std;
int ran[105][105];
int b[105];
int cmp(int *a,int *b,int n)
{
int i;
for(i=1;i<=n;i++)
if(a[i]>b[i])
break;
if(i==n+1)
return -1;
for(i=1;i<=n;i++)
if(a[i]<b[i])
break;
if(i==n+1)
return 1;
return 0;
}
int main()
{
int c,i,j,max,temp,m,n;
cin>>c;
while(c--)
{
cin>>m>>n;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
scanf("%d",&temp);
ran[temp][i]=j;
}
for(i=1;i<=m;i++)
{
max=i;
for(j=1+i;j<=m;j++)
if(cmp(ran[j],ran[max],n)==1)
max=j;
if(max!=i)
{
for(j=1;j<=n;j++)
{
temp=ran[max][j];
ran[max][j]=ran[i][j];
ran[i][j]=temp;
}
}
}
for(i=1;i<=m;i++)
b[i]=1;
for(i=1;i<=m;i++)
{
max=-1;
for(j=1;j<i;j++)
{
if(cmp(ran[i],ran[j],n)==-1)
{
if(b[j]>max)
max=b[j];
}
}
if(max+1>b[i])
b[i]=max+1;
}
max=-1;
for(i=1;i<=m;i++)
{
if(b[i]>max)
max=b[i];
}
cout<<max<<endl;
}
return 0;
}

44
HDOJ/2585_autoAC.cpp Normal file
View File

@ -0,0 +1,44 @@
#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
int find(string a,string b)
{
int i,j;
for(i=0;i<a.length();i++)
{
if(a[i]=='*')
{
if(i==a.length()-1)
return true;
string c=a.substr(i+1);
for(j=i;j<b.length();j++)
if(find(c,b.substr(j))) return 1;
}
else
{
if(i>=b.length()) return 0;
if(a[i]=='?') continue;
if(a[i]!=b[i]) return 0;
}
}
return 1;
}
int main()
{
int ans;
string a,b;
while(cin>>a)
{
int k;
ans=0;
scanf("%d",&k);
while(k--)
{
cin>>b;
if(find(a,b)) ans+=1;
}
printf("%d\n",ans);
}
return 0;
}

65
HDOJ/2586_autoAC.cpp Normal file
View File

@ -0,0 +1,65 @@
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 40001;
struct Edge {
int v, w, next;
}edge[2 * N];
int n, m, size, head[N];
int x[N], y[N], z[N], root[N], dis[N];
bool mark[N];
void Insert(int u, int v, int w) {
edge[size].v = v; edge[size].w = w;
edge[size].next = head[u]; head[u] = size++ ;
edge[size].v = u; edge[size].w = w;
edge[size].next = head[v]; head[v] = size++ ;
}
int Find(int x){
if(root[x] != x) {
return root[x] = Find(root[x]);
}
return root[x];
}
void LCA_Tarjan(int k) {
mark[k] = true;
root[k] = k;
for(int i = 1; i <= m; i++ ) {
if(x[i] == k && mark[y[i]])z[i] =Find(y[i]);
if(y[i] == k && mark[x[i]]) z[i] =Find(x[i]);
}
for(int i = head[k]; i != -1; i = edge[i].next) {
if(!mark[edge[i].v]) {
dis[edge[i].v] = dis[k] + edge[i].w;
LCA_Tarjan(edge[i].v);
root[edge[i].v] = k;
}
}
}
int main() {
int cas, u, v, w, i;
scanf("%d", &cas);
while(cas--) {
scanf("%d %d", &n, &m);
size = 0;
memset(head, -1, sizeof(head));
for(i = 1; i < n; i++ ) {
scanf("%d %d %d", &u, &v, &w);
Insert(u, v, w);
}
for(i = 1; i <= n; i++ ) {
x[i] = y[i] = z[i] = 0;
}
for(i = 1; i <= m; i++ ) {
scanf("%d %d", &u, &v);
x[i] = u; y[i] = v;
}
memset(mark, false, sizeof(mark));
dis[1] = 0;
LCA_Tarjan(1);
for(i = 1; i <= m; i++ ) {
printf("%d\n", dis[x[i]] + dis[y[i]] - 2 * dis[z[i]]);
}
}
return 0;
}

35
HDOJ/2587_autoAC.cpp Normal file
View File

@ -0,0 +1,35 @@
#include<cstdio>
const int mod = 20090308;
const int MAXN=1002;
long long a[MAXN],b[MAXN],c[MAXN],d[MAXN],m;
int n;
int main()
{
a[0]=b[0]=c[0]=d[0]=0;
while(scanf("%d%I64d",&n,&m)!=EOF)
{
if(m==1)
{
a[1]=2;
for(int i=2;i<=n;i++)
a[i] =( 2 * a[i-1]+ 2* a[i-2]+3 ) % mod;
}
else
{
a[1]=b[1]=c[1]=2*m;
d[1]=m;
for(int i=2;i<=n;i++)
{
d[i]= ( 2*b[i-1]+ m ) % mod;
b[i]= ( 2*b[i-1]+2*m+d[i-1] ) % mod;
if(m==2)
c[i]=( 2* b[i-1]+ 4+ 2* d[i-1]+2* b[i-2] + 4+2* d[i-2] +a[i-2] ) %mod;
else
c[i]=( b[i-1]+ m+4*(d[i-1]+m)+c[i-1] ) % mod;
a[i]= ( b[i-1]+ d[i-1]+ c[i-1]+2*m ) % mod;
}
}
printf("%d\n",a[n]);
}
return 0;
}

44
HDOJ/2588_autoAC.cpp Normal file
View File

@ -0,0 +1,44 @@
#include<iostream>
#include<cstdio>
using namespace std;
#define LL long long
#define M 1000010
int euler(int n)
{
int res=n,i;
for(i=2;i*i<=n;i++)
{
if(n%i==0)
{
res=res-res/i;
while(n%i==0)
n/=i;
}
}
if(n>1)
res=res-res/n;
return res;
}
int main()
{
int t,p,q,sum;
LL n,m;
cin>>t;
while(t--)
{
sum=0;
cin>>n>>m;
for(int i=1;i*i<=n;i++)
{
if(n%i==0)
{
if(i>=m)
sum=sum+euler(n/i);
if((n/i)!=i&&(n/i)>=m)
sum=sum+euler(i);
}
}
cout<<sum<<endl;
}
return 0;
}

22
HDOJ/2589_autoAC.cpp Normal file
View File

@ -0,0 +1,22 @@
#include<stdio.h>
#include<string.h>
int main()
{
int t,l,n,a[21][21],i,j,k,o;
scanf("%d",&t);
while(t--)
{
memset(a,0,sizeof(int)*21*21);
k=0;
scanf("%d%d",&l,&n);
o=n;
while(o--)
{
scanf("%d%d",&i,&j);
if(a[i][j]==0) a[i][j]=1;
else k++;
}
if((n-k)%4==0)printf("YES\n");
else printf("NO\n");
}
}

36
HDOJ/2593_autoAC.cpp Normal file
View File

@ -0,0 +1,36 @@
#include<iostream>
#include<set>
#include<algorithm>
using namespace std;
int x[10009];
int main()
{
int n,i,j;
set<int>set1;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
{
scanf("%d",&x[i]);
set1.insert(x[i]);
}
sort(&x[0],&x[n]);
int ok=0;
for(i=0;i<n;i++)
{
for(j=i+2;j<n;j++)
{
if((x[i]+x[j])%2==0&&set1.find((x[i]+x[j])/2)!=set1.end())
{
ok=1;
printf("Sequence is not 3-free. Witness: %d,%d,%d.\n",x[i],(x[i]+x[j])/2,x[j]);
break;
}
}
if(ok==1)break;
}
if(ok==0)
printf("Sequence is 3-free.\n");
}
return 0;
}

66
HDOJ/2594_autoAC.cpp Normal file
View File

@ -0,0 +1,66 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=50001;
char p[N];
char t[N];
int nex[N];
int m;
int n;
void get_nex()
{
int i=0;
int j=-1;
nex[i]=-1;
while(i<m)
{
if(j==-1||p[i]==p[j])
{
++i;
++j;
if(p[i]!=p[j])
nex[i]=j;
else
nex[i]=nex[j];
}
else
j=nex[j];
}
}
void kmp()
{
int i=0;
int j=0;
while(i<n)
{
if(j==-1||t[i]==p[j])
{
++i;
++j;
}
else
j=nex[j];
}
if(!j)
cout<<j<<endl;
else
{
for(int k=0;k<j;k++)
cout<<p[k];
cout<<" "<<j<<endl;
}
}
int main()
{
while(cin>>p)
{
cin>>t;
n=strlen(t);
m=strlen(p);
get_nex();
kmp();
}
return 0;
}

74
HDOJ/2597_autoAC.cpp Normal file
View File

@ -0,0 +1,74 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<set>
#include<string>
#include<iostream>
using namespace std;
#define size 600
char s[size];
struct haha
{
set<string>sset;
}mp[size][size];
void out();
int main()
{
int x=-100,y=-100,i,j,flag=0;
while(scanf("%s",s)!=EOF)
{
if(s[0]>='a'&&s[0]<='z')
{
mp[x][y].sset.insert(s);
flag=1;
}
else
{
int n=0;
if(flag==1) {x=-100;y=-100;flag=0;}
if(s[0]=='-')
{
if(x==-100) {x=-1; continue;}
else
{
if(x==-1)
{
out();
x=-100;
y=-100;
for(i=0;i<size;i++)
for(j=0;j<size;j++)
mp[i][j].sset.clear();
continue;
}
}
}
i=0;
while(s[i]!='\0')
{
n=n*10+s[i]-'0';
i++;
}
if(x==-100) {x=n;continue;}
else y=n;
if(x>y) {int temp=x;x=y;y=temp;}
}
}
return 0;
}
void out()
{
int i,j;
set<string>::iterator it;
for(i=0;i<size;i++)
for(j=i;j<size;j++)
{
if(!mp[i][j].sset.empty())
{
printf("%d %d",i,j);
for(it=mp[i][j].sset.begin();it!=mp[i][j].sset.end();it++)
cout<<" "<<*it;
printf("\n");
}
}
}

112
HDOJ/2598_autoAC.cpp Normal file
View File

@ -0,0 +1,112 @@
#include<stdio.h>
#include<string.h>
char s[10000];
int map[111][111];
int main()
{
int n,i,j,flag=0,x,y,xx,yy,cg;
while(scanf("%s",s)!=EOF)
{
if(strcmp(s,"MOVE")==0)
{
if(flag==1)
{
if(cg==1) printf("MOVED\n");
else printf("FAILED\n");
cg=1;
flag=0;
}
}
else if(strcmp(s,"SHOW")==0)
{
if(flag==1)
{
if(cg==1) printf("MOVED\n");
else printf("FAILED\n");
cg=1;
flag=0;
}
for(i=1;i<=n;i++)
{
for(j=1;j<n;j++)
printf("%d ",map[i][j]);
printf("%d\n",map[i][j]);
}
printf("\n");
}
else if(strcmp(s,"up")==0)
{
if(cg==0) continue;
flag=1;
xx=x-1;yy=y;
if(xx<1||xx>n||yy<1||yy>n) cg=0;
else
{
int temp;
temp=map[x][y];map[x][y]=map[xx][yy];map[xx][yy]=temp;
x=xx;y=yy;
}
}
else if(strcmp(s,"left")==0)
{
if(cg==0) continue;
flag=1;
xx=x;yy=y-1;
if(xx<1||xx>n||yy<1||yy>n) cg=0;
else
{
int temp;
temp=map[x][y];map[x][y]=map[xx][yy];map[xx][yy]=temp;
x=xx;y=yy;
}
}
else if(strcmp(s,"right")==0)
{
if(cg==0) continue;
flag=1;
xx=x;yy=y+1;
if(xx<1||xx>n||yy<1||yy>n) cg=0;
else
{
int temp;
temp=map[x][y];map[x][y]=map[xx][yy];map[xx][yy]=temp;
x=xx;y=yy;
}
}
else if(strcmp(s,"down")==0)
{
if(cg==0) continue;
flag=1;
xx=x+1;yy=y;
if(xx<1||xx>n||yy<1||yy>n) cg=0;
else
{
int temp;
temp=map[x][y];map[x][y]=map[xx][yy];map[xx][yy]=temp;
x=xx;y=yy;
}
}
else
{
if(flag==1)
{
if(cg==1) printf("MOVED\n");
else printf("FAILED\n");
}
flag=0;
n=0;cg=1;
for(i=0;s[i]!='\0';i++) n=n*10+s[i]-'0';
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&map[i][j]);
if(map[i][j]==0) {x=i;y=j;}
}
}
}
if(flag==1)
{
if(cg==1) printf("MOVED\n");
else printf("FAILED\n");
}
}

55
HDOJ/2599_autoAC.cpp Normal file
View File

@ -0,0 +1,55 @@
#include<stdio.h>
#include<string.h>
int map[111][111];
int main()
{
int n,i,j,m;
char s[100];
while(scanf("%s",s)!=EOF)
{
if(strcmp(s,"SHOW")==0)
{
for(i=1;i<=n;i++)
{
for(j=1;j<n;j++)
printf("%d ",map[i][j]);
printf("%d\n",map[i][j]);
}
printf("\n");
}
else if(strcmp(s,"NEIGHBORS")==0)
{
scanf("%d",&m);
int flag=1;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
if(map[i][j]==m)
{flag=0;break;}
if(flag==0) break;
}
int out[5],k,cnt=0;
if(map[i-1][j]!=-1) out[cnt++]=map[i-1][j];
if(map[i][j-1]!=-1) out[cnt++]=map[i][j-1];
if(map[i][j+1]!=-1) out[cnt++]=map[i][j+1];
if(map[i+1][j]!=-1) out[cnt++]=map[i+1][j];
for(k=0;k<cnt-1;k++) printf("%d ",out[k]);
printf("%d\n",out[cnt-1]);
}
else
{
int k=0;
n=0;
while(s[k]!='\0')
{
n=n*10+(s[k]-'0');
k++;
}
memset(map,-1,sizeof(map));
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&map[i][j]);
}
}
return 0;
}