commit
05944d4866
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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]);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
@
|
||||
@@@
|
||||
@
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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]);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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]);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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]);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
int main()
|
||||
{
|
||||
char a[35];
|
||||
int i,c,l;
|
||||
while(gets(a)&&a[0]!='#')
|
||||
{
|
||||
c=0;
|
||||
l=strlen(a);
|
||||
for(i=0;i<=l-2;i++)
|
||||
if(a[i]=='1')
|
||||
c++;
|
||||
if(a[l-1]=='e')
|
||||
{
|
||||
if(c%2!=0)
|
||||
a[l-1]='1';
|
||||
else
|
||||
a[l-1]='0';
|
||||
}
|
||||
else
|
||||
{
|
||||
if(c%2!=0)
|
||||
a[l-1]='0';
|
||||
else
|
||||
a[l-1]='1';
|
||||
}
|
||||
for(i=0;i<l;i++)
|
||||
{
|
||||
if(i==l-1)
|
||||
printf("%c\n",a[i]);
|
||||
else
|
||||
printf("%c",a[i]);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
# include <stdio.h>
|
||||
# include <math.h>
|
||||
int r, x, y ;
|
||||
double dist(double ax, double ay, double bx, double by)
|
||||
{
|
||||
return sqrt((ax-bx)*(ax-bx) + (ay-by)*(ay-by)) ;
|
||||
}
|
||||
void work(int cas)
|
||||
{
|
||||
double sx = x, sy = y, ex, ey, t ;
|
||||
int flag = 0 ;
|
||||
while (1)
|
||||
{
|
||||
scanf ("%lf%lf", &ex, &ey) ;
|
||||
if (ex < 0 && ey < 0) break ;
|
||||
if (flag) continue ;
|
||||
if (dist(sx, sy, ex, ey) <= r+1.0)
|
||||
{
|
||||
printf ("Firefly %d caught at (%.0lf,%.0lf)\n", cas, ex, ey) ;
|
||||
flag = 1 ;
|
||||
}
|
||||
t = r*1.0 / dist(sx, sy, ex, ey) ;
|
||||
sx = sx + (ex-sx)*t ;
|
||||
sy = sy + (ey-sy)*t ;
|
||||
}
|
||||
if (!flag) printf ("Firefly %d not caught\n", cas) ;
|
||||
}
|
||||
int main ()
|
||||
{
|
||||
int nCase = 1 ;
|
||||
while (scanf ("%d%d%d", &r, &x, &y) && (r||x||y))
|
||||
work(nCase++) ;
|
||||
return 0 ;
|
||||
}
|
|
@ -0,0 +1,171 @@
|
|||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cctype>
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
using namespace std;
|
||||
int R,C;
|
||||
char Map[10][10];
|
||||
int Move2[6][2] = {{-1,-1},{-1,0},{0,-1},{0,1},{1,-1},{1,0}};
|
||||
int Move1[6][2] = {{-1,0},{-1,1},{0,-1},{0,1},{1,0},{1,1}};
|
||||
char Path[20];
|
||||
bool bVisited[10][10];
|
||||
int ileft,iright;
|
||||
int cnum,pos;
|
||||
inline bool InBoundary(int x,int y)
|
||||
{
|
||||
if(x >= 0 && x<R && y>=0 && y<C+x%2)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
bool Evaluate(int start,int n,int &value)
|
||||
{
|
||||
stack<int> s1;
|
||||
stack<char> s2;
|
||||
int i = start;
|
||||
int t = 0;
|
||||
t = Path[i]-'0'; i++;
|
||||
if(isdigit(Path[i]))
|
||||
{
|
||||
if(t == 0) return false;
|
||||
}
|
||||
while(isdigit(Path[i]))
|
||||
{
|
||||
t*=10; t+=Path[i]-'0';
|
||||
if(t >= 100)
|
||||
return false;
|
||||
i++;
|
||||
}
|
||||
s1.push(t);
|
||||
while(1)
|
||||
{
|
||||
if(i > n-1)
|
||||
{
|
||||
if(!isdigit(Path[n-1])) return false;
|
||||
break;
|
||||
}
|
||||
if(!isdigit(Path[i]))
|
||||
{
|
||||
if(!s2.empty()) return false;
|
||||
else s2.push(Path[i]);
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
t = Path[i]-'0'; i++;
|
||||
if(isdigit(Path[i]))
|
||||
{
|
||||
if(t == 0)
|
||||
return false;
|
||||
}
|
||||
while(isdigit(Path[i]))
|
||||
{
|
||||
t*=10; t+=Path[i]-'0';
|
||||
if(t >= 100)
|
||||
return false;
|
||||
i++;
|
||||
}
|
||||
char op = s2.top();s2.pop();
|
||||
int re = s1.top();s1.pop();
|
||||
switch(op)
|
||||
{
|
||||
case '+':s1.push(re+t); break;
|
||||
case '-':s1.push(re-t);break;
|
||||
case '*':s1.push(re*t);break;
|
||||
case '/':
|
||||
if(t == 0 || re%t!=0)
|
||||
return false;
|
||||
s1.push(re/t);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
value = s1.top();
|
||||
return true;
|
||||
}
|
||||
bool DFS(int i,int j,int index)
|
||||
{
|
||||
if(index == cnum)
|
||||
{
|
||||
Path[index] = 0;
|
||||
if(Evaluate(pos,index,iright) && iright == ileft)
|
||||
return true;
|
||||
else return false;
|
||||
}
|
||||
if(Map[i][j] == '=')
|
||||
{
|
||||
if(!Evaluate(0,index-1,ileft))
|
||||
return false;
|
||||
pos = index;
|
||||
}
|
||||
if(i%2==1)
|
||||
{
|
||||
for(int k = 0; k<6; k++)
|
||||
{
|
||||
int x = Move2[k][0]+i; int y = Move2[k][1]+j;
|
||||
if(InBoundary(x,y) && bVisited[x][y] == false)
|
||||
{
|
||||
bVisited[x][y] = true;
|
||||
Path[index] = Map[x][y];
|
||||
if(DFS(x,y,index+1))
|
||||
return true;
|
||||
bVisited[x][y] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(int k = 0; k <6; k++)
|
||||
{
|
||||
int x = Move1[k][0]+i; int y = Move1[k][1]+j;
|
||||
if(InBoundary(x,y) && bVisited[x][y] == false)
|
||||
{
|
||||
bVisited[x][y] = true;
|
||||
Path[index] = Map[x][y];
|
||||
if(DFS(x,y,index+1))
|
||||
return true;
|
||||
bVisited[x][y] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while(scanf("%d",&R)==1 && R!=0)
|
||||
{
|
||||
scanf("%d",&C);
|
||||
int i,j;
|
||||
cnum = C*R+R/2;
|
||||
getchar();
|
||||
for(i = 0; i<R; i++)
|
||||
{
|
||||
if(i%2 == 0)
|
||||
{
|
||||
for(j = 0; j<C; j++)
|
||||
{ scanf(" %c",&Map[i][j]); bVisited[i][j] = false;}
|
||||
getchar();
|
||||
}
|
||||
else
|
||||
{
|
||||
for(j = 0; j<=C; j++)
|
||||
{scanf("%c",&Map[i][j]); bVisited[i][j] = false;getchar();}
|
||||
}
|
||||
}
|
||||
for(i = 0; i<R; i++)
|
||||
{
|
||||
for(j = 0; j<C+i%2; j++)
|
||||
{
|
||||
if(isdigit(Map[i][j]))
|
||||
{
|
||||
bVisited[i][j] = true;
|
||||
Path[0] = Map[i][j];
|
||||
if(DFS(i,j,1)) goto PRINT;
|
||||
bVisited[i][j] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
PRINT: printf("%s\n",Path);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
# include <stdio.h>
|
||||
# include <string.h>
|
||||
int n ;
|
||||
int dp[25][2010] ;
|
||||
int p[25], sec[25] ;
|
||||
int min(int a, int b){return a<b?a:b;}
|
||||
int max(int a, int b){return a>b?a:b;}
|
||||
int gao()
|
||||
{
|
||||
int ans = 0, t = 0 ;
|
||||
int i, j ;
|
||||
for (i = 0 ; i < n ; i++)
|
||||
{
|
||||
for (j = 0 ; ; j++)
|
||||
{
|
||||
if (j != 0) dp[i][j] += dp[i][j-1] ;
|
||||
if (j > ans && dp[i][j] == 0) break ;
|
||||
if (t != 0) t-- ;
|
||||
else if (dp[i][j] != 0)
|
||||
{
|
||||
dp[i+1][j+sec[i]] += min(dp[i][j], p[i]) ;
|
||||
dp[i][j] -= min(dp[i][j], p[i]) ;
|
||||
t = sec[i]-1 ;
|
||||
ans = max(ans, j+sec[i]) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ans ;
|
||||
}
|
||||
int main ()
|
||||
{
|
||||
int m, i ;
|
||||
while (1)
|
||||
{
|
||||
scanf ("%d%d", &n, &m) ;
|
||||
if (n == 0 && m == 0) break ;
|
||||
n = -n ;
|
||||
for (i = 0 ; i < n ; i++)
|
||||
scanf ("%d%d", &p[i], &sec[i]) ;
|
||||
memset (dp, 0, sizeof(dp)) ;
|
||||
dp[0][0] = m ;
|
||||
printf ("%d\n", gao()) ;
|
||||
}
|
||||
return 0 ;
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<algorithm>
|
||||
#define MAXN 210
|
||||
#define MAXD 40010
|
||||
int N, W, H, dep[4 * MAXD], ml[4 * MAXD], cnt[4 * MAXD], len[4 * MAXD];
|
||||
struct Seg
|
||||
{
|
||||
int x, y1, y2, c;
|
||||
bool operator < (const Seg &t) const
|
||||
{
|
||||
if(x == t.x) return c < t.c;
|
||||
return x < t.x;
|
||||
}
|
||||
Seg(){}
|
||||
Seg(int _x, int _y1, int _y2, int _c) : x(_x), y1(_y1), y2(_y2), c(_c){}
|
||||
}seg[MAXN];
|
||||
void build(int cur, int x, int y)
|
||||
{
|
||||
int mid = x + y >> 1, ls = cur << 1, rs = cur << 1 | 1;
|
||||
cnt[cur] = dep[cur] = len[cur] = 0, ml[cur] = y - x + 1;
|
||||
if(x == y) return ;
|
||||
build(ls, x, mid), build(rs, mid + 1, y);
|
||||
}
|
||||
void init()
|
||||
{
|
||||
int i, x1, x2, y1, y2;
|
||||
scanf("%d%d", &W, &H);
|
||||
for(i = 0; i < N; i ++)
|
||||
{
|
||||
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
|
||||
seg[i << 1] = Seg(x1, y1, y2, 1), seg[i << 1 | 1] = Seg(x2, y1, y2, -1);
|
||||
}
|
||||
std::sort(seg, seg + 2 * N);
|
||||
build(1, 1, H);
|
||||
}
|
||||
void update(int cur, int x, int y)
|
||||
{
|
||||
int ls = cur << 1, rs = cur << 1 | 1;
|
||||
if(dep[ls] > dep[rs]) dep[cur] = dep[ls], ml[cur] = ml[ls];
|
||||
else if(dep[rs] > dep[ls]) dep[cur] = dep[rs], ml[cur] = ml[rs];
|
||||
else dep[cur] = dep[ls], ml[cur] = ml[ls] + ml[rs];
|
||||
dep[cur] += cnt[cur];
|
||||
if(cnt[cur]) len[cur] = y - x + 1;
|
||||
else len[cur] = len[ls] + len[rs];
|
||||
}
|
||||
void refresh(int cur, int x, int y, int s, int t, int c)
|
||||
{
|
||||
int mid = x + y >> 1, ls = cur << 1, rs = cur << 1 | 1;
|
||||
if(x >= s && y <= t)
|
||||
{
|
||||
cnt[cur] += c;
|
||||
if(c > 0) ++ dep[cur], len[cur] = y - x + 1;
|
||||
else
|
||||
{
|
||||
-- dep[cur];
|
||||
if(cnt[cur]) len[cur] = y - x + 1;
|
||||
else
|
||||
{
|
||||
if(x == y) len[cur] = 0;
|
||||
else len[cur] = len[ls] + len[rs];
|
||||
}
|
||||
}
|
||||
return ;
|
||||
}
|
||||
if(mid >= s) refresh(ls, x, mid, s, t, c);
|
||||
if(mid + 1 <= t) refresh(rs, mid + 1, y, s, t, c);
|
||||
update(cur, x, y);
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
int i, md, ans, area;
|
||||
area = md = ans = 0;
|
||||
seg[2 * N].x = seg[2 * N - 1].x;
|
||||
for(i = 0; i < 2 * N; i ++)
|
||||
{
|
||||
refresh(1, 1, H, seg[i].y1 + 1, seg[i].y2, seg[i].c);
|
||||
area += len[1] * (seg[i + 1].x - seg[i].x);
|
||||
if(dep[1] > md) md = dep[1], ans = ml[1] * (seg[i + 1].x - seg[i].x);
|
||||
else if(dep[1] == md) ans += ml[1] * (seg[i + 1].x - seg[i].x);
|
||||
}
|
||||
printf("%d %d %d\n", W * H - area, md, ans);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while(scanf("%d", &N), N)
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
# include <stdio.h>
|
||||
# include <string.h>
|
||||
char str[110] ;
|
||||
char tab[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ',-.?" ;
|
||||
int cnt, label[1100] ;
|
||||
void gao()
|
||||
{
|
||||
int i, sp = 0 ;
|
||||
for (i = 0 ; str[i] ; i++)
|
||||
{
|
||||
if (str[i] != ' ' && sp != 0)
|
||||
label[cnt++] = ((sp&1)?0:1),
|
||||
sp = 0 ;
|
||||
else if (str[i] == ' ') sp++ ;
|
||||
}
|
||||
}
|
||||
void Print()
|
||||
{
|
||||
int i, j, buff ;
|
||||
for (i = 0 ; i < cnt ; i+= 5)
|
||||
{
|
||||
buff = 0 ;
|
||||
for (j = 0 ;j < 5 ; j++)
|
||||
{
|
||||
if (i+j>=cnt) label[i+j] = 0 ;
|
||||
buff = buff * 2 + label[i+j] ;
|
||||
}
|
||||
putchar (tab[buff]) ;
|
||||
}
|
||||
printf("\n") ;
|
||||
}
|
||||
int main ()
|
||||
{
|
||||
gets (str) ;
|
||||
while (1)
|
||||
{
|
||||
cnt = 0 ;
|
||||
if (strcmp(str, "#") == 0) break ;
|
||||
while (1)
|
||||
{
|
||||
if (strcmp(str, "*") == 0) break ;
|
||||
gao() ;
|
||||
gets (str) ;
|
||||
}
|
||||
Print() ;
|
||||
gets (str) ;
|
||||
}
|
||||
return 0 ;
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
int main()
|
||||
{
|
||||
int i,j,len,flag[28],rear[300];
|
||||
int top;
|
||||
char str[5][74];
|
||||
while(gets(str[1]))
|
||||
{
|
||||
gets(str[2]);
|
||||
gets(str[3]);
|
||||
gets(str[4]);
|
||||
memset(flag,0,sizeof(flag));
|
||||
for(i=1;i<=4;i++)
|
||||
{
|
||||
len=strlen(str[i]);
|
||||
for(j=0;j<len;j++)
|
||||
{
|
||||
if(str[i][j]>='A'&&str[i][j]<='Z')
|
||||
{
|
||||
flag[str[i][j]-'A']++;
|
||||
}
|
||||
}
|
||||
}
|
||||
top=0;
|
||||
for(i=0;i<26;i++)
|
||||
{
|
||||
if(top<flag[i])
|
||||
{
|
||||
top=flag[i];
|
||||
}
|
||||
}
|
||||
memset(rear,0,sizeof(rear));
|
||||
for(i=top;i>0;i--)
|
||||
{
|
||||
for(j=25;j>=0;j--)
|
||||
{
|
||||
if(flag[j]>=i)
|
||||
{
|
||||
rear[i]=j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
for(i=top;i>0;i--)
|
||||
{
|
||||
for(j=0;j<26&&j<=rear[i];j++)
|
||||
{
|
||||
if(flag[j]>=i)
|
||||
{
|
||||
printf("*");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf(" ");
|
||||
}
|
||||
if(j<25&&j<rear[i])
|
||||
{
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
for(i=0;i<25;i++)
|
||||
{
|
||||
printf("%c ",i+'A');
|
||||
}
|
||||
printf("Z\n");
|
||||
}
|
||||
return (0);
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
#include<iostream>
|
||||
#include<algorithm>
|
||||
#include<cstdio>
|
||||
#include<cstring>
|
||||
using namespace std;
|
||||
long long dp[1000001];
|
||||
int main()
|
||||
{
|
||||
int n,i,j;
|
||||
while(scanf("%d",&n)!=EOF)
|
||||
{
|
||||
memset(dp,0,sizeof(dp)); dp[0]=1;
|
||||
for(i=1;i<=n;i*=2)
|
||||
{
|
||||
for(j=i;j<=n;j++)
|
||||
{
|
||||
dp[j]+=dp[j-i];
|
||||
dp[j]%=1000000000;
|
||||
}
|
||||
}
|
||||
printf("%lld\n",dp[n]);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
#include<iostream>
|
||||
#include<cmath>
|
||||
using namespace std;
|
||||
#define MAX 2270
|
||||
int data[MAX];
|
||||
int j;
|
||||
void Judge(int a)
|
||||
{
|
||||
int i;
|
||||
int tmp=sqrt(double(a))+1;
|
||||
for(i=2;i<=tmp;i++)
|
||||
{
|
||||
if(a%i==0)
|
||||
return;
|
||||
}
|
||||
data[j]=a;
|
||||
j++;
|
||||
return;
|
||||
}
|
||||
void Set()
|
||||
{
|
||||
int i;
|
||||
j=1;
|
||||
data[0]=1;
|
||||
data[1]=2;
|
||||
j++;
|
||||
for(i=3;i<=20010;i++)
|
||||
Judge(i);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
Set();
|
||||
int n,tmp,Max,result,num,i;
|
||||
while(cin>>n)
|
||||
{
|
||||
result=0;
|
||||
while(n--)
|
||||
{
|
||||
cin>>tmp;
|
||||
for(i=0;i<2263;i++)
|
||||
{
|
||||
if(data[i]==tmp||(data[i]<tmp&&tmp%data[i]==0))
|
||||
Max=data[i];
|
||||
if(data[i]>tmp)
|
||||
break;
|
||||
}
|
||||
if(result<Max)
|
||||
{
|
||||
result=Max;
|
||||
num=tmp;
|
||||
}
|
||||
}
|
||||
cout<<num<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
#include<stdio.h>
|
||||
#include<iostream>
|
||||
#include<stdlib.h>
|
||||
#include<algorithm>
|
||||
#include<math.h>
|
||||
#include<iomanip>
|
||||
#include<time.h>
|
||||
#include<string.h>
|
||||
#include<queue>
|
||||
#include<string>
|
||||
#include<vector>
|
||||
using namespace std;
|
||||
int p[9000];
|
||||
int ans[9000];
|
||||
int main(){
|
||||
int n;
|
||||
while(~scanf("%d",&n)){
|
||||
memset(p,0,sizeof(p));
|
||||
int i,cas,t=-1;
|
||||
for(i=2;i<=n;i++){
|
||||
scanf("%d",p+i);
|
||||
}
|
||||
for(cas=1;cas<=n;cas++){
|
||||
t=-1;
|
||||
for(i=2;i<=n;i++){
|
||||
if(p[i]==0){
|
||||
t=i;
|
||||
}
|
||||
}
|
||||
if(t==-1)
|
||||
t=1;
|
||||
ans[t]=cas;
|
||||
for(i=t;i<=n;i++)
|
||||
p[i]--;
|
||||
}
|
||||
for(i=1;i<=n;i++)
|
||||
printf("%d\n",ans[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <queue>
|
||||
#include <cstdlib>
|
||||
#include <stack>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <map>
|
||||
#define LL long long
|
||||
#define mod 1000000007
|
||||
#define inf 0x3f3f3f3f
|
||||
#define N 10010
|
||||
#define clr(a) (memset(a,0,sizeof(a)))
|
||||
using namespace std;
|
||||
int n,k,x;
|
||||
int vis[N];
|
||||
int main()
|
||||
{
|
||||
while(scanf("%d%d",&n,&k)>0)
|
||||
{
|
||||
memset(vis,0,sizeof(vis));
|
||||
int num=0,ans=0;
|
||||
for(int i=1;i<=n;i++)
|
||||
{
|
||||
scanf("%d",&x);
|
||||
if(!vis[x])
|
||||
{
|
||||
vis[x]=1;num++;
|
||||
}
|
||||
if(num==k)
|
||||
{
|
||||
memset(vis,0,sizeof(vis));
|
||||
num=0;ans++;
|
||||
}
|
||||
}
|
||||
printf("%d\n",ans+1);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
#include<iostream>
|
||||
#include<string.h>
|
||||
using namespace std;
|
||||
int a[150010];
|
||||
int main()
|
||||
{
|
||||
int p,i;
|
||||
while(scanf("%d",&p)!=EOF)
|
||||
{
|
||||
memset(a,0,sizeof(a));
|
||||
for (i=1;i<=p;i++)
|
||||
scanf("%d",&a[i]);
|
||||
int s=0;
|
||||
int x=0;
|
||||
for (i=0;i<=p;i++)
|
||||
{
|
||||
if(x==0&&a[i]-a[i+1]<=0&&a[i+1]-a[i+2]>=0)
|
||||
{
|
||||
s=s+a[i+1];
|
||||
x=1;
|
||||
}
|
||||
else
|
||||
if (x==1&&a[i]-a[i+1]>=0&&a[i+1]-a[i+2]<=0)
|
||||
{
|
||||
s-=a[i+1];
|
||||
x=0;
|
||||
}
|
||||
}
|
||||
printf("%d\n",s);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <set>
|
||||
#include <map>
|
||||
#include <utility>
|
||||
#include <queue>
|
||||
#include <stack>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
int main ()
|
||||
{
|
||||
string str;
|
||||
while ( cin >> str )
|
||||
{
|
||||
int N = str.size();
|
||||
int sum = 0, ask = 0;
|
||||
for ( int i = 1; i <= N; ++ i )
|
||||
{
|
||||
switch ( str[i-1] )
|
||||
{
|
||||
case 'X':
|
||||
sum += (10-i+1) * 10; break;
|
||||
case '?':
|
||||
ask = 10-i+1; break;
|
||||
default :
|
||||
sum += (10-i+1) * (str[i-1] - '0'); break;
|
||||
}
|
||||
}
|
||||
int pos = -1;
|
||||
for ( int i = 0; i <= 10; ++ i )
|
||||
{
|
||||
if ( ( sum + ask * i ) % 11 == 0 )
|
||||
{
|
||||
pos = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( ask != 1 && pos == 10 ) pos = -1;
|
||||
if ( pos == 10 )
|
||||
cout << 'X' << endl;
|
||||
else
|
||||
cout << pos << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
typedef __int64 ll;
|
||||
ll m;
|
||||
ll cal(ll s,ll x)
|
||||
{
|
||||
return (2*s+x-1)*x;
|
||||
}
|
||||
ll binary_search(ll x)
|
||||
{
|
||||
ll left,right,mid,tmp;
|
||||
left=1,right=m/2+1;
|
||||
while(left<=right)
|
||||
{
|
||||
mid=(left+right)>>1;
|
||||
tmp=cal(mid,x);
|
||||
if(tmp==2*m)
|
||||
return mid;
|
||||
else if(tmp<2*m)
|
||||
left=mid+1;
|
||||
else
|
||||
right=mid-1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
ll i,tmp,num,cnt=1;
|
||||
while(scanf("%I64d",&m)!=EOF)
|
||||
{
|
||||
num=0;
|
||||
for(i=2;i*i<=2*m+10;i++)
|
||||
{
|
||||
tmp=binary_search(i);
|
||||
if(tmp!=-1)
|
||||
num++;
|
||||
}
|
||||
printf("%I64d\n",num+1);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
int main()
|
||||
{
|
||||
char key[28],str[84];
|
||||
while(scanf("%s",key)!=EOF)
|
||||
{
|
||||
getchar();
|
||||
gets(str);
|
||||
int i,len;
|
||||
len=strlen(str);
|
||||
for(i=0;i<len;i++)
|
||||
{
|
||||
if(str[i]>='A'&&str[i]<='Z')
|
||||
{
|
||||
str[i]=key[str[i]-'A']-'a'+'A';
|
||||
}
|
||||
else if(str[i]>='a'&&str[i]<='z')
|
||||
{
|
||||
str[i]=key[str[i]-'a'];
|
||||
}
|
||||
}
|
||||
printf("%s\n",str);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
#include <stdio.h>
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <queue>
|
||||
#include <stack>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
int n,m;
|
||||
int vis[200005];
|
||||
struct node
|
||||
{
|
||||
int x;
|
||||
int step;
|
||||
};
|
||||
int bfs(int n,int m)
|
||||
{
|
||||
int i;
|
||||
queue<node>q;
|
||||
node st,ed;
|
||||
st.x=n;
|
||||
st.step=0;
|
||||
vis[st.x]=1;
|
||||
q.push(st);
|
||||
while(!q.empty())
|
||||
{
|
||||
st=q.front();
|
||||
q.pop();
|
||||
if(st.x==m)
|
||||
return st.step;
|
||||
for(i=0;i<3;i++)
|
||||
{
|
||||
if(i==0)
|
||||
ed.x=st.x+1;
|
||||
if(i==1)
|
||||
ed.x=st.x-1;
|
||||
if(i==2)
|
||||
ed.x=st.x*2;
|
||||
if(vis[ed.x] || ed.x<0 ||ed.x>100000)
|
||||
continue;
|
||||
vis[ed.x]=1;
|
||||
ed.step=st.step+1;
|
||||
q.push(ed);
|
||||
}
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while(cin>>n>>m)
|
||||
{
|
||||
memset(vis,0,sizeof(vis));
|
||||
int ans=bfs(n,m);
|
||||
cout<<ans<<endl;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
char s[85];
|
||||
while(gets(s)&&s[0]!='#')
|
||||
{
|
||||
int l=strlen(s);
|
||||
for(i=0;i<l;i++)
|
||||
{
|
||||
if(s[i]==' ')
|
||||
s[i]=1;
|
||||
if(s[i]=='!')
|
||||
s[i]=2;
|
||||
if(s[i]=='$')
|
||||
s[i]=3;
|
||||
if(s[i]=='%')
|
||||
s[i]=4;
|
||||
if(s[i]=='(')
|
||||
s[i]=5;
|
||||
if(s[i]==')')
|
||||
s[i]=6;
|
||||
if(s[i]=='*')
|
||||
s[i]=7;
|
||||
}
|
||||
for(i=0;i<l;i++)
|
||||
{
|
||||
if(s[i]==1)
|
||||
printf("%%20");
|
||||
if(s[i]==2)
|
||||
printf("%%21");
|
||||
if(s[i]==3)
|
||||
printf("%%24");
|
||||
if(s[i]==4)
|
||||
printf("%%25");
|
||||
if(s[i]==5)
|
||||
printf("%%28");
|
||||
if(s[i]==6)
|
||||
printf("%%29");
|
||||
if(s[i]==7)
|
||||
printf("%%2a");
|
||||
if(s[i]!=1 &&s[i]!=2 &&s[i]!=3 &&s[i]!=4&&s[i]!=5 &&s[i]!=6 &&s[i]!=7)
|
||||
printf("%c",s[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
using namespace std;
|
||||
int aa[100000];
|
||||
void convert(int a,int b)
|
||||
{
|
||||
char s[16];
|
||||
int i=0;
|
||||
for(;i<16;i++)
|
||||
{
|
||||
int k=a%2;
|
||||
if(k==1)s[i]='1';
|
||||
else if(b%2==1) s[i]='?';
|
||||
else s[i]='0';
|
||||
a/=2;b/=2;
|
||||
}
|
||||
for(i=15;i>=0;i--)
|
||||
printf("%c",s[i]);
|
||||
printf("\n");
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int a,b,c,d;
|
||||
int top=0;
|
||||
while(true)
|
||||
{
|
||||
top=1;
|
||||
cin>>a;
|
||||
if(a==0) break;
|
||||
cin>>b>>c>>d;
|
||||
aa[0]=d;
|
||||
for(int i=0;i< 65537;i++)
|
||||
{
|
||||
aa[top]=(a%c*d%c+b%c)%c;
|
||||
d=aa[top++];
|
||||
if(d==aa[0])break;
|
||||
}
|
||||
int result=aa[0];
|
||||
int com=aa[0];
|
||||
for(int i=1;i<top&&i<=65536;i++)
|
||||
{
|
||||
result &= aa[i];
|
||||
com |=aa[i];
|
||||
}
|
||||
convert(result,com);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
#include<iostream>
|
||||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<string>
|
||||
#include<algorithm>
|
||||
#include<map>
|
||||
#include<queue>
|
||||
#define INF 100000000
|
||||
#define L 2520
|
||||
using namespace std;
|
||||
int N,M;
|
||||
int d[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
|
||||
int dp[25][25];
|
||||
struct Block
|
||||
{
|
||||
int a[4];
|
||||
}mp[25][25];
|
||||
int main()
|
||||
{
|
||||
int i,j,k,v;
|
||||
char c;
|
||||
while(cin>>N>>M,N||M)
|
||||
{
|
||||
memset(mp,0,sizeof(mp));
|
||||
for(i=0;i<=N;i++)
|
||||
{
|
||||
for(j=0;j<M;j++)
|
||||
{
|
||||
cin>>v>>c;
|
||||
if(v)
|
||||
v=L/v;
|
||||
if(c=='*')
|
||||
mp[i][j].a[0]=mp[i][j+1].a[1]=v;
|
||||
if(c=='>')
|
||||
mp[i][j].a[0]=v;
|
||||
if(c=='<')
|
||||
mp[i][j+1].a[1]=v;
|
||||
}
|
||||
if(i!=N)
|
||||
for(j=0;j<=M;j++)
|
||||
{
|
||||
cin>>v>>c;
|
||||
if(v)
|
||||
v=L/v;
|
||||
if(c=='*')
|
||||
mp[i][j].a[2]=mp[i+1][j].a[3]=v;
|
||||
if(c=='v')
|
||||
mp[i][j].a[2]=v;
|
||||
if(c=='^')
|
||||
mp[i+1][j].a[3]=v;
|
||||
}
|
||||
}
|
||||
for(i=0;i<=N;i++)
|
||||
for(j=0;j<=M;j++)
|
||||
dp[i][j]=INF;
|
||||
dp[0][0]=0;
|
||||
for(v=0;v<40;v++)
|
||||
for(i=0;i<=N;i++)
|
||||
for(j=0;j<=M;j++)
|
||||
for(k=0;k<4;k++)
|
||||
if(mp[i][j].a[k])
|
||||
if(dp[i+d[k][0]][j+d[k][1]]>dp[i][j]+mp[i][j].a[k])
|
||||
dp[i+d[k][0]][j+d[k][1]]=dp[i][j]+mp[i][j].a[k];
|
||||
if(dp[N][M]==INF)
|
||||
cout<<"Holiday"<<endl;
|
||||
else
|
||||
cout<<dp[N][M]<<" blips"<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
# include <stdio.h>
|
||||
# include <string.h>
|
||||
int ACL[30], out[30][2] ;
|
||||
char str[100] ;
|
||||
char entity[100], right[100] ;
|
||||
void gao(char s[])
|
||||
{
|
||||
char *p = s, op ;
|
||||
int ent, rit, i, j ;
|
||||
memset (ACL, 0, sizeof(ACL)) ;
|
||||
while (*p)
|
||||
{
|
||||
sscanf (p, "%[A-Z]%c%[a-z]%*c", entity, &op, right) ;
|
||||
p += strlen(entity) + strlen(right) + 2 ;
|
||||
for (i = 0 ; entity[i] ; i++)
|
||||
{
|
||||
ent = entity[i]-'A' ;
|
||||
for (rit = 0, j = 0 ; right[j] ; j++) rit |= (1<<(right[j]-'a')) ;
|
||||
if (op == '+') ACL[ent] |= rit ;
|
||||
else if (op == '-') ACL[ent] &= ~rit ;
|
||||
else //op == '='
|
||||
ACL[ent] = rit ;
|
||||
}
|
||||
}
|
||||
}
|
||||
int main ()
|
||||
{
|
||||
int nCase = 1 ;
|
||||
int i, j, cnt ;
|
||||
while (~scanf ("%s", str) && strcmp(str, "#"))
|
||||
{
|
||||
gao(str) ;
|
||||
memset (str, 0, sizeof(str)) ;
|
||||
printf ("%d:", nCase++) ;
|
||||
for (i = 0, cnt = 0 ; i < 26 ; i++) if (ACL[i])
|
||||
out[cnt][0] = i, out[cnt++][1] = ACL[i] ;
|
||||
for (i = 0 ; i < cnt ; i++)
|
||||
{
|
||||
printf ("%c", out[i][0]+'A') ;
|
||||
if (i==cnt-1 || (i<cnt-1 && out[i][1] != out[i+1][1]))
|
||||
for (j = 0 ; j < 26 ; j++) if (out[i][1] & (1<<j))
|
||||
printf ("%c", j+'a') ;
|
||||
}
|
||||
printf ("\n") ;
|
||||
}
|
||||
return 0 ;
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
...=...**..#...@....:.:.:..=..
|
||||
______________________________
|
||||
11111111112222222222
|
||||
012345678901234567890123456789
|
|
@ -0,0 +1,72 @@
|
|||
#include<iostream>
|
||||
#include<stdio.h>
|
||||
#include<list>
|
||||
using namespace std;
|
||||
class Bookshelf;
|
||||
class Book{
|
||||
public:
|
||||
Book(int _id,int _width):id(_id),width(_width){}
|
||||
int id,width;
|
||||
friend class Bookshelf;
|
||||
};
|
||||
class Bookshelf{
|
||||
public:
|
||||
Bookshelf(int w):width(w){
|
||||
occupied_width=0;
|
||||
}
|
||||
void add(const Book &book){
|
||||
shelf.push_front(book);
|
||||
occupied_width += book.width;
|
||||
while(occupied_width>width){
|
||||
occupied_width -= (shelf.back()).width;
|
||||
shelf.pop_back();
|
||||
}
|
||||
}
|
||||
void remove(const int &id){
|
||||
list<Book>::iterator i;
|
||||
for(i=shelf.begin();i!=shelf.end();i++){
|
||||
if(i->id == id) break;
|
||||
}
|
||||
if(i==shelf.end()) return;
|
||||
occupied_width -= i->width;
|
||||
shelf.erase(i);
|
||||
}
|
||||
friend ostream& operator<<(ostream &out,Bookshelf &bookshelf);
|
||||
private:
|
||||
list<Book> shelf;
|
||||
int width;
|
||||
int occupied_width;
|
||||
};
|
||||
ostream& operator<<(ostream &out, Bookshelf &bookshelf)
|
||||
{
|
||||
list<Book>::iterator i;
|
||||
for(i=(bookshelf.shelf).begin();i!=(bookshelf.shelf).end();i++)
|
||||
out<<" "<<i->id;
|
||||
return out;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int width;
|
||||
int p=1;
|
||||
char cmd;
|
||||
while(cin>>width,width!=-1)
|
||||
{
|
||||
Bookshelf bookshelf(width);
|
||||
int id,w;
|
||||
while(cin>>cmd)
|
||||
{
|
||||
if(cmd=='E') break;
|
||||
else if(cmd=='R')
|
||||
{
|
||||
cin>>id;
|
||||
bookshelf.remove(id);
|
||||
}else
|
||||
{
|
||||
cin>>id>>w;
|
||||
bookshelf.add(Book(id,w));
|
||||
}
|
||||
}
|
||||
cout<<"PROBLEM "<<p++<<":"<<bookshelf<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,380 @@
|
|||
#include<iostream>
|
||||
using namespace std;
|
||||
const int size=23;
|
||||
const int num=251;
|
||||
int n;
|
||||
int m;
|
||||
int lastx,lasty;
|
||||
int map[size][size];
|
||||
bool link[num][num];
|
||||
int posx[]={0,-1,-2,-2,-1,1,2,2,1};
|
||||
int posy[]={0,2,1,-1,-2,-2,-1,1,2};
|
||||
typedef class chess
|
||||
{
|
||||
public:
|
||||
int color;
|
||||
int r,c;
|
||||
int connet[8];
|
||||
int pc;
|
||||
chess()
|
||||
{
|
||||
color=-1;
|
||||
pc=0;
|
||||
}
|
||||
}PEG;
|
||||
void LinePeg(PEG* peg,int i);
|
||||
bool CheckWin(PEG* peg,bool flag);
|
||||
int main(void)
|
||||
{
|
||||
while(cin>>n>>m)
|
||||
{
|
||||
if(!n && !m)
|
||||
break;
|
||||
memset(map,0,sizeof(map));
|
||||
memset(link,false,sizeof(link));
|
||||
PEG* peg=new PEG[m+1];
|
||||
for(int i=1;i<=m;i++)
|
||||
{
|
||||
int x,y;
|
||||
cin>>x>>y;
|
||||
map[x][y]=i;
|
||||
peg[i].r=x;
|
||||
peg[i].c=y;
|
||||
if(i%2)
|
||||
peg[i].color=1;
|
||||
else
|
||||
peg[i].color=0;
|
||||
if(i==m)
|
||||
{
|
||||
lastx=x;
|
||||
lasty=y;
|
||||
}
|
||||
LinePeg(peg,i);
|
||||
}
|
||||
if(CheckWin(peg,true) && !CheckWin(peg,false))
|
||||
cout<<"yes"<<endl;
|
||||
else
|
||||
cout<<"no"<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
void LinePeg(PEG* peg,int i)
|
||||
{
|
||||
int color=peg[i].color;
|
||||
for(int k=1;k<=8;k++)
|
||||
{
|
||||
int r=peg[i].r+posx[k];
|
||||
int c=peg[i].c+posy[k];
|
||||
if(r>=0 && r<=n && c>=0 && c<=n)
|
||||
{
|
||||
if(map[r][c] && peg[ map[r][c] ].color==color)
|
||||
{
|
||||
switch(k)
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
if(link[ map[r][c-2] ][ map[r+1][c] ])
|
||||
break;
|
||||
if(c-3>=0 && link[ map[r][c-3] ][ map[r+1][c-1] ])
|
||||
break;
|
||||
if(c+1<=n && link[ map[r][c-1] ][ map[r+1][c+1] ])
|
||||
break;
|
||||
if(r-1>=0)
|
||||
{
|
||||
if(link[ map[r-1][c-2] ][ map[r+1][c-1] ])
|
||||
break;
|
||||
if(link[ map[r-1][c-1] ][ map[r+1][c] ])
|
||||
break;
|
||||
if(link[ map[r-1][c] ][ map[r+1][c-1] ])
|
||||
break;
|
||||
}
|
||||
if(r+2<=n)
|
||||
{
|
||||
if(link[ map[r+2][c-2] ][ map[r][c-1] ])
|
||||
break;
|
||||
if(link[ map[r+2][c-1] ][ map[r][c-2] ])
|
||||
break;
|
||||
if(link[ map[r+2][c] ][ map[r][c-1] ])
|
||||
break;
|
||||
}
|
||||
int a=map[peg[i].r][peg[i].c];
|
||||
int b=map[r][c];
|
||||
peg[a].connet[peg[a].pc++]=b;
|
||||
peg[b].connet[peg[b].pc++]=a;
|
||||
link[a][b]=link[b][a]=true;
|
||||
break;
|
||||
}
|
||||
case 2: //60搴逛
|
||||
{
|
||||
if(link[ map[r][c-1] ][ map[r+2][c] ])
|
||||
break;
|
||||
if(r-1>=0 && link[ map[r-1][c-1] ][ map[r+1][c] ])
|
||||
break;
|
||||
if(r+3<=n && link[ map[r+1][c-1] ][ map[r+3][c] ])
|
||||
break;
|
||||
if(c-2>=0)
|
||||
{
|
||||
if(link[ map[r][c-2] ][ map[r+1][c] ])
|
||||
break;
|
||||
if(link[ map[r+1][c-2] ][ map[r+2][c] ])
|
||||
break;
|
||||
if(link[ map[r+2][c-2] ][ map[r+1][c] ])
|
||||
break;
|
||||
}
|
||||
if(c+1<=n)
|
||||
{
|
||||
if(link[ map[r][c-1] ][ map[r+1][c+1] ])
|
||||
break;
|
||||
if(link[ map[r+1][c-1] ][ map[r][c+1] ])
|
||||
break;
|
||||
if(link[ map[r+1][c-1] ][ map[r+2][c+1] ])
|
||||
break;
|
||||
}
|
||||
int a=map[peg[i].r][peg[i].c];
|
||||
int b=map[r][c];
|
||||
peg[a].connet[peg[a].pc++]=b;
|
||||
peg[b].connet[peg[b].pc++]=a;
|
||||
link[a][b]=link[b][a]=true;
|
||||
break;
|
||||
}
|
||||
case 3: //120搴逛
|
||||
{
|
||||
if(link[ map[r][c+1] ][ map[r+2][c] ])
|
||||
break;
|
||||
if(r-1>=0 && link[ map[r-1][c+1] ][ map[r+1][c] ])
|
||||
break;
|
||||
if(r+3<=n && link[ map[r+1][c+1] ][ map[r+3][c] ])
|
||||
break;
|
||||
if(c-1>=0)
|
||||
{
|
||||
if(link[ map[r][c-1] ][ map[r+1][c+1] ])
|
||||
break;
|
||||
if(link[ map[r+1][c-1] ][ map[r][c+1] ])
|
||||
break;
|
||||
if(link[ map[r+2][c-1] ][ map[r+1][c+1] ])
|
||||
break;
|
||||
}
|
||||
if(c+2<=n)
|
||||
{
|
||||
if(link[ map[r+1][c] ][ map[r][c+2] ])
|
||||
break;
|
||||
if(link[ map[r+2][c] ][ map[r+1][c+2] ])
|
||||
break;
|
||||
if(link[ map[r+1][c] ][ map[r+2][c+2] ])
|
||||
break;
|
||||
}
|
||||
int a=map[peg[i].r][peg[i].c];
|
||||
int b=map[r][c];
|
||||
peg[a].connet[peg[a].pc++]=b;
|
||||
peg[b].connet[peg[b].pc++]=a;
|
||||
link[a][b]=link[b][a]=true;
|
||||
break;
|
||||
}
|
||||
case 4: //150搴逛
|
||||
{
|
||||
if(link[ map[r][c+2] ][ map[r+1][c] ])
|
||||
break;
|
||||
if(c-1>=0 && link[ map[r+1][c-1] ][ map[r][c+1] ])
|
||||
break;
|
||||
if(c+3<=n && link[ map[r+1][c+1] ][ map[r][c+3] ])
|
||||
break;
|
||||
if(r-1>=0)
|
||||
{
|
||||
if(link[ map[r-1][c] ][ map[r+1][c+1] ])
|
||||
break;
|
||||
if(link[ map[r-1][c+1] ][ map[r+1][c] ])
|
||||
break;
|
||||
if(link[ map[r-1][c+2] ][ map[r+1][c+1] ])
|
||||
break;
|
||||
}
|
||||
if(r+2<=n)
|
||||
{
|
||||
if(link[ map[r][c+1] ][ map[r+2][c] ])
|
||||
break;
|
||||
if(link[ map[r][c+1] ][ map[r+2][c+2] ])
|
||||
break;
|
||||
if(link[ map[r][c+2] ][ map[r+2][c+1] ])
|
||||
break;
|
||||
}
|
||||
int a=map[peg[i].r][peg[i].c];
|
||||
int b=map[r][c];
|
||||
peg[a].connet[peg[a].pc++]=b;
|
||||
peg[b].connet[peg[b].pc++]=a;
|
||||
link[a][b]=link[b][a]=true;
|
||||
break;
|
||||
}
|
||||
case 5: //210搴逛
|
||||
{
|
||||
if(link[ map[r-1][c] ][ map[r][c+2] ])
|
||||
break;
|
||||
if(c-1>=0 && link[ map[r-1][c-1] ][ map[r][c+1] ])
|
||||
break;
|
||||
if(c+3<=n && link[ map[r-1][c+1] ][ map[r][c+3] ])
|
||||
break;
|
||||
if(r-2>=0)
|
||||
{
|
||||
if(link[ map[r-2][c] ][ map[r][c+1] ])
|
||||
break;
|
||||
if(link[ map[r-2][c+1] ][ map[r][c+2] ])
|
||||
break;
|
||||
if(link[ map[r-2][c+2] ][ map[r][c+1] ])
|
||||
break;
|
||||
}
|
||||
if(r+1<=n)
|
||||
{
|
||||
if(link[ map[r][c] ][ map[r-1][c+1] ])
|
||||
break;
|
||||
if(link[ map[r+1][c+1] ][ map[r-1][c] ])
|
||||
break;
|
||||
if(link[ map[r+1][c+2] ][ map[r-1][c+1] ])
|
||||
break;
|
||||
}
|
||||
int a=map[peg[i].r][peg[i].c];
|
||||
int b=map[r][c];
|
||||
peg[a].connet[peg[a].pc++]=b;
|
||||
peg[b].connet[peg[b].pc++]=a;
|
||||
link[a][b]=link[b][a]=true;
|
||||
break;
|
||||
}
|
||||
case 6: //240搴逛
|
||||
{
|
||||
if(link[ map[r-2][c] ][ map[r][c+1] ])
|
||||
break;
|
||||
if(r-3>=0 && link[ map[r-3][c] ][ map[r-1][c+1] ])
|
||||
break;
|
||||
if(r+1<=n && link[ map[r-1][c] ][ map[r+1][c+1] ])
|
||||
break;
|
||||
if(c-1>=0)
|
||||
{
|
||||
if(link[ map[r-2][c-1] ][ map[r-1][c+1] ])
|
||||
break;
|
||||
if(link[ map[r-1][c-1] ][ map[r][c+1] ])
|
||||
break;
|
||||
if(link[ map[r][c-1] ][ map[r-1][c+1] ])
|
||||
break;
|
||||
}
|
||||
if(c+2<=n)
|
||||
{
|
||||
if(link[ map[r-1][c] ][ map[r-2][c+2] ])
|
||||
break;
|
||||
if(link[ map[r-2][c] ][ map[r-1][c+2] ])
|
||||
break;
|
||||
if(link[ map[r-1][c] ][ map[r][c+2] ])
|
||||
break;
|
||||
}
|
||||
int a=map[peg[i].r][peg[i].c];
|
||||
int b=map[r][c];
|
||||
peg[a].connet[peg[a].pc++]=b;
|
||||
peg[b].connet[peg[b].pc++]=a;
|
||||
link[a][b]=link[b][a]=true;
|
||||
break;
|
||||
}
|
||||
case 7: //300搴逛
|
||||
{
|
||||
if(link[ map[r-2][c] ][ map[r][c-1] ])
|
||||
break;
|
||||
if(r-3>=0 && link[ map[r-3][c] ][ map[r-1][c-1] ])
|
||||
break;
|
||||
if(r+1<=n && link[ map[r-1][c] ][ map[r+1][c-1] ])
|
||||
break;
|
||||
if(c-2>=0)
|
||||
{
|
||||
if(link[ map[r-2][c-2] ][ map[r-1][c] ])
|
||||
break;
|
||||
if(link[ map[r-1][c-2] ][ map[r-2][c] ])
|
||||
break;
|
||||
if(link[ map[r][c-2] ][ map[r-1][c] ])
|
||||
break;
|
||||
}
|
||||
if(c+1<=n)
|
||||
{
|
||||
if(link[ map[r-1][c-1] ][ map[r-2][c+1] ])
|
||||
break;
|
||||
if(link[ map[r][c-1] ][ map[r-1][c+1] ])
|
||||
break;
|
||||
if(link[ map[r-1][c-1] ][ map[r][c+1] ])
|
||||
break;
|
||||
}
|
||||
int a=map[peg[i].r][peg[i].c];
|
||||
int b=map[r][c];
|
||||
peg[a].connet[peg[a].pc++]=b;
|
||||
peg[b].connet[peg[b].pc++]=a;
|
||||
link[a][b]=link[b][a]=true;
|
||||
break;
|
||||
}
|
||||
case 8: //330搴逛
|
||||
{
|
||||
if(link[ map[r][c-2] ][ map[r-1][c] ])
|
||||
break;
|
||||
if(c-3>=0 && link[ map[r][c-3] ][ map[r-1][c-1] ])
|
||||
break;
|
||||
if(c+1<=n && link[ map[r][c-1] ][ map[r-1][c+1] ])
|
||||
break;
|
||||
if(r-2>=0)
|
||||
{
|
||||
if(link[ map[r-2][c-2] ][ map[r][c-1] ])
|
||||
break;
|
||||
if(link[ map[r-2][c-1] ][ map[r][c-2] ])
|
||||
break;
|
||||
if(link[ map[r-2][c] ][ map[r][c-1] ])
|
||||
break;
|
||||
}
|
||||
if(r+1<=n)
|
||||
{
|
||||
if(link[ map[r-1][c-1] ][ map[r+1][c-2] ])
|
||||
break;
|
||||
if(link[ map[r-1][c-1] ][ map[r+1][c] ])
|
||||
break;
|
||||
if(link[ map[r-1][c] ][ map[r+1][c-1] ])
|
||||
break;
|
||||
}
|
||||
int a=map[peg[i].r][peg[i].c];
|
||||
int b=map[r][c];
|
||||
peg[a].connet[peg[a].pc++]=b;
|
||||
peg[b].connet[peg[b].pc++]=a;
|
||||
link[a][b]=link[b][a]=true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
bool CheckWin(PEG* peg,bool flag)
|
||||
{
|
||||
int NUM;
|
||||
if(!flag)
|
||||
NUM=map[lastx][lasty];
|
||||
for(int k=0;k<=n;k++)
|
||||
{
|
||||
int p=map[0][k];
|
||||
if(p && p!=NUM && peg[p].color==1)
|
||||
{
|
||||
int queue[num];
|
||||
bool vist[num]={false};
|
||||
int head=0;
|
||||
int tail=0;
|
||||
queue[tail++]=p;
|
||||
vist[p]=true;
|
||||
while(head<tail)
|
||||
{
|
||||
int s=queue[head++];
|
||||
if(peg[s].r==n)
|
||||
return true;
|
||||
for(int i=0;i<peg[s].pc;i++)
|
||||
{
|
||||
int x=peg[s].connet[i];
|
||||
if(!vist[x])
|
||||
{
|
||||
vist[x]=true;
|
||||
if(!flag && x==NUM)
|
||||
continue;
|
||||
queue[tail++]=x;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
#include<stdio.h>
|
||||
double max(double a,double b)
|
||||
{
|
||||
if(a>b)
|
||||
return a;
|
||||
else
|
||||
return b;
|
||||
}
|
||||
double min(double a,double b)
|
||||
{
|
||||
if(a>b)
|
||||
return b;
|
||||
else
|
||||
return a;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int a,b,c,d,z;
|
||||
double x1,x2,x3,x4,y1,y2;
|
||||
while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF&&(a!=0||b!=0||c!=0||d!=0))
|
||||
{
|
||||
if(c>=a&&d>=b)
|
||||
printf("100%%\n");
|
||||
else if(c>=b&&d>=a)
|
||||
printf("100%%\n");
|
||||
else
|
||||
{
|
||||
x1=c*1.0/a;
|
||||
x2=d*1.0/b;
|
||||
y1=min(x1,x2);
|
||||
x3=c*1.0/b;
|
||||
x4=d*1.0/a;
|
||||
y2=min(x3,x4);
|
||||
z=max(y1,y2)*100;
|
||||
printf("%d%%\n",z);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,80 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#define oo 100000
|
||||
bool dfs(int, int);
|
||||
int n,m,mini;
|
||||
bool finished[14];
|
||||
struct CLASS {
|
||||
char name[8];
|
||||
int time,preNum;
|
||||
int prerequisites[13];
|
||||
} subject[15];
|
||||
bool permit(int num) {
|
||||
int i;
|
||||
for(i=0; i<subject[num].preNum && finished[subject[num].prerequisites[i]]; i++);
|
||||
if(i == subject[num].preNum) return true;
|
||||
return false;
|
||||
}
|
||||
bool selectAndDfs(int total, int elect, int from, int que[], int result[], int lev, int semester) {
|
||||
int i;
|
||||
if(total-from < elect) return false;
|
||||
if(from == total && elect == 0) {
|
||||
for(i=0; i<m; i++) finished[result[i]] = true;
|
||||
dfs(lev+m, semester+1);
|
||||
for(i=0; i<m; i++) finished[result[i]] = false;
|
||||
return true;
|
||||
}
|
||||
for(i=from; i<total; i++) {
|
||||
result[m-elect] = que[i];
|
||||
selectAndDfs(total, elect-1, i+1, que, result, lev, semester);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool dfs(int lev, int semester) {
|
||||
if(semester-1 >= mini) return false;
|
||||
if(lev == n) {
|
||||
if(semester-1 < mini) mini = semester-1;
|
||||
return true;
|
||||
}
|
||||
int i,tn=0,que[15];
|
||||
for(i=0; i<n; i++) if(!finished[i] && permit(i) && (subject[i].time & (1<<semester%2)) != 0) que[tn++] = i;
|
||||
if(tn <= m) {
|
||||
for(i=0; i<tn; i++) finished[que[i]] = true;
|
||||
dfs(lev+tn,semester+1);
|
||||
for(i=0; i<tn; i++) finished[que[i]] = false;
|
||||
}
|
||||
else {
|
||||
int temp[15];
|
||||
selectAndDfs(tn,m,0,que,temp,lev,semester);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
int main() {
|
||||
while(scanf("%d%d", &n, &m) == 2 && !(n == -1 && m == -1)) {
|
||||
int i,j,k,p,t;
|
||||
for(i=0; i<n; i++) {
|
||||
scanf("%s", &subject[i].name);
|
||||
subject[i].time = subject[i].preNum = 0;
|
||||
}
|
||||
for(i=0; i<n; i++) {
|
||||
char s[10];
|
||||
scanf("%s", s);
|
||||
for(j=0; j<n && strcmp(s, subject[j].name) != 0; j++);
|
||||
scanf("%s", s);
|
||||
if(s[0] == 'B') subject[j].time = 3;
|
||||
else if(s[0] == 'S') subject[j].time = 1;
|
||||
else subject[j].time = 2;
|
||||
scanf("%d", &t);
|
||||
for(k=0; k<t; k++) {
|
||||
scanf("%s", s);
|
||||
for(p=0; p<n && strcmp(s, subject[p].name) != 0; p++);
|
||||
subject[j].prerequisites[subject[j].preNum++] = p;
|
||||
}
|
||||
}
|
||||
memset(finished, 0, sizeof(finished));
|
||||
mini = oo;
|
||||
dfs(0,1);
|
||||
printf("The minimum number of semesters required to graduate is %d.\n", mini);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#define N 13
|
||||
int cmp(const void *p, const void *q)
|
||||
{
|
||||
return *(int *)p > *(int *)q ? -1 : 1;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i, j;
|
||||
int n, grey, count, max, colors[N];
|
||||
while(scanf("%d", &n) && n)
|
||||
{
|
||||
memset(colors, 0, sizeof(colors));
|
||||
max = 0;
|
||||
for(i=0; i<n; i++)
|
||||
{
|
||||
scanf("%d", &colors[i]);
|
||||
if(colors[i] > max)
|
||||
max = colors[i];
|
||||
}
|
||||
scanf("%d", &grey);
|
||||
if(max % 50)
|
||||
count = max / 50 + 1;
|
||||
else count = max / 50;
|
||||
for(i=0; i<n; i++)
|
||||
{
|
||||
colors[i] = count * 50 - colors[i];
|
||||
}
|
||||
qsort(colors, n, sizeof(colors[0]), cmp);
|
||||
while(1)
|
||||
{
|
||||
if(colors[2] == 0 && grey > 0)
|
||||
{
|
||||
count++;
|
||||
for(i=0; i<n; i++)
|
||||
colors[i] += 50;
|
||||
}
|
||||
grey --;
|
||||
if(grey <= 0)
|
||||
break;
|
||||
else {
|
||||
colors[0]--;
|
||||
colors[1]--;
|
||||
colors[2]--;
|
||||
qsort(colors, n, sizeof(colors[0]), cmp);
|
||||
}
|
||||
}
|
||||
printf("%d\n", count);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
#include<iostream>
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
int cases, x = 1;
|
||||
cin>>cases;
|
||||
while (cases--) {
|
||||
int a, b, c, d;
|
||||
cin>>a>>b>>c>>d;
|
||||
printf("Problem set %d: %d / %d, base 7 digits %d through %d: ", x++, a, b, c, d);
|
||||
int i;
|
||||
a %= b;
|
||||
for (i = 0; i <= d; ++i) {
|
||||
a *= 7;
|
||||
if (i >= c) {
|
||||
printf("%d", a / b);
|
||||
}
|
||||
a %= b;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,158 @@
|
|||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <algorithm>
|
||||
#include <queue>
|
||||
#define RE(x) ((x)^1)
|
||||
#define CP(x) ((x)+500)
|
||||
#define INF 0x3fffffff
|
||||
using namespace std;
|
||||
int N, M, MM, dis[1000], head[1000], idx;
|
||||
const int source = 980, sink = 981;
|
||||
char G[25][25], S[25][25];
|
||||
struct Edge
|
||||
{
|
||||
int v, cap, next;
|
||||
}e[20000];
|
||||
void init()
|
||||
{
|
||||
idx = -1;
|
||||
memset(head, 0xff, sizeof (head));
|
||||
}
|
||||
inline int to(int x, int y)
|
||||
{
|
||||
return x*M+y;
|
||||
}
|
||||
inline bool out(int x, int y)
|
||||
{
|
||||
int u = x+1, d = N-x, l = y+1, r = M-y;
|
||||
int dist = min(u, min(d, min(l, r)));
|
||||
return dist <= MM;
|
||||
}
|
||||
inline bool judge(int x, int y)
|
||||
{
|
||||
if (x < 0 || x >= N || y < 0 || y >= M) {
|
||||
return false;
|
||||
}
|
||||
else if (!G[x][y]) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void insert(int a, int b, int c)
|
||||
{
|
||||
++idx;
|
||||
e[idx].v = b, e[idx].cap = c;
|
||||
e[idx].next = head[a], head[a] = idx;
|
||||
}
|
||||
void build(int x, int y)
|
||||
{
|
||||
int xx, yy;
|
||||
if (G[x][y]) {
|
||||
insert(to(x, y), CP(to(x,y)), G[x][y]);
|
||||
insert(CP(to(x, y)), to(x,y), G[x][y]);
|
||||
if (out(x, y)) {
|
||||
insert(CP(to(x, y)), sink, INF);
|
||||
insert(sink, CP(to(x, y)), 0);
|
||||
}
|
||||
else {
|
||||
for (int i = -MM; i <= MM; ++i) {
|
||||
for (int j = -(MM-abs(i)); j <= (MM-abs(i)); ++j) {
|
||||
xx = x + i, yy = y + j;
|
||||
if (judge(xx, yy) && !(x == xx && y == yy)) {
|
||||
insert(CP(to(x, y)), to(xx, yy), G[x][y]);
|
||||
insert(to(xx, yy), CP(to(x, y)), 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
bool spfa(int u)
|
||||
{
|
||||
queue<int>q;
|
||||
memset(dis, 0xff, sizeof (dis));
|
||||
dis[u] = 0;
|
||||
q.push(u);
|
||||
while (!q.empty()) {
|
||||
u = q.front();
|
||||
q.pop();
|
||||
for (int i = head[u]; i != -1; i = e[i].next) {
|
||||
if (dis[e[i].v] == -1 && e[i].cap > 0) {
|
||||
dis[e[i].v] = dis[u] + 1;
|
||||
q.push(e[i].v);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dis[sink] != -1;
|
||||
}
|
||||
int dfs(int u, int flow)
|
||||
{
|
||||
if (u == sink) {
|
||||
return flow;
|
||||
}
|
||||
int tf = 0, sf;
|
||||
for (int i = head[u]; i != -1; i = e[i].next) {
|
||||
if (dis[u]+1 == dis[e[i].v] && e[i].cap > 0 && (sf = dfs(e[i].v, min(flow-tf, e[i].cap)))) {
|
||||
e[i].cap -= sf, e[RE(i)].cap += sf;
|
||||
tf += sf;
|
||||
if (tf == flow) {
|
||||
return flow;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!tf) {
|
||||
dis[u] = -1;
|
||||
}
|
||||
return tf;
|
||||
}
|
||||
int dinic()
|
||||
{
|
||||
int ans = 0;
|
||||
while (spfa(source)) {
|
||||
ans += dfs(source, INF);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int T, ca = 0, ans;
|
||||
scanf("%d", &T);
|
||||
while (T--) {
|
||||
init();
|
||||
ans = 0;
|
||||
scanf("%d %d", &N, &MM);
|
||||
for (int i = 0; i < N; ++i) {
|
||||
scanf("%s", G[i]);
|
||||
}
|
||||
M = strlen(G[0]);
|
||||
for (int i = 0; i < N; ++i) {
|
||||
for (int j = 0; j < M; ++j) {
|
||||
G[i][j] -= '0';
|
||||
build(i, j);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < N; ++i) {
|
||||
scanf("%s", S[i]);
|
||||
for (int j = 0; j < M; ++j) {
|
||||
if (S[i][j] == 'L') {
|
||||
++ans;
|
||||
insert(source, to(i, j), 1);
|
||||
insert(to(i, j), source, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
ans -= dinic();
|
||||
printf("Case #%d: ", ++ca);
|
||||
if (!ans) {
|
||||
puts("no lizard was left behind.");
|
||||
}
|
||||
else if (ans == 1){
|
||||
printf("%d lizard was left behind.\n", ans);
|
||||
}
|
||||
else {
|
||||
printf("%d lizards were left behind.\n", ans);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,144 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
char s[81];
|
||||
int l;
|
||||
int Dig(char c)
|
||||
{
|
||||
if (c >= '0' && c <= '9')
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int Upper(char c)
|
||||
{
|
||||
if (c >= 'A' && c <= 'Z')
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int Lower(char c)
|
||||
{
|
||||
if (c >= 'a' && c <= 'z')
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int Char(char c)
|
||||
{
|
||||
if (Upper(c) || Lower(c))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int Punc(char c)
|
||||
{
|
||||
if (Dig(c) || Char(c) || c == ' ')
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int Iso(char c)
|
||||
{
|
||||
if (c == 'A' || c == 'a' || c == 'I')
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int check1()
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < l - 1; i++)
|
||||
{
|
||||
if(Upper(s[i]) && Upper(s[i + 1]))
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int check2()
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < l - 1; i++)
|
||||
{
|
||||
if ((Char(s[i]) && Dig(s[i + 1])) || (Char(s[i + 1]) && Dig(s[i])))
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int check3()
|
||||
{
|
||||
int i;
|
||||
if (l == 1)
|
||||
{
|
||||
if (Iso(s[0]))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if (s[1] == ' '&& Iso(s[0]) == 0 && s[0] != ' ')
|
||||
return 0;
|
||||
if (s[l - 2] == ' ' && Iso(s[l - 1]) == 0 && s[l - 1] != ' ')
|
||||
return 0;
|
||||
for (i = 1; i < l - 1; i++)
|
||||
{
|
||||
if(s[i] != ' ' && s[i - 1] == ' ' && s[i + 1] == ' ' && Iso(s[i]) == 0)
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int check4()
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < l - 1; i++)
|
||||
{
|
||||
if(Punc(s[i]) && Punc(s[i + 1]) && s[i] != '\"' && s[i + 1] != '\"')
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int check()
|
||||
{
|
||||
if (check1() == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (check2() == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (check3() == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (check4() == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
gets(s);
|
||||
if (strcmp(s, "#") == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
l = strlen(s);
|
||||
if (check())
|
||||
{
|
||||
printf("OK\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("suspicious\n");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
int main()
|
||||
{
|
||||
char a[300];
|
||||
int i;
|
||||
int b[300];
|
||||
while(gets(a) &&a[0]!='#')
|
||||
{
|
||||
memset(b,0,sizeof(b));
|
||||
int l=strlen(a);
|
||||
for(i=0;i<l;i++)
|
||||
{
|
||||
if(a[i]!=' ')
|
||||
b[i]=a[i]-'A'+1;
|
||||
else
|
||||
b[i]=0;
|
||||
}
|
||||
int sum=0;
|
||||
for(i=0;i<l;i++)
|
||||
sum+=b[i]*(i+1);
|
||||
printf("%d\n",sum);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
using namespace std;
|
||||
int main(){
|
||||
char str[100];
|
||||
while(scanf("%s", str)&&str[0]!='#'){
|
||||
int flag=1, ans=0, len=strlen(str), t=0, i;
|
||||
for(i=0; i<len; i++){
|
||||
switch(str[i]){
|
||||
case '/':if(flag)ans+=100;flag=0;break;
|
||||
case '\\':t=100;break;
|
||||
case '.':ans+=t+100;t=0;flag=1;break;
|
||||
case '_':break;
|
||||
case '|':if(flag)ans+=50;flag=0;t=50;break;
|
||||
}
|
||||
}
|
||||
printf("%d\n", (ans+t)/len);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
int main()
|
||||
{
|
||||
int i,j,len,k,z,l,flag;
|
||||
char ch[100];
|
||||
char s[100][2];
|
||||
while(gets(ch))
|
||||
{
|
||||
memset(s,0,sizeof(s));
|
||||
flag=0;
|
||||
if(ch[0]=='*')
|
||||
break;
|
||||
len=strlen(ch);
|
||||
for(i=1;i<len-1;i++)
|
||||
{
|
||||
k=0;
|
||||
for(j=0;j+i<len;j++)
|
||||
{
|
||||
s[k][0]=ch[j];
|
||||
s[k][1]=ch[j+i];
|
||||
k++;
|
||||
}
|
||||
for(z=0;z<k;z++)
|
||||
for(l=z+1;l<k;l++)
|
||||
{
|
||||
if(s[z][0]==s[l][0]&&s[z][1]==s[l][1])
|
||||
{flag=1;break;}
|
||||
}
|
||||
if(flag==1)
|
||||
break;
|
||||
}
|
||||
if(flag==1)
|
||||
{
|
||||
printf("%s is NOT surprising.\n",ch);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%s is surprising.\n",ch);
|
||||
}
|
||||
memset(ch,0,sizeof(ch));
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,113 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
#define maxn 101
|
||||
struct Term
|
||||
{
|
||||
int s[maxn];
|
||||
char ch[maxn];
|
||||
}term[maxn];
|
||||
string word[maxn];
|
||||
string st;
|
||||
int n, m, termnum[maxn];
|
||||
bool input()
|
||||
{
|
||||
int i = 0;
|
||||
while (1)
|
||||
{
|
||||
getline(cin, word[i]);
|
||||
if (word[i] == "#")
|
||||
return false;
|
||||
if (word[i] == "*")
|
||||
break;
|
||||
i++;
|
||||
}
|
||||
n = i;
|
||||
return true;
|
||||
}
|
||||
void getterm(string &a, Term &term, int &num)
|
||||
{
|
||||
int i = 0, j = 0;
|
||||
while (i < a.length())
|
||||
{
|
||||
if (a[i] != '-' && a[i] != '+')
|
||||
{
|
||||
term.ch[j] = a[i];
|
||||
term.s[j] = 1;
|
||||
i++;
|
||||
j++;
|
||||
continue;
|
||||
}
|
||||
if (a[i] == '-')
|
||||
term.s[j] = 0;
|
||||
if (a[i] == '+')
|
||||
term.s[j] = 2;
|
||||
i++;
|
||||
term.ch[j] = a[i];
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
num = j;
|
||||
}
|
||||
void maketerms()
|
||||
{
|
||||
int temp;
|
||||
int i = 0;
|
||||
temp = st.find('|');
|
||||
while (temp != string::npos)
|
||||
{
|
||||
string sub = st.substr(0, temp);
|
||||
getterm(sub, term[i], termnum[i]);
|
||||
st.erase(0, temp + 1);
|
||||
i++;
|
||||
temp = st.find('|');
|
||||
}
|
||||
getterm(st, term[i], termnum[i]);
|
||||
m = i + 1;
|
||||
}
|
||||
bool match(string &word, Term &term, int &num)
|
||||
{
|
||||
for (int i = 0; i < num; i++)
|
||||
if (term.s[i] == 0 && word.find(term.ch[i]) != string::npos)
|
||||
return false;
|
||||
for (int i = 0; i < num; i++)
|
||||
if (term.s[i] == 2 && word.find(term.ch[i]) == string::npos)
|
||||
return false;
|
||||
for (int i = 0; i < num; i++)
|
||||
if (term.s[i] == 1 && word.find(term.ch[i]) != string::npos)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
if (!input())
|
||||
break;
|
||||
sort(word, word + n);
|
||||
while (getline(cin, st) && st != "**")
|
||||
{
|
||||
bool found = false;
|
||||
maketerms();
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < m; j++)
|
||||
if (match(word[i], term[j], termnum[j]))
|
||||
{
|
||||
found = true;
|
||||
cout << word[i] << endl;
|
||||
break;
|
||||
}
|
||||
if (found)
|
||||
break;
|
||||
}
|
||||
if (!found)
|
||||
cout << "NONE" << endl;
|
||||
}
|
||||
cout << "$" << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,96 @@
|
|||
#include<stdio.h>
|
||||
#include "math.h"
|
||||
int e[2001][2];//杈[0],to.[1]next;
|
||||
int a[1001][5];//[0],ID,[1]next;2x3y4z
|
||||
int c[1001];
|
||||
int cnum;
|
||||
int max;int m;
|
||||
bool sign[1001];
|
||||
int n;//圭扮
|
||||
int en;//杈圭扮
|
||||
void initt();
|
||||
void init();
|
||||
int DFS(int s);
|
||||
int eq(int x);
|
||||
int main()
|
||||
{
|
||||
while(1)
|
||||
{
|
||||
scanf("%d",&n);
|
||||
if(n==0)return 0;
|
||||
initt();
|
||||
init();
|
||||
int min=1000;
|
||||
int i;
|
||||
for(i=1;i<=n;i++)//n娆DFS
|
||||
{
|
||||
max=0;
|
||||
m=0;
|
||||
DFS(i);
|
||||
if(max<min)min=max,c[1]=a[i][0],cnum=1;
|
||||
else if(max==min)cnum++,c[cnum]=a[i][0];
|
||||
else ;
|
||||
}
|
||||
if(cnum==1)printf("%d",c[1]);
|
||||
else
|
||||
{
|
||||
if(c[1]>c[2])printf("%d %d",c[2],c[1]);
|
||||
else printf("%d %d",c[1],c[2]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
int DFS(int s)
|
||||
{
|
||||
if(sign[s])return 0;
|
||||
if((!e[a[s][1]][1])&&sign[e[a[s][1]][0]]){
|
||||
if(max<m){max=m; return 0;}
|
||||
else return 0;}
|
||||
m++;
|
||||
sign[s]=true;
|
||||
int next=a[s][1];
|
||||
while(next)
|
||||
{
|
||||
DFS(e[next][0]);
|
||||
next=e[next][1];
|
||||
}
|
||||
m--;sign[s]=false;
|
||||
return 0;
|
||||
}
|
||||
void initt()
|
||||
{
|
||||
int i;
|
||||
cnum=0;
|
||||
for(i=0;i<=n;i++)
|
||||
a[i][1]=0;
|
||||
for(i=0;i<=2*n;i++)
|
||||
e[i][1]=0;
|
||||
}
|
||||
void init()
|
||||
{
|
||||
int k=0;int en=0;
|
||||
k++;
|
||||
int id;int x;int y;int z;
|
||||
scanf("%d%d%d%d",&id,&x,&y,&z);
|
||||
a[k][0]=id;a[k][2]=x,a[k][3]=y,a[k][4]=z;
|
||||
while(k<n)
|
||||
{
|
||||
k++;
|
||||
scanf("%d%d%d%d",&id,&x,&y,&z);
|
||||
a[k][0]=id;a[k][2]=x,a[k][3]=y,a[k][4]=z;
|
||||
int ee;ee=eq(k);
|
||||
en++;e[en][0]=ee;e[en][1]=a[k][1];a[k][1]=en;
|
||||
en++;e[en][0]=k;e[en][1]=a[ee][1];a[ee][1]=en;
|
||||
}
|
||||
}
|
||||
int eq(int x)
|
||||
{
|
||||
int i;double min=10000;int xx;
|
||||
for(i=1;i<x;i++)
|
||||
{
|
||||
double temp=((a[x][3]-a[i][3])*(a[x][3]-a[i][3])+(a[x][4]-a[i][4])*(a[x][4]-a[i][4])+(a[x][2]-a[i][2])*(a[x][2]-a[i][2]));
|
||||
double val=sqrt(temp);
|
||||
if(val<min)min=val,xx=i;
|
||||
}
|
||||
return xx;
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
#include <cstdio>
|
||||
#include <cmath>
|
||||
int main()
|
||||
{
|
||||
double B,N;
|
||||
int A;
|
||||
while(scanf("%lf%lf",&B,&N)&&(B!=0.0||N!=0.0))
|
||||
{
|
||||
A=pow(B,1/N);
|
||||
if((B-pow(A,N))<(pow(A+1,N)-B))
|
||||
printf("%d\n",A);
|
||||
else
|
||||
printf("%d\n",A+1);
|
||||
}
|
||||
return 0;
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue