parent
5ed1cf19d0
commit
120ba634b8
|
@ -0,0 +1,170 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <deque>
|
||||
#include <queue>
|
||||
#include <stack>
|
||||
#include <bitset>
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <numeric>
|
||||
#include <utility>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <cstdio>
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <cctype>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
#include <ctime>
|
||||
#define MAXN 510005
|
||||
#define MAXM 164444
|
||||
#define INF 100000000
|
||||
#define eps 1e-7
|
||||
#define L(X) X<<1
|
||||
#define R(X) X<<1|1
|
||||
using namespace std;
|
||||
struct node
|
||||
{
|
||||
int left, right;
|
||||
int mi;
|
||||
}tree[4 * MAXN];
|
||||
int a[MAXN], pos[MAXN];
|
||||
void up(int C)
|
||||
{
|
||||
tree[C].mi = min(tree[L(C)].mi, tree[R(C)].mi);
|
||||
}
|
||||
void make_tree(int s, int e, int C)
|
||||
{
|
||||
tree[C].left = s;
|
||||
tree[C].right = e;
|
||||
tree[C].mi = INF;
|
||||
if(s == e) return;
|
||||
int mid = (s + e) >> 1;
|
||||
make_tree(s, mid, L(C));
|
||||
make_tree(mid + 1, e, R(C));
|
||||
}
|
||||
void update(int p, int C)
|
||||
{
|
||||
if(tree[C].left == tree[C].right)
|
||||
{
|
||||
tree[C].mi = p;
|
||||
return;
|
||||
}
|
||||
int mid = (tree[C].left + tree[C].right) >> 1;
|
||||
if(mid >= p) update(p, L(C));
|
||||
else update(p, R(C));
|
||||
up(C);
|
||||
}
|
||||
int ans;
|
||||
void query(int s, int e, int C)
|
||||
{
|
||||
if(tree[C].left >= s && tree[C].right <= e)
|
||||
{
|
||||
ans = min(ans, tree[C].mi);
|
||||
return;
|
||||
}
|
||||
int mid = (tree[C].left + tree[C].right) >> 1;
|
||||
if(mid >= s) query(s, e, L(C));
|
||||
if(mid < e) query(s, e, R(C));
|
||||
}
|
||||
int in()
|
||||
{
|
||||
int flag = 1;
|
||||
char ch;
|
||||
int a = 0;
|
||||
while((ch = getchar()) == ' ' || ch == '\n');
|
||||
if(ch == '-') flag = -1;
|
||||
else
|
||||
a += ch - '0';
|
||||
while((ch = getchar()) != ' ' && ch != '\n')
|
||||
{
|
||||
a *= 10;
|
||||
a += ch - '0';
|
||||
}
|
||||
return flag * a;
|
||||
}
|
||||
void out(int a)
|
||||
{
|
||||
if(a >= 10)out(a / 10);
|
||||
putchar(a % 10 + '0');
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int t, x, cas = 0;
|
||||
char s;
|
||||
while(scanf("%d", &t) != EOF && t)
|
||||
{
|
||||
if(cas) puts("");
|
||||
int cnt = 0;
|
||||
memset(pos, -1, sizeof(pos));
|
||||
printf("Case %d:\n", ++cas);
|
||||
make_tree(1, MAXN - 1, 1);
|
||||
getchar();
|
||||
while(t--)
|
||||
{
|
||||
s = getchar();
|
||||
x = in();
|
||||
if(s == 'A')
|
||||
{
|
||||
if(x < 5000)
|
||||
{
|
||||
ans = INF;
|
||||
int tx = x;
|
||||
for(int i = cnt - 1; i >= 0; i--)
|
||||
{
|
||||
if(a[i] % x < tx)
|
||||
{
|
||||
tx = a[i] % x;
|
||||
ans = a[i];
|
||||
}
|
||||
if(tx == 0) break;
|
||||
}
|
||||
if(ans == INF) {puts("-1");}
|
||||
else {out(pos[ans] + 1); putchar('\n');}
|
||||
}
|
||||
else
|
||||
{
|
||||
int bd = 500055 / x;
|
||||
int res = INF;
|
||||
int tx = x;
|
||||
for(int i = 0; i <= bd; i++)
|
||||
{
|
||||
ans = INF;
|
||||
int low = i * x;
|
||||
if(low < 1) low = 1;
|
||||
int high = (i + 1) * x - 1;
|
||||
if(high > 500054) high = 500054;
|
||||
query(low, high, 1);
|
||||
if(ans == INF) continue;
|
||||
if(ans % x < tx)
|
||||
{
|
||||
tx = ans % x;
|
||||
res = ans;
|
||||
}
|
||||
else if(ans % x == tx)
|
||||
{
|
||||
if(pos[ans] > pos[res]) res = ans;
|
||||
}
|
||||
}
|
||||
if(res == INF) {puts("-1");}
|
||||
else {out(pos[res] + 1); putchar('\n');}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(pos[x] == -1)
|
||||
{
|
||||
pos[x] = cnt;
|
||||
a[cnt++] = x;
|
||||
update(x, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
#define LL long long
|
||||
const int mod=10000;
|
||||
int main()
|
||||
{
|
||||
int p,n,tt=0;
|
||||
while(scanf("%d%d",&p,&n)!=EOF)
|
||||
{
|
||||
if(p==0&&n==0)
|
||||
break;
|
||||
int ans=1;
|
||||
while(n)
|
||||
{
|
||||
ans*=(n%p+1);
|
||||
n/=p;
|
||||
if(ans>=mod)
|
||||
ans%=mod;
|
||||
}
|
||||
printf("Case %d: %04d\n",++tt,ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
#include<stdio.h>
|
||||
#include<algorithm>
|
||||
#include<string.h>
|
||||
using namespace std;
|
||||
struct mart
|
||||
{
|
||||
__int64 mat[5][5];
|
||||
};
|
||||
__int64 mod=10007;
|
||||
mart kk;
|
||||
mart multi(mart a,mart b)
|
||||
{
|
||||
mart c;
|
||||
int i,j,k;
|
||||
for(i=1;i<=4;i++)
|
||||
for(j=1;j<=4;j++)
|
||||
{
|
||||
c.mat[i][j]=0;
|
||||
for(k=1;k<=4;k++)
|
||||
c.mat[i][j]=(c.mat[i][j]+a.mat[i][k]*b.mat[k][j])%mod;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
mart power(int k)
|
||||
{
|
||||
mart p,q;
|
||||
int i,j;
|
||||
for(i=1;i<=4;i++)
|
||||
for(j=1;j<=4;j++)
|
||||
{
|
||||
p.mat[i][j]=kk.mat[i][j];
|
||||
if(i==j)
|
||||
q.mat[i][j]=1;
|
||||
else
|
||||
q.mat[i][j]=0;
|
||||
}
|
||||
if(k==0)
|
||||
return q;
|
||||
while(k!=1)
|
||||
{
|
||||
if(k&1)
|
||||
{
|
||||
k--;
|
||||
q=multi(p,q);
|
||||
}
|
||||
else
|
||||
{
|
||||
k=k>>1;
|
||||
p=multi(p,p);
|
||||
}
|
||||
}
|
||||
p=multi(p,q);
|
||||
return p;
|
||||
}
|
||||
void init(__int64 x,__int64 y)
|
||||
{
|
||||
kk.mat[1][1]=1;
|
||||
kk.mat[1][2]=(x%mod)*(x%mod)%mod;
|
||||
kk.mat[1][3]=(y%mod)*(y%mod)%mod;
|
||||
kk.mat[1][4]=(2*x%mod)*(y%mod)%mod;
|
||||
kk.mat[2][1]=0;
|
||||
kk.mat[2][2]=(x%mod)*(x%mod)%mod;
|
||||
kk.mat[2][3]=(y%mod)*(y%mod)%mod;
|
||||
kk.mat[2][4]=(2*x%mod)*(y%mod)%mod;
|
||||
kk.mat[3][1]=0;
|
||||
kk.mat[3][2]=1;
|
||||
kk.mat[3][3]=0;
|
||||
kk.mat[3][4]=0;
|
||||
kk.mat[4][1]=0;
|
||||
kk.mat[4][2]=x%mod;
|
||||
kk.mat[4][3]=0;
|
||||
kk.mat[4][4]=y%mod;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n,i;
|
||||
__int64 a[5],x,y;
|
||||
mart xx;
|
||||
while(scanf("%d%I64d%I64d",&n,&x,&y)!=EOF)
|
||||
{
|
||||
a[1]=2,a[2]=1,a[3]=1,a[4]=1;
|
||||
init(x,y);
|
||||
xx=power(n-1);
|
||||
__int64 ans=0;
|
||||
for(i=1;i<=4;i++)
|
||||
ans=(ans+a[i]*xx.mat[1][i])%mod;
|
||||
printf("%I64d\n",ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
#define LL __int64
|
||||
const LL maxn=1001;
|
||||
LL e[maxn],t;
|
||||
LL gcd(LL a,LL b)
|
||||
{
|
||||
return b==0?a:gcd(b,a%b);
|
||||
}
|
||||
LL euler_phi(LL n)
|
||||
{
|
||||
LL m=(LL)sqrt(n+0.5);
|
||||
LL i,ans=n;
|
||||
for(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;
|
||||
}
|
||||
void find(LL n)
|
||||
{
|
||||
LL m=(LL)sqrt(n+0.5);
|
||||
for(LL i=1;i<m;i++)
|
||||
if(n%i==0){e[t++]=i;e[t++]=n/i;}
|
||||
if(m*m==n)e[t++]=m;
|
||||
}
|
||||
LL pow_mod(LL a,LL b,LL mod)
|
||||
{
|
||||
LL s=1;
|
||||
while(b)
|
||||
{
|
||||
if(b&1)
|
||||
s=(s*a)%mod;
|
||||
a=(a*a)%mod;
|
||||
b=b>>1;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
LL a,x,y;
|
||||
while(cin>>x>>y>>a)
|
||||
{
|
||||
LL m,phi,i;
|
||||
if(y==0){cout<<"1"<<endl;continue;}
|
||||
m=a/gcd(y/(x-1),a);
|
||||
if(gcd(m,x)!=1){cout<<"Impossible!"<<endl;continue;}
|
||||
phi=euler_phi(m);
|
||||
t=0;
|
||||
find(phi);
|
||||
sort(e,e+t);
|
||||
for(i=0;i<t;i++)
|
||||
{
|
||||
if(pow_mod(x,e[i],m)==1)
|
||||
{
|
||||
cout<<e[i]<<endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,130 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#define MAXD 100010
|
||||
int N, M, a[MAXD], mc[4 * MAXD], lc[4 * MAXD], rc[4 * MAXD];
|
||||
int getmax(int x, int y)
|
||||
{
|
||||
return x > y ? x : y;
|
||||
}
|
||||
void update(int cur, int x, int y)
|
||||
{
|
||||
int mid = (x + y) / 2, ls = 2 * cur, rs = 2 * cur + 1;
|
||||
mc[cur] = getmax(mc[ls], mc[rs]);
|
||||
lc[cur] = lc[ls], rc[cur] = rc[rs];
|
||||
if(a[mid] < a[mid + 1])
|
||||
{
|
||||
mc[cur] = getmax(mc[cur], rc[ls] + lc[rs]);
|
||||
if(lc[ls] == mid - x + 1)
|
||||
lc[cur] += lc[rs];
|
||||
if(rc[rs] == y - mid)
|
||||
rc[cur] += rc[ls];
|
||||
}
|
||||
}
|
||||
void build(int cur, int x, int y)
|
||||
{
|
||||
int mid = (x + y) / 2, ls = 2 * cur, rs = 2 * cur + 1;
|
||||
if(x == y)
|
||||
{
|
||||
mc[cur] = lc[cur] = rc[cur] = 1;
|
||||
return ;
|
||||
}
|
||||
build(ls, x, mid);
|
||||
build(rs, mid + 1, y);
|
||||
update(cur, x, y);
|
||||
}
|
||||
void init()
|
||||
{
|
||||
int i, j, k;
|
||||
for(i = 0; i < N; i ++)
|
||||
scanf("%d", &a[i]);
|
||||
build(1, 0, N - 1);
|
||||
}
|
||||
void refresh(int cur, int x, int y, int k, int v)
|
||||
{
|
||||
int mid = (x + y) / 2, ls = 2 * cur, rs = 2 * cur + 1;
|
||||
if(x == y)
|
||||
{
|
||||
a[x] = v;
|
||||
return ;
|
||||
}
|
||||
if(k <= mid)
|
||||
refresh(ls, x, mid, k, v);
|
||||
else
|
||||
refresh(rs, mid + 1, y, k, v);
|
||||
update(cur, x, y);
|
||||
}
|
||||
int query(int cur, int x, int y, int s, int t, int p, int &ans)
|
||||
{
|
||||
int mid = (x + y) / 2, ls = 2 * cur, rs = 2 * cur + 1;
|
||||
if(x == y)
|
||||
return 1;
|
||||
if(mid >= t)
|
||||
return query(ls, x, mid, s, t, 0, ans);
|
||||
else if(mid + 1 <= s)
|
||||
return query(rs, mid + 1, y, s, t, 1, ans);
|
||||
else
|
||||
{
|
||||
int ln, rn;
|
||||
if(x >= s && y <= t)
|
||||
{
|
||||
ans = getmax(ans, mc[cur]);
|
||||
return p ? rc[cur] : lc[cur];
|
||||
}
|
||||
ln = query(ls, x, mid, s, t, 1, ans), rn = query(rs, mid + 1, y, s, t, 0, ans);
|
||||
if(x >= s && mid <= t)
|
||||
{
|
||||
if(a[mid] < a[mid + 1])
|
||||
{
|
||||
ans = getmax(ans, rc[ls] + rn);
|
||||
if(rc[ls] == mid - x + 1)
|
||||
return rc[ls] + rn;
|
||||
}
|
||||
return lc[ls];
|
||||
}
|
||||
else if(mid + 1 >= s && y <= t)
|
||||
{
|
||||
if(a[mid] < a[mid + 1])
|
||||
{
|
||||
ans = getmax(ans, ln + lc[rs]);
|
||||
if(lc[rs] == y - mid)
|
||||
return ln + lc[rs];
|
||||
}
|
||||
return rc[rs];
|
||||
}
|
||||
else
|
||||
{
|
||||
if(a[mid] < a[mid + 1])
|
||||
ans = getmax(ans, ln + rn);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
int i, j, k, x, y, ans;
|
||||
char b[5];
|
||||
for(i = 0; i < M; i ++)
|
||||
{
|
||||
scanf("%s%d%d", b, &x, &y);
|
||||
if(b[0] == 'Q')
|
||||
{
|
||||
ans = 1;
|
||||
query(1, 0, N - 1, x, y, 0, ans);
|
||||
printf("%d\n", ans);
|
||||
}
|
||||
else
|
||||
refresh(1, 0, N - 1, x, y);
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int t;
|
||||
scanf("%d", &t);
|
||||
while(t --)
|
||||
{
|
||||
scanf("%d%d", &N, &M);
|
||||
init();
|
||||
solve();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,97 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <queue>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
struct node
|
||||
{
|
||||
int x[2],y[2],step;
|
||||
int b[2],h[2];
|
||||
};
|
||||
char map[25][25];
|
||||
bool vis[25][25][25][25];
|
||||
int sx[2],sy[2],n,m;
|
||||
int to[4][2] = {0,1,1,0,0,-1,-1,0};
|
||||
int bfs()
|
||||
{
|
||||
queue<node> Q;
|
||||
node a,next;
|
||||
a.x[0] = sx[0],a.y[0]=sy[0];
|
||||
a.x[1] = sx[1],a.y[1]=sy[1];
|
||||
a.step = 0;
|
||||
a.b[0] = a.b[1] = a.h[0] = a.h[1] = 0;
|
||||
Q.push(a);
|
||||
memset(vis,false,sizeof(vis));
|
||||
while(!Q.empty())
|
||||
{
|
||||
a = Q.front();
|
||||
Q.pop();
|
||||
int i,j;
|
||||
for(i = 0; i<4; i++)
|
||||
{
|
||||
next = a;
|
||||
for(j = 0; j<2; j++)
|
||||
{
|
||||
if(next.b[j]) continue;
|
||||
next.x[j]+=to[i][0];
|
||||
next.y[j]+=to[i][1];
|
||||
if(map[next.x[j]][next.y[j]]=='*')
|
||||
{
|
||||
next.x[j]-=to[i][0];
|
||||
next.y[j]-=to[i][1];
|
||||
}
|
||||
}
|
||||
if(vis[next.x[0]][next.y[0]][next.x[1]][next.y[1]])
|
||||
continue;
|
||||
if(next.x[0]==next.x[1]&& next.y[0]==next.y[1] && next.b[0]+next.b[1]==0)
|
||||
continue;
|
||||
next.step++;
|
||||
vis[next.x[0]][next.y[0]][next.x[1]][next.y[1]] = true;
|
||||
int flag = 1;
|
||||
for(j = 0; j<2; j++)
|
||||
{
|
||||
int now = map[next.x[j]][next.y[j]];
|
||||
if(now<2 && !next.h[now])
|
||||
{
|
||||
next.h[now]=1;
|
||||
next.b[j] = 1;
|
||||
}
|
||||
if(!next.b[j])
|
||||
flag = 0;
|
||||
}
|
||||
if(flag)
|
||||
return next.step;
|
||||
Q.push(next);
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int t,i,j;
|
||||
scanf("%d",&t);
|
||||
while(t--)
|
||||
{
|
||||
scanf("%d%d",&n,&m);
|
||||
int cnt = 0,len = 0;
|
||||
for(i = 0; i<n; i++)
|
||||
{
|
||||
scanf("%s",map[i]);
|
||||
for(j = 0; j<m; j++)
|
||||
{
|
||||
if(map[i][j]=='H') map[i][j] = cnt++;
|
||||
else if(map[i][j]=='B')
|
||||
{
|
||||
sx[len] = i,sy[len] = j;
|
||||
len++;
|
||||
}
|
||||
}
|
||||
}
|
||||
int ans = bfs();
|
||||
if(ans!=-1)
|
||||
printf("%d\n",ans);
|
||||
else
|
||||
printf("Sorry , sir , my poor program fails to get an answer.\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
#include<stdio.h>
|
||||
#include<math.h>
|
||||
double cal(double r1,double r2,double dx)
|
||||
{
|
||||
return sqrt(r1*r1-dx*dx)*sqrt(r2*r2-dx*dx);
|
||||
}
|
||||
double fun(double r1,double r2)
|
||||
{
|
||||
int n=1000000;
|
||||
double dx=(r1<r2?r1:r2)/n;
|
||||
double pi=3.141592653;
|
||||
double v=0;
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
v+=cal(r1,r2,(double)dx*i+dx/2);
|
||||
}
|
||||
return v*8*dx;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int t;
|
||||
double r1,r2;
|
||||
scanf("%d",&t);
|
||||
while(t--)
|
||||
{
|
||||
scanf("%lf%lf",&r1,&r2);
|
||||
printf("%.2lf\n",fun(r1,r2));
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,104 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#define MAXD 1010
|
||||
#define MAXM 10010
|
||||
#define ST 74
|
||||
#define MAXQ 74010
|
||||
#define INF 0x3f3f3f3f
|
||||
const int Q = 74000;
|
||||
int N, M, P, first[MAXD], e, next[MAXM], v[MAXM], w[MAXM];
|
||||
int bit[MAXD], q[MAXQ], front, rear, inq[MAXD][ST];
|
||||
int f[MAXD][ST], dp[ST];
|
||||
void add(int x, int y, int z)
|
||||
{
|
||||
v[e] = y, w[e] = z;
|
||||
next[e] = first[x], first[x] = e ++;
|
||||
}
|
||||
void init()
|
||||
{
|
||||
int i, x, y, z;
|
||||
memset(f, 0x3f, sizeof(f));
|
||||
memset(bit, 0, sizeof(bit));
|
||||
for(i = 1; i <= N + M; i ++)
|
||||
{
|
||||
if(i <= N) bit[i] = 1 << i - 1;
|
||||
scanf("%d", &z);
|
||||
if(i <= N) f[i][bit[i]] = 0, f[i][bit[i] | 1 << N] = z;
|
||||
else f[i][1 << N] = z;
|
||||
}
|
||||
memset(first, -1, sizeof(first));
|
||||
e = 0;
|
||||
for(i = 0; i < P; i ++)
|
||||
{
|
||||
scanf("%d%d%d", &x, &y, &z);
|
||||
add(x, y, z), add(y, x, z);
|
||||
}
|
||||
}
|
||||
int Min(int x, int y)
|
||||
{
|
||||
return x < y ? x : y;
|
||||
}
|
||||
void spfa()
|
||||
{
|
||||
int i, x, st, y, nst;
|
||||
while(front != rear)
|
||||
{
|
||||
x = q[front] & 1023, st = q[front] >> 10;
|
||||
inq[x][st] = 0;
|
||||
++ front > Q ? front = 0 : 0;
|
||||
for(i = first[x]; i != -1; i = next[i])
|
||||
{
|
||||
y = v[i], nst = st | bit[y];
|
||||
if(f[x][st] + w[i] < f[y][nst])
|
||||
{
|
||||
f[y][nst] = f[x][st] + w[i];
|
||||
if(st == nst && !inq[y][nst])
|
||||
{
|
||||
q[rear ++] = nst << 10 | y, inq[y][nst] = 1;
|
||||
rear > Q ? rear = 0 : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
int i, j, k, nn = 1 << N + 1;
|
||||
front = rear = 0;
|
||||
memset(inq, 0, sizeof(inq));
|
||||
for(i = 0; i < nn; i ++)
|
||||
{
|
||||
for(j = 1; j <= N + M; j ++)
|
||||
{
|
||||
for(k = i - 1 & i; k; k = k - 1 & i)
|
||||
f[j][i] = Min(f[j][i], f[j][k | bit[j]] + f[j][i - k | bit[j]]);
|
||||
if(f[j][i] < INF)
|
||||
{
|
||||
q[rear ++] = i << 10 | j, inq[j][i] = 1;
|
||||
rear > Q ? rear = 0 : 0;
|
||||
}
|
||||
}
|
||||
spfa();
|
||||
}
|
||||
memset(dp, 0x3f, sizeof(dp));
|
||||
for(i = 0; i < nn; i ++)
|
||||
for(j = 1; j <= N + M; j ++)
|
||||
dp[i] = Min(dp[i], f[j][i]);
|
||||
for(i = 0; i < nn; i ++)
|
||||
if(i & 1 << N)
|
||||
{
|
||||
for(j = i - 1 & i; j; j = j - 1 & i)
|
||||
if(j & 1 << N)
|
||||
dp[i] = Min(dp[i], dp[j] + dp[i - j | 1 << N]);
|
||||
}
|
||||
printf("%d\n", dp[(1 << N + 1) - 1]);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while(scanf("%d%d%d", &N, &M, &P) == 3)
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <queue>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
using namespace std;
|
||||
#define MAXN 100010
|
||||
int pre[MAXN],vis[MAXN],low[MAXN];
|
||||
vector <int> e[MAXN];
|
||||
int bfs1(int st,int et){
|
||||
queue <int> q;
|
||||
q.push(st);
|
||||
vis[st]=1;pre[st]=-1;
|
||||
while(!q.empty()){
|
||||
int now=q.front();
|
||||
q.pop();
|
||||
for(int i=e[now].size()-1;i>=0;i--){
|
||||
int next=e[now][i];
|
||||
if(vis[next])continue;
|
||||
vis[next]=1;
|
||||
pre[next]=now;
|
||||
if(next==et)return 1;
|
||||
q.push(next);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int bfs2(int u){
|
||||
queue <int> q;
|
||||
q.push(u);
|
||||
int res=u;
|
||||
vis[u]=1;
|
||||
while(!q.empty()){
|
||||
int now=q.front();
|
||||
q.pop();
|
||||
for(int i=e[now].size()-1;i>=0;i--){
|
||||
int next=e[now][i];
|
||||
if(vis[next])continue;
|
||||
vis[next]=1;
|
||||
if(low[next]==0)q.push(next);
|
||||
else if(low[next]<low[res])res=next;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n,m,st,et;
|
||||
while(~scanf("%d%d",&n,&m)){
|
||||
for(int i=0;i<=n;i++)
|
||||
e[i].clear();
|
||||
memset(vis,0,sizeof(vis));
|
||||
memset(low,0,sizeof(low));
|
||||
for(int i=0,x,y;i<m;i++){
|
||||
scanf("%d%d",&x,&y);
|
||||
e[x].push_back(y);
|
||||
}
|
||||
scanf("%d%d",&st,&et);
|
||||
if(!bfs1(st,et)){
|
||||
printf("%d\n",n);
|
||||
continue;
|
||||
}
|
||||
int u=et,tot=1;
|
||||
memset(vis,0,sizeof(vis));
|
||||
while(u!=-1){
|
||||
low[u]=tot++;
|
||||
u=pre[u];
|
||||
}
|
||||
int cnt=1;
|
||||
u=st;
|
||||
while(u!=et){
|
||||
u=bfs2(u);
|
||||
++cnt;
|
||||
}
|
||||
printf("%d\n",cnt);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
#include<iostream>
|
||||
#include<string.h>
|
||||
using namespace std;
|
||||
const int maxn=100000+10;
|
||||
int p[maxn],r[maxn],cnt[maxn];
|
||||
void make()
|
||||
{
|
||||
memset(r,0,sizeof(r));
|
||||
memset(p,255,sizeof(p));
|
||||
for(int i=0;i<maxn;i++) cnt[i]=1;
|
||||
}
|
||||
int find(int x)
|
||||
{
|
||||
int px,t;
|
||||
for(px=x;p[px]>=0;px=p[px]);
|
||||
while (x!=px) {
|
||||
t=p[x];
|
||||
p[x]=px;
|
||||
x=t;
|
||||
}
|
||||
return px;
|
||||
}
|
||||
int unio(int x,int y)
|
||||
{
|
||||
x=find(x),y=find(y);
|
||||
if(x==y) return -1;
|
||||
if(r[x]>r[y])
|
||||
{
|
||||
p[y]=x;
|
||||
cnt[y]+=cnt[x];
|
||||
cnt[x]=cnt[y];
|
||||
return x;
|
||||
}
|
||||
else
|
||||
{
|
||||
p[x]=y;
|
||||
cnt[x]+=cnt[y];
|
||||
cnt[y]=cnt[x];
|
||||
if(r[x]==r[y]) r[y]++;
|
||||
return y;
|
||||
}
|
||||
}
|
||||
int d[maxn];
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
while (scanf("%d",&n)!=EOF)
|
||||
{
|
||||
make();
|
||||
for(int i=0;i<n;i++){
|
||||
scanf("%d",&d[i]);
|
||||
unio(i,d[i]);
|
||||
}
|
||||
int tmp_max=-1,k=-1;
|
||||
bool flag=false;
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
if(i==d[i])
|
||||
{
|
||||
if(tmp_max==cnt[find(i)])
|
||||
{
|
||||
flag=false;
|
||||
}
|
||||
if(cnt[find(i)]>tmp_max)
|
||||
{
|
||||
flag=true;
|
||||
tmp_max=cnt[find(i)];
|
||||
k=i;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!flag)
|
||||
{
|
||||
printf("Trouble\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%d\n",k);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,169 @@
|
|||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <queue>
|
||||
#include <iostream>
|
||||
#define inf 0x3f3f3f3f
|
||||
#define MAXN 3000
|
||||
#define MAXM 30000
|
||||
using namespace std;
|
||||
struct node
|
||||
{
|
||||
int u,v,f,c;
|
||||
};
|
||||
node e[MAXM];
|
||||
int first[MAXN],ne[MAXM],cc;
|
||||
int inq[MAXN],pre[MAXN],preedge[MAXN],d[MAXN];
|
||||
inline void add_edge(int u,int v,int f,int c)
|
||||
{
|
||||
e[cc].u=u;
|
||||
e[cc].v=v;
|
||||
e[cc].f=f;
|
||||
e[cc].c=c;
|
||||
ne[cc]=first[u];
|
||||
first[u]=cc;
|
||||
cc++;
|
||||
e[cc].v=u;
|
||||
e[cc].u=v;
|
||||
e[cc].f=0;
|
||||
e[cc].c=-c;
|
||||
ne[cc]=first[v];
|
||||
first[v]=cc;
|
||||
cc++;
|
||||
}
|
||||
int SPFA(int s,int t)
|
||||
{
|
||||
memset(inq,0,sizeof(inq));
|
||||
int i;
|
||||
for(i=0;i<=t;i++)
|
||||
d[i]=-inf;
|
||||
memset(pre,-1,sizeof(pre));
|
||||
memset(preedge,-1,sizeof(preedge));
|
||||
d[s]=0;
|
||||
queue<int> q;
|
||||
q.push(s);
|
||||
while(!q.empty())
|
||||
{
|
||||
int u=q.front();
|
||||
q.pop();
|
||||
inq[u]=0;
|
||||
int i;
|
||||
for(i=first[u];i!=-1;i=ne[i])
|
||||
{
|
||||
int v=e[i].v;
|
||||
if(e[i].f)
|
||||
{
|
||||
if(d[v]<d[u]+e[i].c)
|
||||
{
|
||||
d[v]=d[u]+e[i].c;
|
||||
pre[v]=u;
|
||||
preedge[v]=i;
|
||||
if(!inq[v])
|
||||
{
|
||||
inq[v]=1;
|
||||
q.push(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(d[t]==-inf)
|
||||
return 0;
|
||||
else
|
||||
return 1;
|
||||
}
|
||||
int Min_Cost_Flow(int s,int t)
|
||||
{
|
||||
int ans_flow=0,ans_cost=0,mm,tmp;
|
||||
while(SPFA(s,t))
|
||||
{
|
||||
mm=inf;
|
||||
int u=t;
|
||||
while(pre[u]!=-1)
|
||||
{
|
||||
tmp=preedge[u];
|
||||
mm=min(mm,e[tmp].f);
|
||||
u=pre[u];
|
||||
}
|
||||
u=t;
|
||||
while(pre[u]!=-1)
|
||||
{
|
||||
tmp=preedge[u];
|
||||
e[tmp].f-=mm;
|
||||
e[tmp^1].f+=mm;
|
||||
u=pre[u];
|
||||
}
|
||||
ans_flow+=mm;
|
||||
ans_cost+=mm*d[t];
|
||||
}
|
||||
return ans_cost;
|
||||
}
|
||||
int V[100],H[100],P[100],A[100],B[100];
|
||||
int win(int i,int j)
|
||||
{
|
||||
int x,y;
|
||||
if(P[j]%A[i]==0)
|
||||
x=P[j]/A[i];
|
||||
else
|
||||
x=P[j]/A[i]+1;
|
||||
if(H[i]%B[j]==0)
|
||||
y=H[i]/B[j];
|
||||
else
|
||||
y=H[i]/B[j]+1;
|
||||
if(x<=y)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
while(scanf("%d",&n),n)
|
||||
{
|
||||
memset(first,-1,sizeof(first));
|
||||
memset(ne,-1,sizeof(ne));
|
||||
cc=0;
|
||||
int i;
|
||||
for(i=1;i<=n;i++)
|
||||
scanf("%d",&V[i]);
|
||||
for(i=1;i<=n;i++)
|
||||
scanf("%d",&H[i]);
|
||||
for(i=1;i<=n;i++)
|
||||
scanf("%d",&P[i]);
|
||||
for(i=1;i<=n;i++)
|
||||
scanf("%d",&A[i]);
|
||||
for(i=1;i<=n;i++)
|
||||
scanf("%d",&B[i]);
|
||||
int s=0,t=2*n+1,j;
|
||||
for(i=1;i<=n;i++)
|
||||
{
|
||||
for(j=1;j<=n;j++)
|
||||
{
|
||||
if(win(i,j))
|
||||
{
|
||||
if(i==j)
|
||||
add_edge(i,n+j,1,V[i]*100+1);
|
||||
else
|
||||
add_edge(i,n+j,1,V[i]*100);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(i==j)
|
||||
add_edge(i,n+j,1,-V[i]*100+1);
|
||||
else
|
||||
add_edge(i,n+j,1,-V[i]*100);
|
||||
}
|
||||
}
|
||||
}
|
||||
for(i=1;i<=n;i++)
|
||||
{
|
||||
add_edge(0,i,1,0);
|
||||
add_edge(n+i,t,1,0);
|
||||
}
|
||||
int res=Min_Cost_Flow(s,t);
|
||||
if(res/100<=0)
|
||||
printf("Oh, I lose my dear seaco!\n");
|
||||
else
|
||||
printf("%d %.3f%%\n",res/100,100*1.0*(res%100)/n);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,89 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<queue>
|
||||
using namespace std;
|
||||
int n;
|
||||
int s_x,s_y;
|
||||
int map[111][111];
|
||||
int flag[111][111];
|
||||
int dir[8][2]={1,0, -1,0, 0,1, 0,-1, -1,-1, -1,1, 1,-1, 1,1};
|
||||
struct node
|
||||
{
|
||||
int x,y;
|
||||
};
|
||||
int judge(int x,int y)
|
||||
{
|
||||
int tot=0;
|
||||
int i;
|
||||
int t_x,t_y;
|
||||
for(i=0;i<8;i++)
|
||||
{
|
||||
t_x=x+dir[i][0];
|
||||
t_y=y+dir[i][1];
|
||||
if(t_x<0 || t_x>=n || t_y<0 || t_y>=n) continue;
|
||||
if(map[t_x][t_y]==-2) tot++;
|
||||
}
|
||||
return tot;
|
||||
}
|
||||
void BFS()
|
||||
{
|
||||
queue<node>q;
|
||||
node cur,next;
|
||||
int i;
|
||||
int temp;
|
||||
cur.x=s_x;
|
||||
cur.y=s_y;
|
||||
flag[cur.x][cur.y]=1;
|
||||
q.push(cur);
|
||||
while(!q.empty())
|
||||
{
|
||||
cur=q.front();
|
||||
q.pop();
|
||||
temp=judge(cur.x,cur.y);
|
||||
map[cur.x][cur.y]=temp;
|
||||
if(temp) continue;
|
||||
for(i=0;i<8;i++)
|
||||
{
|
||||
next.x=cur.x+dir[i][0];
|
||||
next.y=cur.y+dir[i][1];
|
||||
if(next.x<0 || next.x>=n || next.y<0 || next.y>=n) continue;
|
||||
if(!flag[next.x][next.y])
|
||||
{
|
||||
q.push(next);
|
||||
flag[next.x][next.y]=1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i,l;
|
||||
char str[111];
|
||||
while(scanf("%d",&n)!=-1)
|
||||
{
|
||||
for(i=0;i<n;i++)
|
||||
{
|
||||
scanf("%s",str);
|
||||
for(l=0;l<n;l++)
|
||||
{
|
||||
if(str[l]=='O') map[i][l]=-1;
|
||||
else if(str[l]=='X')map[i][l]=-2;
|
||||
}
|
||||
}
|
||||
scanf("%d%d",&s_x,&s_y);
|
||||
if(map[s_x][s_y]==-2) {printf("it is a beiju!\n\n");continue;}
|
||||
memset(flag,0,sizeof(flag));
|
||||
BFS();
|
||||
for(i=0;i<n;i++)
|
||||
{
|
||||
for(l=0;l<n;l++)
|
||||
{
|
||||
if(map[i][l]<0) printf(".");
|
||||
else printf("%d",map[i][l]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
#include<iostream>
|
||||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<string>
|
||||
#include<algorithm>
|
||||
#include<sstream>
|
||||
#include<map>
|
||||
#include<set>
|
||||
#include<cmath>
|
||||
#include<iomanip>
|
||||
#include<cstdlib>
|
||||
#include<cctype>
|
||||
#include<assert.h>
|
||||
#include<stack>
|
||||
#include<queue>
|
||||
#include<vector>
|
||||
#include<list>
|
||||
using namespace std;
|
||||
double pi=acos(-1);
|
||||
double w,r,sita;
|
||||
int main() {
|
||||
while(~scanf("%lf%lf%lf",&r,&w,&sita))
|
||||
{
|
||||
sita/=180.;
|
||||
sita*=pi;
|
||||
double umc=cos(sita/2.)-1.;
|
||||
r-=w/umc;
|
||||
printf("%.3f\n",r);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,79 @@
|
|||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <climits>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
const int N = 100;
|
||||
int n, m;
|
||||
int boss[N + 2];
|
||||
int cur[N + 2];
|
||||
int occupy[N + 2][N + 2];
|
||||
int need[N + 2][N + 2];
|
||||
int mark[N + 2];
|
||||
bool check(int x)
|
||||
{
|
||||
if (mark[x])return false;
|
||||
int i;
|
||||
for (i = 1; i <= n; i++)
|
||||
{
|
||||
if (need[x][i] > cur[i])return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void callBack(int x)
|
||||
{
|
||||
int i;
|
||||
for (i = 1; i <= n; i++)
|
||||
{
|
||||
cur[i] += occupy[x][i];
|
||||
need[x][i] = 0;
|
||||
}
|
||||
mark[x] = 1;
|
||||
}
|
||||
int main ()
|
||||
{
|
||||
int i, j, tmp;
|
||||
while (scanf("%d%d", &n, &m) != EOF)
|
||||
{
|
||||
memset(mark, 0, sizeof(mark));
|
||||
for (i = 1; i <= n; i++)
|
||||
{
|
||||
scanf("%d", &boss[i]);
|
||||
cur[i] = boss[i];
|
||||
}
|
||||
for (i = 1; i <= m; i++)
|
||||
for (j = 1; j <= n; j++)
|
||||
{
|
||||
scanf("%d", &need[i][j]);
|
||||
need[i][j] < 0 ? 0 : need[i][j];
|
||||
}
|
||||
for (i = 1; i <= m; i++)
|
||||
for (j = 1; j <= n; j++)
|
||||
{
|
||||
scanf("%d", &tmp);
|
||||
need[i][j] -= tmp;
|
||||
occupy[i][j] = tmp;
|
||||
cur[j] -= tmp;
|
||||
}
|
||||
int flag;
|
||||
while (1)
|
||||
{
|
||||
flag = 0;
|
||||
for (i = 1; i <= m; i++)
|
||||
{
|
||||
if (check(i))
|
||||
{
|
||||
callBack(i);
|
||||
flag = 1;
|
||||
}
|
||||
}
|
||||
if (!flag)break;
|
||||
}
|
||||
for (i = 1; i <= n; i++)
|
||||
if (cur[i] != boss[i])break;
|
||||
if (i > n)printf("Yes\n");
|
||||
else
|
||||
printf("No\n");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,306 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#define eps 1e-8
|
||||
using namespace std;
|
||||
const double PI=acos(-1.0);
|
||||
const int MAXN=105;
|
||||
struct point3{
|
||||
double x,y,z;
|
||||
};
|
||||
struct order{
|
||||
char c;
|
||||
double x,y,z,ang;
|
||||
}ord[MAXN];
|
||||
point3 rotate3(point3 q,double x0,double y0,double z0,double ang)
|
||||
{
|
||||
double x2=x0*x0;
|
||||
double y2=y0*y0;
|
||||
double z2=z0*z0;
|
||||
double d2=x2+y2+z2;
|
||||
double d=sqrt(d2);
|
||||
double sina=sin(ang);
|
||||
double cosa=cos(ang);
|
||||
point3 ans;
|
||||
ans.x=(x2+(y2+z2)*cosa)/d2*q.x+(x0*y0*(1-cosa)/d2-z0*sina/d)*q.y+(x0*z0*(1-cosa)/d2+y0*sina/d)*q.z;
|
||||
ans.y=(y0*x0*(1-cosa)/d2+z0*sina/d)*q.x+(y2+(x2+z2)*cosa)/d2*q.y+(y0*z0*(1-cosa)/d2-x0*sina/d)*q.z;
|
||||
ans.z=(z0*x0*(1-cosa)/d2-y0*sina/d)*q.x+(z0*y0*(1-cosa)/d2+x0*sina/d)*q.y+(z2+(x2+y2)*cosa)/d2*q.z;
|
||||
return ans;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int T;
|
||||
char str[500];
|
||||
scanf("%d",&T);
|
||||
while(T--){
|
||||
int i,j;
|
||||
double tmp;
|
||||
double tail,muti;
|
||||
double sig;
|
||||
for(i=0;;i++){
|
||||
scanf("%s",str);
|
||||
if(str[2]=='B') ord[i].c='B';
|
||||
else if(str[2]=='S'){
|
||||
ord[i].c='S';
|
||||
for(j=2;str[j]!='(';j++);
|
||||
tmp=0.0;
|
||||
sig=1.0;
|
||||
for(j++;str[j]!='.';j++){
|
||||
if(str[j]=='-'){
|
||||
sig=-1.0;
|
||||
continue;
|
||||
}
|
||||
tmp=tmp*10+str[j]-'0';
|
||||
}
|
||||
tail=0.0;
|
||||
muti=0.1;
|
||||
for(j++;str[j]!=',';j++){
|
||||
tail+=(str[j]-'0')*muti;
|
||||
muti/=10.0;
|
||||
}
|
||||
tmp+=tail;
|
||||
ord[i].x=tmp*sig;
|
||||
tmp=0.0;
|
||||
sig=1.0;
|
||||
for(j++;str[j]!='.';j++){
|
||||
if(str[j]=='-'){
|
||||
sig=-1.0;
|
||||
continue;
|
||||
}
|
||||
tmp=tmp*10+str[j]-'0';
|
||||
}
|
||||
tail=0.0;
|
||||
muti=0.1;
|
||||
for(j++;str[j]!=',';j++){
|
||||
tail+=(str[j]-'0')*muti;
|
||||
muti/=10.0;
|
||||
}
|
||||
tmp+=tail;
|
||||
ord[i].y=tmp*sig;
|
||||
tmp=0.0;
|
||||
sig=1.0;
|
||||
for(j++;str[j]!='.';j++){
|
||||
if(str[j]=='-'){
|
||||
sig=-1.0;
|
||||
continue;
|
||||
}
|
||||
tmp=tmp*10+str[j]-'0';
|
||||
}
|
||||
tail=0.0;
|
||||
muti=0.1;
|
||||
for(j++;str[j]!=')';j++){
|
||||
tail+=(str[j]-'0')*muti;
|
||||
muti/=10.0;
|
||||
}
|
||||
tmp+=tail;
|
||||
ord[i].z=tmp*sig;
|
||||
}
|
||||
else if(str[2]=='T'){
|
||||
ord[i].c='T';
|
||||
for(j=2;str[j]!='(';j++);
|
||||
tmp=0.0;
|
||||
sig=1.0;
|
||||
for(j++;str[j]!='.';j++){
|
||||
if(str[j]=='-'){
|
||||
sig=-1.0;
|
||||
continue;
|
||||
}
|
||||
tmp=tmp*10+str[j]-'0';
|
||||
}
|
||||
tail=0.0;
|
||||
muti=0.1;
|
||||
for(j++;str[j]!=',';j++){
|
||||
tail+=(str[j]-'0')*muti;
|
||||
muti/=10.0;
|
||||
}
|
||||
tmp+=tail;
|
||||
ord[i].x=tmp*sig;
|
||||
tmp=0.0;
|
||||
sig=1.0;
|
||||
for(j++;str[j]!='.';j++){
|
||||
if(str[j]=='-'){
|
||||
sig=-1.0;
|
||||
continue;
|
||||
}
|
||||
tmp=tmp*10+str[j]-'0';
|
||||
}
|
||||
tail=0.0;
|
||||
muti=0.1;
|
||||
for(j++;str[j]!=',';j++){
|
||||
tail+=(str[j]-'0')*muti;
|
||||
muti/=10.0;
|
||||
}
|
||||
tmp+=tail;
|
||||
ord[i].y=tmp*sig;
|
||||
tmp=0.0;
|
||||
sig=1.0;
|
||||
for(j++;str[j]!='.';j++){
|
||||
if(str[j]=='-'){
|
||||
sig=-1.0;
|
||||
continue;
|
||||
}
|
||||
tmp=tmp*10+str[j]-'0';
|
||||
}
|
||||
tail=0.0;
|
||||
muti=0.1;
|
||||
for(j++;str[j]!=')';j++){
|
||||
tail+=(str[j]-'0')*muti;
|
||||
muti/=10.0;
|
||||
}
|
||||
tmp+=tail;
|
||||
ord[i].z=tmp*sig;
|
||||
}
|
||||
else if(str[2]=='V'){
|
||||
ord[i].c='V';
|
||||
for(j=2;str[j]!='(';j++);
|
||||
tmp=0.0;
|
||||
sig=1.0;
|
||||
for(j++;str[j]!='.';j++){
|
||||
if(str[j]=='-'){
|
||||
sig=-1.0;
|
||||
continue;
|
||||
}
|
||||
tmp=tmp*10+str[j]-'0';
|
||||
}
|
||||
tail=0.0;
|
||||
muti=0.1;
|
||||
for(j++;str[j]!=',';j++){
|
||||
tail+=(str[j]-'0')*muti;
|
||||
muti/=10.0;
|
||||
}
|
||||
tmp+=tail;
|
||||
ord[i].x=tmp*sig;
|
||||
tmp=0.0;
|
||||
sig=1.0;
|
||||
for(j++;str[j]!='.';j++){
|
||||
if(str[j]=='-'){
|
||||
sig=-1.0;
|
||||
continue;
|
||||
}
|
||||
tmp=tmp*10+str[j]-'0';
|
||||
}
|
||||
tail=0.0;
|
||||
muti=0.1;
|
||||
for(j++;str[j]!=',';j++){
|
||||
tail+=(str[j]-'0')*muti;
|
||||
muti/=10.0;
|
||||
}
|
||||
tmp+=tail;
|
||||
ord[i].y=tmp*sig;
|
||||
tmp=0.0;
|
||||
sig=1.0;
|
||||
for(j++;str[j]!='.';j++){
|
||||
if(str[j]=='-'){
|
||||
sig=-1.0;
|
||||
continue;
|
||||
}
|
||||
tmp=tmp*10+str[j]-'0';
|
||||
}
|
||||
tail=0.0;
|
||||
muti=0.1;
|
||||
for(j++;str[j]!=')';j++){
|
||||
tail+=(str[j]-'0')*muti;
|
||||
muti/=10.0;
|
||||
}
|
||||
tmp+=tail;
|
||||
ord[i].z=tmp*sig;
|
||||
}
|
||||
else if(str[2]=='E') break;
|
||||
else{
|
||||
ord[i].c='R';
|
||||
for(j=2;str[j]!='(';j++);
|
||||
tmp=0.0;
|
||||
sig=1.0;
|
||||
for(j++;str[j]!='.';j++){
|
||||
if(str[j]=='-'){
|
||||
sig=-1.0;
|
||||
continue;
|
||||
}
|
||||
tmp=tmp*10+str[j]-'0';
|
||||
}
|
||||
tail=0.0;
|
||||
muti=0.1;
|
||||
for(j++;str[j]!=',';j++){
|
||||
tail+=(str[j]-'0')*muti;
|
||||
muti/=10.0;
|
||||
}
|
||||
tmp+=tail;
|
||||
ord[i].ang=tmp*sig;
|
||||
tmp=0.0;
|
||||
sig=1.0;
|
||||
for(j++;str[j]!='.';j++){
|
||||
if(str[j]=='-'){
|
||||
sig=-1.0;
|
||||
continue;
|
||||
}
|
||||
tmp=tmp*10+str[j]-'0';
|
||||
}
|
||||
tail=0.0;
|
||||
muti=0.1;
|
||||
for(j++;str[j]!=',';j++){
|
||||
tail+=(str[j]-'0')*muti;
|
||||
muti/=10.0;
|
||||
}
|
||||
tmp+=tail;
|
||||
ord[i].x=tmp*sig;
|
||||
tmp=0.0;
|
||||
sig=1.0;
|
||||
for(j++;str[j]!='.';j++){
|
||||
if(str[j]=='-'){
|
||||
sig=-1.0;
|
||||
continue;
|
||||
}
|
||||
tmp=tmp*10+str[j]-'0';
|
||||
}
|
||||
tail=0.0;
|
||||
muti=0.1;
|
||||
for(j++;str[j]!=',';j++){
|
||||
tail+=(str[j]-'0')*muti;
|
||||
muti/=10.0;
|
||||
}
|
||||
tmp+=tail;
|
||||
ord[i].y=tmp*sig;
|
||||
tmp=0.0;
|
||||
sig=1.0;
|
||||
for(j++;str[j]!='.';j++){
|
||||
if(str[j]=='-'){
|
||||
sig=-1.0;
|
||||
continue;
|
||||
}
|
||||
tmp=tmp*10+str[j]-'0';
|
||||
}
|
||||
tail=0.0;
|
||||
muti=0.1;
|
||||
for(j++;str[j]!=')';j++){
|
||||
tail+=(str[j]-'0')*muti;
|
||||
muti/=10.0;
|
||||
}
|
||||
tmp+=tail;
|
||||
ord[i].z=tmp*sig;
|
||||
}
|
||||
}
|
||||
point3 pnt;
|
||||
i--;
|
||||
pnt.x=ord[i].x;
|
||||
pnt.y=ord[i].y;
|
||||
pnt.z=ord[i].z;
|
||||
for(i--;i>=0;i--){
|
||||
if(ord[i].c=='T'){
|
||||
pnt.x+=ord[i].x;
|
||||
pnt.y+=ord[i].y;
|
||||
pnt.z+=ord[i].z;
|
||||
}
|
||||
else if(ord[i].c=='S'){
|
||||
pnt.x*=ord[i].x;
|
||||
pnt.y*=ord[i].y;
|
||||
pnt.z*=ord[i].z;
|
||||
}
|
||||
else if(ord[i].c=='R'){
|
||||
pnt=rotate3(pnt,ord[i].x,ord[i].y,ord[i].z,ord[i].ang);
|
||||
}
|
||||
}
|
||||
printf("%.1f %.1f %.1f\n",pnt.x,pnt.y,pnt.z);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
#include <string.h>
|
||||
using namespace std;
|
||||
const int MAXN = 501;
|
||||
struct data { double x,y;
|
||||
}p[MAXN];
|
||||
inline double sqr(double x)
|
||||
{
|
||||
return x * x;
|
||||
}
|
||||
double dis(int a,int b)
|
||||
{
|
||||
return sqrt(sqr(p[a].x - p[b].x) + sqr(p[a].y - p[b].y));
|
||||
}
|
||||
bool cmp(data a,data b)
|
||||
{
|
||||
return a.x == b.x ? a.y < b.y : a.x < b.x;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
double dp[MAXN][MAXN];
|
||||
while (scanf("%d",&n) != EOF && n)
|
||||
{
|
||||
for (int i = 1;i <= n;i++)
|
||||
scanf("%lf%lf",&p[i].x,&p[i].y);
|
||||
sort(p,p+n+1,cmp);
|
||||
memset(dp,0,sizeof(dp));
|
||||
for (int i = 0;i <= n;i++)
|
||||
for (int j = 0;j <= n;j++)
|
||||
dp[i][j] = 1e20;
|
||||
dp[0][0] = 0;
|
||||
for (int i = 0;i < n;i++)
|
||||
for (int j = 0;j <= i;j++)
|
||||
{
|
||||
dp[i+1][j] = min(dp[i+1][j],dp[i][j] + dis(i,i+1));
|
||||
dp[i+1][i] = min(dp[i+1][i],dp[i][j] + dis(j,i+1));
|
||||
}
|
||||
double ans = 1e20;
|
||||
for (int i = 0;i <= n;i++)
|
||||
{
|
||||
if (dp[n][i] + dis(i,n) < ans)
|
||||
ans = dp[n][i] + dis(i,n);
|
||||
}
|
||||
printf("%.2lf\n",ans);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<string>
|
||||
#include<iostream>
|
||||
#include<queue>
|
||||
#include<vector>
|
||||
#define MAX 22222
|
||||
using namespace std;
|
||||
vector<int>vt[MAX];
|
||||
int vis[MAX];
|
||||
int a;
|
||||
int d[MAX];
|
||||
int BFS(int n){
|
||||
queue<int> q;
|
||||
q.push(n);
|
||||
int cnt=0;
|
||||
while(!q.empty()){
|
||||
int now=q.front();
|
||||
for(int i=0;i<vt[now].size();i++){
|
||||
int to=vt[now][i];
|
||||
if(vis[to]==0){
|
||||
vis[to]=1;
|
||||
cnt++;
|
||||
q.push(to);
|
||||
}
|
||||
}
|
||||
q.pop();
|
||||
}
|
||||
return cnt;
|
||||
}
|
||||
int main(){
|
||||
int T;
|
||||
scanf("%d",&T);
|
||||
while(T--){
|
||||
memset(vis,0,sizeof(vis));
|
||||
int n,m,c;
|
||||
scanf("%d%d%d",&n,&m,&c);
|
||||
scanf("%d",&a);
|
||||
for(int i=0;i<n;i++)vt[i].clear();
|
||||
for(int i=0;i<c;i++){
|
||||
scanf("%d",&d[i]);
|
||||
}
|
||||
for(int i=0;i<m;i++){
|
||||
int u,v;
|
||||
scanf("%d%d",&u,&v);
|
||||
vt[u].push_back(v);
|
||||
vt[v].push_back(u);
|
||||
}
|
||||
vis[a]=1;
|
||||
for(int i=0;i<c;i++){
|
||||
vis[d[i]]=1;
|
||||
BFS(d[i]);
|
||||
}
|
||||
printf("%d\n",BFS(a));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
#include<iostream>
|
||||
#include<stdio.h>
|
||||
#include<algorithm>
|
||||
#include<string.h>
|
||||
#include<map>
|
||||
using namespace std;
|
||||
int arry[5];
|
||||
bool visit[5];
|
||||
int result[1000000];
|
||||
int ind;
|
||||
map<int ,bool> mp;
|
||||
int Work(int x,int y,int op)
|
||||
{
|
||||
if(op==0)
|
||||
return x+y;
|
||||
else if(op==1)
|
||||
return x-y;
|
||||
else if(op==2)
|
||||
return x*y;
|
||||
else if(op==3 && y && x%y==0)
|
||||
return x/y;
|
||||
else if(op==4 && x && y%x==0)
|
||||
return y/x;
|
||||
else
|
||||
return y-x;
|
||||
}
|
||||
void DFS(int cnt)
|
||||
{
|
||||
int i,j;
|
||||
int x,y,k;
|
||||
if(cnt==4)
|
||||
{
|
||||
for(i=0;i<4;i++)
|
||||
if(!visit[i] && !mp[arry[i]])
|
||||
{
|
||||
result[ind++]=arry[i];
|
||||
mp[arry[i]]=true;
|
||||
}
|
||||
return ;
|
||||
}
|
||||
for(i=0;i<4;i++)
|
||||
{
|
||||
if(visit[i])
|
||||
continue;
|
||||
for(j=i+1;j<4;j++)
|
||||
{
|
||||
if(visit[j])
|
||||
continue;
|
||||
visit[j]=true;
|
||||
x=arry[i];
|
||||
y=arry[j];
|
||||
for(k=0;k<6;k++)
|
||||
{
|
||||
arry[i]=Work(x,y,k);
|
||||
DFS(cnt+1);
|
||||
}
|
||||
arry[i]=x;
|
||||
arry[j]=y;
|
||||
visit[j]=false;
|
||||
}
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i,j,k;
|
||||
int t=1;
|
||||
while(scanf("%d%d%d%d",&arry[0],&arry[1],&arry[2],&arry[3])!=EOF)
|
||||
{
|
||||
if(!arry[0] && !arry[1] && !arry[2] && !arry[3])
|
||||
break;
|
||||
mp.clear();
|
||||
ind=0;
|
||||
memset(visit,false,sizeof(visit));
|
||||
DFS(1);
|
||||
sort(result,result+ind);
|
||||
int cnt;
|
||||
int Max=1;
|
||||
int beg,end;
|
||||
beg=end=result[0];
|
||||
i=0;
|
||||
while(i<ind)
|
||||
{
|
||||
j=i;
|
||||
cnt=1;
|
||||
while(i<ind)
|
||||
{
|
||||
if(result[i]+1==result[i+1])
|
||||
cnt++;
|
||||
else
|
||||
{
|
||||
i++;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
if(cnt>=Max)
|
||||
{
|
||||
Max=cnt;
|
||||
beg=result[j];
|
||||
if(j<i)
|
||||
end=result[i-1];
|
||||
else
|
||||
end=result[i];
|
||||
}
|
||||
}
|
||||
printf("Case %d: %d to %d\n",t++,beg,end);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
#include<cstdio>
|
||||
#include<iostream>
|
||||
#include<cstring>
|
||||
using namespace std;
|
||||
int n,m[5],l[5],len;
|
||||
double p[5],dp[6][3125][32];
|
||||
bool f[6][3125][32];
|
||||
char ans[100];
|
||||
double solve(int g,int r,int mask)
|
||||
{
|
||||
if(mask==0) return 1.0;
|
||||
if(f[g][r][mask]) return dp[g][r][mask];
|
||||
int a[5],c=r;
|
||||
double b[2][5],t1,t2;
|
||||
for(int i=n-1;i>=0;--i)
|
||||
{
|
||||
a[i]=c%5;
|
||||
c/=5;
|
||||
}
|
||||
for(int i=0;i<n;++i)
|
||||
{
|
||||
if(l[i]) t1=p[i]/l[i]*(l[i]-a[i]);
|
||||
else t1=0.0;
|
||||
t2=(1-p[i])/(m[i]-l[i])*(m[i]-l[i]-g+a[i]);
|
||||
if(t1+t2<1e-8) b[0][i]=b[1][i]=0.0;
|
||||
else
|
||||
{
|
||||
b[0][i]=(1-p[i])/(m[i]-l[i])/(t1+t2);
|
||||
b[1][i]=p[i]/l[i]/(t1+t2);
|
||||
}
|
||||
}
|
||||
dp[g][r][mask]=0.0;
|
||||
for(int i=mask;;i=((i-1)&mask))
|
||||
{
|
||||
double tmp=0.0;
|
||||
int choose[5],chs=0;
|
||||
for(int j=0;j<n;++j)
|
||||
{
|
||||
if(i&(1<<j)) choose[j]=1;
|
||||
else choose[j]=0;
|
||||
if((mask&(1<<j))&&choose[j]+a[j]>l[j]) goto fuck;
|
||||
if((mask&(1<<j))&&choose[j]==0&&m[j]-l[j]-g+a[j]<=0) goto fuck;
|
||||
chs=chs*5+choose[j]+a[j];
|
||||
}
|
||||
for(int j=mask;j;j=((j-1)&mask))
|
||||
{
|
||||
double pp=1.0;
|
||||
for(int k=0;k<n;++k)
|
||||
if(j&(1<<k)) pp*=b[choose[k]][k];
|
||||
else if(mask&(1<<k)) pp*=1-b[choose[k]][k];
|
||||
tmp+=pp*solve(g+1,chs,mask-j);
|
||||
}
|
||||
dp[g][r][mask]=max(dp[g][r][mask],tmp);
|
||||
fuck:if(i==0) break;
|
||||
}
|
||||
f[g][r][mask]=1;
|
||||
return dp[g][r][mask];
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while(scanf("%d",&n)&&n)
|
||||
{
|
||||
for(int i=0;i<n;++i) scanf("%d%d%lf",m+i,l+i,p+i);
|
||||
memset(f,0,sizeof(f));
|
||||
memset(ans,0,sizeof(ans));
|
||||
sprintf(ans,"%.3lf",solve(0,0,(1<<n)-1));
|
||||
len=strlen(ans);
|
||||
while(ans[len-1]=='0')
|
||||
ans[--len]='\0';
|
||||
if(ans[len-1]=='.') ans[len-1]='\0';
|
||||
printf("%s\n",ans);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
#include<iostream>
|
||||
#include<cstdio>
|
||||
#include<cstring>
|
||||
using namespace std;
|
||||
char s[10001],lib[100][10001],*arr[20000],ans[20000],*t;
|
||||
int cas,n,len[20000],a,b;
|
||||
int main()
|
||||
{
|
||||
while(scanf("%s",s)&&strcmp(s,"0"))
|
||||
{
|
||||
scanf("%d",&n);
|
||||
for(int i=0;i<n;++i)
|
||||
{
|
||||
scanf(" %s",lib[i]);
|
||||
arr[i]=lib[i];
|
||||
len[i]=strlen(lib[i]);
|
||||
}
|
||||
t=ans;
|
||||
a=strlen(s);
|
||||
for(int i=0;i<a;)
|
||||
{
|
||||
b=s[i++]-'0';
|
||||
for(int j=10;n>j;j*=10) b=b*10+s[i++]-'0';
|
||||
arr[n]=t;
|
||||
len[n++]=len[b]+1;
|
||||
for(int j=0;j<len[b];++j)
|
||||
{
|
||||
*t=arr[b][j];
|
||||
t++;
|
||||
}
|
||||
}
|
||||
*t='\0';
|
||||
printf("Case %d: %s\n",++cas,ans);
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
Pile <em>t</em>
|
|
@ -0,0 +1 @@
|
|||
Case <em>n</em>: Island splits when ocean rises <em>f</em> feet.
|
|
@ -0,0 +1,68 @@
|
|||
#include<iostream>
|
||||
#include<cstring>
|
||||
#include<cstdio>
|
||||
#include<set>
|
||||
#define MAXN 150
|
||||
using namespace std;
|
||||
int n,m,ids,ide,ans;
|
||||
int br[MAXN][MAXN],dist[MAXN];
|
||||
struct Node{
|
||||
int g,y,r;
|
||||
}node[MAXN];
|
||||
struct L{
|
||||
int x,t;
|
||||
bool operator <(L othr)const{
|
||||
if(x==othr.x)return t<othr.t;
|
||||
else return x<othr.x;
|
||||
}
|
||||
}now;
|
||||
void SPFA(){
|
||||
set<L>Q;
|
||||
Q.clear();
|
||||
memset(dist,-1,sizeof(dist));
|
||||
Q.insert((L){ids,5});
|
||||
dist[ids]=5;
|
||||
set<L>::iterator it;
|
||||
while(!Q.empty()){
|
||||
it=Q.begin();
|
||||
now=*it;
|
||||
Q.erase(it);
|
||||
for(int i=0;i<n;i++){
|
||||
if(br[now.x][i]==-1)continue;
|
||||
int add=0,nowtime,tot,res;
|
||||
nowtime=br[now.x][i]+dist[now.x];
|
||||
tot=node[i].g+node[i].r+node[i].y;
|
||||
res=nowtime%tot;
|
||||
if(res>=node[i].g+node[i].y)add=5+tot-res;
|
||||
if(i==ide&&(ans==-1||ans>nowtime))ans=nowtime;
|
||||
if(add+nowtime<dist[i]||dist[i]==-1){
|
||||
dist[i]=nowtime+add;
|
||||
Q.insert((L){i,dist[i]});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int main(){
|
||||
while(scanf("%d%d%d%d",&n,&m,&ids,&ide)!=EOF&&(n||m||ids||ide)){
|
||||
if(ids==ide){
|
||||
printf("0:00\n");
|
||||
continue;
|
||||
}
|
||||
for(int i=0;i<n;i++){
|
||||
scanf("%d%d%d",&node[i].g,&node[i].y,&node[i].r);
|
||||
}
|
||||
memset(br,-1,sizeof(br));
|
||||
for(int i=0;i<m;i++){
|
||||
int xx,yy,zz;
|
||||
scanf("%d%d%d",&xx,&yy,&zz);
|
||||
if(br[xx][yy]==-1||br[xx][yy]>zz)
|
||||
br[xx][yy]=br[yy][xx]=zz;
|
||||
}
|
||||
ans=-1;
|
||||
SPFA();
|
||||
int second=ans;
|
||||
int minute=second/60;
|
||||
second=second%60;
|
||||
printf("%d:%02d\n",minute,second);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
#include<iostream>
|
||||
using namespace std;
|
||||
int cnt=1;
|
||||
struct jsj
|
||||
{
|
||||
int x1,y1;
|
||||
int x2,y2;
|
||||
}a[101];
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
while(scanf("%d",&n)!=EOF)
|
||||
{
|
||||
if(n==0) break;
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
int c,d;
|
||||
scanf("%d%d%d%d",&a[i].x1,&a[i].y1,&c,&d);
|
||||
a[i].x2=a[i].x1+d-1;
|
||||
a[i].y2=a[i].y1+c-1;
|
||||
}
|
||||
int m;
|
||||
scanf("%d",&m);
|
||||
printf("Desktop %d:\n",cnt++);
|
||||
for(int i=0;i<m;i++)
|
||||
{
|
||||
bool f=false;
|
||||
int q1,q2;
|
||||
scanf("%d%d",&q1,&q2);
|
||||
for(int j=n-1;j>=0;j--)
|
||||
{
|
||||
if(q1>=a[j].x1 && q1<=a[j].x2 && q2>=a[j].y1 && q2<=a[j].y2)
|
||||
{
|
||||
f=true;
|
||||
printf("window %d\n",j+1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!f) printf("background\n");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<algorithm>
|
||||
#include<map>
|
||||
using namespace std;
|
||||
typedef __int64 ll;
|
||||
#define N 300010
|
||||
#define M 100010
|
||||
struct
|
||||
{
|
||||
int l,r;
|
||||
ll num;
|
||||
}root[N*4];
|
||||
struct Q
|
||||
{
|
||||
int s,t,ind;
|
||||
}q[M];
|
||||
bool cmp(Q i,Q j)
|
||||
{
|
||||
return i.t<j.t;
|
||||
}
|
||||
inline void build(int t,int x,int y)
|
||||
{
|
||||
root[t].l=x;
|
||||
root[t].r=y;
|
||||
root[t].num=0;
|
||||
if(x==y) return;
|
||||
int m=(x+y)>>1;
|
||||
build(t*2,x,m);
|
||||
build(t*2+1,m+1,y);
|
||||
}
|
||||
inline void Modefiy(int t,int x,ll val)
|
||||
{
|
||||
int l=root[t].l;
|
||||
int r=root[t].r;
|
||||
if(l==r)
|
||||
{
|
||||
root[t].num+=val;return;
|
||||
}
|
||||
int m=(l+r)>>1;
|
||||
if(x<=m) Modefiy(t*2,x,val);
|
||||
else Modefiy(t*2+1,x,val);
|
||||
root[t].num=root[t*2].num+root[t*2+1].num;
|
||||
}
|
||||
inline ll query(int t,int x,int y)
|
||||
{
|
||||
int l=root[t].l;
|
||||
int r=root[t].r;
|
||||
if(l==x&&r==y)
|
||||
{
|
||||
return root[t].num;
|
||||
}
|
||||
int m=(l+r)>>1;
|
||||
ll ans=0;
|
||||
if(x<=m) ans+=query(t*2,x,min(m,y));
|
||||
if(y>m) ans+=query(t*2+1,max(m+1,x),y);
|
||||
return ans;
|
||||
}
|
||||
int t,n,qn;
|
||||
ll a[N];
|
||||
map<ll,int>m;
|
||||
ll ans[M];
|
||||
int main()
|
||||
{
|
||||
scanf("%d",&t);
|
||||
while(t--)
|
||||
{
|
||||
m.clear();
|
||||
scanf("%d",&n);
|
||||
for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
|
||||
build(1,1,n);
|
||||
scanf("%d",&qn);
|
||||
for(int i=0;i<qn;i++)
|
||||
{
|
||||
scanf("%d%d",&q[i].s,&q[i].t);
|
||||
q[i].ind=i;
|
||||
}
|
||||
sort(q,q+qn,cmp);
|
||||
int k=1;
|
||||
for(int i=0;i<qn;i++)
|
||||
{
|
||||
for(;k<=q[i].t;k++)
|
||||
{
|
||||
if(m[a[k]]!=0) Modefiy(1,m[a[k]],-a[k]);
|
||||
m[a[k]]=k;
|
||||
Modefiy(1,k,a[k]);
|
||||
}
|
||||
ans[q[i].ind]=query(1,q[i].s,q[i].t);
|
||||
}
|
||||
for(int i=0;i<qn;i++) printf("%I64d\n",ans[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <map>
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
typedef __int64 ll;
|
||||
struct node{
|
||||
int s,t,nxt ;
|
||||
}e[100005] ;
|
||||
int k,m,n,head[505],cnt,match[505],vis[505] ;
|
||||
int find(int s)
|
||||
{
|
||||
for(int i=head[s] ;i!=-1 ;i=e[i].nxt)
|
||||
{
|
||||
int tt=e[i].t ;
|
||||
if(!vis[tt])
|
||||
{
|
||||
vis[tt]=1 ;
|
||||
if(match[tt]==-1 || find(match[tt]))
|
||||
{
|
||||
match[tt]=s ;
|
||||
return 1 ;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0 ;
|
||||
}
|
||||
int max_match()
|
||||
{
|
||||
int ans=0 ;
|
||||
memset(match,-1,sizeof(match)) ;
|
||||
for(int i=1 ;i<=n ;i++)
|
||||
{
|
||||
memset(vis,0,sizeof(vis)) ;
|
||||
ans+=find(i);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
void add(int s,int t) {e[cnt].s=s ;e[cnt].t=t ;e[cnt].nxt=head[s] ;head[s]=cnt++ ;}
|
||||
ll a[1005],b[1005];
|
||||
void read_graph()
|
||||
{
|
||||
memset(head,-1,sizeof(head)) ;
|
||||
cnt=0 ;
|
||||
for(int i=1;i<=n;i++){
|
||||
for(int j=1;j<=n;j++){
|
||||
if(i==j)continue;
|
||||
if(b[i]%b[j]==0)
|
||||
add(i,j);
|
||||
}
|
||||
}
|
||||
}
|
||||
int main(){
|
||||
int T;
|
||||
scanf("%d",&T);
|
||||
while(T--){
|
||||
scanf("%d",&n);
|
||||
for(int i=1;i<=n;i++){
|
||||
scanf("%I64d",&a[i]);
|
||||
}
|
||||
int st=0;
|
||||
sort(a+1,a+1+n);
|
||||
b[++st]=a[1];
|
||||
for(int i=2;i<=n;i++){
|
||||
if(a[i]!=a[i-1])b[++st]=a[i];
|
||||
}
|
||||
n=st;
|
||||
read_graph();
|
||||
printf("%d\n",n-max_match());
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
#include <cstdio>
|
||||
#include <cstring>
|
||||
int const MAX = 200000 + 5;
|
||||
int const MOD = 10007;
|
||||
int next[MAX];
|
||||
char s[MAX];
|
||||
void get_next()
|
||||
{
|
||||
int i = 0, j = -1;
|
||||
next[0] = -1;
|
||||
while(s[i] != '\0')
|
||||
{
|
||||
if(j == - 1 || s[i] == s[j])
|
||||
{
|
||||
i++;
|
||||
j++;
|
||||
next[i] = j;
|
||||
}
|
||||
else
|
||||
j = next[j];
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int T, len, ans;
|
||||
scanf("%d", &T);
|
||||
while(T--)
|
||||
{
|
||||
memset(next, 0, sizeof(next));
|
||||
scanf("%d %s", &len, s);
|
||||
get_next();
|
||||
ans = len;
|
||||
for(int i = 1; i <= len; i++)
|
||||
{
|
||||
int tmp = next[i];
|
||||
while(tmp)
|
||||
{
|
||||
ans = (ans + 1) % MOD;
|
||||
tmp = next[tmp];
|
||||
}
|
||||
}
|
||||
printf("%d\n", ans);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
printf("72\n");
|
||||
}
|
|
@ -0,0 +1,172 @@
|
|||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
using namespace std;
|
||||
#define Del(a,b) memset(a,b,sizeof(a))
|
||||
const int N = 20500;
|
||||
const int inf = 0x3f3f3f3f;
|
||||
int n,m;
|
||||
struct Node
|
||||
{
|
||||
int from,to,cap,flow;
|
||||
};
|
||||
vector<int> v[N];
|
||||
vector<Node> e;
|
||||
int vis[N];
|
||||
int cur[N];
|
||||
void add_Node(int from,int to,int cap)
|
||||
{
|
||||
e.push_back((Node){from,to,cap,0});
|
||||
e.push_back((Node){to,from,0,0});
|
||||
int tmp=e.size();
|
||||
v[from].push_back(tmp-2);
|
||||
v[to].push_back(tmp-1);
|
||||
}
|
||||
bool bfs(int s,int t)
|
||||
{
|
||||
Del(vis,-1);
|
||||
queue<int> q;
|
||||
q.push(s);
|
||||
vis[s] = 0;
|
||||
while(!q.empty())
|
||||
{
|
||||
int x=q.front();
|
||||
q.pop();
|
||||
for(int i=0;i<v[x].size();i++)
|
||||
{
|
||||
Node tmp = e[v[x][i]];
|
||||
if(vis[tmp.to]<0 && tmp.cap>tmp.flow)
|
||||
{
|
||||
vis[tmp.to]=vis[x]+1;
|
||||
q.push(tmp.to);
|
||||
}
|
||||
}
|
||||
}
|
||||
if(vis[t]>0)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
int dfs(int o,int f,int t)
|
||||
{
|
||||
if(o==t || f==0)
|
||||
return f;
|
||||
int a = 0,ans=0;
|
||||
for(int &i=cur[o];i<v[o].size();i++)
|
||||
{
|
||||
Node &tmp = e[v[o][i]];
|
||||
if(vis[tmp.to]==(vis[o]+1) && (a = dfs(tmp.to,min(f,tmp.cap-tmp.flow),t))>0)
|
||||
{
|
||||
tmp.flow+=a;
|
||||
e[v[o][i]^1].flow-=a;
|
||||
ans+=a;
|
||||
f-=a;
|
||||
if(f==0)
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
int dinci(int s,int t)
|
||||
{
|
||||
int ans=0;
|
||||
while(bfs(s,t))
|
||||
{
|
||||
Del(cur,0);
|
||||
int tm=dfs(s,inf,t);
|
||||
ans+=tm;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
int mp[250][250];
|
||||
struct Node1
|
||||
{
|
||||
int x,y,z;
|
||||
};
|
||||
Node1 raw[N],col[N];
|
||||
int solve(int raw_cnt,int id,int s)
|
||||
{
|
||||
int cnt = 0,pos = id+raw_cnt;
|
||||
return e[v[pos][1]].flow+1;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while(~scanf("%d%d",&n,&m))
|
||||
{
|
||||
memset(mp,-1,sizeof(mp));
|
||||
memset(col,0,sizeof(col));
|
||||
memset(raw,0,sizeof(raw));
|
||||
int cnt=0,raw_cnt=0,col_cnt=0;
|
||||
for(int i=1;i<=n;i++)
|
||||
{
|
||||
for(int j=1;j<=m;j++)
|
||||
{
|
||||
string str;
|
||||
cin>>str;
|
||||
if(str[0] == '.'){
|
||||
mp[i][j] = ++cnt;
|
||||
}else{
|
||||
mp[i][j] = -1;
|
||||
if(str[0] != 'X'){
|
||||
int tmp = (str[0]-'0')*100 + (str[1]-'0')*10 + str[2]-'0';
|
||||
col[++col_cnt].x = i;
|
||||
col[col_cnt].y = j;
|
||||
col[col_cnt].z = tmp;
|
||||
}
|
||||
if(str[4] != 'X'){
|
||||
int tmp = (str[4]-'0')*100 + (str[5]-'0')*10 + str[6]-'0';
|
||||
raw[++raw_cnt].x = i;
|
||||
raw[raw_cnt].y = j;
|
||||
raw[raw_cnt].z = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int start=0,t=col_cnt+cnt+raw_cnt+2;
|
||||
for(int i = 1;i <= raw_cnt;i++){
|
||||
int x = raw[i].x;
|
||||
int y = raw[i].y;
|
||||
int cnt_len = 0;
|
||||
for(y = y+1;y <= m;y++){
|
||||
if(mp[x][y] != -1){
|
||||
cnt_len++;
|
||||
add_Node(i,raw_cnt+mp[x][y],8);
|
||||
}
|
||||
else break;
|
||||
}
|
||||
add_Node(start,i,raw[i].z-cnt_len);
|
||||
}
|
||||
for(int i = 1;i <= col_cnt;i++){
|
||||
int x = col[i].x;
|
||||
int y = col[i].y;
|
||||
int cnt_len = 0;
|
||||
for(x = x+1;x <= n;x++){
|
||||
if(mp[x][y] != -1){
|
||||
cnt_len++;
|
||||
add_Node(raw_cnt+mp[x][y],raw_cnt+cnt+i,8);
|
||||
}
|
||||
else break;
|
||||
}
|
||||
add_Node(raw_cnt+cnt+i,t,col[i].z-cnt_len);
|
||||
}
|
||||
int ans=dinci(start,t);
|
||||
for(int i=1;i<=n;i++)
|
||||
{
|
||||
for(int j=1;j<=m;j++)
|
||||
{
|
||||
if(mp[i][j]==-1)
|
||||
printf("_");
|
||||
else
|
||||
printf("%d",solve(raw_cnt,mp[i][j],cnt));
|
||||
printf("%c",j==m?'\n':' ');
|
||||
}
|
||||
}
|
||||
for(int i=0;i<=t;i++)
|
||||
v[i].clear();
|
||||
e.clear();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,99 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<iostream>
|
||||
#include<algorithm>
|
||||
#define INF 100000000
|
||||
#define max 101
|
||||
using namespace std;
|
||||
int dp[10001];
|
||||
int N,g[max][max];
|
||||
int dis[max];
|
||||
int pp[max];
|
||||
int v[max];
|
||||
void dijk()
|
||||
{
|
||||
int i,j,mindis,mark;
|
||||
for(i=0;i<=N;i++)
|
||||
{
|
||||
dis[i] = INF;
|
||||
v[i] = 0;
|
||||
}
|
||||
dis[0] = 0;
|
||||
v[0] = 1;
|
||||
for(i=0;i<=N;i++)
|
||||
{
|
||||
mark = 0;
|
||||
mindis = INF;
|
||||
for(j=0;j<=N;j++)
|
||||
{
|
||||
if(!v[j] && dis[j] < mindis)
|
||||
{
|
||||
mindis = dis[j];
|
||||
mark = j;
|
||||
}
|
||||
}
|
||||
v[mark] = 1;
|
||||
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 M,a,b,c,i,j,t,sum,MAX,MIN,sumdis;
|
||||
scanf("%d",&t);
|
||||
while(t--)
|
||||
{
|
||||
scanf("%d%d",&N,&M);
|
||||
for(i=0;i<=N;i++)
|
||||
for(j=0;j<=N;j++)
|
||||
{
|
||||
if(i==j)g[i][j] = 0;
|
||||
else
|
||||
g[i][j] = INF;
|
||||
}
|
||||
while(M--)
|
||||
{
|
||||
scanf("%d%d%d",&a,&b,&c);
|
||||
if(c<g[a][b])
|
||||
{
|
||||
g[a][b] = c;
|
||||
g[b][a] = c;
|
||||
}
|
||||
}
|
||||
for(sum=0,i=1;i<=N;i++)
|
||||
{
|
||||
scanf("%d",&c);
|
||||
sum+=c;
|
||||
pp[i] = c;
|
||||
}
|
||||
dijk();
|
||||
memset(dp,0,sizeof(dp));
|
||||
for(sumdis=0,i=0;i<=N;i++)
|
||||
{
|
||||
if(dis[i]!=INF)
|
||||
sumdis += dis[i];
|
||||
}
|
||||
for(i=1;i<=N;i++)
|
||||
{
|
||||
for(j=sumdis;j>=dis[i];j--)
|
||||
{
|
||||
if(dp[j]<dp[j-dis[i]]+pp[i])
|
||||
dp[j] = dp[j-dis[i]]+pp[i];
|
||||
}
|
||||
}
|
||||
for(i=0;i<=sumdis;i++)
|
||||
{
|
||||
if(dp[i] > sum/2)
|
||||
break;
|
||||
}
|
||||
if(i == sumdis+1)
|
||||
printf("impossible\n");
|
||||
else
|
||||
printf("%d\n",i);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,161 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<stdlib.h>
|
||||
#define MAXD 125010
|
||||
double dl[4 * MAXD], dr[4 * MAXD], volume[4 * MAXD];
|
||||
int N, tx[MAXD], X, flag[4 * MAXD];
|
||||
struct Question
|
||||
{
|
||||
char b[5];
|
||||
int n, x[10], y[10];
|
||||
}question[MAXD];
|
||||
int cmpint(const void *_p, const void *_q)
|
||||
{
|
||||
int *p = (int *)_p, *q = (int *)_q;
|
||||
return *p < *q ? -1 : 1;
|
||||
}
|
||||
void build(int cur, int x, int y)
|
||||
{
|
||||
int mid = (x + y) >> 1, ls = cur << 1, rs = (cur << 1) | 1;
|
||||
dl[cur] = dr[cur] = volume[cur] = flag[cur] = 0;
|
||||
if(x == y)
|
||||
return ;
|
||||
build(ls, x, mid);
|
||||
build(rs, mid + 1, y);
|
||||
}
|
||||
void init()
|
||||
{
|
||||
int i, j, k, n;
|
||||
scanf("%d", &N);
|
||||
k = 0;
|
||||
for(i = 0; i < N; i ++)
|
||||
{
|
||||
scanf("%s", question[i].b);
|
||||
if(question[i].b[0] == 'Q')
|
||||
{
|
||||
scanf("%d%d", &question[i].x[0], &question[i].y[0]);
|
||||
tx[k ++] = question[i].x[0], tx[k ++] = question[i].y[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
scanf("%d", &n);
|
||||
question[i].n = n;
|
||||
for(j = 0; j < n; j ++)
|
||||
{
|
||||
scanf("%d%d", &question[i].x[j], &question[i].y[j]);
|
||||
tx[k ++] = question[i].x[j];
|
||||
}
|
||||
question[i].x[n] = question[i].x[0], question[i].y[n] = question[i].y[0];
|
||||
}
|
||||
}
|
||||
qsort(tx, k, sizeof(tx[0]), cmpint);
|
||||
X = -1;
|
||||
for(i = 0; i < k; i ++)
|
||||
if(i == 0 || tx[i] != tx[i - 1])
|
||||
tx[++ X] = tx[i];
|
||||
build(1, 0, X - 1);
|
||||
}
|
||||
int BS(int x)
|
||||
{
|
||||
int mid, min = 0, max = X + 1;
|
||||
for(;;)
|
||||
{
|
||||
mid = (min + max) >> 1;
|
||||
if(mid == min)
|
||||
break;
|
||||
if(tx[mid] <= x)
|
||||
min = mid;
|
||||
else
|
||||
max = mid;
|
||||
}
|
||||
return mid;
|
||||
}
|
||||
double calculate(double x1, double y1, double x2, double y2, double x)
|
||||
{
|
||||
return ((x2 - x) * y1 + (x - x1) * y2) / (x2 - x1);
|
||||
}
|
||||
void pushdown(int cur, int x, int y)
|
||||
{
|
||||
int mid = (x + y) >> 1, ls = cur << 1, rs = (cur << 1) | 1;
|
||||
if(flag[cur])
|
||||
{
|
||||
flag[ls] = flag[rs] = 1;
|
||||
double delta = calculate(tx[x], dl[cur], tx[y + 1], dr[cur], tx[mid + 1]);
|
||||
dl[ls] += dl[cur], dr[ls] += delta, volume[ls] += (dl[cur] + delta) * (tx[mid + 1] - tx[x]) / 2;
|
||||
dl[rs] += delta, dr[rs] += dr[cur], volume[rs] += (delta + dr[cur]) * (tx[y + 1] - tx[mid + 1]) / 2;
|
||||
dl[cur] = dr[cur] = flag[cur] = 0;
|
||||
}
|
||||
}
|
||||
void update(int cur)
|
||||
{
|
||||
volume[cur] = volume[cur << 1] + volume[(cur << 1) | 1];
|
||||
}
|
||||
double query(int cur, int x, int y, int s, int t)
|
||||
{
|
||||
int mid = (x + y) >> 1, ls = cur << 1, rs = (cur << 1) | 1;
|
||||
if(x >= s && y <= t)
|
||||
return volume[cur];
|
||||
pushdown(cur, x, y);
|
||||
if(mid >= t)
|
||||
return query(ls, x, mid, s, t);
|
||||
else if(mid + 1 <= s)
|
||||
return query(rs, mid + 1, y, s, t);
|
||||
else
|
||||
return query(ls, x, mid, s, t) + query(rs, mid + 1, y, s, t);
|
||||
}
|
||||
void refresh(int cur, int x, int y, int s, int t, double ys, double yt)
|
||||
{
|
||||
int mid = (x + y) >> 1, ls = cur << 1, rs = (cur << 1) | 1;
|
||||
if(x >= s && y <= t)
|
||||
{
|
||||
flag[cur] = 1;
|
||||
double dleft, dright;
|
||||
dleft = calculate(tx[s], ys, tx[t + 1], yt, tx[x]);
|
||||
dright = calculate(tx[s], ys, tx[t + 1], yt, tx[y + 1]);
|
||||
dl[cur] += dleft, dr[cur] += dright, volume[cur] += (dleft + dright) * (tx[y + 1] - tx[x]) / 2;
|
||||
return ;
|
||||
}
|
||||
pushdown(cur, x, y);
|
||||
if(mid >= s)
|
||||
refresh(ls, x, mid, s, t, ys, yt);
|
||||
if(mid + 1 <= t)
|
||||
refresh(rs, mid + 1, y, s, t, ys, yt);
|
||||
update(cur);
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
int i, j, k, e, x1, y1, x2, y2;
|
||||
for(i = 0; i < N; i ++)
|
||||
{
|
||||
if(question[i].b[0] == 'Q')
|
||||
{
|
||||
j = BS(question[i].x[0]), k = BS(question[i].y[0]);
|
||||
if(j < k)
|
||||
printf("%.3f\n", query(1, 0, X - 1, j, k - 1));
|
||||
else
|
||||
printf("0.000\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
for(e = 0; e < question[i].n; e ++)
|
||||
{
|
||||
j = BS(question[i].x[e]), k = BS(question[i].x[e + 1]);
|
||||
if(j < k)
|
||||
refresh(1, 0, X - 1, j, k - 1, -question[i].y[e], -question[i].y[e + 1]);
|
||||
else if(j > k)
|
||||
refresh(1, 0, X - 1, k, j - 1, question[i].y[e + 1], question[i].y[e]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int t;
|
||||
scanf("%d", &t);
|
||||
while(t --)
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,132 @@
|
|||
#include <iostream>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <algorithm>
|
||||
#include <queue>
|
||||
using namespace std;
|
||||
const int INF = 0x3f3f3f3f;
|
||||
struct Trie
|
||||
{
|
||||
int next[510][4],fail[510];
|
||||
int end[510];
|
||||
int root,L;
|
||||
int newnode()
|
||||
{
|
||||
for(int i = 0;i < 4;i++)
|
||||
next[L][i] = -1;
|
||||
end[L++] = 0;
|
||||
return L-1;
|
||||
}
|
||||
void init()
|
||||
{
|
||||
L = 0;
|
||||
root = newnode();
|
||||
}
|
||||
int getch(char ch)
|
||||
{
|
||||
if(ch == 'A')return 0;
|
||||
else if(ch == 'C')return 1;
|
||||
else if(ch == 'G')return 2;
|
||||
else return 3;
|
||||
}
|
||||
void insert(char buf[])
|
||||
{
|
||||
int len = strlen(buf);
|
||||
int now = root;
|
||||
for(int i = 0;i < len;i++)
|
||||
{
|
||||
if(next[now][getch(buf[i])] == -1)
|
||||
next[now][getch(buf[i])] = newnode();
|
||||
now = next[now][getch(buf[i])];
|
||||
}
|
||||
end[now] ++;
|
||||
}
|
||||
void build()
|
||||
{
|
||||
queue<int>Q;
|
||||
fail[root] = root;
|
||||
for(int i = 0;i < 4;i++)
|
||||
if(next[root][i] == -1)
|
||||
next[root][i] = root;
|
||||
else
|
||||
{
|
||||
fail[next[root][i]] = root;
|
||||
Q.push(next[root][i]);
|
||||
}
|
||||
while(!Q.empty())
|
||||
{
|
||||
int now = Q.front();
|
||||
Q.pop();
|
||||
end[now] += end[fail[now]];/********/
|
||||
for(int i = 0;i < 4;i++)
|
||||
if(next[now][i] == -1)
|
||||
next[now][i] = next[fail[now]][i];
|
||||
else
|
||||
{
|
||||
fail[next[now][i]] = next[fail[now]][i];
|
||||
Q.push(next[now][i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
int dp[510][11*11*11*11+5];
|
||||
int bit[4];
|
||||
int num[4];
|
||||
int solve(char buf[])
|
||||
{
|
||||
int len = strlen(buf);
|
||||
memset(num,0,sizeof(num));
|
||||
for(int i = 0;i < len;i++)
|
||||
num[getch(buf[i])]++;
|
||||
bit[0] = (num[1]+1)*(num[2]+1)*(num[3]+1);
|
||||
bit[1] = (num[2]+1)*(num[3]+1);
|
||||
bit[2] = (num[3]+1);
|
||||
bit[3] = 1;
|
||||
memset(dp,-1,sizeof(dp));
|
||||
dp[root][0] = 0;
|
||||
for(int A = 0;A <= num[0];A++)
|
||||
for(int B = 0;B <= num[1];B++)
|
||||
for(int C = 0;C <= num[2];C++)
|
||||
for(int D = 0;D <= num[3];D++)
|
||||
{
|
||||
int s = A*bit[0] + B*bit[1] + C*bit[2] + D*bit[3];
|
||||
for(int i = 0;i < L;i++)
|
||||
if(dp[i][s] >= 0)
|
||||
{
|
||||
for(int k = 0;k < 4;k++)
|
||||
{
|
||||
if(k == 0 && A == num[0])continue;
|
||||
if(k == 1 && B == num[1])continue;
|
||||
if(k == 2 && C == num[2])continue;
|
||||
if(k == 3 && D == num[3])continue;
|
||||
dp[next[i][k]][s+bit[k]] = max(dp[next[i][k]][s+bit[k]],dp[i][s]+end[next[i][k]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
int ans = 0;
|
||||
int status = num[0]*bit[0] + num[1]*bit[1] + num[2]*bit[2] + num[3]*bit[3];
|
||||
for(int i = 0;i < L;i++)
|
||||
ans = max(ans,dp[i][status]);
|
||||
return ans;
|
||||
}
|
||||
};
|
||||
char buf[50];
|
||||
Trie ac;
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
int iCase = 0;
|
||||
while( scanf("%d",&n) == 1 && n)
|
||||
{
|
||||
iCase++;
|
||||
ac.init();
|
||||
for(int i = 0;i < n;i++)
|
||||
{
|
||||
scanf("%s",buf);
|
||||
ac.insert(buf);
|
||||
}
|
||||
ac.build();
|
||||
scanf("%s",buf);
|
||||
printf("Case %d: %d\n",iCase,ac.solve(buf));
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
#include<iostream>
|
||||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<algorithm>
|
||||
#include<queue>
|
||||
#include<vector>
|
||||
using namespace std;
|
||||
bool map[111][111];
|
||||
vector<int>vet[111];
|
||||
int from[111];
|
||||
int n,m;
|
||||
void floyd()
|
||||
{
|
||||
for(int k=0;k<n;k++)
|
||||
for(int i=0;i<n;i++)
|
||||
for(int j=0;j<n;j++)
|
||||
map[i][j]=(map[i][j]||(map[i][k]&&map[k][j]));
|
||||
}
|
||||
bool Solve()
|
||||
{
|
||||
queue<int>Q;
|
||||
int cnt=0;
|
||||
for(int i=0;i<n;i++){
|
||||
if(from[i]==0){ Q.push(i);cnt++; }
|
||||
}
|
||||
while(!Q.empty()){
|
||||
int u=Q.front();
|
||||
Q.pop();
|
||||
for(int i=0;i<vet[u].size();i++){
|
||||
int v=vet[u][i];
|
||||
if(from[v]>0){
|
||||
from[v]--;
|
||||
if(from[v]==0){ Q.push(v);cnt++; }
|
||||
}
|
||||
}
|
||||
}
|
||||
if(cnt==n)return true;
|
||||
return false;
|
||||
}
|
||||
int main(){
|
||||
int u,v;
|
||||
while(scanf("%d%d",&n,&m),n){
|
||||
memset(map,false,sizeof(map));
|
||||
memset(from,0,sizeof(from));
|
||||
for(int i=0;i<n;i++)vet[i].clear();
|
||||
while(m--){
|
||||
scanf("%d%d",&u,&v);
|
||||
map[u][v]=true;
|
||||
}
|
||||
for(int i=0;i<n;i++)
|
||||
for(int j=0;j<n;j++)if(i!=j){
|
||||
if(map[i][j]){ from[j]++;vet[i].push_back(j); }
|
||||
}
|
||||
bool flag=Solve();
|
||||
flag?puts("YES"):puts("NO");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
#include<stdio.h>
|
||||
int main()
|
||||
{
|
||||
int T;
|
||||
int l,v,m;
|
||||
scanf("%d",&T);
|
||||
while(T--)
|
||||
{
|
||||
scanf("%d%d%d",&l,&v,&m);
|
||||
if(v>0) printf("YES\n");
|
||||
else printf("NO\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
#include<cstdio>
|
||||
#include<algorithm>
|
||||
#include<iostream>
|
||||
#include<cstring>
|
||||
#include<cmath>
|
||||
using namespace std;
|
||||
struct node {
|
||||
int r;
|
||||
int d;
|
||||
bool ok;
|
||||
} a[111][111];
|
||||
int b[111][111];
|
||||
int n,m;
|
||||
int num(int x) {
|
||||
if(x==0)return 1;
|
||||
int ans=0;
|
||||
while(x)
|
||||
ans++,x/=10;
|
||||
return 3-ans;
|
||||
}
|
||||
int main() {
|
||||
int t;
|
||||
cin>>t;
|
||||
while(t--) {
|
||||
char c;
|
||||
scanf("%d%d",&n,&m);
|
||||
memset(a,0,sizeof a);
|
||||
memset(b,0,sizeof b);
|
||||
for(int i=1; i<=n; i++) {
|
||||
for(int j=1; j<=m; j++) {
|
||||
scanf(" %c",&c);
|
||||
if(c=='_') {
|
||||
a[i][j].ok=1;
|
||||
} else {
|
||||
b[i][j]=c-'0';
|
||||
}
|
||||
}
|
||||
}
|
||||
for(int i=1; i<=n; i++) {
|
||||
for(int j=1; j<=m; j++) {
|
||||
if(a[i][j].ok) {
|
||||
for(int k=j+1; k<=m; k++) {
|
||||
if(a[i][k].ok)break;
|
||||
a[i][j].r+=b[i][k];
|
||||
}
|
||||
for(int k=i+1; k<=n; k++) {
|
||||
if(a[k][j].ok)break;
|
||||
a[i][j].d+=b[k][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for(int i=1; i<=n; i++) {
|
||||
for(int j=1; j<=m; j++) {
|
||||
if(a[i][j].ok) {
|
||||
if(a[i][j].d) {
|
||||
int h=num(a[i][j].d);
|
||||
for(int i=0; i<h; i++)
|
||||
printf("0");
|
||||
printf("%d",a[i][j].d);
|
||||
} else
|
||||
printf("XXX");
|
||||
if(a[i][j].r==0&&a[i][j].d==0) {
|
||||
printf("XXXX");
|
||||
} else if(a[i][j].d&&a[i][j].r==0)
|
||||
printf("\\XXX");
|
||||
else{
|
||||
printf("\\");
|
||||
int h=num(a[i][j].r);
|
||||
for(int i=0; i<h; i++)
|
||||
printf("0");
|
||||
printf("%d",a[i][j].r);
|
||||
}
|
||||
} else if(b[i][j])
|
||||
printf(".......");
|
||||
if(j!=m)printf(" ");
|
||||
else printf("\n");
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,114 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
char map[105][105];
|
||||
int sx,sy,n,m;
|
||||
int visit[105][105];
|
||||
int dir[4][2]= {1,0,0,1,-1,0,0,-1};
|
||||
struct node
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
int cost;
|
||||
} a[1000015];
|
||||
int mvCost(int tx,int ty,int cost)
|
||||
{
|
||||
if(map[tx][ty]=='R')
|
||||
cost-=3;
|
||||
else if(map[tx][ty]=='T')
|
||||
cost-=2;
|
||||
else
|
||||
cost-=1;
|
||||
int flag=0;
|
||||
for(int i=0;i<4;i++)
|
||||
{
|
||||
int ttx=tx+dir[i][0];
|
||||
int tty=ty+dir[i][1];
|
||||
if(ttx>=0 && ttx<n && tty>=0 && tty<m)
|
||||
{
|
||||
if(map[ttx][tty]=='E')
|
||||
{
|
||||
flag=1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(flag==1 && cost>0)
|
||||
cost=0;
|
||||
return cost;
|
||||
}
|
||||
void bfs(int mv)
|
||||
{
|
||||
memset(visit,-1,sizeof(visit));
|
||||
a[0].x=sx;
|
||||
a[0].y=sy;
|
||||
a[0].cost=mv;
|
||||
visit[sx][sy]=mv;
|
||||
int start=0,end=1;
|
||||
node cur,change;
|
||||
while(start<end)
|
||||
{
|
||||
cur=a[start++];
|
||||
if(cur.cost==0)
|
||||
continue;
|
||||
for(int i=0; i<4; i++)
|
||||
{
|
||||
int tx=cur.x+dir[i][0];
|
||||
int ty=cur.y+dir[i][1];
|
||||
if(map[tx][ty]=='#' ||map[tx][ty]=='E' || map[tx][ty]=='Y' )
|
||||
continue;
|
||||
else if(tx>=0 && tx<n && ty>=0 && ty<m )
|
||||
{
|
||||
int Cost=mvCost(tx,ty,cur.cost);
|
||||
if(Cost<0)
|
||||
continue;
|
||||
else
|
||||
{
|
||||
if(Cost>visit[tx][ty])
|
||||
{
|
||||
change.x=tx;
|
||||
change.y=ty;
|
||||
change.cost=Cost;
|
||||
a[end++]=change;
|
||||
visit[tx][ty]=Cost;
|
||||
if(map[tx][ty]!='P')
|
||||
map[tx][ty]='*';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int t,mv;
|
||||
cin>>t;
|
||||
while(t--)
|
||||
{
|
||||
cin>>n>>m>>mv;
|
||||
for(int i=0; i<n; i++)
|
||||
{
|
||||
for(int j=0; j<m; j++)
|
||||
{
|
||||
cin>>map[i][j];
|
||||
if(map[i][j]=='Y')
|
||||
{
|
||||
sx=i;
|
||||
sy=j;
|
||||
}
|
||||
}
|
||||
}
|
||||
bfs(mv);
|
||||
for(int i=0; i<n; i++)
|
||||
{
|
||||
for(int j=0; j<m; j++)
|
||||
{
|
||||
printf("%c",map[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
#include<iostream>
|
||||
#include<cstdlib>
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
int n,m,sum1,sum2;
|
||||
cin>>n;
|
||||
while(n--)
|
||||
{
|
||||
cin>>m;
|
||||
sum1=0;
|
||||
sum2=0;
|
||||
if(m%8==0)
|
||||
{cout<<"Lucky number!"<<endl;}
|
||||
else
|
||||
{
|
||||
while(m)
|
||||
{
|
||||
sum1+=m%10;
|
||||
sum2+=(m%10)*(m%10);
|
||||
m=m/10;
|
||||
}
|
||||
if(sum1%8==0||sum2%8==0)
|
||||
cout<<"Lucky number!"<<endl;
|
||||
else
|
||||
cout<<"What a pity!"<<endl;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
#include<iostream>
|
||||
#include<map>
|
||||
#include<string.h>
|
||||
using namespace std;
|
||||
const int MAX=1000000;
|
||||
string str,var;
|
||||
int n;
|
||||
int Str2Int(string s)
|
||||
{
|
||||
int end=0,a=1;
|
||||
if(s[0]=='-')
|
||||
end=1;
|
||||
int res=0;
|
||||
for(int i=s.size()-1;i>=end; i--)
|
||||
{
|
||||
res += (s[i]-'0')*a;
|
||||
a *= 10;
|
||||
}
|
||||
if(end) return -res;
|
||||
return res;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
map<string,int> m;
|
||||
int T,i,value;
|
||||
char c;
|
||||
cin>>T;
|
||||
while(T--)
|
||||
{
|
||||
cin>>n;
|
||||
for(i=0;i<n-1;i++)
|
||||
{
|
||||
cin>>var>>c>>value;
|
||||
m[var]=value;
|
||||
}
|
||||
int tmp,ans=0;
|
||||
bool plus=true;
|
||||
while(cin>>str)
|
||||
{
|
||||
if(str=="?") break;
|
||||
else if(str=="+") plus=true;
|
||||
else if(str=="-") plus=false;
|
||||
else if(str=="=") continue;
|
||||
else if(str[0]>='a' && str[0]<='z')
|
||||
{
|
||||
if(plus) ans += m[str];
|
||||
else ans -= m[str];
|
||||
}
|
||||
else
|
||||
{
|
||||
tmp=Str2Int(str);
|
||||
if(plus) ans += tmp;
|
||||
else ans -= tmp;
|
||||
}
|
||||
}
|
||||
cout<<ans<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
#include<iostream>
|
||||
#include<stdio.h>
|
||||
using namespace std;
|
||||
int coin[5]={1,5,10,50,100};
|
||||
int main()
|
||||
{
|
||||
int p,a[5];
|
||||
int total_case,iii;
|
||||
scanf("%d",&total_case);
|
||||
for(iii=0;iii<total_case;iii++)
|
||||
{
|
||||
scanf("%d%d%d%d%d%d",&p,&a[0],&a[1],&a[2],&a[3],&a[4]);
|
||||
int ssum=0,i,n1=0;
|
||||
for(i=4;i>=0;i--)
|
||||
{
|
||||
if((p-ssum)/coin[i]<a[i])
|
||||
{
|
||||
n1+=(p-ssum)/coin[i];
|
||||
ssum+=((p-ssum)/coin[i])*coin[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
n1+=a[i];
|
||||
ssum+=a[i]*coin[i];
|
||||
}
|
||||
}
|
||||
int sum=0,n2=0;
|
||||
for(i=0;i<5;i++)
|
||||
{
|
||||
int aa=a[i];
|
||||
if((p-sum)/coin[i]<a[i])
|
||||
{
|
||||
n2+=(p-sum)/coin[i];
|
||||
sum+=((p-sum)/coin[i])*coin[i];
|
||||
a[i]=(p-sum)/coin[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
n2+=a[i];
|
||||
sum+=a[i]*coin[i];
|
||||
}
|
||||
if(p-sum<coin[i]&&p-sum!=0&&aa-a[i]>0)
|
||||
{
|
||||
int j;
|
||||
for(j=i-1;j>=0;j--)
|
||||
{
|
||||
if((coin[i]-(p-sum))/coin[j]>a[j])
|
||||
{
|
||||
n2-=a[j];
|
||||
sum-=coin[j]*a[j];
|
||||
}
|
||||
else
|
||||
{
|
||||
n2-=(coin[i]-(p-sum))/coin[j];
|
||||
sum-=((coin[i]-(p-sum))/coin[j])*coin[j];
|
||||
}
|
||||
}
|
||||
n2++;
|
||||
sum+=coin[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(sum==p&&ssum==p)
|
||||
printf("%d %d\n",n1,n2);
|
||||
else
|
||||
printf("-1 -1\n");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
#include<cstdio>
|
||||
#include<algorithm>
|
||||
#include<iostream>
|
||||
#include<cstring>
|
||||
#include<cmath>
|
||||
using namespace std;
|
||||
double a,b,c;
|
||||
int main() {
|
||||
int t;
|
||||
cin>>t;
|
||||
while(t--) {
|
||||
scanf("%lf%lf%lf",&a,&b,&c);
|
||||
if(b<c)
|
||||
swap(b,c);
|
||||
double x=b/2;
|
||||
double y=c/2;
|
||||
double r=sqrt(2*a*a);
|
||||
double ans;
|
||||
if(r<y) {
|
||||
ans=a*a;
|
||||
} else {
|
||||
if(y<r/2) {
|
||||
ans=y*y;
|
||||
} else {
|
||||
double z=r-y;
|
||||
ans=a*a-z*z;
|
||||
}
|
||||
}
|
||||
printf("%.4f\n",ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<string>
|
||||
#include<iostream>
|
||||
#include<algorithm>
|
||||
#include<cmath>
|
||||
#include<stack>
|
||||
using namespace std;
|
||||
struct state
|
||||
{
|
||||
int tot,num;
|
||||
}tem,in,out;
|
||||
stack <struct state> st;
|
||||
stack < char > cst;
|
||||
int n,m;
|
||||
char s[10005];
|
||||
int main()
|
||||
{
|
||||
int t;
|
||||
scanf("%d",&t);getchar();
|
||||
while(t--)
|
||||
{
|
||||
gets(s);
|
||||
int len=strlen(s);
|
||||
for(int i=0;i<len;i++)
|
||||
{
|
||||
if(s[i]<='9'&&s[i]>='0')
|
||||
{
|
||||
int tep=0;
|
||||
while(s[i]<='9'&&s[i]>='0')
|
||||
{
|
||||
tep=tep*10+s[i]-'0';
|
||||
i++;
|
||||
}
|
||||
if(!cst.empty()&&cst.top()=='+')
|
||||
{
|
||||
tem=st.top();st.pop();
|
||||
tem.num+=tep;tem.tot++;
|
||||
st.push(tem);cst.pop();
|
||||
}
|
||||
else
|
||||
{
|
||||
tem.num=tep;tem.tot=0;
|
||||
st.push(tem);
|
||||
}
|
||||
i--;
|
||||
}
|
||||
else if(s[i]=='+'||s[i]=='(')
|
||||
cst.push(s[i]);
|
||||
else if(s[i]==')'||s[i]==',')
|
||||
{
|
||||
while(!cst.empty()&&cst.top()=='+')
|
||||
{
|
||||
in=st.top();st.pop();
|
||||
out=st.top();st.pop();
|
||||
tem.num=in.num+out.num;
|
||||
tem.tot=in.tot+out.tot+1;
|
||||
st.push(tem);cst.pop();
|
||||
}
|
||||
if(s[i]==')'&&st.size()>=2)
|
||||
{
|
||||
in=st.top();st.pop();
|
||||
out=st.top();st.pop();
|
||||
if(out.num>in.num)
|
||||
{
|
||||
out.tot=out.tot*2+in.tot;
|
||||
st.push(out);
|
||||
}
|
||||
else
|
||||
{
|
||||
in.tot=in.tot*2+out.tot;
|
||||
st.push(in);
|
||||
}
|
||||
cst.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
while(!cst.empty())
|
||||
{
|
||||
while(!cst.empty()&&cst.top()=='+')
|
||||
{
|
||||
in=st.top();st.pop();
|
||||
out=st.top();st.pop();
|
||||
tem.num=in.num+out.num;
|
||||
tem.tot=in.tot+out.tot+1;
|
||||
st.push(tem);
|
||||
cst.pop();
|
||||
}
|
||||
}
|
||||
printf("%d %d\n",st.top().num,st.top().tot);
|
||||
st.pop();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<iostream>
|
||||
#include<cmath>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
const int maxn=2005;
|
||||
char str[maxn];
|
||||
int main(){
|
||||
int cas=1;
|
||||
while(scanf("%s", str)&&str[0]!='-'){
|
||||
int i, ans=0, lef=0;
|
||||
for(i=0; str[i]!='\0'; i++){
|
||||
if(str[i]=='{'){
|
||||
lef++;
|
||||
}
|
||||
else if(lef>0){
|
||||
lef--;
|
||||
}
|
||||
else {
|
||||
ans++;lef++;
|
||||
}
|
||||
}
|
||||
printf("%d. %d\n", cas++, ans+lef/2);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
#include<stdio.h>
|
||||
long long a,b;
|
||||
long long up(long long a,long long b)
|
||||
{
|
||||
long long t;
|
||||
if(a==0) return b;
|
||||
while(b!=0)
|
||||
{
|
||||
t=b;b=a%b;a=t;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while(scanf("%lld%lld",&a,&b)!=EOF)
|
||||
{
|
||||
if(a==0&&b==0) break;
|
||||
printf("%lld\n",a*b/up(a,b)/up(a,b));
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,103 @@
|
|||
#include<stdio.h>
|
||||
#include<math.h>
|
||||
#include<iostream>
|
||||
#include<string.h>
|
||||
using namespace std;
|
||||
#define MAXN 1000000
|
||||
int prime[MAXN],num;
|
||||
bool notprime[MAXN];
|
||||
int pa[MAXN],ma[MAXN];
|
||||
int pb[MAXN],mb[MAXN];
|
||||
void PRIME()
|
||||
{
|
||||
int i,j;
|
||||
num=0;
|
||||
memset(notprime,false,sizeof(notprime));
|
||||
for(i=2;i<MAXN;i++)
|
||||
if(!notprime[i])
|
||||
{
|
||||
prime[num++]=i;
|
||||
for(j=i+i;j<MAXN;j+=i)
|
||||
notprime[j]=true;
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int a,b;
|
||||
int na,nb;
|
||||
int i,j,t;
|
||||
int iCase=0;
|
||||
PRIME();
|
||||
while(scanf("%d%d",&a,&b))
|
||||
{
|
||||
if(a==0&&b==0) break;
|
||||
iCase++;
|
||||
na=nb=0;
|
||||
for(i=0;i<num&&a>0;i++)
|
||||
{
|
||||
if(a%prime[i]==0)
|
||||
{
|
||||
t=0;
|
||||
while(a%prime[i]==0)
|
||||
{
|
||||
t++;
|
||||
a/=prime[i];
|
||||
}
|
||||
pa[na]=prime[i];
|
||||
ma[na++]=t;
|
||||
}
|
||||
}
|
||||
for(i=0;i<num&&b>0;i++)
|
||||
{
|
||||
if(b%prime[i]==0)
|
||||
{
|
||||
t=0;
|
||||
while(b%prime[i]==0)
|
||||
{
|
||||
t++;
|
||||
b/=prime[i];
|
||||
}
|
||||
pb[nb]=prime[i];
|
||||
mb[nb++]=t;
|
||||
}
|
||||
}
|
||||
int X=0,D=0;
|
||||
i=0;j=0;
|
||||
while(i<na&&j<nb)
|
||||
{
|
||||
if(pa[i]==pb[j])
|
||||
{
|
||||
X++;
|
||||
D+=abs(ma[i]-mb[j]);
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
else if(pa[i]<pb[j])
|
||||
{
|
||||
X++;
|
||||
D+=ma[i];
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
X++;
|
||||
D+=mb[j];
|
||||
j++;
|
||||
}
|
||||
}
|
||||
while(i==na&&j<nb)
|
||||
{
|
||||
X++;
|
||||
D+=mb[j];
|
||||
j++;
|
||||
}
|
||||
while(i<na&&j==nb)
|
||||
{
|
||||
X++;
|
||||
D+=ma[i];
|
||||
i++;
|
||||
}
|
||||
printf("%d. %d:%d\n",iCase,X,D);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
#include<stdio.h>
|
||||
int main()
|
||||
{
|
||||
int n0;
|
||||
int iCase=0;
|
||||
while(scanf("%d",&n0),n0)
|
||||
{
|
||||
iCase++;
|
||||
printf("%d. ",iCase);
|
||||
if(n0%2==0)
|
||||
printf("even %d\n",n0/2);
|
||||
else
|
||||
printf("odd %d\n",(n0-1)/2);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,99 @@
|
|||
#include<iostream>
|
||||
#include<map>
|
||||
#include<string>
|
||||
#include<queue>
|
||||
using namespace std;
|
||||
bool is(string s)
|
||||
{
|
||||
int i,k=0,flag=0,t=0;
|
||||
int n=s.size();
|
||||
for(i=0;i<n;++i) {
|
||||
if(s[i]=='W'&&flag==1) k++;
|
||||
if(s[i]=='B') {
|
||||
flag=1;
|
||||
if(k!=0) {
|
||||
t=1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(t) return false;
|
||||
else return true;
|
||||
}
|
||||
int BFS(string s)
|
||||
{
|
||||
queue<string> Qu;
|
||||
int len=s.size(),f;
|
||||
string u,v;
|
||||
map<string,int> step;
|
||||
step[s]=0;
|
||||
Qu.push(s);
|
||||
while(!Qu.empty()) {
|
||||
u=Qu.front();
|
||||
Qu.pop();
|
||||
if(step[u]>=10) return -1;
|
||||
f=u.find('F');
|
||||
v=u;
|
||||
if(f+1<len) {
|
||||
v[f]=v[f+1];
|
||||
v[f+1]='F';
|
||||
if(step.find(v)==step.end()) {
|
||||
step[v]=step[u]+1;
|
||||
if(is(v)) return step[v];
|
||||
Qu.push(v);
|
||||
}
|
||||
}
|
||||
v=u;
|
||||
if(f-1>=0) {
|
||||
v[f]=v[f-1];
|
||||
v[f-1]='F';
|
||||
if(step.find(v)==step.end()) {
|
||||
step[v]=step[u]+1;
|
||||
if(is(v)) return step[v];
|
||||
Qu.push(v);
|
||||
}
|
||||
}
|
||||
v=u;
|
||||
if(f+2<len) {
|
||||
if(v[f+2]=='B')
|
||||
v[f]='W';
|
||||
if(v[f+2]=='W')
|
||||
v[f]='B';
|
||||
v[f+2]='F';
|
||||
if(step.find(v)==step.end()) {
|
||||
step[v]=step[u]+1;
|
||||
if(is(v)) return step[v];
|
||||
Qu.push(v);
|
||||
}
|
||||
}
|
||||
v=u;
|
||||
if(f-2>=0) {
|
||||
if(v[f-2]=='B')
|
||||
v[f]='W';
|
||||
if(v[f-2]=='W')
|
||||
v[f]='B';
|
||||
v[f-2]='F';
|
||||
if(step.find(v)==step.end()) {
|
||||
step[v]=step[u]+1;
|
||||
if(is(v)) return step[v];
|
||||
Qu.push(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
string state;
|
||||
int count=1,r;
|
||||
while(cin>>state)
|
||||
{
|
||||
if(state[0]=='-') break;
|
||||
if(is(state)) cout<<count++<<". 0"<<endl;
|
||||
else {
|
||||
r=BFS(state);
|
||||
r==-1?cout<<count++<<". -1"<<endl:cout<<count++<<". "<<r<<endl;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#define maxn 1000
|
||||
#define PI 3.141
|
||||
const double inf = 0.00001;
|
||||
double d1[maxn], d2[maxn];
|
||||
double dis(double x1, double y1, double x2, double y2)
|
||||
{
|
||||
return 1.0 * (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n, i, j, ans, sum, k = 0;
|
||||
double r1, r2, r3, r4, r;
|
||||
double x1, y1, x2, y2, x, y;
|
||||
while (scanf("%d", &n), n)
|
||||
{
|
||||
scanf("%lf%lf%lf%lf%lf", &x1, &y1, &x2, &y2, &r);
|
||||
r = 1.0 * r / PI;
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
scanf("%lf%lf", &x, &y);
|
||||
d1[i] = dis(x, y, x1, y1);
|
||||
d2[i] = dis(x, y, x2, y2);
|
||||
}
|
||||
for (i = 0, ans = n; i < n; i++)
|
||||
{
|
||||
r1 = d1[i];
|
||||
if (r1 <= r)//涓句涓瑰朵腑涓涓濉璺绂r1,╀纾娉㈣绂讳负r2
|
||||
{
|
||||
r2 = r - r1;
|
||||
for (j = 0, sum = 0; j < n; j++)
|
||||
{
|
||||
if (d1[j] <= r1)
|
||||
{
|
||||
sum++;
|
||||
}
|
||||
else if (d2[j] <= r2)
|
||||
{
|
||||
sum++;
|
||||
}
|
||||
}
|
||||
if (ans > n - sum)
|
||||
{
|
||||
ans = n - sum;
|
||||
}
|
||||
}
|
||||
r2 = d2[i];
|
||||
if (r2 <= r)
|
||||
{
|
||||
r1 = r - r2;
|
||||
for (j = 0, sum = 0; j < n; j++)
|
||||
{
|
||||
if (d1[j] <= r1)
|
||||
{
|
||||
sum++;
|
||||
}
|
||||
else if (d2[j] <= r2)
|
||||
{
|
||||
sum++;
|
||||
}
|
||||
}
|
||||
if (ans > n - sum)
|
||||
{
|
||||
ans = n - sum;
|
||||
}
|
||||
}
|
||||
}
|
||||
k++;
|
||||
printf("%d. %d\n", k, ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <map>
|
||||
#include <algorithm>
|
||||
#include <queue>
|
||||
#include <cmath>
|
||||
#include <stack>
|
||||
#include <set>
|
||||
using namespace std;
|
||||
int mp[300][300];
|
||||
int n;
|
||||
void add(int a,int b){
|
||||
mp[a][b]=1;
|
||||
for(int i=1;i<=n;i++)
|
||||
if(mp[i][a]){
|
||||
mp[i][b]=1;
|
||||
for(int j=1;j<=n;j++)
|
||||
if(mp[a][j])
|
||||
mp[i][j]=1;
|
||||
}
|
||||
for(int i=1;i<=n;i++)
|
||||
if(mp[b][i]){
|
||||
mp[a][i]=1;
|
||||
for(int j=1;j<=n;j++)
|
||||
if(mp[j][b])
|
||||
mp[j][i]=1;
|
||||
}
|
||||
}
|
||||
int main(){
|
||||
int t;
|
||||
int cas=1;
|
||||
while(~scanf("%d%d",&n,&t)){
|
||||
if(!n && !t)break;
|
||||
memset(mp,0,sizeof(mp));
|
||||
int ans=0;
|
||||
while(t--){
|
||||
int a,b;
|
||||
scanf("%d%d",&a,&b);
|
||||
if(mp[b][a])ans++;
|
||||
else if(a==b)ans++;
|
||||
else if(!mp[a][b]) add(a,b);
|
||||
}
|
||||
printf("%d. %d\n",cas++,ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
#include<cstdio>
|
||||
#include<iostream>
|
||||
#include<string>
|
||||
#include<cmath>
|
||||
#include<algorithm>
|
||||
#include<vector>
|
||||
using namespace std;
|
||||
int n, k;
|
||||
int x[100005], y[100005];
|
||||
vector<int> s1, s2;
|
||||
void gao(int *p, vector<int>& V) {
|
||||
sort(p, p+n);
|
||||
V.clear();
|
||||
p[n] = -1;
|
||||
int cnt = 1;
|
||||
for(int i=0; i<n; i++) {
|
||||
if(p[i] != p[i+1]) {
|
||||
V.push_back(cnt);
|
||||
cnt = 1;
|
||||
} else cnt ++;
|
||||
}
|
||||
for(int i=0; i<V.size(); i++) {
|
||||
}
|
||||
}
|
||||
int ans;
|
||||
vector<int> v;
|
||||
void dfs(int pi, int ni, int cur) {
|
||||
if(pi == k || ni == v.size()) {
|
||||
cur += (k - pi) * abs(n);
|
||||
if(ans > cur) ans = cur;
|
||||
return ;
|
||||
}
|
||||
int sum = (ni == 0) ? 0 : v[ni-1];
|
||||
if(pi == k-1) {
|
||||
dfs(pi+1, v.size(), cur+abs(v[v.size()-1] - sum - n));
|
||||
return ;
|
||||
}
|
||||
int id = lower_bound(v.begin(), v.end(), n + sum) - v.begin();
|
||||
id --;
|
||||
if(ni <= id && id < v.size()) dfs(pi+1, id+1, cur+abs(v[id] - sum - n));
|
||||
id ++;
|
||||
if(ni <= id && id < v.size()) dfs(pi+1, id+1, cur+abs(v[id] - sum - n));
|
||||
}
|
||||
int solve(vector<int> V) {
|
||||
ans = 1000000;
|
||||
v = V;
|
||||
for(int i=1; i<v.size(); i++) {
|
||||
v[i] += v[i-1];
|
||||
}
|
||||
for(int i=0; i<v.size(); i++) {
|
||||
v[i] *= k;
|
||||
}
|
||||
dfs(0, 0, 0);
|
||||
return ans;
|
||||
}
|
||||
int gcd(int x, int y) {
|
||||
while(x%=y^=x^=y^=x);
|
||||
return y;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int kase = 0;
|
||||
while(scanf("%d%d", &n, &k) == 2 && n && k) {
|
||||
for(int i=0; i<n; i++) {
|
||||
scanf("%d%d", &x[i], &y[i]);
|
||||
}
|
||||
gao(x, s1);
|
||||
gao(y, s2);
|
||||
int ans = solve(s1);
|
||||
ans = min(ans, solve(s2));
|
||||
if(ans == 0) {
|
||||
printf("%d. 0/1\n", ++kase);
|
||||
continue;
|
||||
}
|
||||
k *= k;
|
||||
int g = gcd(ans, k);
|
||||
ans /= g; k/= g;
|
||||
printf("%d. %d/%d\n", ++kase, ans, k);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<cmath>
|
||||
#include<algorithm>
|
||||
#define MAXN 110
|
||||
using namespace std;
|
||||
double a[MAXN][MAXN], g[MAXN][MAXN], x[MAXN];
|
||||
int pos[MAXN][MAXN];
|
||||
int n, m, d, cnt;
|
||||
int MHT(int x1, int y1, int x2, int y2) {
|
||||
return abs(x1 - x2) + abs(y1 - y2);
|
||||
}
|
||||
void Gauss() {
|
||||
int i, j, k;
|
||||
double tmp, big;
|
||||
for (i = 0; i < cnt; i++) {
|
||||
for (big = 0, j = i; j < cnt; j++) {
|
||||
if (abs(g[j][i]) > big) {
|
||||
big = abs(g[j][i]);
|
||||
k = j;
|
||||
}
|
||||
}
|
||||
if (k != i) {
|
||||
for (j = 0; j <= cnt; j++)
|
||||
swap(g[i][j], g[k][j]);
|
||||
}
|
||||
for (j = i + 1; j < cnt; j++) {
|
||||
if (g[j][i]) {
|
||||
tmp = -g[j][i] / g[i][i];
|
||||
for (k = i; k <= cnt; k++)
|
||||
g[j][k] += tmp * g[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
for (i = cnt - 1; i >= 0; i--) {
|
||||
tmp = 0;
|
||||
for (j = i + 1; j < cnt; j++)
|
||||
tmp += g[i][j] * x[j];
|
||||
x[i] = (g[i][j] - tmp) / g[i][i];
|
||||
}
|
||||
}
|
||||
int main() {
|
||||
int i, j, k, l;
|
||||
bool flag = true;
|
||||
while (scanf("%d%d%d", &m, &n, &d), n) {
|
||||
if (flag)
|
||||
flag = false;
|
||||
else
|
||||
putchar('\n');
|
||||
for (i = cnt = 0; i < n; i++) {
|
||||
for (j = 0; j < m; j++) {
|
||||
scanf("%lf", &a[i][j]);
|
||||
pos[i][j] = cnt++;
|
||||
}
|
||||
}
|
||||
memset(g, 0, sizeof(g));
|
||||
for (i = 0; i < n; i++) {
|
||||
for (j = 0; j < m; j++) {
|
||||
for (k = 0; k < n; k++) {
|
||||
for (l = 0; l < m; l++) {
|
||||
if (MHT(i, j, k, l) <= d) {
|
||||
g[pos[i][j]][pos[k][l]] = 1;
|
||||
g[pos[i][j]][cnt] += a[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Gauss();
|
||||
for (i = 0; i < n; i++) {
|
||||
for (j = 0; j < m; j++)
|
||||
printf("%8.2lf", x[i * m + j]);
|
||||
putchar('\n');
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
const int maxn=2505;
|
||||
const int maxc=55;
|
||||
vector<int>e[maxn];
|
||||
int vis[maxn],pre[maxn];
|
||||
int map[maxc][maxc];
|
||||
int dir[12][2]={{-1,-2},{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,0},{0,1},{1,0},{0,-1}};
|
||||
int find(int u)
|
||||
{
|
||||
int i,j,v;
|
||||
for(i=0;i<e[u].size();i++)
|
||||
{
|
||||
v=e[u][i];
|
||||
if(!vis[v])
|
||||
{
|
||||
vis[v]=1;
|
||||
if(pre[v]==-1||find(pre[v]))
|
||||
{
|
||||
pre[v]=u;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
string a[maxn],b[maxn];
|
||||
int main()
|
||||
{
|
||||
int n,m,tt=0;
|
||||
while(scanf("%d%d",&n,&m)!=EOF)
|
||||
{
|
||||
if(n==0&&m==0)break;
|
||||
int i,j,k;
|
||||
for(i=1;i<=n*m;i++)
|
||||
e[i].clear();
|
||||
for(i=1;i<=n;i++)
|
||||
for(j=1;j<=m;j++)
|
||||
cin>>map[i][j];
|
||||
int x,y,ans=0,t;
|
||||
for(i=1;i<=n;i++)
|
||||
{
|
||||
for(j=1;j<=m;j++)
|
||||
{
|
||||
t=(i+j)%2;
|
||||
if(map[i][j]==-1)continue;
|
||||
for(k=0;k<12;k++)
|
||||
{
|
||||
if(!(map[i][j]&(1<<k)))continue;
|
||||
x=i+dir[k][0];
|
||||
y=j+dir[k][1];
|
||||
if(x<=0||y<=0||x>n||y>m||map[x][y]==-1)continue;
|
||||
if(t)e[(i-1)*m+j].push_back((x-1)*m+y);
|
||||
else e[(x-1)*m+y].push_back((i-1)*m+j);
|
||||
}
|
||||
}
|
||||
}
|
||||
memset(pre,-1,sizeof(pre));
|
||||
for(i=1;i<=n*m;i++)
|
||||
{
|
||||
memset(vis,0,sizeof(vis));
|
||||
ans+=find(i);
|
||||
}
|
||||
printf("%d. %d\n",++tt,ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
#include<iostream>
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
int n, t, i;
|
||||
scanf("%d", &n);
|
||||
for( i=0; i < n; i++ )
|
||||
scanf("%d", &t), printf("%c", t);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
#define inf 1e9
|
||||
using namespace std;
|
||||
pair<int, int> point[20];
|
||||
bool fix[20];
|
||||
double dp[1<<18];
|
||||
double Distance(int a, int b) {
|
||||
int x1 = point[a].first, y1 = point[a].second;
|
||||
int x2 = point[b].first, y2 = point[b].second;
|
||||
return sqrt(1.0*(x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));
|
||||
}
|
||||
double Fixed(int n, int state, int cur) {
|
||||
double ans = 0;
|
||||
bool mark[20];
|
||||
double dist[20];
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (((1<<i) & state)) {
|
||||
mark[i] = 1;
|
||||
dist[i] = Distance(i, cur);
|
||||
}
|
||||
else
|
||||
mark[i] = 0;
|
||||
}
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
double Min = inf;
|
||||
int p = 19;
|
||||
for (int j = 0; j < n; ++j) {
|
||||
if (mark[j] && dist[j] < Min) {
|
||||
Min = dist[j];
|
||||
p = j;
|
||||
}
|
||||
}
|
||||
ans += Min;
|
||||
mark[p] = 0;
|
||||
}
|
||||
if (ans >= inf) return -1;
|
||||
return ans;
|
||||
}
|
||||
int main() {
|
||||
int n;
|
||||
while (cin>>n) {
|
||||
if (!n) break;
|
||||
int begin = 0, target = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
cin>>point[i].first>>point[i].second>>fix[i];
|
||||
if (fix[i])
|
||||
begin += (1<<i);
|
||||
target += (1<<i);
|
||||
}
|
||||
for (int i = 0; i < (1<<n); ++i)
|
||||
dp[i] = inf;
|
||||
dp[begin] = 0;
|
||||
for (int i = begin; i < target; ++i) {
|
||||
if (dp[i] == inf) continue;
|
||||
for (int j = 0; j < n; ++j) {
|
||||
if (i & (1<<j)) continue;
|
||||
double sum = Fixed(n, i, j);
|
||||
if (sum >= 0)
|
||||
dp[i|(1<<j)] = min(dp[i|(1<<j)], dp[i] + sum);
|
||||
}
|
||||
}
|
||||
if (dp[target] == inf)
|
||||
printf("No Solution\n");
|
||||
else
|
||||
printf("%.6lf\n", dp[target]);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
#include<iostream>
|
||||
#include<algorithm>
|
||||
#include<string>
|
||||
using namespace std;
|
||||
char str[100005];
|
||||
int n;
|
||||
int main()
|
||||
{
|
||||
while(scanf("%d",&n)==1&&n)
|
||||
{
|
||||
scanf("%s",str);
|
||||
int h=0,t=0;
|
||||
for(int i=0;i<n;i++)
|
||||
if(str[i]=='H')
|
||||
h++;
|
||||
else t++;
|
||||
if(h%2==0 && t%2==0)
|
||||
{
|
||||
h/=2;t/=2;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("-1\n");
|
||||
continue;
|
||||
}
|
||||
int hh=0,tt=0;
|
||||
for(int i=0;i<n/2;i++)
|
||||
if(str[i]=='H')
|
||||
hh++;
|
||||
else tt++;
|
||||
if(hh==h && tt==t)
|
||||
{
|
||||
printf("1\n%d\n",n/2);
|
||||
continue;
|
||||
}
|
||||
for(int i=n/2,j=0;i<n;i++,j++)
|
||||
{
|
||||
if(str[i]=='H')
|
||||
hh++;
|
||||
else tt++;
|
||||
if(str[j]=='H')
|
||||
hh--;
|
||||
else tt--;
|
||||
if(hh==h && tt==t)
|
||||
{
|
||||
printf("2\n%d %d\n",j+1,i+1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,165 @@
|
|||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
#include <map>
|
||||
#include <set>
|
||||
using namespace std ;
|
||||
const int MAXN=55;
|
||||
int a[MAXN][MAXN],b[MAXN][MAXN];
|
||||
int x[MAXN];
|
||||
bool free_x[MAXN];
|
||||
/*
|
||||
void Debug(void)
|
||||
{
|
||||
int i, j;
|
||||
for (i = 0; i < equ; i++)
|
||||
{
|
||||
for (j = 0; j < var + 1; j++)
|
||||
{
|
||||
cout << a[i][j] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
*/
|
||||
inline int gcd(int a,int b)
|
||||
{
|
||||
int t;
|
||||
while(b!=0)
|
||||
{
|
||||
t=b;
|
||||
b=a%b;
|
||||
a=t;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
inline int lcm(int a,int b)
|
||||
{
|
||||
return a/gcd(a,b)*b;
|
||||
}
|
||||
int Gauss(int equ,int var)
|
||||
{
|
||||
int i,j,k;
|
||||
int max_r;
|
||||
int col;
|
||||
int ta,tb;
|
||||
int LCM;
|
||||
int temp;
|
||||
int free_x_num;
|
||||
int free_index;
|
||||
for(int i=0;i<=var;i++)
|
||||
{
|
||||
x[i]=0;
|
||||
free_x[i]=true;
|
||||
}
|
||||
col=0;
|
||||
for(k = 0;k < equ && col < var;k++,col++)
|
||||
{
|
||||
max_r=k;
|
||||
for(i=k+1;i<equ;i++)
|
||||
{
|
||||
if(abs(a[i][col])>abs(a[max_r][col])) max_r=i;
|
||||
}
|
||||
if(max_r!=k)
|
||||
{
|
||||
for(j=k;j<var+1;j++) swap(a[k][j],a[max_r][j]);
|
||||
}
|
||||
if(a[k][col]==0)
|
||||
{
|
||||
k--;
|
||||
continue;
|
||||
}
|
||||
for(i=k+1;i<equ;i++)
|
||||
{
|
||||
if(a[i][col]!=0)
|
||||
{
|
||||
for(j=col;j<var+1;j++)
|
||||
{
|
||||
a[i][j] ^= a[k][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (i = k; i < equ; i++)
|
||||
{
|
||||
if (a[i][col] != 0) return -1;
|
||||
}
|
||||
if (k < var)
|
||||
{
|
||||
for (i = k - 1; i >= 0; i--)
|
||||
{
|
||||
free_x_num = 0;
|
||||
for (j = 0; j < var; j++)
|
||||
{
|
||||
if (a[i][j] != 0 && free_x[j]) free_x_num++, free_index = j;
|
||||
}
|
||||
if (free_x_num > 1) continue;
|
||||
temp = a[i][var];
|
||||
for (j = 0; j < var; j++)
|
||||
{
|
||||
if (a[i][j] != 0 && j != free_index) temp -= a[i][j] * x[j];
|
||||
}
|
||||
x[free_index] = temp / a[i][free_index];
|
||||
free_x[free_index] = 0;
|
||||
}
|
||||
return var - k;
|
||||
}
|
||||
for (i = var - 1; i >= 0; i--)
|
||||
{
|
||||
temp = a[i][var];
|
||||
for (j = i + 1; j < var; j++)
|
||||
{
|
||||
if (a[i][j] != 0) temp -= a[i][j] * x[j];
|
||||
}
|
||||
if (temp % a[i][i] != 0) return -2;
|
||||
x[i] = temp / a[i][i];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int main(void)
|
||||
{
|
||||
int T ;
|
||||
scanf("%d",&T) ;
|
||||
for(int cas=1 ;cas<=T ;cas++)
|
||||
{
|
||||
int equ,var ;
|
||||
scanf("%d%d",&equ,&var) ;
|
||||
memset(a,0,sizeof(a)) ;
|
||||
memset(b,0,sizeof(b)) ;
|
||||
for(int i=0 ;i<var ;i++)
|
||||
{
|
||||
int k ;
|
||||
scanf("%d",&k) ;
|
||||
for(int j=0 ;j<k ;j++)
|
||||
{
|
||||
int v ;
|
||||
scanf("%d",&v) ;
|
||||
b[v-1][i]=1 ;
|
||||
}
|
||||
}
|
||||
printf("Case %d:\n",cas) ;
|
||||
int q ;
|
||||
scanf("%d",&q) ;
|
||||
while(q--)
|
||||
{
|
||||
memcpy(a,b,sizeof(b)) ;
|
||||
for(int i=0 ;i<equ ;i++)
|
||||
{
|
||||
int v ;
|
||||
scanf("%d",&v) ;
|
||||
a[i][var]=v ;
|
||||
}
|
||||
int x=Gauss(equ,var) ;
|
||||
if(x<0)
|
||||
{
|
||||
puts("0") ;
|
||||
continue ;
|
||||
}
|
||||
printf("%I64d\n",1LL<<x) ;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
#include<stdio.h>
|
||||
#include<math.h>
|
||||
#define N 10010
|
||||
#define PI acos(-1)
|
||||
#define EPS 1e-8
|
||||
struct Point
|
||||
{
|
||||
double x,y;
|
||||
void input(){scanf("%lf%lf",&x,&y);}
|
||||
void output(){printf("%.2lf %.2lf\n",x,y);}
|
||||
}A[N],B[2],PA,PB;
|
||||
double cosa,sina,k;
|
||||
double length(Point P)
|
||||
{
|
||||
return sqrt(P.x*P.x+P.y*P.y);
|
||||
}
|
||||
void rotate(Point &P)
|
||||
{
|
||||
double t=P.x;
|
||||
P.x=P.x*cosa-P.y*sina;
|
||||
P.y=P.y*cosa+t*sina;
|
||||
}
|
||||
void expand(Point &P)
|
||||
{
|
||||
P.x*=k;
|
||||
P.y*=k;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n,i,t,cnt=0;
|
||||
double a;
|
||||
scanf("%d",&t);
|
||||
while(t--)
|
||||
{
|
||||
scanf("%d",&n);
|
||||
for(i=0;i<n;++i)
|
||||
A[i].input();
|
||||
for(i=0;i<2;++i)
|
||||
B[i].input();
|
||||
PA.x=A[1].x-A[0].x;
|
||||
PA.y=A[1].y-A[0].y;
|
||||
PB.x=B[1].x-B[0].x;
|
||||
PB.y=B[1].y-B[0].y;
|
||||
a=acos((PA.x*PB.x+PA.y*PB.y)/(length(PA)*length(PB)));
|
||||
if(PA.x*PB.y-PA.y*PB.x<EPS)
|
||||
a = 2.0 * PI - a;
|
||||
cosa=cos(a);
|
||||
sina=sin(a);
|
||||
k=length(PB)/length(PA);
|
||||
for(i=2;i<n;++i)
|
||||
{
|
||||
A[i].x-=A[0].x;
|
||||
A[i].y-=A[0].y;
|
||||
rotate(A[i]);
|
||||
expand(A[i]);
|
||||
A[i].x+=B[0].x;
|
||||
A[i].y+=B[0].y;
|
||||
}
|
||||
printf("Case %d:\n",++cnt);
|
||||
B[0].output();
|
||||
B[1].output();
|
||||
for(i=2;i<n;++i)
|
||||
A[i].output();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
const int maxn=1100;
|
||||
struct route{
|
||||
double p,q;
|
||||
friend bool operator < (route a,route b){
|
||||
return a.p/a.q>b.p/b.q;
|
||||
}
|
||||
}r[maxn];
|
||||
int n,m;
|
||||
double dp[maxn][20];
|
||||
void input(){
|
||||
scanf("%d%d",&n,&m);
|
||||
for(int i=0;i<n;i++){
|
||||
scanf("%lf%lf",&r[i].p,&r[i].q);
|
||||
}
|
||||
sort(r,r+n);
|
||||
for(int i=0;i<=n;i++){
|
||||
for(int j=0;j<=m;j++){
|
||||
dp[i][j]=0;
|
||||
}
|
||||
}
|
||||
}
|
||||
double solve(){
|
||||
double ans=0;
|
||||
dp[0][m]=1;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=m;j>=0;j--){
|
||||
ans+=dp[i][j]*r[i].p;
|
||||
if(j-1>=0) dp[i+1][j-1]+=dp[i][j]*r[i].q;
|
||||
dp[i+1][j]+=dp[i][j]*(1.0-r[i].p-r[i].q);
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
int main(){
|
||||
int t;
|
||||
scanf("%d",&t);
|
||||
for(int i=1;i<=t;i++){
|
||||
input();
|
||||
printf("Case %d: %.5lf\n",i,solve());
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
#include<iostream>
|
||||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<algorithm>
|
||||
const int N=10010;
|
||||
using namespace std;
|
||||
int n,m;
|
||||
int parent[N];
|
||||
int visited[N];
|
||||
struct Edge {
|
||||
int u,v,w;
|
||||
bool operator < (const Edge &p) const {
|
||||
return p.w<w;
|
||||
}
|
||||
}edge[N*10];
|
||||
void UFset(){
|
||||
for(int i=0;i<n;i++)
|
||||
parent[i]=-1;
|
||||
}
|
||||
int Find(int x){
|
||||
int s;
|
||||
for(s=x;parent[s]>=0;s=parent[s]);
|
||||
while(s!=x){
|
||||
int temp=parent[x];
|
||||
parent[x]=s;
|
||||
x=temp;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
void Union(int R1,int R2){
|
||||
int r1=Find(R1);
|
||||
int r2=Find(R2);
|
||||
int temp=parent[r1]+parent[r2];
|
||||
if(parent[r1]>parent[r2]){
|
||||
parent[r1]=r2;
|
||||
parent[r2]=temp;
|
||||
}else {
|
||||
parent[r2]=r1;
|
||||
parent[r1]=temp;
|
||||
}
|
||||
}
|
||||
int main(){
|
||||
while(scanf("%d%d",&n,&m)!=EOF){
|
||||
if(n==0&&m==0)break;
|
||||
memset(visited,0,sizeof(visited));
|
||||
int u,v,ans=0;
|
||||
for(int i=0;i<m;i++){
|
||||
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
|
||||
}
|
||||
sort(edge,edge+m);
|
||||
UFset();
|
||||
for(int i=0;i<m;i++){
|
||||
u=Find(edge[i].u);
|
||||
v=Find(edge[i].v);
|
||||
if(u!=v){
|
||||
if(visited[u]&&visited[v])continue;
|
||||
if(visited[u]||visited[v])
|
||||
visited[u]=visited[v]=1;
|
||||
ans+=edge[i].w;
|
||||
Union(u,v);
|
||||
}else if(!visited[u]||!visited[v]){
|
||||
Union(u,v);
|
||||
visited[u]=visited[v]=1;
|
||||
ans+=edge[i].w;
|
||||
}
|
||||
}
|
||||
printf("%d\n",ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,252 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
const int maxn = 10;
|
||||
char mat[ maxn ][ maxn ];
|
||||
const int dx[]={-1,1,-1,1};
|
||||
const int dy[]={1,-1,-1,1};
|
||||
int max( int a,int b ){
|
||||
return a>b?a:b;
|
||||
}
|
||||
bool in( int x,int y ){
|
||||
if( x>=1&&x<=8&&y>=1&&y<=8 ) return true;
|
||||
else return false;
|
||||
}
|
||||
int solve( int x,int y ){
|
||||
int ans = 0;
|
||||
int pos;
|
||||
pos = -1;
|
||||
for( int i=y+1;i<=8;i++ ){
|
||||
if( mat[x][i]=='D' ){
|
||||
pos = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( pos!=-1 ){
|
||||
for( int i=y+1;i<=pos;i++ ){
|
||||
if( mat[x][i]=='*' ){
|
||||
pos = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if( pos!=-1 ){
|
||||
for( int i=y+1;i<=pos;i++ ){
|
||||
if( mat[x][i]=='L' )
|
||||
ans++;
|
||||
}
|
||||
}
|
||||
pos = -1;
|
||||
for( int i=y-1;i>=1;i-- ){
|
||||
if( mat[x][i]=='D' ){
|
||||
pos = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( pos!=-1 ){
|
||||
for( int i=y-1;i>=pos;i-- ){
|
||||
if( mat[x][i]=='*' ){
|
||||
pos = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if( pos!=-1 ){
|
||||
for( int i=y-1;i>=pos;i-- ){
|
||||
if( mat[x][i]=='L' )
|
||||
ans++;
|
||||
}
|
||||
}
|
||||
pos = -1;
|
||||
for( int i=x+1;i<=8;i++ ){
|
||||
if( mat[i][y]=='D' ){
|
||||
pos = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( pos!=-1 ){
|
||||
for( int i=x+1;i<=pos;i++ ){
|
||||
if( mat[i][y]=='*' ){
|
||||
pos = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if( pos!=-1 ){
|
||||
for( int i=x+1;i<=pos;i++ ){
|
||||
if( mat[i][y]=='L' )
|
||||
ans++;
|
||||
}
|
||||
}
|
||||
pos = -1;
|
||||
for( int i=x-1;i>=1;i-- ){
|
||||
if( mat[i][y]=='D' ){
|
||||
pos = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( pos!=-1 ){
|
||||
for( int i=x-1;i>=pos;i-- ){
|
||||
if( mat[i][y]=='*' ){
|
||||
pos = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if( pos!=-1 ){
|
||||
for( int i=x-1;i>=pos;i-- ){
|
||||
if( mat[i][y]=='L' )
|
||||
ans++;
|
||||
}
|
||||
}
|
||||
int posx,posy;
|
||||
int K;
|
||||
posx = -1;
|
||||
for( int k=1;k<=8;k++ ){
|
||||
int tx = x+k*dx[0];
|
||||
int ty = y+k*dy[0];
|
||||
if( in(tx,ty)==false ) break;
|
||||
if( mat[tx][ty]=='D' ){
|
||||
posx = tx;
|
||||
posy = ty;
|
||||
K = k;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( posx!=-1 ){
|
||||
for( int k=1;k<=K;k++ ){
|
||||
int tx = x+k*dx[0];
|
||||
int ty = y+k*dy[0];
|
||||
if( in(tx,ty)==false ) break;
|
||||
if( mat[tx][ty]=='*' ){
|
||||
posx = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if( posx!=-1 ){
|
||||
for( int k=1;k<=K;k++ ){
|
||||
int tx = x+k*dx[0];
|
||||
int ty = y+k*dy[0];
|
||||
if( in(tx,ty)==false ) break;
|
||||
if( mat[tx][ty]=='L' )
|
||||
ans++;
|
||||
}
|
||||
}
|
||||
posx = -1;
|
||||
for( int k=1;k<=8;k++ ){
|
||||
int tx = x+k*dx[1];
|
||||
int ty = y+k*dy[1];
|
||||
if( in(tx,ty)==false ) break;
|
||||
if( mat[tx][ty]=='D' ){
|
||||
posx = tx;
|
||||
posy = ty;
|
||||
K = k;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( posx!=-1 ){
|
||||
for( int k=1;k<=K;k++ ){
|
||||
int tx = x+k*dx[1];
|
||||
int ty = y+k*dy[1];
|
||||
if( in(tx,ty)==false ) break;
|
||||
if( mat[tx][ty]=='*' ){
|
||||
posx = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if( posx!=-1 ){
|
||||
for( int k=1;k<=K;k++ ){
|
||||
int tx = x+k*dx[1];
|
||||
int ty = y+k*dy[1];
|
||||
if( in(tx,ty)==false ) break;
|
||||
if( mat[tx][ty]=='L' )
|
||||
ans++;
|
||||
}
|
||||
}
|
||||
posx = -1;
|
||||
for( int k=1;k<=8;k++ ){
|
||||
int tx = x+k*dx[2];
|
||||
int ty = y+k*dy[2];
|
||||
if( in(tx,ty)==false ) break;
|
||||
if( mat[tx][ty]=='D' ){
|
||||
posx = tx;
|
||||
posy = ty;
|
||||
K = k;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( posx!=-1 ){
|
||||
for( int k=1;k<=K;k++ ){
|
||||
int tx = x+k*dx[2];
|
||||
int ty = y+k*dy[2];
|
||||
if( in(tx,ty)==false ) break;
|
||||
if( mat[tx][ty]=='*' ){
|
||||
posx = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if( posx!=-1 ){
|
||||
for( int k=1;k<=K;k++ ){
|
||||
int tx = x+k*dx[2];
|
||||
int ty = y+k*dy[2];
|
||||
if( in(tx,ty)==false ) break;
|
||||
if( mat[tx][ty]=='L' )
|
||||
ans++;
|
||||
}
|
||||
}
|
||||
posx = -1;
|
||||
for( int k=1;k<=8;k++ ){
|
||||
int tx = x+k*dx[3];
|
||||
int ty = y+k*dy[3];
|
||||
if( in(tx,ty)==false ) break;
|
||||
if( mat[tx][ty]=='D' ){
|
||||
posx = tx;
|
||||
posy = ty;
|
||||
K = k;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( posx!=-1 ){
|
||||
for( int k=1;k<=K;k++ ){
|
||||
int tx = x+k*dx[3];
|
||||
int ty = y+k*dy[3];
|
||||
if( in(tx,ty)==false ) break;
|
||||
if( mat[tx][ty]=='*' ){
|
||||
posx = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if( posx!=-1 ){
|
||||
for( int k=1;k<=K;k++ ){
|
||||
int tx = x+k*dx[3];
|
||||
int ty = y+k*dy[3];
|
||||
if( in(tx,ty)==false ) break;
|
||||
if( mat[tx][ty]=='L' )
|
||||
ans++;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
int main(){
|
||||
int T;
|
||||
scanf("%d",&T);
|
||||
int ca = 1;
|
||||
while( T-- ){
|
||||
for( int i=1;i<=8;i++ ){
|
||||
scanf("%s",mat[i]+1);
|
||||
}
|
||||
int ans = 0;
|
||||
for( int i=1;i<=8;i++ ){
|
||||
for( int j=1;j<=8;j++ ){
|
||||
if( mat[i][j]=='*' ){
|
||||
ans = max( ans,solve( i,j ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("Case %d: %d\n",ca++,ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,180 @@
|
|||
#include <utility>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
#include <queue>
|
||||
#include <bitset>
|
||||
#include <map>
|
||||
#include <iterator>
|
||||
using namespace std;
|
||||
#define clr(a,v) memset(a,v,sizeof(a))
|
||||
#define lson l,m,rt<<1
|
||||
#define rson m+1,r,rt<<1|1
|
||||
const int INF = 0x7f7f7f7f;
|
||||
const int maxn = 211111;
|
||||
const double pi = acos(-1.0);
|
||||
const double eps = 1e-10;
|
||||
const int mod = 1000000007;
|
||||
typedef long long LL;
|
||||
typedef pair<int, int> pii;
|
||||
typedef vector<LL> VI;
|
||||
typedef vector<VI> VVI;
|
||||
typedef vector<VVI> VVVI;
|
||||
VVI E, I, A;
|
||||
int size;
|
||||
LL f[20][20];
|
||||
LL P[20];
|
||||
LL num[20][20];
|
||||
LL arr[20];
|
||||
int convert(char *s) {
|
||||
if (strcmp(s, "Monday") == 0)
|
||||
return 1;
|
||||
if (strcmp(s, "Tuesday") == 0)
|
||||
return 2;
|
||||
if (strcmp(s, "Wednesday") == 0)
|
||||
return 3;
|
||||
if (strcmp(s, "Thursday") == 0)
|
||||
return 4;
|
||||
if (strcmp(s, "Friday") == 0)
|
||||
return 5;
|
||||
if (strcmp(s, "Saturday") == 0)
|
||||
return 6;
|
||||
if (strcmp(s, "Sunday") == 0)
|
||||
return 7;
|
||||
return -1;
|
||||
}
|
||||
LL POW(LL a, LL b) {
|
||||
LL res = 1;
|
||||
while (b) {
|
||||
if (b & 1)
|
||||
res = res * a % mod;
|
||||
a = a * a % mod;
|
||||
b >>= 1;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
VVI multi(const VVI& A, const VVI& B) {
|
||||
VVI res = VVI(size, VI(size, 0));
|
||||
int i, j, k;
|
||||
for (i = 0; i < size; ++i) {
|
||||
for (j = 0; j < size; ++j) {
|
||||
for (k = 0; k < size; ++k) {
|
||||
res[i][j] += A[i][k] * B[k][j];
|
||||
if (res[i][j] >= mod)
|
||||
res[i][j] %= mod;
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
VVI cal(const VVI& A, LL k) {
|
||||
VVI res = E, t = A;
|
||||
while (k) {
|
||||
if (k & 1)
|
||||
res = multi(res, t);
|
||||
t = multi(t, t);
|
||||
k >>= 1;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
void output(const VVI &arr) {
|
||||
for (int i = 0; i < size; ++i) {
|
||||
cout.width(5);
|
||||
for (int j = 0; j < size; ++j) {
|
||||
cout << arr[i][j] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
LL go(int n, int k, int s) {
|
||||
size = k + 2;
|
||||
int i, j, t;
|
||||
int a = (7 - s % 7) % 7, b = (8 - s % 7) % 7;
|
||||
LL ans = 0;
|
||||
if (n <= 7) {
|
||||
for (i = 1; i <= n; ++i) {
|
||||
t = i % 7;
|
||||
if (t == a || t == b)
|
||||
continue;
|
||||
ans += POW(i, k);
|
||||
ans = ans >= mod ? ans % mod : ans;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
E = VVI(size, VI(size, 0));
|
||||
for (i = 0; i < size; ++i)
|
||||
E[i][i] = 1;
|
||||
I = VVI(size, VI(size, 0));
|
||||
I[0][0] = 1;
|
||||
for (i = 1; i < size; ++i)
|
||||
I[0][i] = f[k][i - 1] * P[i - 1] % mod;
|
||||
for (i = 1; i < size; ++i) {
|
||||
for (j = i; j < size; ++j) {
|
||||
I[i][j] = f[k + 1 - i][j - i] * P[j - i] % mod;
|
||||
}
|
||||
}
|
||||
LL p = n / 7, tmp;
|
||||
VVI res = E, ta, tb;
|
||||
int r = n % 7;
|
||||
ta = cal(I, p - 1);
|
||||
tb = multi(ta, I);
|
||||
for (i = 1; i <= 7; ++i) {
|
||||
if (i % 7 == a || i % 7 == b)
|
||||
continue;
|
||||
arr[0] = num[i][k];
|
||||
tmp = 0;
|
||||
for (j = 1; j < size; ++j)
|
||||
arr[j] = num[i][k + 1 - j];
|
||||
if (i <= r) {
|
||||
for (j = 0; j < size; ++j) {
|
||||
tmp += tb[0][j] * arr[j];
|
||||
tmp %= mod;
|
||||
}
|
||||
} else {
|
||||
for (j = 0; j < size; ++j) {
|
||||
tmp += ta[0][j] * arr[j];
|
||||
tmp %= mod;
|
||||
}
|
||||
}
|
||||
ans += tmp;
|
||||
ans %= mod;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
int main() {
|
||||
ios::sync_with_stdio(false);
|
||||
char s[14];
|
||||
int n, k, i, j;
|
||||
P[0] = 1;
|
||||
for (i = 1; i < 20; ++i)
|
||||
P[i] = P[i - 1] * 7 % mod;
|
||||
for (i = 0; i < 20; ++i)
|
||||
f[i][0] = f[i][i] = 1;
|
||||
for (i = 1; i < 20; ++i) {
|
||||
for (j = 1; j < i; ++j) {
|
||||
f[i][j] = f[i - 1][j - 1] + f[i - 1][j];
|
||||
f[i][j] %= mod;
|
||||
}
|
||||
}
|
||||
for (i = 0; i < 20; ++i)
|
||||
num[i][0] = 1;
|
||||
for (i = 1; i < 20; ++i) {
|
||||
for (j = 1; j < 20; ++j) {
|
||||
num[i][j] = num[i][j - 1] * i % mod;
|
||||
}
|
||||
}
|
||||
int T, cas;
|
||||
scanf("%d", &T);
|
||||
for (cas = 1; cas <= T; ++cas) {
|
||||
scanf("%s", s);
|
||||
scanf("%d%d", &n, &k);
|
||||
printf("Case %d: %I64d\n", cas, go(n, k, convert(s)));
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#define inf 99999999
|
||||
int flag[500];
|
||||
int D[500];
|
||||
int map[505][505];
|
||||
int n,i,j;
|
||||
int prime()
|
||||
{
|
||||
int v,k;
|
||||
int ret=0,mi;
|
||||
for(i=1;i<=n;i++){
|
||||
flag[i]=0;
|
||||
D[i]=inf;
|
||||
}D[1]=0;flag[1]=1;v=1;
|
||||
for(k=1;k<n;k++){
|
||||
for(i=1;i<=n;i++)if(!flag[i]){
|
||||
if(map[v][i]<D[i])
|
||||
D[i]=map[v][i];
|
||||
}
|
||||
mi=inf;
|
||||
for(i=1;i<=n;i++)
|
||||
if(!flag[i]&&D[i]<mi)
|
||||
mi=D[v=i];
|
||||
flag[v]=1;
|
||||
ret+=mi;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
int num[505];
|
||||
int main()
|
||||
{
|
||||
int t,m,k,x,i,j;
|
||||
scanf("%d",&t);
|
||||
while(t--)
|
||||
{
|
||||
int a,b,w;
|
||||
scanf("%d%d%d",&n,&m,&k);
|
||||
for(i=1;i<=n;i++)
|
||||
{
|
||||
map[i][i]=0;
|
||||
for(j=i+1;j<=n;j++)
|
||||
map[i][j]=map[j][i]=inf;
|
||||
}
|
||||
for(i=0;i<m;i++)
|
||||
{
|
||||
scanf("%d%d%d",&a,&b,&w);
|
||||
if(w<map[a][b])
|
||||
map[a][b]=map[b][a]=w;
|
||||
}
|
||||
for(i=0;i<k;i++)
|
||||
{
|
||||
scanf("%d",&m);
|
||||
for(j=0;j<m;j++)
|
||||
scanf("%d",&num[j]);
|
||||
for(j=0;j<m;j++)
|
||||
for(x=j+1;x<m;x++)
|
||||
map[num[j]][num[x]]=map[num[x]][num[j]]=0;
|
||||
}
|
||||
int ans=prime();
|
||||
if(ans>=inf)
|
||||
printf("-1\n");
|
||||
else printf("%d\n",ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,164 @@
|
|||
#include<iostream>
|
||||
#include<cmath>
|
||||
#include<complex>
|
||||
#include<algorithm>
|
||||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<cstdlib>
|
||||
#include<cassert>
|
||||
#define MAXN 20
|
||||
using namespace std;
|
||||
typedef complex<double> point;
|
||||
const double eps = 1e-8;
|
||||
const double pi = acos( -1.0 );
|
||||
double operator^( point p1, point p2 ){ return imag( conj( p1 ) * p2 ); }
|
||||
int dblcmp( double x ){ return ( x < -eps ? -1 : x > eps ); }
|
||||
bool com( point p1, point p2 )
|
||||
{
|
||||
return dblcmp( p1.real( ) - p2.real( ) ) < 0 ||
|
||||
dblcmp( p1.real( ) - p2.real( ) ) == 0 && dblcmp( p1.imag( ) - p2.imag( ) ) < 0;
|
||||
}
|
||||
point p[MAXN],np[MAXN],out[MAXN];
|
||||
int N,Ns;
|
||||
int num1[MAXN],num2[MAXN];
|
||||
int compute( int n, int q )
|
||||
{
|
||||
int j,i=0,k=0;
|
||||
for( j=0; j<n; j++ )
|
||||
{
|
||||
if( q & 1 ) num1[i++] = j;
|
||||
else num2[k++] = j;
|
||||
q >>= 1;
|
||||
}
|
||||
return i; //杩烘ュ涓
|
||||
}
|
||||
void convex_hull( int n )
|
||||
{
|
||||
int i,k;
|
||||
if( n<=2 )
|
||||
{
|
||||
for( i=0; i<n; i++ )
|
||||
np[i] = p[i];
|
||||
Ns = n;
|
||||
return ;
|
||||
}
|
||||
sort( p, p+n, com );
|
||||
Ns = 0;
|
||||
for( i=0; i<n; i++ )
|
||||
{
|
||||
while( Ns >= 2 && dblcmp( np[Ns-1] - np[Ns-2] ^ p[i] - np[Ns-1] ) <= 0 )
|
||||
Ns--;
|
||||
np[Ns++] = p[i];
|
||||
}
|
||||
k = Ns;
|
||||
for( i = n-2; i >= 0; i-- )
|
||||
{
|
||||
while( Ns > k && dblcmp( np[Ns-1] - np[Ns-2] ^ p[i] - np[Ns-2] ) <= 0 )
|
||||
Ns--;
|
||||
np[Ns++] = p[i];
|
||||
}
|
||||
Ns--;
|
||||
}
|
||||
double disptoseg( point p0, point l1, point l2 )
|
||||
{
|
||||
point t = p0;
|
||||
t.real( l1.imag( ) - l2.imag( ) );
|
||||
t.imag( l2.real( ) - l1.real( ) );
|
||||
if( dblcmp( ( l1 - p0 ^ t - p0 ) * ( l2 - p0 ^ t - p0 ) ) > 0 )
|
||||
return abs( p0 - l1 ) < abs( p0 - l2 ) ? abs( p0 - l1 ) : abs( p0 - l2 );
|
||||
return fabs( p0 - l1 ^ p0 - l2 ) / abs( l1 - l2 );
|
||||
}
|
||||
bool point_in_polygon( const point &p0, const double &area2 )
|
||||
{
|
||||
int i;
|
||||
double area1 = 0.0;
|
||||
if( Ns == 2 )
|
||||
return dblcmp( real( conj( np[0] - p0 ) * ( p0 - np[1] ) ) ) >= 0 ;
|
||||
for( i = 0; i < Ns; i++ )
|
||||
{
|
||||
area1 += fabs( np[i] - p0 ^ np[i+1] - p0 );
|
||||
}
|
||||
if( area1 - fabs( area2 ) > eps ) return false;
|
||||
return true;
|
||||
}
|
||||
bool touch( int n, const double &d )
|
||||
{
|
||||
int i,j;
|
||||
double len;
|
||||
for( i = 0; i < n; i++ )
|
||||
{
|
||||
for( j = 0; j < Ns; j++ )
|
||||
{
|
||||
len = disptoseg( out[i], np[j], np[j+1] );
|
||||
if( len - d < eps )return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
int main( )
|
||||
{
|
||||
point pp[MAXN];
|
||||
double d,r,L,carea,maxarea,area,len,x,y,C;
|
||||
int i,j,max,n,t,k,q;
|
||||
scanf("%d",&t);
|
||||
while( t-- )
|
||||
{
|
||||
scanf("%d%lf%lf",&n,&d,&L);
|
||||
for( i=0; i<n; i++ )
|
||||
{
|
||||
scanf("%lf%lf",&x,&y);
|
||||
pp[i].real( x );
|
||||
pp[i].imag( y );
|
||||
p[i] = pp[i];
|
||||
}
|
||||
r = d / 2.0;
|
||||
C = pi * d;
|
||||
carea = pi * r * r ;
|
||||
convex_hull( n );
|
||||
len = 0.0;
|
||||
np[Ns] = np[0];
|
||||
for( j = 0; j < Ns; j++ )
|
||||
len += abs( np[j] - np[j+1] );
|
||||
if( L - ( len + C ) > -eps )
|
||||
{
|
||||
area = 0.0;
|
||||
for( j = 0; j < Ns; j++ )
|
||||
area += np[j] ^ np[j+1];
|
||||
area = fabs( area ) / 2.0 + len * r - ( n - 1 ) * carea;
|
||||
if( area > 0 ) maxarea = area;
|
||||
}
|
||||
else
|
||||
{
|
||||
max = ( 1 << n );
|
||||
maxarea = 0.0;
|
||||
for( i = max-1; i > 0; i-- )
|
||||
{
|
||||
k = compute( n, i );
|
||||
q = n - k;
|
||||
for( j = 0; j < k; j++ )
|
||||
p[j] = pp[ num1[j] ];
|
||||
for( j = 0; j < q; j++ )
|
||||
out[j] = pp[ num2[j] ];
|
||||
convex_hull( k );
|
||||
len = 0.0;
|
||||
np[Ns] = np[0];
|
||||
for( j = 0; j < Ns; j++ )
|
||||
len += abs( np[j] - np[j+1] );
|
||||
if( L - ( len + C ) > -eps )
|
||||
{
|
||||
area = 0.0;
|
||||
for( j = 0; j < Ns; j++ )
|
||||
area += np[j] ^ np[j+1];
|
||||
k = 0;
|
||||
for( j = 0; j < n; j++ )
|
||||
if( point_in_polygon( pp[j], area ) )
|
||||
k++;
|
||||
area = fabs( area ) / 2.0 + len * r - ( k - 1 ) * carea;
|
||||
if( area > maxarea && !touch( q, d ) ) maxarea = area;
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("%.3lf\n",maxarea);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,183 @@
|
|||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
#define maxn 200010
|
||||
#define eps 1e-6
|
||||
#define maxaxis 1000000000
|
||||
#define ll __int64
|
||||
typedef int Type;
|
||||
Type bin[maxn];
|
||||
int size, tot;
|
||||
int B(Type val) {
|
||||
int l = 0;
|
||||
int r = tot-1;
|
||||
while(l <= r) {
|
||||
int m = (l + r) >> 1;
|
||||
if(bin[m] == val)
|
||||
return m;
|
||||
if(val > bin[m]) {
|
||||
l = m + 1;
|
||||
}else
|
||||
r = m - 1;
|
||||
}
|
||||
}
|
||||
struct point {
|
||||
int x, y;
|
||||
int _ix, _iy;
|
||||
}P[maxn];
|
||||
struct Tree {
|
||||
int son[4];
|
||||
Type Sum;
|
||||
void clear() {
|
||||
Sum = 0;
|
||||
for(int i = 0; i < 4; i++)
|
||||
son[i] = -1;
|
||||
}
|
||||
}T[maxn*6];
|
||||
int root, all;
|
||||
void Insert(int& root, int x, int y, int lx, int rx, int ly, int ry) {
|
||||
if(x > rx || x < lx || y > ry || y < ly)
|
||||
return ;
|
||||
if(root == -1) {
|
||||
root = all++;
|
||||
T[root].clear();
|
||||
}
|
||||
if(lx == rx && x == lx && y == ly && ly == ry) {
|
||||
T[root].Sum ++;
|
||||
return ;
|
||||
}
|
||||
int midx0 = (lx + rx) >> 1;
|
||||
int midy0 = (ly + ry) >> 1;
|
||||
int midx1 = (lx==rx) ? midx0 : midx0 + 1;
|
||||
int midy1 = (ly==ry) ? midy0 : midy0 + 1;
|
||||
if(lx == rx) {
|
||||
Insert(T[root].son[0], x, y, lx, midx0, ly, midy0);
|
||||
Insert(T[root].son[1], x, y, lx, midx0, midy1, ry);
|
||||
}else if(ly == ry) {
|
||||
Insert(T[root].son[1], x, y, lx, midx0, midy1, ry);
|
||||
Insert(T[root].son[2], x, y, midx1, rx, ly, midy0);
|
||||
}else {
|
||||
Insert(T[root].son[0], x, y, lx, midx0, ly, midy0);
|
||||
Insert(T[root].son[1], x, y, lx, midx0, midy1, ry);
|
||||
Insert(T[root].son[2], x, y, midx1, rx, ly, midy0);
|
||||
Insert(T[root].son[3], x, y, midx1, rx, midy1, ry);
|
||||
}
|
||||
int i;
|
||||
T[root].Sum = 0;
|
||||
for(i = 0; i < 4; i++) {
|
||||
if(T[root].son[i] != -1) {
|
||||
T[root].Sum += T[ T[root].son[i] ].Sum;
|
||||
}
|
||||
}
|
||||
}
|
||||
void Query(int root, int x0, int x1, int y0, int y1,
|
||||
int lx, int rx, int ly, int ry, Type& Num) {
|
||||
if(x0 > rx || x1 < lx || y0 > ry || y1 < ly || root == -1)
|
||||
return ;
|
||||
if(Num > 1)
|
||||
return ;
|
||||
if(x0 <= lx && rx <= x1 && y0 <= ly && ry <= y1) {
|
||||
Num += T[root].Sum;
|
||||
return ;
|
||||
}
|
||||
int midx0 = (lx + rx) >> 1;
|
||||
int midy0 = (ly + ry) >> 1;
|
||||
int midx1 = (lx==rx) ? midx0 : midx0 + 1;
|
||||
int midy1 = (ly==ry) ? midy0 : midy0 + 1;
|
||||
if(lx == rx) {
|
||||
Query(T[root].son[0], x0, x1, y0, y1, lx, midx0, ly, midy0, Num);
|
||||
Query(T[root].son[1], x0, x1, y0, y1, lx, midx0, midy1, ry, Num);
|
||||
}else if(ly == ry) {
|
||||
Query(T[root].son[1], x0, x1, y0, y1, lx, midx0, midy1, ry, Num);
|
||||
Query(T[root].son[2], x0, x1, y0, y1, midx1, rx, ly, midy0, Num);
|
||||
}else {
|
||||
Query(T[root].son[0], x0, x1, y0, y1, lx, midx0, ly, midy0, Num);
|
||||
Query(T[root].son[1], x0, x1, y0, y1, lx, midx0, midy1, ry, Num);
|
||||
Query(T[root].son[2], x0, x1, y0, y1, midx1, rx, ly, midy0, Num);
|
||||
Query(T[root].son[3], x0, x1, y0, y1, midx1, rx, midy1, ry, Num);
|
||||
}
|
||||
}
|
||||
int LargeThan(int val, int pre) {
|
||||
int l = 0;
|
||||
int r = pre;
|
||||
int ans = l;
|
||||
while(l <= r) {
|
||||
int m = (l + r) >> 1;
|
||||
if(val <= bin[m]) {
|
||||
r = m - 1;
|
||||
ans = m;
|
||||
}else
|
||||
l = m + 1;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
int LessThan(int val, int pre) {
|
||||
int l = pre;
|
||||
int r = tot - 1;
|
||||
int ans = r;
|
||||
while(l <= r) {
|
||||
int m = (l + r) >> 1;
|
||||
if(val >= bin[m]) {
|
||||
l = m + 1;
|
||||
ans = m;
|
||||
}else
|
||||
r = m - 1;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
int Process(int idx, int dis) {
|
||||
int xleft = LargeThan( P[idx].x - dis, P[idx]._ix );
|
||||
int yleft = LargeThan( P[idx].y - dis, P[idx]._iy );
|
||||
int xright = LessThan( P[idx].x + dis, P[idx]._ix );
|
||||
int yright = LessThan( P[idx].y + dis, P[idx]._iy );
|
||||
int Num = 0;
|
||||
Query(root, xleft, xright, yleft, yright, 0, tot-1, 0, tot-1, Num);
|
||||
return Num;
|
||||
}
|
||||
int n;
|
||||
int main() {
|
||||
int i;
|
||||
int Max;
|
||||
while(scanf("%d", &n) != EOF) {
|
||||
size = 0;
|
||||
tot = 0;
|
||||
all = 0;
|
||||
Max = 0;
|
||||
for(i = 0; i < n; i++) {
|
||||
scanf("%d %d", &P[i].x, &P[i].y);
|
||||
bin[ size++ ] = P[i].x;
|
||||
bin[ size++ ] = P[i].y;
|
||||
if(P[i].x > Max) Max = P[i].x;
|
||||
if(P[i].y > Max) Max = P[i].y;
|
||||
}
|
||||
sort(bin, bin + size);
|
||||
for(i = 0; i < size; i++) {
|
||||
if(i == 0 || bin[i] != bin[i-1])
|
||||
bin[tot++] = bin[i];
|
||||
}
|
||||
root = -1;
|
||||
for(i = 0; i < n; i++) {
|
||||
P[i]._ix = B(P[i].x);
|
||||
P[i]._iy = B(P[i].y);
|
||||
Insert(root, P[i]._ix, P[i]._iy, 0, tot-1, 0, tot-1);
|
||||
}
|
||||
ll ans = 0;
|
||||
for(i = 0; i < n; i++) {
|
||||
int l = 0;
|
||||
int r = Max;
|
||||
int dis = 0;
|
||||
while(l <= r) {
|
||||
int m = (l + r) >> 1;
|
||||
if(Process(i, m) > 1) {
|
||||
r = m - 1;
|
||||
dis = m;
|
||||
}else
|
||||
l = m + 1;
|
||||
}
|
||||
ans += dis;
|
||||
}
|
||||
printf("%I64d\n", ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
# include<stdio.h>
|
||||
# include<string.h>
|
||||
# include<stdlib.h>
|
||||
# define N 1000005
|
||||
char s1[2*N],s2[2*N],s[N],ch1[N],ch2[N];;
|
||||
int next[N];
|
||||
void Gnext(char str[])
|
||||
{
|
||||
int i,j,len=strlen(str);
|
||||
next[0]=0;
|
||||
j=0;
|
||||
for(i=1;i<len;i++)
|
||||
{
|
||||
while(j>0 && str[j]!=str[i]) j=next[j-1];
|
||||
if(str[i]==str[j]) j++;
|
||||
next[i]=j;
|
||||
}
|
||||
}
|
||||
int KMP(char *a,char *b)
|
||||
{
|
||||
int i,j,lena,lenb,count;
|
||||
j=0;
|
||||
lena=strlen(a);
|
||||
lenb=strlen(b);
|
||||
count=0;
|
||||
for(i=0;i<lenb;i++)
|
||||
{
|
||||
while(j>0 && a[j]!=b[i]) j=next[j-1];
|
||||
if(a[j]==b[i]) j++;
|
||||
if(j==lena) {j=next[j-1];count++;}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i,j,len1,len2,len,k,minnum,maxnum,count1,count2;
|
||||
while(scanf("%s",s)!=EOF)
|
||||
{
|
||||
len=strlen(s);
|
||||
strcpy(s1,s);
|
||||
strcat(s1,s);
|
||||
strcpy(s2,s1);
|
||||
i=0;j=1;
|
||||
while(i<=len && j<=len)
|
||||
{
|
||||
for(k=0;k<len;k++)
|
||||
if(s1[i+k]!=s2[j+k]) break;
|
||||
if(k==len) break;
|
||||
if(s1[i+k]>s2[j+k]) i=i+k+1;
|
||||
else j=j+k+1;
|
||||
if(i==j) j++;
|
||||
}
|
||||
if(j<i) i=j;
|
||||
minnum=i+1;
|
||||
for(j=0;j<len;j++)
|
||||
ch1[j]=s1[i+j];
|
||||
ch1[j]=0;
|
||||
i=0;j=1;
|
||||
while(i<=len && j<=len)
|
||||
{
|
||||
for(k=0;k<len;k++)
|
||||
if(s1[i+k]!=s2[j+k]) break;
|
||||
if(k==len) break;
|
||||
if(s1[i+k]<s2[j+k]) i=i+k+1;
|
||||
else j=j+k+1;
|
||||
if(i==j) j++;
|
||||
}
|
||||
if(j<i) j=i;
|
||||
maxnum=i+1;
|
||||
for(j=0;j<len;j++)
|
||||
ch2[j]=s1[i+j];
|
||||
ch2[j]=0;
|
||||
Gnext(ch1);
|
||||
count1=KMP(ch1,s1);
|
||||
if(minnum==1) count1--;
|
||||
Gnext(ch2);
|
||||
count2=KMP(ch2,s1);
|
||||
if(maxnum==1) count2--;
|
||||
printf("%d %d %d %d\n",minnum,count1,maxnum,count2);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,143 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<queue>
|
||||
using namespace std;
|
||||
#define max 510
|
||||
int mo[4][2]={ {-1,0}, {0,1}, {1,0}, {0,-1} };
|
||||
struct node{
|
||||
int ge[4];
|
||||
int vis[4];
|
||||
int fuhao;
|
||||
}map[max][max];
|
||||
struct data{
|
||||
int x,y,g;
|
||||
}d[max][max][4];
|
||||
int m,n,color,white;
|
||||
void bfs(int x,int y,int r)
|
||||
{
|
||||
queue <data> q;
|
||||
int i,j,k;
|
||||
if(map[x][y].vis[r])
|
||||
return;
|
||||
data t;
|
||||
t.x=x;t.y=y;t.g=r;
|
||||
q.push(t);
|
||||
map[x][y].vis[r]=1;
|
||||
map[x][y].ge[r]=color;
|
||||
while(!q.empty())
|
||||
{
|
||||
data p;
|
||||
p=q.front();
|
||||
q.pop();
|
||||
i=p.x;
|
||||
j=p.y;
|
||||
k=p.g;
|
||||
if(map[i][j].fuhao=='\\')
|
||||
{
|
||||
if(map[i][j].vis[(5-k)%4]==0) {
|
||||
q.push(d[i][j][(5-k)%4]);
|
||||
map[i][j].vis[(5-k)%4]=1;
|
||||
map[i][j].ge[(5-k)%4]=color;
|
||||
}
|
||||
if(0<=i+mo[k][0]&&i+mo[k][0]<m && 0<=j+mo[k][1]&&j+mo[k][1]<n)
|
||||
{
|
||||
if(map[i+mo[k][0]][j+mo[k][1]].vis[(k+2)%4]==0)
|
||||
{
|
||||
map[i+mo[k][0]][j+mo[k][1]].vis[(k+2)%4]=1;
|
||||
map[i+mo[k][0]][j+mo[k][1]].ge[(k+2)%4]=color;
|
||||
q.push(d[i+mo[k][0]][j+mo[k][1]][(k+2)%4]);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(map[i][j].vis[3-k]==0)
|
||||
{
|
||||
q.push(d[i][j][3-k]);
|
||||
map[i][j].vis[3-k]=1;
|
||||
map[i][j].ge[3-k]=color;
|
||||
}
|
||||
if(0<=i+mo[k][0]&&i+mo[k][0]<m && 0<=j+mo[k][1]&&j+mo[k][1]<n)
|
||||
{
|
||||
if(map[i+mo[k][0]][j+mo[k][1]].vis[(k+2)%4]==0)
|
||||
{
|
||||
map[i+mo[k][0]][j+mo[k][1]].vis[(k+2)%4]=1;
|
||||
map[i+mo[k][0]][j+mo[k][1]].ge[(k+2)%4]=color;
|
||||
q.push(d[i+mo[k][0]][j+mo[k][1]][(k+2)%4]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void init()
|
||||
{
|
||||
int i,j,k;
|
||||
for(i=0;i<m;i++)
|
||||
{
|
||||
for(j=0;j<n;j++)
|
||||
for(k=0;k<4;k++)
|
||||
{
|
||||
map[i][j].vis[k]=0;
|
||||
map[i][j].fuhao=0;
|
||||
map[i][j].ge[k]=0;
|
||||
}
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i,j,k;
|
||||
while(scanf("%d %d",&n,&m)!=EOF)
|
||||
{
|
||||
getchar();
|
||||
init();
|
||||
color=0;
|
||||
white=0;
|
||||
for(i=0;i<m;i++)
|
||||
{
|
||||
for(j=0;j<n;j++)
|
||||
{
|
||||
scanf("%c",&map[i][j].fuhao);
|
||||
for(k=0;k<4;k++)
|
||||
{
|
||||
d[i][j][k].x=i;
|
||||
d[i][j][k].y=j;
|
||||
d[i][j][k].g=k;
|
||||
}
|
||||
}
|
||||
getchar();
|
||||
}
|
||||
for(i=0;i<m;i++)
|
||||
{
|
||||
for(j=0;j<n;j++)
|
||||
{
|
||||
for(k=0;k<4;k++)
|
||||
{
|
||||
if(map[i][j].vis[k]==0)
|
||||
{
|
||||
color++;
|
||||
bfs(i,j,k);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for(i=0;i<m;i++)
|
||||
{
|
||||
for(j=0;j<n;j++)
|
||||
{
|
||||
int flag=0,t=map[i][j].ge[0];
|
||||
for(k=1;k<4;k++)
|
||||
{
|
||||
if(t!=map[i][j].ge[k])
|
||||
{
|
||||
flag=1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!flag)
|
||||
white++;
|
||||
}
|
||||
}
|
||||
printf("%d\n",white);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,102 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <queue>
|
||||
using namespace std ;
|
||||
const int INF=0xfffffff ;
|
||||
struct node{
|
||||
int s,t,cap,cost,nxt ;
|
||||
}e[4000005] ;
|
||||
int sumflow ;
|
||||
int n,m,cnt,head[1000005],vis[1000005],dis[1000005],pre[1000005] ;
|
||||
void add(int s,int t,int cap,int cost)
|
||||
{
|
||||
e[cnt].s=s ;e[cnt].t=t ;e[cnt].cap=cap ;e[cnt].cost=cost ;e[cnt].nxt=head[s] ;head[s]=cnt++ ;
|
||||
e[cnt].s=t ;e[cnt].t=s ;e[cnt].cap=0 ;e[cnt].cost=-cost ;e[cnt].nxt=head[t] ;head[t]=cnt++ ;
|
||||
}
|
||||
int spfa(int s,int t,int N)
|
||||
{
|
||||
for(int i=0 ;i<=N ;i++)
|
||||
dis[i]=INF ;
|
||||
dis[s]=0 ;
|
||||
memset(vis,0,sizeof(vis)) ;
|
||||
memset(pre,-1,sizeof(pre)) ;
|
||||
vis[s]=1 ;
|
||||
queue <int> q ;
|
||||
q.push(s) ;
|
||||
while(!q.empty())
|
||||
{
|
||||
int u=q.front() ;
|
||||
q.pop() ;
|
||||
vis[u]=0 ;
|
||||
for(int i=head[u] ;i!=-1 ;i=e[i].nxt)
|
||||
{
|
||||
int tt=e[i].t ;
|
||||
if(e[i].cap && dis[tt]>dis[u]+e[i].cost)
|
||||
{
|
||||
dis[tt]=dis[u]+e[i].cost ;
|
||||
pre[tt]=i ;
|
||||
if(!vis[tt])
|
||||
{
|
||||
vis[tt]=1 ;
|
||||
q.push(tt) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(dis[t]==INF)return 0 ;
|
||||
return 1 ;
|
||||
}
|
||||
int MCMF(int s,int t,int N)
|
||||
{
|
||||
int flow,minflow,mincost ;
|
||||
mincost=flow=0 ;
|
||||
while(spfa(s,t,N))
|
||||
{
|
||||
minflow=INF ;
|
||||
for(int i=pre[t] ;i!=-1 ;i=pre[e[i].s])
|
||||
minflow=min(minflow,e[i].cap) ;
|
||||
flow+=minflow ;
|
||||
for(int i=pre[t] ;i!=-1 ;i=pre[e[i].s])
|
||||
{
|
||||
e[i].cap-=minflow ;
|
||||
e[i^1].cap+=minflow ;
|
||||
}
|
||||
mincost+=dis[t]*minflow ;
|
||||
}
|
||||
sumflow=flow ;
|
||||
return mincost ;
|
||||
}
|
||||
int num[605][605],M[605][605] ;
|
||||
int main()
|
||||
{
|
||||
while(~scanf("%d",&n))
|
||||
{
|
||||
cnt=0 ;
|
||||
memset(head,-1,sizeof(head)) ;
|
||||
int S=0 ;
|
||||
int T=2*n*n+1 ;
|
||||
int ct=1 ;
|
||||
for(int i=1 ;i<=n ;i++)
|
||||
for(int j=1 ;j<=n ;j++)
|
||||
{
|
||||
scanf("%d",&M[i][j]) ;
|
||||
num[i][j]=ct++ ;
|
||||
}
|
||||
add(S,1,2,0) ;
|
||||
add(n*n*2,T,2,0) ;
|
||||
for(int i=1 ;i<=n ;i++)
|
||||
{
|
||||
for(int j=1 ;j<=n ;j++)
|
||||
{
|
||||
if(num[i][j]==1)add(num[i][j],num[i][j]+n*n,2,-M[i][j]) ;
|
||||
else if(num[i][j]==n*n)add(num[i][j],num[i][j]+n*n,2,-M[i][j]) ;
|
||||
else add(num[i][j],num[i][j]+n*n,1,-M[i][j]) ;
|
||||
if(i<n)add(num[i][j]+n*n,num[i][j]+n,1,0) ;
|
||||
if(j<n)add(num[i][j]+n*n,num[i][j]+1,1,0) ;
|
||||
}
|
||||
}
|
||||
printf("%d\n",-(MCMF(S,T,T+1)+M[1][1]+M[n][n])) ;
|
||||
}
|
||||
return 0 ;
|
||||
}
|
|
@ -0,0 +1,190 @@
|
|||
#include<stdio.h>
|
||||
#include<iostream>
|
||||
#include<algorithm>
|
||||
#include<string.h>
|
||||
using namespace std;
|
||||
const int MAXD=15;
|
||||
const int HASH=10007;
|
||||
const int STATE=1000010;
|
||||
int N,M;
|
||||
int maze[MAXD][MAXD];
|
||||
int score[MAXD][MAXD];
|
||||
int code[MAXD];
|
||||
int ch[MAXD];
|
||||
struct HASHMAP
|
||||
{
|
||||
int head[HASH],state[STATE],next[STATE],size;
|
||||
int dp[STATE];
|
||||
void init()
|
||||
{
|
||||
size=0;
|
||||
memset(head,-1,sizeof(head));
|
||||
}
|
||||
void push(int st,int ans)
|
||||
{
|
||||
int i,h=st%HASH;
|
||||
for(i=head[h];i!=-1;i=next[i])
|
||||
if(state[i]==st)
|
||||
{
|
||||
if(dp[i]<ans)dp[i]=ans;
|
||||
return;
|
||||
}
|
||||
state[size]=st;
|
||||
dp[size]=ans;
|
||||
next[size]=head[h];
|
||||
head[h]=size++;
|
||||
}
|
||||
}hm[2];
|
||||
void decode(int *code,int m,int st)
|
||||
{
|
||||
for(int i=m;i>=0;i--)
|
||||
{
|
||||
code[i]=st&7;
|
||||
st>>=3;
|
||||
}
|
||||
}
|
||||
int encode(int *code,int m)
|
||||
{
|
||||
int cnt=1;
|
||||
memset(ch,-1,sizeof(ch));
|
||||
ch[0]=0;
|
||||
int st=0;
|
||||
for(int i=0;i<=m;i++)
|
||||
{
|
||||
if(ch[code[i]]==-1)ch[code[i]]=cnt++;
|
||||
code[i]=ch[code[i]];
|
||||
st<<=3;
|
||||
st|=code[i];
|
||||
}
|
||||
return st;
|
||||
}
|
||||
void shift(int *code,int m)
|
||||
{
|
||||
for(int i=m;i>0;i--)code[i]=code[i-1];
|
||||
code[0]=0;
|
||||
}
|
||||
void dpblank(int i,int j,int cur)
|
||||
{
|
||||
int k,left,up;
|
||||
for(k=0;k<hm[cur].size;k++)
|
||||
{
|
||||
decode(code,M,hm[cur].state[k]);
|
||||
left=code[j-1];
|
||||
up=code[j];
|
||||
if((i==1&&j==1)||(i==N&&j==M))
|
||||
{
|
||||
if((left&&(!up))||((!left)&&up))
|
||||
{
|
||||
code[j-1]=code[j]=0;
|
||||
if(j==M)shift(code,M);
|
||||
hm[cur^1].push(encode(code,M),hm[cur].dp[k]+score[i][j]);
|
||||
}
|
||||
else if(left==0&&up==0)
|
||||
{
|
||||
if(maze[i][j+1])
|
||||
{
|
||||
code[j-1]=0;
|
||||
code[j]=13;
|
||||
hm[cur^1].push(encode(code,M),hm[cur].dp[k]+score[i][j]);
|
||||
}
|
||||
if(maze[i][j+1])
|
||||
{
|
||||
code[j-1]=13;
|
||||
code[j]=0;
|
||||
if(j==M)shift(code,M);
|
||||
hm[cur^1].push(encode(code,M),hm[cur].dp[k]+score[i][j]);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if(left&&up)
|
||||
{
|
||||
if(left==up)
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
code[j-1]=code[j]=0;
|
||||
for(int t=0;t<=M;t++)
|
||||
if(code[t]==up)
|
||||
code[t]=left;
|
||||
if(j==M)shift(code,M);
|
||||
hm[cur^1].push(encode(code,M),hm[cur].dp[k]+score[i][j]);
|
||||
}
|
||||
}
|
||||
else if((left&&(!up))||((!left)&&up))
|
||||
{
|
||||
int t;
|
||||
if(left)t=left;
|
||||
else t=up;
|
||||
if(maze[i][j+1])
|
||||
{
|
||||
code[j-1]=0;
|
||||
code[j]=t;
|
||||
hm[cur^1].push(encode(code,M),hm[cur].dp[k]+score[i][j]);
|
||||
}
|
||||
if(maze[i+1][j])
|
||||
{
|
||||
code[j]=0;
|
||||
code[j-1]=t;
|
||||
if(j==M)shift(code,M);
|
||||
hm[cur^1].push(encode(code,M),hm[cur].dp[k]+score[i][j]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(maze[i][j+1]&&maze[i+1][j])
|
||||
{
|
||||
code[j]=code[j-1]=13;
|
||||
hm[cur^1].push(encode(code,M),hm[cur].dp[k]+score[i][j]);
|
||||
}
|
||||
code[j-1]=code[j]=0;
|
||||
if(j==M)shift(code,M);
|
||||
hm[cur^1].push(encode(code,M),hm[cur].dp[k]);
|
||||
}
|
||||
}
|
||||
}
|
||||
void init()
|
||||
{
|
||||
memset(maze,0,sizeof(maze));
|
||||
for(int i=1;i<=N;i++)
|
||||
for(int j=1;j<=M;j++)
|
||||
{
|
||||
maze[i][j]=1;
|
||||
scanf("%d",&score[i][j]);
|
||||
}
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
int i,j,cur=0;
|
||||
hm[cur].init();
|
||||
hm[cur].push(0,0);
|
||||
for(int i=1;i<=N;i++)
|
||||
for(int j=1;j<=M;j++)
|
||||
{
|
||||
hm[cur^1].init();
|
||||
dpblank(i,j,cur);
|
||||
cur^=1;
|
||||
}
|
||||
int ans=0;
|
||||
for(int i=0;i<hm[cur].size;i++)
|
||||
ans+=hm[cur].dp[i];
|
||||
printf("%d\n",ans);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int iCase=0;
|
||||
while(scanf("%d%d",&N,&M)!=EOF)
|
||||
{
|
||||
iCase++;
|
||||
printf("Case %d: ",iCase);
|
||||
init();
|
||||
if(N==1&&M==1)
|
||||
{
|
||||
printf("%d\n",score[1][1]);
|
||||
continue;
|
||||
}
|
||||
solve();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,100 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
struct cal{
|
||||
int id;
|
||||
int state;
|
||||
int score;
|
||||
int kill_ZG;
|
||||
int kill_ZC;
|
||||
int kill_FZ;
|
||||
int kill_NJ;
|
||||
int killed_by;
|
||||
}player[101];
|
||||
int num_ZG,num_ZC,num_FZ,num_NJ;
|
||||
int n,m,flag;
|
||||
int whether_win();
|
||||
void calc_score(int num);
|
||||
int main()
|
||||
{
|
||||
int t,i,a,b,id;
|
||||
char name[3];
|
||||
scanf("%d",&t);
|
||||
while(t--){
|
||||
scanf("%d%d",&n,&m);
|
||||
memset(player,0,sizeof(player[0])*n);
|
||||
num_ZG=num_ZC=num_FZ=num_NJ=0;
|
||||
for(i=0;i<n;i++){
|
||||
scanf("%s",name);
|
||||
switch(name[1]){
|
||||
case 'G':player[i].id=1;num_ZG++;break;
|
||||
case 'C':player[i].id=2;num_ZC++;break;
|
||||
case 'Z':player[i].id=3;num_FZ++;break;
|
||||
case 'J':player[i].id=4;num_NJ++;break;
|
||||
}
|
||||
}
|
||||
flag=whether_win();
|
||||
while(m--){
|
||||
scanf("%d%d",&a,&b);
|
||||
if(flag)
|
||||
continue;
|
||||
player[b].state=1;
|
||||
if(!num_FZ&&!num_ZC&&num_NJ==1&&num_ZG==1)
|
||||
player[b].killed_by=player[a].id;
|
||||
id=player[b].id;
|
||||
switch(id){
|
||||
case 1:num_ZG--;player[a].kill_ZG++;break;
|
||||
case 2:num_ZC--;player[a].kill_ZC++;break;
|
||||
case 3:num_FZ--;player[a].kill_FZ++;break;
|
||||
case 4:num_NJ--;player[a].kill_NJ++;break;
|
||||
}
|
||||
flag=whether_win();
|
||||
}
|
||||
calc_score(flag);
|
||||
printf("%d",player[0].score);
|
||||
for(i=1;i<n;i++)
|
||||
printf(" %d",player[i].score);
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int whether_win(void)
|
||||
{
|
||||
if(!num_FZ&&!num_NJ)
|
||||
return 1;
|
||||
if(!num_ZG){
|
||||
if(num_NJ==1&&!num_FZ&&!num_ZC)
|
||||
return 2;
|
||||
else
|
||||
return 3;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
void calc_score(int num)
|
||||
{
|
||||
int i;
|
||||
switch(num){
|
||||
case 1:
|
||||
for(i=0;i<n;i++)
|
||||
switch(player[i].id){
|
||||
case 1:player[i].score=4+num_ZC*2+player[i].kill_FZ+player[i].kill_NJ;break;
|
||||
case 2:player[i].score=5+num_ZC+player[i].kill_FZ+player[i].kill_NJ;break;
|
||||
case 4:if(player[i].killed_by==1) player[i].score=n;break;
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
for(i=0;i<n;i++)
|
||||
switch(player[i].id){
|
||||
case 1:player[i].score=1;break;
|
||||
case 4:if(!player[i].state) player[i].score=4+n*2;break;
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
for(i=0;i<n;i++)
|
||||
switch(player[i].id){
|
||||
case 3:player[i].score=num_FZ*3+player[i].kill_ZC+player[i].kill_NJ+player[i].kill_ZG*2;break;
|
||||
case 4:if(!player[i].state) player[i].score=1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
|
@ -0,0 +1,87 @@
|
|||
#include<iostream>
|
||||
#include<string.h>
|
||||
using namespace std;
|
||||
const int maxn=1005;
|
||||
int map[maxn][maxn],n,cnt[maxn][maxn];
|
||||
char name[maxn][100];
|
||||
int match_name(char str[])
|
||||
{
|
||||
for(int i=1;i<n;i++)
|
||||
if(strcmp(str,name[i])==0)
|
||||
return i;
|
||||
strcpy(name[n],str);
|
||||
return n++;
|
||||
}
|
||||
void str_convert(char str[])
|
||||
{
|
||||
int i,j,first_node,k;
|
||||
bool flag=0;
|
||||
char str1[100];
|
||||
for(i=0;i<strlen(str);)
|
||||
{
|
||||
for(j=0;str[i]!=' '&&str[i]!='\0';i++,j++)
|
||||
str1[j]=str[i];
|
||||
str1[j]='\0';
|
||||
i++;
|
||||
k=match_name(str1);
|
||||
if(flag==0)
|
||||
{
|
||||
first_node=k;
|
||||
flag=1;
|
||||
}
|
||||
else if(map[first_node][k]==0)
|
||||
{
|
||||
map[first_node][k]=map[k][first_node]=1;
|
||||
cnt[first_node][k]=cnt[k][first_node]=1;
|
||||
}
|
||||
}
|
||||
}
|
||||
void floyd()
|
||||
{
|
||||
int i,j,k;
|
||||
for(k=1;k<n;k++)
|
||||
for(i=1;i<n;i++)
|
||||
for(j=1;j<n;j++)
|
||||
if(map[i][k] && map[k][j])
|
||||
if(map[i][j]==0)
|
||||
{
|
||||
map[i][j]=map[i][k]+map[k][j];
|
||||
cnt[i][j]=cnt[i][k]*cnt[k][j];
|
||||
}
|
||||
else if(map[i][k]+map[k][j]<map[i][j])
|
||||
{
|
||||
map[i][j]=map[i][k]+map[k][j];
|
||||
cnt[i][j]=cnt[i][k]*cnt[k][j];
|
||||
}
|
||||
else if(map[i][k]+map[k][j]==map[i][j])
|
||||
cnt[i][j] += cnt[i][k]*cnt[k][j];
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i,j;
|
||||
char str[100],str1[100],str2[100];
|
||||
gets(str);
|
||||
while(gets(str))
|
||||
{
|
||||
n=1;
|
||||
memset(map,0,sizeof(map));
|
||||
memset(cnt,0,sizeof(cnt));
|
||||
str_convert(str);
|
||||
while(gets(str))
|
||||
{
|
||||
if(strcmp(str,"GRAPH END")==0) break;
|
||||
str_convert(str);
|
||||
}
|
||||
for(i=1;i<n;i++)
|
||||
map[i][i]=1;
|
||||
floyd();
|
||||
while(cin>>str1>>str2)
|
||||
{
|
||||
if(strcmp(str1,"GRAPH")==0 && strcmp(str2,"BEGIN")==0) break;
|
||||
i=match_name(str1);
|
||||
j=match_name(str2);
|
||||
cout<<str1<<" "<<str2<<" "<<cnt[i][j]<<endl;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,177 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#define OPER 1
|
||||
#define NUM 2
|
||||
#define INF 0x3fffffff
|
||||
#define null x
|
||||
#define END -1
|
||||
#define MUTI 3
|
||||
int n,T;
|
||||
char oper_now,oper_last;
|
||||
char in[1000];
|
||||
__int64 ans,temp,num_equal;
|
||||
int num,l;
|
||||
int isnum(char a)
|
||||
{
|
||||
if(a>='0'&& a<='9')
|
||||
return 1;
|
||||
else return 0;
|
||||
}
|
||||
int findp()
|
||||
{
|
||||
if(num==l)return END;
|
||||
int i,j;
|
||||
int t=num;
|
||||
if(in[t]=='='&& in[t+1]=='=')
|
||||
{
|
||||
num_equal =0;
|
||||
while(in[num]=='=')
|
||||
{
|
||||
num_equal++;
|
||||
num++;
|
||||
}
|
||||
return MUTI;
|
||||
}
|
||||
if(!isnum(in[num]))
|
||||
{
|
||||
num++;
|
||||
return OPER;
|
||||
}
|
||||
__int64 tt=0;
|
||||
int jj=10;
|
||||
int ttp;
|
||||
while(isnum(in[num]))
|
||||
{
|
||||
if((tt*jj+ (in[num]-'0'))>99999999|| (tt*jj+ (in[num]-'0'))<-99999999)
|
||||
{
|
||||
num++;
|
||||
continue;
|
||||
}
|
||||
tt= tt*jj+ (in[num]-'0');
|
||||
num++;
|
||||
}
|
||||
temp= tt;
|
||||
return NUM;
|
||||
}
|
||||
__int64 cal_op()
|
||||
{
|
||||
__int64 aaa;
|
||||
switch(oper_now)
|
||||
{
|
||||
case'-':return ans-temp;
|
||||
case'+':return temp+ans;
|
||||
case'*':aaa= (__int64)ans*temp;if(aaa>99999999|| aaa<-99999999)return INF;else return ans*temp;
|
||||
case'/':if(temp!=0)return ans/temp;else return INF;
|
||||
}
|
||||
}
|
||||
void cal()
|
||||
{
|
||||
if(in[num-1]!='=')
|
||||
{
|
||||
if(temp!=INF&& oper_now!=NULL)
|
||||
{
|
||||
ans= cal_op();
|
||||
temp= INF;
|
||||
}
|
||||
else if(oper_now==NULL&& temp!=INF)
|
||||
{
|
||||
ans= temp;
|
||||
temp= INF;
|
||||
}
|
||||
oper_last= oper_now;
|
||||
oper_now= in[num-1];
|
||||
}
|
||||
else
|
||||
{
|
||||
if(oper_now!=NULL)
|
||||
if(temp!=INF)
|
||||
{
|
||||
ans= cal_op();
|
||||
temp= INF;
|
||||
}
|
||||
else
|
||||
{
|
||||
temp= ans;
|
||||
ans= cal_op();
|
||||
temp= INF;
|
||||
}
|
||||
oper_now= NULL;
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i,j,t;
|
||||
int op,pp;
|
||||
scanf("%d",&T);
|
||||
getchar();
|
||||
for(pp=0; pp<T; pp++)
|
||||
{
|
||||
num=0;
|
||||
ans=0;
|
||||
temp= INF;
|
||||
oper_now= NULL;
|
||||
oper_last= NULL;
|
||||
gets(in);
|
||||
l= strlen(in);
|
||||
int ttt;
|
||||
int error=0;
|
||||
while(op= findp(),op!=END&& !error)
|
||||
{
|
||||
if(ans>99999999|| ans<-99999999)
|
||||
{
|
||||
error=1;
|
||||
goto end;
|
||||
}
|
||||
if(op==OPER)
|
||||
{
|
||||
cal();
|
||||
}
|
||||
else if(op==MUTI)
|
||||
{
|
||||
if(oper_now!=NULL)
|
||||
{
|
||||
if(temp!=INF)
|
||||
{
|
||||
for(i=0;i<num_equal;i++)
|
||||
{
|
||||
ans= cal_op();
|
||||
if(ans>99999999|| ans<-99999999)
|
||||
{
|
||||
error=1;
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
temp= INF;
|
||||
}
|
||||
else
|
||||
{
|
||||
ttt= ans;
|
||||
for(i=0;i<num_equal;i++)
|
||||
{
|
||||
temp= ttt;
|
||||
ans= cal_op();
|
||||
if(ans>99999999|| ans<-99999999)
|
||||
{
|
||||
error=1;
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
temp= INF;
|
||||
}
|
||||
}
|
||||
oper_now= NULL;
|
||||
}
|
||||
if(ans>99999999|| ans<-99999999)
|
||||
{
|
||||
error=1;
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
end:;
|
||||
printf("Case %d: ",pp+1);
|
||||
if(!error)
|
||||
printf("%I64d\n",temp==INF?ans:temp);
|
||||
else printf("ERROR\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,112 @@
|
|||
#include <iostream>
|
||||
#include <string.h>
|
||||
#include <algorithm>
|
||||
#include <stdio.h>
|
||||
using namespace std;
|
||||
typedef long long LL;
|
||||
const int N=1000005;
|
||||
const LL INF=(LL)1<<62;
|
||||
bool prime[N];
|
||||
LL p[N];
|
||||
LL fac[N];
|
||||
LL k,cnt,num,ans,n,m,K;
|
||||
void isprime()
|
||||
{
|
||||
k=0;
|
||||
int i,j;
|
||||
memset(prime,true,sizeof(prime));
|
||||
for(i=2; i<N; i++)
|
||||
{
|
||||
if(prime[i])
|
||||
{
|
||||
p[k++]=i;
|
||||
for(j=i+i; j<N; j+=i)
|
||||
{
|
||||
prime[j]=false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void Solve(LL m,LL n)
|
||||
{
|
||||
cnt=0;
|
||||
LL i;
|
||||
for(i=0; p[i]*p[i]<=n; i++)
|
||||
{
|
||||
if(n%p[i]==0)
|
||||
{
|
||||
fac[cnt++]=p[i];
|
||||
while(n%p[i]==0) n/=p[i];
|
||||
}
|
||||
}
|
||||
if(n>1)
|
||||
fac[cnt++]=n;
|
||||
for(i=0; p[i]*p[i]<=m; i++)
|
||||
{
|
||||
if(m%p[i]==0)
|
||||
{
|
||||
fac[cnt++]=p[i];
|
||||
while(m%p[i]==0) m/=p[i];
|
||||
}
|
||||
}
|
||||
if(m>1)
|
||||
fac[cnt++]=m;
|
||||
}
|
||||
void dfs(LL k,LL t,LL s,LL n)
|
||||
{
|
||||
if(k==num)
|
||||
{
|
||||
if(t&1) ans-=n/s;
|
||||
else ans+=n/s;
|
||||
return;
|
||||
}
|
||||
dfs(k+1,t,s,n);
|
||||
dfs(k+1,t+1,s*fac[k],n);
|
||||
}
|
||||
LL Binary()
|
||||
{
|
||||
LL l=1,r=INF,mid,ret;
|
||||
while(l<=r)
|
||||
{
|
||||
mid=(l+r)/2;
|
||||
ans=0;
|
||||
dfs(0,0,1,mid);
|
||||
if(ans>=K)
|
||||
{
|
||||
ret=mid;
|
||||
r=mid-1;
|
||||
}
|
||||
else
|
||||
l=mid+1;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
isprime();
|
||||
LL t,ct,tt=1;
|
||||
scanf("%I64d",&t);
|
||||
while(t--)
|
||||
{
|
||||
scanf("%I64d%I64d%I64d",&m,&n,&K);
|
||||
printf("Case %d: ",tt++);
|
||||
if(n==1&&m==1)
|
||||
{
|
||||
printf("%I64d\n",k);
|
||||
continue;
|
||||
}
|
||||
Solve(m,n);
|
||||
sort(fac,fac+cnt);
|
||||
num=1;
|
||||
for(LL i=1; i<cnt; i++)
|
||||
{
|
||||
if(fac[i]!=fac[i-1])
|
||||
{
|
||||
fac[num++]=fac[i];
|
||||
}
|
||||
}
|
||||
ct=num;
|
||||
printf("%I64d\n",Binary());
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
#include <iostream>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
int T,tt=1;
|
||||
int n;
|
||||
scanf("%d",&T);
|
||||
while(T--)
|
||||
{
|
||||
scanf("%d",&n);
|
||||
int ans = 0;
|
||||
for(int i=1;i<=n;i++)
|
||||
{
|
||||
int x;
|
||||
scanf("%d",&x);
|
||||
if(i%6==0 || i%6==2 || i%6==5)
|
||||
ans ^= x;
|
||||
}
|
||||
printf("Case %d: ",tt++);
|
||||
if(ans) puts("Alice");
|
||||
else puts("Bob");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
#include <stdio.h>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
#define mod 100003
|
||||
typedef long long LL;
|
||||
LL a[106][3],b[106][3],c[106][3];
|
||||
int main()
|
||||
{
|
||||
memset(a,0,sizeof(a));
|
||||
memset(b,0,sizeof(b));
|
||||
memset(c,0,sizeof(c));
|
||||
c[0][0]=1;
|
||||
for (int i=1;i<=100;i++)
|
||||
{
|
||||
a[i][0]=(a[i-1][0]*90%mod+c[i-1][0]*44%mod+a[i-1][1]*80%mod+c[i-1][1]*41%mod)%mod;
|
||||
a[i][1]=(a[i-1][0]*10%mod+c[i-1][0]+a[i-1][1]*10%mod+c[i-1][1])%mod;
|
||||
a[i][2]=(a[i-1][1]*10%mod+c[i-1][1]*3%mod+a[i-1][2]*100%mod+c[i-1][2]*45%mod)%mod;
|
||||
b[i][0]=(b[i-1][0]*900%mod+a[i-1][0]*880+c[i-1][0]*284+b[i-1][1]*800%mod+
|
||||
a[i-1][1]*820%mod+c[i-1][1]*275%mod)%mod;
|
||||
b[i][1]=(b[i-1][0]*100%mod+a[i-1][0]*20%mod+c[i-1][0]+b[i-1][1]*100%mod
|
||||
+a[i-1][1]*20%mod+c[i-1][1])%mod;
|
||||
b[i][2]=(b[i-1][1]*100%mod+a[i-1][1]*60%mod+c[i-1][1]*9%mod+
|
||||
b[i-1][2]*1000%mod+a[i-1][2]*900%mod+c[i-1][2]*285%mod)%mod;
|
||||
c[i][0]=(c[i-1][0]*9%mod+c[i-1][1]*8%mod)%mod;
|
||||
c[i][1]=(c[i-1][0]+c[i-1][1])%mod;
|
||||
c[i][2]=(c[i-1][1]+c[i-1][2]*10%mod)%mod;
|
||||
}
|
||||
int test;
|
||||
scanf("%d",&test);
|
||||
for (int ii=1;ii<=test;ii++)
|
||||
{
|
||||
int n;
|
||||
scanf("%d",&n);
|
||||
printf("Case %d: %I64d\n",ii,b[n][2]);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <cstdio>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
const int maxn=11000;
|
||||
using namespace std;
|
||||
double a[maxn],b[maxn];
|
||||
double dp[2][maxn];
|
||||
int main()
|
||||
{
|
||||
int n,m;
|
||||
while(cin>>n>>m)
|
||||
{
|
||||
if(n==0&&m==0)break;
|
||||
for(int i=1;i<=n;i++)scanf("%lf",&a[i]);
|
||||
for(int i=1;i<=m;i++)scanf("%lf",&b[i]);
|
||||
double *A=a,*B=b;
|
||||
if(n>m){swap(n,m);swap(A,B);}
|
||||
sort(A+1,A+1+n);
|
||||
sort(B+1,B+1+m);
|
||||
memset(dp,0,sizeof(dp));
|
||||
for(int i=1;i<=n;i++)
|
||||
for(int j=i;j<=i+m-n;j++)
|
||||
{
|
||||
if(i==j)
|
||||
{
|
||||
dp[i&1][j]=dp[(i-1)&1][j-1]+fabs(A[i]-B[j]);
|
||||
}
|
||||
else
|
||||
{
|
||||
dp[i&1][j]=min(dp[(i-1)&1][j-1]+fabs(A[i]-B[j]),dp[i&1][j-1]);
|
||||
}
|
||||
}
|
||||
printf("%.6lf\n",dp[n&1][m]);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,84 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
using namespace std;
|
||||
const int N=10006;
|
||||
struct Edge
|
||||
{
|
||||
int st, en;
|
||||
Edge() {}
|
||||
Edge(int a, int b)
|
||||
{
|
||||
st=a, en=b;
|
||||
}
|
||||
};
|
||||
stack <Edge> palm;
|
||||
vector <int> arc[N];
|
||||
vector <Edge> block[N];
|
||||
int dfn[N], low[N];
|
||||
bool vs[N];
|
||||
int n, m, ind, T, sum1, sum2;
|
||||
void tarjan(int u, int pre)
|
||||
{
|
||||
dfn[u]=low[u]=T++;
|
||||
int len=(int)arc[u].size();
|
||||
for(int i=0; i<len; i++)
|
||||
{
|
||||
int v=arc[u][i];
|
||||
if(dfn[v]==-1)
|
||||
{
|
||||
palm.push(Edge(u, v));
|
||||
tarjan(v, u);
|
||||
if(low[u]>low[v]) low[u]=low[v];
|
||||
if(dfn[u]<=low[v])
|
||||
{
|
||||
for(Edge temp; !palm.empty(); )
|
||||
{
|
||||
temp=palm.top();
|
||||
if(dfn[temp.st]<dfn[v]) break;
|
||||
block[ind].push_back(temp), palm.pop();
|
||||
}
|
||||
block[ind++].push_back(Edge(u, v));
|
||||
palm.pop();
|
||||
if(dfn[u]<low[v]) sum1++;
|
||||
}
|
||||
}
|
||||
else if(v!=pre && dfn[v]<dfn[u])
|
||||
{
|
||||
palm.push(Edge(u, v));
|
||||
if(low[u]>dfn[v]) low[u]=dfn[v];
|
||||
}
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while(scanf("%d%d", &n, &m), n!=0 || m!=0)
|
||||
{
|
||||
for(int i=0; i<n; i++) arc[i].clear();
|
||||
for(int i=0, a, b; i<m; i++)
|
||||
{
|
||||
scanf("%d%d", &a, &b);
|
||||
arc[a].push_back(b);
|
||||
arc[b].push_back(a);
|
||||
}
|
||||
for(int i=0; i<n; i++) dfn[i]=-1, block[i].clear();
|
||||
while(!palm.empty()) palm.pop();
|
||||
ind=T=sum1=sum2=0;
|
||||
for(int i=0; i<n; i++) if(dfn[i]==-1) tarjan(i, -1);
|
||||
for(int i=0; i<ind; i++)
|
||||
{
|
||||
for(int j=0; j<n; j++) vs[j]=0;
|
||||
int len=(int)block[i].size(), tot=0;
|
||||
for(int j=0; j<len; j++)
|
||||
{
|
||||
if(!vs[block[i][j].st]) vs[block[i][j].st]=1, tot++;
|
||||
if(!vs[block[i][j].en]) vs[block[i][j].en]=1, tot++;
|
||||
}
|
||||
if(len>tot) sum2+=len;
|
||||
}
|
||||
printf("%d %d\n", sum1, sum2);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,86 @@
|
|||
#include <stdio.h>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
#define clr(x) memset(x, 0, sizeof(x))
|
||||
#define M 210
|
||||
const int INF = 0x1f1f1f1f;
|
||||
int v;
|
||||
int link[M], lx[M], ly[M];
|
||||
int visx[M], visy[M], G[M][M];
|
||||
int slack;
|
||||
int DFS(int x)
|
||||
{
|
||||
visx[x] = 1;
|
||||
for (int y = 1; y <= v; ++y)
|
||||
{
|
||||
if (visy[y])
|
||||
continue;
|
||||
int t = lx[x] + ly[y] - G[x][y];
|
||||
if (t == 0)
|
||||
{
|
||||
visy[y] = 1;
|
||||
if (link[y] == -1||DFS(link[y]))
|
||||
{
|
||||
link[y] = x;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else if (slack > t)
|
||||
slack = t;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int KM()
|
||||
{
|
||||
int i,j;
|
||||
memset (link,-1,sizeof(link));
|
||||
memset (ly,0,sizeof(ly));
|
||||
for (i = 1;i <= v; ++i)
|
||||
for (j = 1,lx[i] = -INF;j <= v; ++j)
|
||||
if (G[i][j] > lx[i])
|
||||
lx[i] = G[i][j];
|
||||
for (int x = 1;x <= v; ++x)
|
||||
{
|
||||
memset(visx,0,sizeof(visx));
|
||||
memset(visy,0,sizeof(visy));
|
||||
while (1)
|
||||
{
|
||||
slack=INF;
|
||||
if (DFS(x))
|
||||
break;
|
||||
for(i = 1; i <= v; i++)
|
||||
{
|
||||
if(visx[i]) { lx[i]-=slack; visx[i]=0; }
|
||||
if(visy[i]) { ly[i]+=slack; visy[i]=0; }
|
||||
}
|
||||
}
|
||||
}
|
||||
int res = 0;
|
||||
for (i = 1;i <= v; ++i)
|
||||
res += G[link[i]][i];
|
||||
return res;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i, j, k;
|
||||
int n, val[110];
|
||||
char w[110];
|
||||
while(scanf("%d", &n) != EOF)
|
||||
{
|
||||
if(n == 0) break;
|
||||
clr(G);
|
||||
v = n;
|
||||
for(i = 1; i <= n; ++i)
|
||||
scanf("%d", &val[i]);
|
||||
for(i = 1; i <= n; ++i) {
|
||||
scanf("%s", w+1);
|
||||
for(j = 1; j <= n; ++j)
|
||||
if(w[j]-'0') G[i][j] = val[i]^val[j];
|
||||
}
|
||||
int ans = KM();
|
||||
printf("%d\n", ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,132 @@
|
|||
#include<iostream>
|
||||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
#define lson u<<1
|
||||
#define rson u<<1|1
|
||||
#define MAXN 100010
|
||||
int dat[MAXN];
|
||||
struct Node {
|
||||
int lef,rig;
|
||||
int lsum,rsum,msum;
|
||||
int lz,rz,mz;
|
||||
int sum;
|
||||
int COVER,XOR;
|
||||
}T[MAXN<<2];
|
||||
void makeXOR(int u){
|
||||
swap(T[u].lsum,T[u].lz);
|
||||
swap(T[u].rsum,T[u].rz);
|
||||
swap(T[u].msum,T[u].mz);
|
||||
T[u].sum=T[u].rig-T[u].lef+1-T[u].sum;
|
||||
}
|
||||
void PushUp(int u){
|
||||
if(T[u].lef==T[u].rig)return;
|
||||
int len=T[u].rig-T[u].lef+1;
|
||||
T[u].lsum=T[lson].lsum;
|
||||
T[u].rsum=T[rson].rsum;
|
||||
if(T[u].lsum==(len+1)>>1)T[u].lsum+=T[rson].lsum;
|
||||
if(T[u].rsum==len>>1)T[u].rsum+=T[lson].rsum;
|
||||
T[u].msum=max(T[lson].msum,T[rson].msum);
|
||||
T[u].msum=max(T[u].msum,T[lson].rsum+T[rson].lsum);
|
||||
T[u].lz=T[lson].lz;
|
||||
T[u].rz=T[rson].rz;
|
||||
if(T[u].lz==(len+1)>>1)T[u].lz+=T[rson].lz;
|
||||
if(T[u].rz==len>>1)T[u].rz+=T[lson].rz;
|
||||
T[u].mz=max(T[lson].mz,T[rson].mz);
|
||||
T[u].mz=max(T[u].mz,T[lson].rz+T[rson].lz);
|
||||
T[u].sum=T[lson].sum+T[rson].sum;
|
||||
}
|
||||
void PushDown(int u){
|
||||
if(T[u].lef==T[u].rig)return;
|
||||
if(T[u].COVER!=-1){
|
||||
int len=T[u].rig-T[u].lef+1;
|
||||
T[lson].COVER=T[rson].COVER=T[u].COVER;
|
||||
T[lson].XOR=T[rson].XOR=0;
|
||||
T[lson].lsum=T[lson].rsum=T[lson].msum=T[u].COVER?(len+1)>>1:0;
|
||||
T[lson].lz=T[lson].rz=T[lson].mz=T[u].COVER?0:(len+1)>>1;
|
||||
T[lson].sum=T[u].COVER?(len+1)>>1:0;
|
||||
T[rson].lsum=T[rson].rsum=T[rson].msum=T[u].COVER?len>>1:0;
|
||||
T[rson].lz=T[rson].rz=T[rson].mz=T[u].COVER?0:len>>1;
|
||||
T[rson].sum=T[u].COVER?len>>1:0;
|
||||
T[u].COVER=-1;
|
||||
}
|
||||
if(T[u].XOR){
|
||||
T[u].XOR=0;
|
||||
T[lson].XOR^=1;
|
||||
T[rson].XOR^=1;
|
||||
makeXOR(lson);
|
||||
makeXOR(rson);
|
||||
}
|
||||
}
|
||||
void Build(int u,int l,int r){
|
||||
T[u].lef=l;
|
||||
T[u].rig=r;
|
||||
T[u].COVER=-1;
|
||||
T[u].XOR=0;
|
||||
if(l==r){
|
||||
T[u].lsum=T[u].rsum=T[u].msum=(dat[l]==1);
|
||||
T[u].lz=T[u].rz=T[u].mz=(dat[l]==0);
|
||||
T[u].sum=dat[l];
|
||||
T[u].COVER=dat[l];
|
||||
return;
|
||||
}
|
||||
int mid=(l+r)>>1;
|
||||
Build(lson,l,mid);
|
||||
Build(rson,mid+1,r);
|
||||
PushUp(u);
|
||||
}
|
||||
void Update(int u,int l,int r,int op){
|
||||
PushDown(u);
|
||||
if(l<=T[u].lef&&T[u].rig<=r){
|
||||
if(op<2){
|
||||
int len=T[u].rig-T[u].lef+1;
|
||||
T[u].COVER=op;
|
||||
T[u].lsum=T[u].rsum=T[u].msum=op?len:0;
|
||||
T[u].lz=T[u].rz=T[u].mz=op?0:len;
|
||||
T[u].sum=op?len:0;
|
||||
}
|
||||
else {
|
||||
T[u].XOR=1;
|
||||
makeXOR(u);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if(l<=T[lson].rig)Update(lson,l,r,op);
|
||||
if(r>=T[rson].lef)Update(rson,l,r,op);
|
||||
PushUp(u);
|
||||
}
|
||||
}
|
||||
int Query(int u,int l,int r,int op){
|
||||
PushDown(u);
|
||||
if(l<=T[u].lef&&T[u].rig<=r){
|
||||
if(op==3)return T[u].sum;
|
||||
else return T[u].msum;
|
||||
}
|
||||
else {
|
||||
if(r<=T[lson].rig)return Query(lson,l,r,op);
|
||||
if(l>=T[rson].lef)return Query(rson,l,r,op);
|
||||
if(op==3) return Query(lson,l,T[lson].rig,op)+Query(rson,T[rson].lef,r,op);
|
||||
int ret=min(T[lson].rsum,T[lson].rig-l+1)+min(T[rson].lsum,r-T[rson].lef+1);
|
||||
int ans=max(Query(lson,l,T[lson].rig,op),Query(rson,T[rson].lef,r,op));
|
||||
return max(ans,ret);
|
||||
}
|
||||
}
|
||||
int main(){
|
||||
int t;
|
||||
scanf("%d",&t);
|
||||
while(t--){
|
||||
int n,m;
|
||||
int cmd,a,b;
|
||||
scanf("%d%d",&n,&m);
|
||||
for(int i=1;i<=n;i++)scanf("%d",dat+i);
|
||||
Build(1,1,n);
|
||||
while(m--){
|
||||
scanf("%d%d%d",&cmd,&a,&b);
|
||||
a++,b++;
|
||||
if(cmd<3)Update(1,a,b,cmd);
|
||||
else printf("%d\n",Query(1,a,b,cmd));
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,111 @@
|
|||
#include <stdio.h>
|
||||
#define MOD 20100501
|
||||
typedef struct
|
||||
{
|
||||
__int64 val,num;
|
||||
}Pri;
|
||||
__int64 prime[2000005];
|
||||
Pri a[200000];
|
||||
__int64 up;
|
||||
void Isprime()
|
||||
{
|
||||
__int64 k,i,j;
|
||||
up=0;
|
||||
for (i=0;i<2000003;i++)
|
||||
{
|
||||
prime[i]=true;
|
||||
}
|
||||
for (i=2;i<2000003;i++)
|
||||
{
|
||||
if (prime[i]==false) continue;
|
||||
k=i;
|
||||
while(i*k<=2000000)
|
||||
{
|
||||
prime[i*k]=false;
|
||||
k++;
|
||||
}
|
||||
a[up++].val=i;
|
||||
}
|
||||
}
|
||||
void Cplus(__int64 t)
|
||||
{
|
||||
__int64 i,s;
|
||||
__int64 k;
|
||||
for (i=0;i<up;i++)
|
||||
{
|
||||
s=0;
|
||||
k=a[i].val;
|
||||
while(t>=k)
|
||||
{
|
||||
s=s+t/k;
|
||||
k=k*a[i].val;
|
||||
}
|
||||
a[i].num=a[i].num+s;
|
||||
}
|
||||
}
|
||||
void Cminus(__int64 t)
|
||||
{
|
||||
__int64 i,s;
|
||||
__int64 k;
|
||||
for (i=0;i<up;i++)
|
||||
{
|
||||
s=0;
|
||||
k=a[i].val;
|
||||
while(t>=k)
|
||||
{
|
||||
s=s+t/k;
|
||||
k=k*a[i].val;
|
||||
}
|
||||
a[i].num=a[i].num-s;
|
||||
}
|
||||
}
|
||||
__int64 Count(__int64 n,__int64 p)
|
||||
{
|
||||
__int64 i,j,s;
|
||||
s=1;
|
||||
while(n!=0)
|
||||
{
|
||||
if (n%2==1)
|
||||
{
|
||||
s=s*p%MOD;
|
||||
}
|
||||
p=p*p%MOD;
|
||||
n=n/2;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
__int64 i,t,j,n,T,m;
|
||||
__int64 ans;
|
||||
scanf("%I64d",&T);
|
||||
Isprime();
|
||||
while(T--)
|
||||
{
|
||||
scanf("%I64d%I64d",&n,&m);
|
||||
for (i=0;i<up;i++)
|
||||
{
|
||||
a[i].num=0;
|
||||
}
|
||||
Cplus(m+n);
|
||||
Cminus(m);
|
||||
Cminus(n+1);
|
||||
t=n+1-m;
|
||||
for (i=0;i<up;i++)
|
||||
{
|
||||
while(t%a[i].val==0)
|
||||
{
|
||||
a[i].num++;
|
||||
t=t/a[i].val;
|
||||
}
|
||||
if (t==1) break;
|
||||
}
|
||||
ans=1;
|
||||
for (i=0;i<up;i++)
|
||||
{
|
||||
ans=ans*Count(a[i].num,a[i].val)%MOD;
|
||||
}
|
||||
printf("%I64d\n",ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
#include <iostream>
|
||||
#include <string.h>
|
||||
using namespace std;
|
||||
const int maxn=1100000,maxm=4*maxn;
|
||||
struct Edge
|
||||
{
|
||||
int y;
|
||||
int ne;
|
||||
} e[maxm];
|
||||
int st[maxn],ee;
|
||||
void addedge(int x,int y)
|
||||
{
|
||||
e[ee].y=y;e[ee].ne=st[x];st[x]=ee++;
|
||||
e[ee].y=x;e[ee].ne=st[y];st[y]=ee++;
|
||||
}
|
||||
int c[maxn];
|
||||
int numn[maxn],numm[maxn],lost[maxn];
|
||||
int s[maxn],head,tail;
|
||||
void bfs(int k,int col)
|
||||
{
|
||||
int i;
|
||||
head=0;tail=1;s[0]=k;
|
||||
c[k]=col;
|
||||
while (head<tail)
|
||||
{
|
||||
k=s[head++];
|
||||
for (i=st[k];i!=-1;i=e[i].ne)
|
||||
{
|
||||
numm[col]++;
|
||||
if (c[e[i].y]==0)
|
||||
{
|
||||
c[e[i].y]=col;
|
||||
s[tail++]=e[i].y;
|
||||
}
|
||||
}
|
||||
}
|
||||
numn[col]=tail;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int cass;
|
||||
for (scanf("%d",&cass);cass--;)
|
||||
{
|
||||
int n;
|
||||
scanf("%d",&n);
|
||||
int i,x,y,z1,z2;
|
||||
memset(st,-1,sizeof(st));
|
||||
ee=0;
|
||||
for (i=0;i<n;i++)
|
||||
{
|
||||
scanf("%d.%d %d.%d",&z1,&x,&z2,&y);
|
||||
x=z1*1000000+x-1500000;
|
||||
y=z2*1000000+y-1500000;
|
||||
addedge(x,y);
|
||||
}
|
||||
memset(c,0,sizeof(c));
|
||||
memset(numn,0,sizeof(numn));
|
||||
memset(numm,0,sizeof(numm));
|
||||
memset(lost,-1,sizeof(lost));
|
||||
int col=0;
|
||||
for (i=0;i<maxn;i++) if (c[i]==0)
|
||||
{
|
||||
col++;
|
||||
bfs(i,col);
|
||||
}
|
||||
for (i=1;i<=col;i++) numm[i]/=2;
|
||||
for (i=0;i<maxn;i++) if (numn[c[i]]>numm[c[i]]) if (lost[c[i]]<i) lost[c[i]]=i;
|
||||
for (i=0;i<maxn;i++)
|
||||
if (lost[c[i]]==i) break;
|
||||
printf("%d\n",i);
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue