Powered By HC TECH : AutoACer Engine

4300-4399
This commit is contained in:
KiritoTRw 2016-09-04 15:55:06 +08:00 committed by GitHub
parent 1c7c39b791
commit 53497060fc
93 changed files with 8047 additions and 0 deletions

54
HDOJ/4300_autoAC.cpp Normal file
View File

@ -0,0 +1,54 @@
#include<iostream>
#include<cstring>
using namespace std;
char a[100001],c[100001];
int ne[100001];
int main()
{
int T,i,j,len,nima[27],mid;
char b[27];
while(scanf("%d",&T)!=EOF)
{
getchar();
while(T--)
{
scanf("%s%s",b+1,a+1);
for(i=1;i<=26;i++)
nima[b[i]-'a']=i-1;
len=strlen(a+1);
for(i=1;i<=len;i++)
c[i]='a'+nima[a[i]-'a'];
i=1;j=0;ne[1]=0;
while(i<len)
{
if(j==0||c[i]==c[j])
{
i++;j++;
ne[i]=j;
}
else
j=ne[j];
}
if(len%2==0)
i=len/2+1;
else
i=len/2+2;
j=1;mid=i;
while(i<=len)
{
if(j==0||a[i]==c[j])
{
i++;j++;
}
else
j=ne[j];
}
for(i=1;i<=len;i++)
printf("%c",a[i]);
for(i=j;i<j+len-2*j+2;i++)
printf("%c",c[i]);
printf("\n");
}
}
return 0;
}

36
HDOJ/4301_autoAC.cpp Normal file
View File

@ -0,0 +1,36 @@
#include <iostream>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <cstdlib>
#include <queue>
#include <algorithm>
using namespace std;
#define N 1050
#define mod 100000007
int dp[N][N*2][2];
int main ()
{
memset(dp,0,sizeof(dp));
dp[1][1][0]=1;
dp[1][2][1]=1;
for(int i=1;i<=1000;++i)
{
for(int j=1;j<=2*i;++j)
{
dp[i+1][j][0]=(dp[i+1][j][0]+dp[i][j][0]+2*dp[i][j][1])%mod;
dp[i+1][j+1][0]=(dp[i+1][j+1][0]+dp[i][j][1]+dp[i][j][0])%mod;
dp[i+1][j][1]=(dp[i+1][j][1]+dp[i][j][1])%mod;
dp[i+1][j+1][1]=(dp[i+1][j+1][1]+2*dp[i][j][0]+2*dp[i][j][1])%mod;
dp[i+1][j+2][1]=(dp[i+1][j+2][1]+dp[i][j][0]+dp[i][j][1])%mod;
}
}
int n,k;
int t;scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
printf("%d\n",(dp[n][k][0]+dp[n][k][1])%mod);
}
return 0;
}

86
HDOJ/4302_autoAC.cpp Normal file
View File

@ -0,0 +1,86 @@
#include <queue>
#include <iostream>
#define LL __int64
using namespace std;
struct cmp
{
bool operator()(LL a, LL b)
{
return a > b;
}
};
int main()
{
int t,l,n,f1,f2;
LL sum,a,b,x,y;
int i,j;
scanf("%d", &t);
for(j = 0; j < t; j++)
{
priority_queue<LL> asc;
priority_queue<LL, vector<LL>, cmp> desc;
scanf("%d%d", &l, &n);
y = 0;
sum = 0;
for(i = 0; i < n; i++)
{
scanf("%d", &f2);
if(!f2)
{
scanf("%I64d", &x);
if(x > y) desc.push(x);
else asc.push(x);
}
else
{
a = b = -1;
if(!asc.empty()) b = asc.top();
if(!desc.empty()) a = desc.top();
if(b == y)
{
asc.pop();
continue;
}
if(a+1 && b+1)
{
if(a-y > y-b)
{
sum += y-b;
y = b;
asc.pop();
f1 = 0;
}
else if(a-y == y-b)
{
sum += a-y;
if(f1) {y = a; desc.pop();}
else {y = b; asc.pop();}
}
else
{
sum += a-y;
y = a;
desc.pop();
f1 = 1;
}
}
else if(a+1)
{
sum += a-y;
y = a;
desc.pop();
f1 = 1;
}
else if(b+1)
{
sum += y-b;;
y = b;
asc.pop();
f1 = 0;
}
}
}
printf("Case %d: %I64d\n", j+1, sum);
}
return 0;
}

71
HDOJ/4303_autoAC.cpp Normal file
View File

@ -0,0 +1,71 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
#define pb push_back
#define NN 301000
typedef pair<int,int> pii;
vector<pii> ed[NN];
long long ans;
int son[NN],fc[NN];
int val[NN];
void dfs2(int u,int fa,int ecol,int fcnt){
ans+=((long long)son[u]-fc[u])*fcnt*val[u];
int i,j,sz=ed[u].size(),v,tcol=-1,vv,tfcnt;
int totc=0,tott=0;
for(i=0,j=0;i<sz;++i){
v=ed[u][i].second;
if (v==fa) continue;
if (tcol!=ed[u][i].first){
tcol=ed[u][i].first;
totc=0;
if (j<sz&&ed[u][j].second==fa) ++j;
for(;j<sz&&ed[u][j].first==tcol;++j){
vv=ed[u][j].second;
if (vv==fa) continue;
totc=totc+son[vv]-fc[vv];
}
tott+=totc;
}
ans+=((long long)son[u]-tott)*(son[v]-fc[v])*val[u];
if (ecol==ed[u][i].first) tfcnt=son[u]-totc;
else tfcnt=son[u]-totc+fcnt;
dfs2(v,u,ed[u][i].first,tfcnt);
}
}
int dfs1(int u,int fa,int ecol){
son[u]=1;
fc[u]=0;
int i,sz=ed[u].size(),v,tmp;
sort(ed[u].begin(),ed[u].end());
for(i=0;i<sz;++i){
if (ed[u][i].second==fa) continue;
v=ed[u][i].second;
tmp=dfs1(v,u,ed[u][i].first);
son[u]+=tmp;
if (ecol==ed[u][i].first) fc[u]+=tmp;
}
return son[u]-fc[u];
}
int main(){
int n,i,a,b,c;
while(scanf("%d",&n)!=EOF){
for(i=1;i<=n;++i){
scanf("%d",&val[i]);
ed[i].clear();
}
for(i=1;i<n;++i){
scanf("%d%d%d",&a,&b,&c);
ed[a].pb(make_pair(c,b));
ed[b].pb(make_pair(c,a));
}
dfs1(1,-1,-1);
ans=0;
dfs2(1,-1,-1,0);
printf("%I64d\n",ans);
}
return 0;
}

89
HDOJ/4304_autoAC.cpp Normal file
View File

@ -0,0 +1,89 @@
#include <cstdio>
#include <cstring>
int coef[20],delt[20],inx[20],base[20];
int mark[1000010],pri[1000010],priN;
__int64 dp[2000][20];
void init()
{
int i,j;
for(i=2; i<=1000000; i++) mark[i]=i;
for(i=2; i<=1000000; i++)
{
if(mark[i]==i) pri[priN++]=i;
for(j=0; j<priN&&i*pri[j]<=1000000; j++)
{
mark[i*pri[j]]=pri[j];
if(i%pri[j]==0) break;
}
}
}
void deal()
{
__int64 u,ans,num,t,tmpv;
int cnt,i,j,k,l,prod,tot,amn,m;
scanf("%I64d", &u);
for(cnt=0,num=u; num-1;)
{
if(num>1000000)
{
t=num;
for(j=0; j<priN; j++)
{
if(num%pri[j]==0)
{
t=pri[j];
break;
}
}
}
else t=mark[num];
inx[cnt]=0;
while(num%t==0) num/=t,inx[cnt]++;
cnt++;
}
for(tot=0,prod=1,i=cnt-1; i>=0; i--)
{
tot+=inx[i],base[i]=prod,prod*=(inx[i]+1);
}
for(i=0; i<=tot; i++) for(j=0; j<prod; j++) dp[j][i]=0;
dp[0][0]=1;
for(i=1; i<prod; i++)
{
for(j=0; j<cnt; j++) delt[j]=i/base[j]%(inx[j]+1);
for(j=prod-1; j>=0; j--)
{
amn=0x3fffffff;
for(k=0; k<cnt; k++)
{
coef[k]=j/base[k]%(inx[k]+1);
if(delt[k])
{
tmpv=(inx[k]-coef[k])/delt[k];
if(tmpv<amn) amn=tmpv;
}
}
for(l=1; l<=amn; l++)
{
for(k=0; k+l<=tot; k++)
{
tmpv=dp[j][k];
for(m=1; m<=l; m++) tmpv*=m+k,tmpv/=m;
dp[j+i*l][k+l]+=tmpv;
}
}
}
}
for(ans=dp[prod-1][tot]*dp[prod-1][tot],i=0; i<tot; i++)
{
ans+=(dp[prod-1][i]+dp[prod-1][i+1])*(dp[prod-1][i]+dp[prod-1][i+1]);
}
printf("%I64d\n", ans);
}
int main()
{
init();
int t;
scanf("%d", &t);
while(t--) deal();
return 0;
}

141
HDOJ/4305_autoAC.cpp Normal file
View File

@ -0,0 +1,141 @@
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <list>
#include <string>
#include <math.h>
using namespace std;
struct Point
{
int x,y;
Point(int _x = 0,int _y = 0)
{
x = _x,y = _y;
}
Point operator - (const Point &b)const
{
return Point(x-b.x,y-b.y);
}
int operator ^(const Point &b)const
{
return x*b.y - y*b.x;
}
void input()
{
scanf("%d%d",&x,&y);
}
};
struct Line
{
Point s,e;
Line(){}
Line(Point _s,Point _e)
{
s = _s;
e = _e;
}
};
bool onSeg(Point P,Line L)
{
return
((L.s-P)^(L.e-P)) == 0 &&
(P.x-L.s.x)*(P.x-L.e.x) <= 0 &&
(P.y-L.s.y)*(P.y-L.e.y) <= 0;
}
int sqdis(Point a,Point b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
const int MOD = 10007;
int INV[MOD];
long long inv(long long a,long long m)
{
if(a == 1)return 1;
return inv(m%a,m)*(m-m/a)%m;
}
struct Matrix
{
int mat[330][330];
void init()
{
memset(mat,0,sizeof(mat));
}
int det(int n)
{
for(int i = 0;i < n;i++)
for(int j = 0;j < n;j++)
mat[i][j] = (mat[i][j]%MOD+MOD)%MOD;
int res = 1;
for(int i = 0;i < n;i++)
{
for(int j = i;j < n;j++)
if(mat[j][i]!=0)
{
for(int k = i;k < n;k++)
swap(mat[i][k],mat[j][k]);
if(i != j)
res = (-res+MOD)%MOD;
break;
}
if(mat[i][i] == 0)
{
res = -1;
break;
}
for(int j = i+1;j < n;j++)
{
int mut = (mat[j][i]*inv(mat[i][i],MOD))%MOD;
for(int k = i;k < n;k++)
mat[j][k] = (mat[j][k]-(mat[i][k]*mut)%MOD+MOD)%MOD;
}
res = (res * mat[i][i])%MOD;
}
return res;
}
};
Point p[330];
int n,R;
bool check(int k1,int k2)
{
if(sqdis(p[k1],p[k2]) > R*R)return false;
for(int i = 0;i < n;i++)
if(i!=k1 && i!=k2)
if(onSeg(p[i],Line(p[k1],p[k2])))
return false;
return true;
}
int g[330][330];
int main()
{
for(int i = 1;i < MOD;i++)
INV[i] = inv(i,MOD);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&R);
for(int i = 0;i < n;i++)
p[i].input();
memset(g,0,sizeof(g));
for(int i = 0;i < n;i++)
for(int j = i+1;j <n;j++)
if(check(i,j))
g[i][j] = g[j][i] = 1;
Matrix ret;
ret.init();
for(int i = 0;i < n;i++)
for(int j = 0;j < n;j++)
if(i != j && g[i][j])
{
ret.mat[i][j] = -1;
ret.mat[i][i]++;
}
printf("%d\n",ret.det(n-1));
}
return 0;
}

343
HDOJ/4306_autoAC.cpp Normal file
View File

@ -0,0 +1,343 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 305;
const int MAXM = 100010;
struct Edge
{
int v, next;
} shift[MAXN*MAXN*2], edge[MAXN*MAXN*4];
int shiftNumber, edgeNumber;
int shiftHead[MAXN*MAXN*2], edgeHead[MAXN*MAXN*2];
int a[MAXN][MAXN], b[MAXN][MAXN];
int al[MAXN*MAXN], bl[MAXN*MAXN];
int ta[MAXN], tb[MAXN];
int na[MAXM], nb[MAXM];
int pa[MAXM][2], pb[MAXM][2];
bool va[MAXM], vb[MAXM];
int n, nn, ca, cb;
int differ[MAXN*MAXN], differNumber;
int maxNumber;
bool inStack[MAXN*MAXN*2];
int dfn[MAXN*MAXN*2], low[MAXN*MAXN*2];
int stack[MAXN*MAXN*2], belong[MAXN*MAXN*2];
int top, index, componentNumber;
inline int min(int x, int y)
{
return x < y ? x : y;
}
inline int max(int x, int y)
{
return x > y ? x : y;
}
inline void swap(int &x, int &y)
{
int temp = x;
x = y;
y = temp;
}
inline int getPosition(int x, int y)
{
return x * n + y;
}
inline int getRow(int pos)
{
return pos / n;
}
inline int getColumn(int pos)
{
return pos % n;
}
void clearEdges()
{
shiftNumber = 0;
edgeNumber = 0;
memset(shiftHead, -1, sizeof(shiftHead));
memset(edgeHead, -1, sizeof(edgeHead));
}
inline void addShift(int u, int v)
{
shift[shiftNumber].v = v;
shift[shiftNumber].next = shiftHead[u];
shiftHead[u] = shiftNumber ++;
}
inline void addEdge(int u, int v)
{
edge[edgeNumber].v = v;
edge[edgeNumber].next = edgeHead[u];
edgeHead[u] = edgeNumber ++;
}
void transpose()
{
for(int i=0; i<n; ++i)
{
for(int j=i+1; j<n; ++j)
{
swap(a[i][j], a[j][i]);
swap(b[i][j], b[j][i]);
}
}
}
void getDifference()
{
differ[al[0]] = 0;
differNumber = 1;
for(int i=1; i<nn; ++i)
{
if(al[i] != al[i-1])
{
differ[al[i]] = differNumber ++;
}
}
}
void getNumberPosition()
{
memset(na, 0, sizeof(na));
memset(nb, 0, sizeof(nb));
for(int i=0; i<n; ++i)
{
for(int j=0; j<n; ++j)
{
pa[a[i][j]][na[a[i][j]]++] = getPosition(i, j);
pb[b[i][j]][nb[b[i][j]]++] = getPosition(i, j);
}
}
}
bool isUnreachable()
{
sort(al, al + nn);
sort(bl, bl + nn);
for(int i=0; i<nn; ++i)
{
if(al[i] != bl[i])
{
return true;
}
}
return false;
}
bool isReachableByOneStep()
{
for(int i=0; i<n; ++i)
{
for(int j=0; j<n; ++j)
{
ta[j] = a[i][j];
tb[j] = b[i][j];
}
sort(ta, ta + n);
sort(tb, tb + n);
for(int j=0; j<n; ++j)
{
if(ta[j] != tb[j])
{
return false;
}
}
}
return true;
}
int myStack[MAXN * MAXN * 8];
int myStackTop;
void push(int x)
{
myStack[myStackTop ++] = x;
}
int pop()
{
return myStack[-- myStackTop];
}
bool isEmpty()
{
return myStackTop == 0;
}
void dfs(int x, int depth)
{
int i, t;
myStackTop = 0;
start:
dfn[x] = low[x] = index ++;
inStack[x] = true;
stack[top++] = x;
for(i=edgeHead[x]; i!=-1; i=edge[i].next)
{
if(dfn[edge[i].v] == -1)
{
push(x);
push(depth);
push(i);
push(t);
x = edge[i].v;
depth = depth + 1;
goto start;
ret:
low[x] = min(low[x], low[edge[i].v]);
}
else if(inStack[edge[i].v])
{
low[x] = min(low[x] ,dfn[edge[i].v]);
}
}
if(dfn[x] == low[x])
{
do
{
t = stack[--top];
inStack[t] = false;
belong[t] = componentNumber;
}
while(t != x);
++ componentNumber;
}
if(isEmpty())
{
return;
}
t = pop();
i = pop();
depth = pop();
x = pop();
goto ret;
}
void tarjan()
{
memset(inStack, false, sizeof(inStack));
memset(dfn, -1, sizeof(dfn));
memset(low, -1, sizeof(low));
top = index = componentNumber = 0;
for(int i=0; i<differNumber*2; ++i)
{
if(dfn[i] == -1)
{
dfs(i, 0);
}
}
}
bool isReachableByTwoStep()
{
clearEdges();
for(int i=1; i<=maxNumber; ++i)
{
if(na[i] == 1)
{
addShift(getPosition(getRow(pa[i][0]), getColumn(pb[i][0])), differ[i]<<1);
addShift(getPosition(getRow(pa[i][0]), getColumn(pb[i][0])), (differ[i]<<1) + 1);
}
else if(na[i] == 2)
{
addShift(getPosition(getRow(pa[i][0]), getColumn(pb[i][1])), differ[i]<<1);
addShift(getPosition(getRow(pa[i][0]), getColumn(pb[i][0])), (differ[i]<<1) + 1);
addShift(getPosition(getRow(pa[i][1]), getColumn(pb[i][0])), differ[i]<<1);
addShift(getPosition(getRow(pa[i][1]), getColumn(pb[i][1])), (differ[i]<<1) + 1);
}
}
for(int i=0; i<n; ++i)
{
for(int j=0; j<n; ++j)
{
for(int k=shiftHead[getPosition(i, j)]; k!=-1; k=shift[k].next)
{
for(int l=shift[k].next; l!=-1; l=shift[l].next)
{
if((shift[k].v ^ 1) == shift[l].v)
{
continue;
}
addEdge(shift[k].v, shift[l].v ^ 1);
addEdge(shift[l].v, shift[k].v ^ 1);
}
}
}
}
tarjan();
for(int i=0; i<differNumber; ++i)
{
if(belong[i<<1] == belong[(i<<1)+1])
{
return false;
}
}
return true;
}
int main()
{
int caseNumber;
scanf("%d", &caseNumber);
for(int cas=1; cas<=caseNumber; ++cas)
{
scanf("%d%d%d", &n, &ca, &cb);
nn = n * n;
bool isSame = true;
maxNumber = 0;
for(int i=0,k=0; i<n; ++i)
{
for(int j=0; j<n; ++j)
{
scanf("%d", &a[i][j]);
al[k++] = a[i][j];
maxNumber = max(maxNumber, a[i][j]);
}
}
for(int i=0,k=0; i<n; ++i)
{
for(int j=0; j<n; ++j)
{
scanf("%d", &b[i][j]);
bl[k++] = b[i][j];
if(a[i][j] != b[i][j])
{
isSame = false;
}
}
}
printf("Case #%d: ", cas);
if(isSame)
{
printf("0\n");
}
else
{
if(isUnreachable())
{
printf("Take off the shoes!\n");
}
else
{
int ans = min(ca * 2 + cb, cb * 2 + ca);
if(isReachableByOneStep())
{
ans = ca;
}
transpose();
if(ans > cb)
{
if(isReachableByOneStep())
{
ans = cb;
}
}
if(ans > ca + cb)
{
getDifference();
getNumberPosition();
if(isReachableByTwoStep())
{
ans = ca + cb;
}
else
{
transpose();
getNumberPosition();
if(isReachableByTwoStep())
{
ans = ca + cb;
}
}
}
printf("%d\n", ans);
}
}
}
return 0;
}

91
HDOJ/4307_autoAC.cpp Normal file
View File

@ -0,0 +1,91 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAXD 1010
#define MAXM 2004010
#define INF 0x7fffffff
int N, first[MAXD], e, v[MAXM], next[MAXM], flow[MAXM];
int S, T, d[MAXD], q[MAXD], work[MAXD];
long long SUM;
void add(int x, int y, int z)
{
v[e] = y, flow[e] = z;
next[e] = first[x], first[x] = e ++;
}
void init()
{
int i, j, x, a;
scanf("%d", &N);
S = 0, T = N + 1;
memset(first, -1, sizeof(first[0]) * (T + 1));
SUM = e = 0;
for(i = 1; i <= N; i ++)
{
a = 0;
for(j = 1; j <= N; j ++)
{
scanf("%d", &x), a += x;
add(i, j, x), add(j, i, 0);
}
SUM += a;
add(S, i, a), add(i, S, 0);
}
for(i = 1; i <= N; i ++)
{
scanf("%d", &x);
add(i, T, x), add(T, i, 0);
}
}
int bfs()
{
int i, j, rear = 0;
memset(d, -1, sizeof(d[0]) * (T + 1));
d[S] = 0, q[rear ++] = S;
for(i = 0; i < rear; i ++)
for(j = first[q[i]]; j != -1; j = next[j])
if(flow[j] && d[v[j]] == -1)
{
d[v[j]] = d[q[i]] + 1, q[rear ++] = v[j];
if(v[j] == T) return 1;
}
return 0;
}
int dfs(int cur, int a)
{
if(cur == T)
return a;
for(int &i = work[cur]; i != -1; i = next[i])
if(flow[i] && d[v[i]] == d[cur] + 1)
if(int t = dfs(v[i], std::min(a, flow[i])))
{
flow[i] -= t, flow[i ^ 1] += t;
return t;
}
return 0;
}
long long dinic()
{
long long ans = 0;
while(bfs())
{
memcpy(work, first, sizeof(first[0]) * (T + 1));
while(int t = dfs(S, INF))
ans += t;
}
return ans;
}
void solve()
{
printf("%lld\n", SUM - dinic());
}
int main()
{
int t;
scanf("%d", &t);
while(t --)
{
init();
solve();
}
return 0;
}

34
HDOJ/4310_autoAC.cpp Normal file
View File

@ -0,0 +1,34 @@
#include <stdio.h>
#include <algorithm>
using namespace std;
struct Node
{
int dps;
int hp;
}a[30];
int cmp(Node x,Node y)
{
return x.hp*y.dps<y.hp*x.dps;
}
int main()
{
int t,i;
int sum,ans;
while(~scanf("%d",&t))
{
sum = ans = 0;
for(i = 0;i<t;i++)
{
scanf("%d%d",&a[i].dps,&a[i].hp);
sum+=a[i].dps;
}
sort(a,a+t,cmp);
for(i = 0;i<t;i++)
{
ans+=sum*a[i].hp;
sum-=a[i].dps;
}
printf("%d\n",ans);
}
return 0;
}

55
HDOJ/4311_autoAC.cpp Normal file
View File

@ -0,0 +1,55 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 100010
using namespace std;
struct point{long long x,y;}a[MAXN];
bool cmp(point A,point B)
{
if(A.x<B.x) return true;
if(A.x==B.x&&A.y<B.y) return true;
return false;
}
long long ABS(long long A)
{
if(A<0) A=-A;
return A;
}
long long work(int k,int n)
{
int i;
long long ans=0;
for(i=0;i<n;i++)
{
ans+=ABS(a[k].x-a[i].x);
ans+=ABS(a[k].y-a[i].y);
}
return ans;
}
int main()
{
int test,n,s,e,i;
long long ans,t;
scanf("%d",&test);
while(test--)
{
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%I64d%I64d",&a[i].x,&a[i].y);
}
sort(a,a+n,cmp);
s=n/2-250;e=n/2+250;
if(s<0) s=0;
if(e>n-1) e=n-1;
ans=1;ans<<=60;
for(i=s;i<=e;i++)
{
t=work(i,n);
if(ans>t) ans=t;
}
cout<<ans<<endl;
}
return 0;
}

59
HDOJ/4312_autoAC.cpp Normal file
View File

@ -0,0 +1,59 @@
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long int Int64;
int N;
struct Point
{
Int64 x, y, sum;
}e[100005];
bool cmpx(Point a, Point b)
{
return a.x < b.x;
}
bool cmpy(Point a, Point b)
{
return a.y < b.y;
}
int main()
{
int T, t;
Int64 sum, Min;
scanf("%d", &T);
while (T--) {
Min = 1LL << 62;
scanf("%d", &N);
for (int i = 1; i <= N; ++i) {
scanf("%I64d %I64d", &e[i].x, &e[i].y);
t = e[i].x, e[i].x += e[i].y;
e[i].y -= t, e[i].sum = 0;
}
sort(e+1, e+1+N, cmpx);
sum = 0;
for (int i = 1; i <= N; ++i) {
e[i].sum += (i-1) * e[i].x - sum;
sum += e[i].x;
}
sum = 0;
for (int i = N; i >= 1; --i) {
e[i].sum += sum - (N-i) * e[i].x;
sum += e[i].x;
}
sort(e+1, e+1+N, cmpy);
sum = 0;
for (int i = 1; i <= N; ++i) {
e[i].sum += (i-1) * e[i].y - sum;
sum += e[i].y;
}
sum = 0;
for (int i = N; i >= 1; --i) {
e[i].sum += sum - (N-i) * e[i].y;
Min = min(Min, e[i].sum);
sum += e[i].y;
}
printf("%I64d\n", Min >> 1);
}
return 0;
}

77
HDOJ/4313_autoAC.cpp Normal file
View File

@ -0,0 +1,77 @@
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
const int maxn = 100010;
int t, n, k;
int mach[maxn], father[maxn];
struct Edge {
int u, v, c;
}edge;
vector<Edge> vv;
bool cmp(Edge a, Edge b)
{
return a.c > b.c;
}
void initSet()
{
for (int i = 0; i < maxn; ++i) {
father[i] = i;
mach[i] = 0;
}
}
int find(int x)
{
if (x != father[x]) {
father[x] = find(father[x]);
}
return father[x];
}
void merge(int a, int b)
{
a = find(a);
b = find(b);
if (a == b) return ;
if (a < b) {
father[b] = a;
mach[a] += mach[b];
} else {
father[a] = b;
mach[b] += mach[a];
}
}
int main()
{
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &k);
vv.clear();
initSet();
int u, v, c;
__int64 ans = 0;
for (int i = 0; i < n - 1; ++i) {
scanf("%d%d%d", &u, &v, &c);
edge.u = u; edge.v = v; edge.c = c;
vv.push_back(edge);
ans += c;
}
for (int i = 0; i < k; ++i) {
scanf("%d", &u);
mach[u] = 1;
}
sort(vv.begin(), vv.end(), cmp);
for (int i = 0; i < n - 1; ++i) {
u = find(vv[i].u);
v = find(vv[i].v);
if (mach[u] + mach[v] <= 1) {
merge(u, v);
ans -= vv[i].c;
}
}
printf("%I64d\n", ans);
}
return 0;
}

44
HDOJ/4314_autoAC.cpp Normal file
View File

@ -0,0 +1,44 @@
#include<iostream>
#include<stdio.h>
#include<algorithm>
const int N=3000;
const int inf=1e9;
using namespace std;
struct node
{
int a,b;
void read(){ scanf("%d%d",&a,&b); }
bool operator <(const node tmp)const { return a+b<tmp.a+tmp.b; }
}no[N];
int opt[N][N],n,h;
int solve()
{
int sum=0;
for(int i=1;i<=n;i++) sum+=no[i].a;
for(int j=1;j<=n;j++) opt[0][j]=inf;
opt[0][0]=0;
for(int i=1;i<=n;i++)
{
opt[i][0]=0;
for(int j=1;j<=n;j++){
opt[i][j]=opt[i-1][j];
if(sum-opt[i-1][j-1]+no[i].b>=h)
opt[i][j]=min(opt[i][j],opt[i-1][j-1]+no[i].a);
}
}
for(int j=n;j>=0;j--)
if(opt[n][j]!=inf)
return j;
}
int main()
{
while(scanf("%d",&n)!=-1)
{
for(int i=1;i<=n;i++)
no[i].read();
scanf("%d",&h);
sort(no+1,no+n+1);
cout<<solve()<<endl;
}
return 0;
}

27
HDOJ/4315_autoAC.cpp Normal file
View File

@ -0,0 +1,27 @@
#include<stdio.h>
#define N 1001
int n,k,a[N],i,tmp,sg;
int main()
{
while(scanf("%d%d",&n,&k)!=-1)
{
a[0]=-1;
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
if(k==1)
{
puts("Alice");
continue;
}
sg=0;
for(i=n;i>0;i-=2)
sg^=(tmp=a[i]-a[i-1]-1);
if(n%2==1&&k==2)
sg=sg^(tmp-1)^tmp;
if(sg==0)
puts("Bob");
else
puts("Alice");
}
return 0;
}

216
HDOJ/4316_autoAC.cpp Normal file
View File

@ -0,0 +1,216 @@
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAXN = 1001;
const double eps = 1e-12;
int dblcmp(double x)
{
if(fabs(x) < eps)
{
return 0;
}
return x > 0 ? 1 : -1;
}
struct Point
{
double x, y, z;
inline void input2d()
{
scanf("%lf%lf",&x,&y);
z = 100.0;
}
inline void input3d()
{
scanf("%lf%lf%lf",&x,&y,&z);
}
bool operator < (const Point &point) const
{
if(y == point.y)
{
return x < point.x;
}
return y < point.y;
}
}machine[MAXN], light[3], shadow[3][MAXN], final[MAXN * 3];
int n, shadowNumber[3], shadowPoly[3][MAXN], shadowPolyTop[3], finalNumber;
double operator * (const Point &x, const Point &y)
{
return x.x * y.y - x.y * y.x;
}
Point operator - (const Point &x, const Point &y)
{
Point r;
r.x = x.x - y.x;
r.y = x.y - y.y;
return r;
}
bool operator == (const Point &a, const Point &b)
{
return fabs(a.x - b.x) < eps && fabs(a.y - b.y) < eps;
}
struct Line
{
Point a, b;
double ang;
}line[MAXN*3], stack[MAXN*3];
int lineNumber, stackTop;
bool operator < (const Line &x, const Line &y)
{
if(fabs(x.ang - y.ang) < eps)
{
return (y.b - x.a) * (x.b - y.a) > eps;
}
return x.ang < y.ang;
}
Point operator * (const Line &x, const Line &y)
{
double a1 = (y.b - x.a) * (y.a - x.a);
double a2 = (y.a - x.b) * (y.b - x.b);
Point r;
r.x = (x.a.x * a2 + x.b.x * a1) / (a1 + a2);
r.y = (x.a.y * a2 + x.b.y * a1) / (a1 + a2);
return r;
}
bool mult(const Point &s, const Point &e, const Point &o)
{
return (s.x - o.x) * (e.y - o.y) >= (e.x - o.x) * (s.y - o.y);
}
void graham(Point p[], int n, int res[], int &top)
{
int len;
top = 1;
sort(p, p + n);
if(n == 0) return;
res[0] = 0;
if(n == 1) return;
res[1] = 1;
if(n == 2) return;
res[2] = 2;
for(int i=2;i<n;++i)
{
while(top && mult(p[i], p[res[top]], p[res[top-1]]))
{
-- top;
}
res[++top] = i;
}
len = top;
res[++top] = n - 2;
for(int i=n-3;i>=0;--i)
{
while(top!=len && mult(p[i], p[res[top]], p[res[top-1]]))
{
-- top;
}
res[++top] = i;
}
}
double cross(Point &a, Point &b, Point &o)
{
return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
}
bool judgeOut(const Line &x, const Point &p)
{
return (p - x.a) * (x.b - x.a) > eps;
}
bool isParellel(const Line &x, const Line &y)
{
return fabs((x.b - x.a) * (y.b - y.a)) < eps;
}
double getArea(Point p[], int n)
{
double ans = 0.0;
for(int i=2;i<n;++i)
{
ans += fabs(cross(p[i], p[i-1], p[0]));
}
return ans * 0.5;
}
void halfPlane()
{
finalNumber = 0;
sort(line, line + lineNumber);
stackTop = 1;
int bottom = 0, tmp = 1;
for(int i=1;i<lineNumber;++i)
{
if(line[i].ang - line[i-1].ang > eps)
{
line[tmp++] = line[i];
}
}
lineNumber = tmp;
stack[0] = line[0], stack[1] = line[1];
for(int i=2;i<lineNumber;++i)
{
if(isParellel(stack[stackTop], stack[stackTop-1]) || isParellel(stack[bottom], stack[bottom+1]))
{
return;
}
while(bottom < stackTop && judgeOut(line[i], stack[stackTop] * stack[stackTop - 1]))
{
-- stackTop;
}
while(bottom < stackTop && judgeOut(line[i], stack[bottom] * stack[bottom + 1]))
{
++ bottom;
}
stack[++stackTop] = line[i];
}
while(bottom < stackTop && judgeOut(stack[bottom], stack[stackTop] * stack[stackTop - 1]))
{
-- stackTop;
}
while(bottom < stackTop && judgeOut(stack[stackTop], stack[bottom] * stack[bottom + 1]))
{
++ bottom;
}
if(stackTop <= bottom + 1)
{
return;
}
stack[++stackTop] = stack[bottom];
for(int i=bottom;i<stackTop;++i)
{
final[finalNumber ++] = stack[i] * stack[i+1];
}
}
int main()
{
while(~scanf("%d", &n))
{
for(int i=0;i<n;++i)
{
machine[i].input3d();
}
for(int i=0;i<3;++i)
{
light[i].input2d();
}
lineNumber = 0;
for(int i=0;i<3;++i)
{
shadowNumber[i] = 0;
for(int j=0;j<n;++j)
{
shadow[i][j].x = 100.0 * (light[i].x - machine[j].x) / (machine[j].z - 100.0) + light[i].x;
shadow[i][j].y = 100.0 * (light[i].y - machine[j].y) / (machine[j].z - 100.0) + light[i].y;
}
graham(shadow[i], n, shadowPoly[i], shadowPolyTop[i]);
shadowPoly[i][shadowPolyTop[i]] = shadowPoly[i][0];
for(int j=0;j<shadowPolyTop[i];++j)
{
line[lineNumber].a = shadow[i][shadowPoly[i][j]];
line[lineNumber].b = shadow[i][shadowPoly[i][j+1]];
line[lineNumber].ang = atan2(line[lineNumber].b.y - line[lineNumber].a.y,
line[lineNumber].b.x - line[lineNumber].a.x);
++ lineNumber;
}
}
halfPlane();
printf("%.2lf\n", getArea(final, finalNumber));
}
return 0;
}

79
HDOJ/4317_autoAC.cpp Normal file
View File

@ -0,0 +1,79 @@
#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
const int M=1<<11;
const int MAX=0x1f1f1f1f;
int s[25],dp[25][M];
int t[M];
int a[11];
int main()
{
int N,i,j,k,len,tmp,r;
for(i=0;i<M;i++)
{
tmp=0;
j=i;
while(j!=0)
{
tmp+=(j&1);
j=j>>1;
}
t[i]=tmp;
}
while(scanf("%d",&N)!=EOF)
{
memset(s,0,sizeof(s));;
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
if(N<2)
{
printf("impossible\n");
continue;
}
for(i=1;i<=22;i++)
{
for(j=0;j<N;j++)
{
if(a[j]&(1<<(i-1)))
{
s[i]|=(1<<j);
}
}
if(s[i]!=0)
len=i+1;
}
memset(dp,MAX,sizeof(dp));
dp[0][0]=0;
for(r=1;r<=len;r++)
{
for(j=0;j<(1<<N);j++)
{
if(dp[r-1][j]<MAX)
{
tmp=j&s[r];
for(k=tmp;k<(1<<N);k++)
{
if((k&tmp)==tmp&&
((s[r]^j)&(k^tmp))==(k^tmp)&&
((t[(s[r]^j)^(k^tmp)]&1)==0||t[(s[r]^j)^(k^tmp)]<N))
{
dp[r][k]=min(dp[r][k],dp[r-1][j]+(t[k^tmp]+(t[(s[r]^j)^(k^tmp)]&1))*(1<<(r-1)));
}
}
}
}
}
int Min=MAX;
for(j=0;j<(1<<N);j++)
{
if((s[j]&1)==0)
Min=min(Min,dp[len][j]);
}
printf("%d\n",Min);
}
return 0;
}

56
HDOJ/4318_autoAC.cpp Normal file
View File

@ -0,0 +1,56 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct p
{
int a[50][2];
}p[50010];
double b[51000];
queue<int> q;
int s,t,n;
double m;
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(p,-1,sizeof(p));
for(int i=0;i<51000;i++)
b[i]=-1;
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
for(int j=0;j<x;j++)
{
scanf("%d",&p[i].a[j][0]);
scanf("%d",&p[i].a[j][1]);
}
}
scanf("%d%d%lf",&s,&t,&m);
b[s]=0;
q.push(s);
int hd;
while(!q.empty())
{
hd=q.front();
q.pop();
for(int i=0;i<50;i++)
{
if(p[hd].a[i][0]==hd)
continue;
if(p[hd].a[i][0]!=-1&&(b[p[hd].a[i][0]]+1<1e-8||b[p[hd].a[i][0]]>b[hd]+(100.0-b[hd])*p[hd].a[i][1]/100))
{
q.push(p[hd].a[i][0]);
b[p[hd].a[i][0]]=b[hd]+(100.0-b[hd])*p[hd].a[i][1]/100;
}
}
}
if(b[t]+1<1e-8)
cout<<"IMPOSSIBLE!\n";
else
printf("%.2lf\n",m*b[t]/100);
}
return 0;
}

183
HDOJ/4319_autoAC.cpp Normal file
View File

@ -0,0 +1,183 @@
#include <cstdio>
#include <cstring>
#include <cmath>
const int MAXN = 100010;
struct Point
{
__int64 x, y;
}point[MAXN], high[2][MAXN], low[2][MAXN], merge[MAXN * 2];
int pointNumber, highNumber[2], lowNumber[2], mergeNumber;
int n, stack[MAXN], top;
__int64 a[MAXN];
Point operator + (const Point &a, const Point &b)
{
Point r;
r.x = a.x + b.x;
r.y = a.y + b.y;
return r;
}
Point operator - (const Point &a, const Point &b)
{
Point r;
r.x = a.x - b.x;
r.y = a.y - b.y;
return r;
}
inline __int64 operator * (const Point &a, const Point &b)
{
return a.x * b.y - a.y * b.x;
}
inline __int64 absolute(int x)
{
return x >= 0 ? x : -x;
}
inline double max(const double &x, const double &y)
{
return x > y ? x : y;
}
inline __int64 cross(const Point &a, const Point &b, const Point &o)
{
return (a - o) * (b - o);
}
void getBound(Point result[], int &resultNumber, bool isHigh)
{
if(pointNumber == 1)
{
result[0] = point[0];
resultNumber = 1;
return;
}
stack[0] = 0;
stack[1] = 1;
top = 2;
if(isHigh)
{
for(int i=2;i<pointNumber;++i)
{
while(top >= 2 && cross(point[stack[top-2]], point[i], point[stack[top-1]]) < 0)
{
-- top;
}
stack[top++] = i;
}
}
else
{
for(int i=2;i<pointNumber;++i)
{
while(top >= 2 && cross(point[stack[top-2]], point[i], point[stack[top-1]]) > 0)
{
-- top;
}
stack[top++] = i;
}
}
resultNumber = top;
for(int i=0;i<top;++i)
{
result[i].x = point[stack[i]].x;
result[i].y = point[stack[i]].y;
}
}
void mergeBound(Point p1[], int len1, Point p2[], int len2, bool isHigh)
{
merge[mergeNumber++] = p1[0] + p2[0];
int pNum1 = 1, pNum2 = 1;
while(pNum1 < len1 || pNum2 < len2)
{
if(pNum1 == len1)
{
merge[mergeNumber++] = p1[pNum1 - 1] + p2[pNum2 ++];
}
else if(pNum2 == len2)
{
merge[mergeNumber++] = p1[pNum1 ++] + p2[pNum2 - 1];
}
else
{
Point temp0 = p1[pNum1 - 1] + p2[pNum2 - 1];
Point temp1 = p1[pNum1] + p2[pNum2 - 1];
Point temp2 = p1[pNum1 - 1] + p2[pNum2];
if(isHigh)
{
if(cross(temp1, temp2, temp0) < 0)
{
merge[mergeNumber++] = temp1;
++ pNum1;
}
else
{
merge[mergeNumber++] = temp2;
++ pNum2;
}
}
else
{
if(cross(temp1, temp2, temp0) > 0)
{
merge[mergeNumber++] = temp1;
++ pNum1;
}
else
{
merge[mergeNumber++] = temp2;
++ pNum2;
}
}
}
}
}
double solve(int l, int r)
{
if(l == r)
{
return absolute(a[l]);
}
int mid = (l + r) >> 1;
double ansL = solve(l, mid);
double ansR = solve(mid + 1, r);
double ans = max(ansL, ansR);
__int64 sum = 0;
pointNumber = 0;
for(int i=mid;i>=l;--i)
{
sum += a[i];
point[pointNumber].x = mid - i + 1;
point[pointNumber].y = sum;
++ pointNumber;
}
getBound(high[0], highNumber[0], true);
getBound(low[0], lowNumber[0], false);
sum = 0;
pointNumber = 0;
for(int i=mid+1;i<=r;++i)
{
sum += a[i];
point[pointNumber].x = i - mid;
point[pointNumber].y = sum;
++ pointNumber;
}
getBound(high[1], highNumber[1], true);
getBound(low[1], lowNumber[1], false);
mergeNumber = 0;
mergeBound(high[0], highNumber[0], high[1], highNumber[1], true);
mergeBound(low[0], lowNumber[0], low[1], lowNumber[1], false);
for(int i=0;i<mergeNumber;++i)
{
ans = max(ans, fabs((double)merge[i].y) / sqrt((double)merge[i].x));
}
return ans;
}
int main()
{
while(~scanf("%d", &n))
{
for(int i=0;i<n;++i)
{
scanf("%I64d", &a[i]);
}
double ans = solve(0, n - 1);
printf("%.6lf\n", ans * ans);
}
return 0;
}

57
HDOJ/4320_autoAC.cpp Normal file
View File

@ -0,0 +1,57 @@
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long int Int64;
int p[1000005], rec[100005], cnt;
void getprime()
{
int k;
for (int i = 4; i <= 1000000; i += 2) {
p[i] = 1;
}
for (int i = 3; i <= 1000; i += 2) {
k = 2 * i;
for (int j = i * i; j <= 1000000; j += k) {
p[j] = 1;
}
}
rec[1] = 2;
cnt = 1;
for (int i = 3; i <= 1000000; i += 2) {
if (!p[i]) {
rec[++cnt] = i;
}
}
}
int main()
{
int T, LIM, flag, ca = 0;
Int64 a, b;
getprime();
scanf("%d", &T);
while (T--) {
flag = 0;
scanf("%I64d %I64d", &a, &b);
LIM = (int)sqrt(double(a));
for (int i = 1; rec[i] <= LIM; ++i) {
if (a % rec[i] == 0) {
if (b % rec[i] != 0) {
flag = 1;
break;
}
while (a % rec[i] == 0) {
a /= rec[i];
}
}
}
if (a != 1 && b % a != 0) {
flag = 1;
}
printf("Case #%d: ", ++ca);
printf(flag ? "NO\n" : "YES\n");
}
return 0;
}

48
HDOJ/4321_autoAC.cpp Normal file
View File

@ -0,0 +1,48 @@
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
typedef long long LL;
void Solve(LL a,LL b,LL n)
{
LL cnt=0;
LL max=b+a*n;
for(LL i=0;i<64;i++)
{
LL m=(LL)1<<i;
LL mm=m;
if(m>max) break;
m<<=1;
LL cur=a+b;
LL j=0;
while(j<m&&j<n)
{
LL upper=cur+(mm-(cur%mm))-(LL)1;
LL step=(upper-cur)/a+(LL)1;
if(j+step>=n) step=n-j;
if(j+step>=m) step=m-j;
if(cur&(LL)1<<i)
{
cnt+=step*(n/m);
if(j+step<(n%m)) cnt+=step;
else if(j<(n%m)) cnt+=(n%m)-j;
}
cur+=a*step;
j+=step;
}
}
cout<<cnt<<endl;
}
int main()
{
int t,k=1;
LL a,b,n,i,j;
cin>>t;
while(t--)
{
cin>>a>>b>>n;
cout<<"Case #"<<k++<<": ";
Solve(a,b,n);
}
return 0;
}

115
HDOJ/4323_autoAC.cpp Normal file
View File

@ -0,0 +1,115 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<string>
#include<math.h>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
int dp[40][40];
char s1[100], s2[100], st[10010][30];
const int inf = 0x7f7f7f7f;
struct node
{
char word[30];
node *next[30];
}root;
node p[100000];
int num, flag, vnum, fuck;
map<string,int>mp;
int f[100000];
void init( )
{
for( int i = 0; i < 40; i++)
for( int j = 0; j < 40; j++)
dp[i][j] = inf;
}
int diff( char *s1, char *s2)
{
init();
int x = strlen(s1+1);
int y = strlen(s2+1);
for( int i = 0; i <= x; i++)
dp[i][0] = i;
for( int j = 0; j <= y; j++)
dp[0][j] = j;
for( int i = 1; i <= x; i++)
{
for( int j = 1; j <= y; j++)
{
dp[i][j] = min(min(dp[i-1][j]+1, dp[i][j-1]+1), dp[i-1][j-1]+ !(s1[i]==s2[j]) );
}
}
return dp[x][y];
}
void insert(node *q, char *str)
{
node *l = q;
while( l )
{
int dis = diff( l->word, str);
if( ! l->next[dis] )
{
l->next[dis] = &p[num++];
strcpy(l->next[dis]->word + 1, str + 1);
break;
}
l = l->next[dis];
}
}
void sfind(node *q, char *str, int d)
{
if( flag )
return ;
node *l = q;
if( l == NULL )
return;
int dis = diff(str, l->word);
if( dis <= d )
{
fuck++;
}
for( int x = dis-d; x <= dis+d; x++)
{
if( x >= 0 && x <= 20 && l->next[x] )
sfind(l->next[x], str, d);
}
}
int main( )
{
int N, M, d, cnt, T, abc = 1;
char str[1000];
scanf("%d",&T);
while( T-- )
{
scanf("%d%d",&N,&M);
memset(p,0,sizeof(p));
for( int i = 0; i < 30; i++)
root.next[i] = NULL;
num = 0;
int cnum = 1;
strcpy(st[0] + 1, root.word+1);
for( int i = 1; i <= N; i++)
{
scanf("%s",st[i]+1);
insert(&root, st[i]);
}
d = 1;
printf("Case #%d:\n", abc++);
for( int i = 1; i <= M; i++)
{
vnum = 0;
flag = 0;
fuck = 0;
scanf("%s%d",str+1, &d);
sfind(&root, str, d);
printf("%d\n", fuck);
}
}
return 0;
}

42
HDOJ/4324_autoAC.cpp Normal file
View File

@ -0,0 +1,42 @@
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=2010;
int n,indeg[N];
char str[N][N];
int main(){
int t,cases=0;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int flag=0;
memset(indeg,0,sizeof(indeg));
int i,j;
for(i=0;i<n;i++){
scanf("%s",str[i]);
for(j=0;j<n;j++)
if(str[i][j]=='1')
indeg[j]++;
}
for(i=0;i<n;i++){
for(j=0;j<n;j++)
if(indeg[j]==0)
break;
if(j==n){
flag=1;
break;
}else{
indeg[j]--;
for(int k=0;k<n;k++)
if(str[j][k]=='1')
indeg[k]--;
}
}
if(flag)
printf("Case #%d: Yes\n",++cases);
else
printf("Case #%d: No\n",++cases);
}
return 0;
}

74
HDOJ/4325_autoAC.cpp Normal file
View File

@ -0,0 +1,74 @@
#include<iostream>
#include<algorithm>
using namespace std;
#define lson u<<1
#define rson u<<1|1
const int maxn=100010;
int st[maxn],ed[maxn];
int q[maxn],dat[maxn<<1];
struct Node{
int lef,rig,delta;
}T[maxn<<2];
void Build(int u,int l,int r){
T[u].lef=l;
T[u].rig=r;
T[u].delta=0;
if(l==r)return;
int mid=(l+r)>>1;
Build(lson,l,mid);
Build(rson,mid+1,r);
}
void PushDown(int u){
if(T[u].delta>0){
T[lson].delta+=T[u].delta;
T[rson].delta+=T[u].delta;
T[u].delta=0;
}
}
void Update(int u,int l,int r){
if(l<=T[u].lef&&T[u].rig<=r){T[u].delta++;return;}
else {
PushDown(u);
if(l<=T[lson].rig)Update(lson,l,r);
if(r>=T[rson].lef)Update(rson,l,r);
}
}
int Query(int u,int index){
if(T[u].lef==T[u].rig)return T[u].delta;
else {
PushDown(u);
if(index<=T[lson].rig)return Query(lson,index);
else return Query(rson,index);
}
}
int main(){
int t,n,m;
int cnt;
scanf("%d",&t);
for(int cas=1;cas<=t;cas++){
scanf("%d%d",&n,&m);
cnt=0;
for(int i=1;i<=n;i++){
scanf("%d%d",&st[i],&ed[i]);
dat[cnt++]=st[i];
dat[cnt++]=ed[i];
}
for(int i=1;i<=m;i++){
scanf("%d",&q[i]);
dat[cnt++]=q[i];
}
sort(dat,dat+cnt);
cnt=unique(dat,dat+cnt)-dat;
Build(1,1,cnt);
for(int i=1;i<=n;i++){
st[i]=lower_bound(dat,dat+cnt,st[i])-dat+1;
ed[i]=lower_bound(dat,dat+cnt,ed[i])-dat+1;
Update(1,st[i],ed[i]);
}
printf("Case #%d:\n",cas);
for(int i=1;i<=m;i++){
q[i]=lower_bound(dat,dat+cnt,q[i])-dat+1;
printf("%d\n",Query(1,q[i]));
}
}
}

102
HDOJ/4326_autoAC.cpp Normal file
View File

@ -0,0 +1,102 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
#define maxn 102
#define eps 1e-10
double g[maxn][maxn];
double x[maxn];
int n,m,k;
void add(int cnt,int i,int j,double val)
{
int t=i*n+j;
if(i==m)
{
if(j==1)
g[cnt][m*n+1]+=-1.0*val;
return;
}
g[cnt][t]+=val;
}
void gauss(int n,int m)
{
int row,col,i,j,k;
for(row=1,col=1;row<n,col<m;row++,col++)
{
k=row;
for(i=row+1;i<=n;i++)
if(fabs(g[i][col])>fabs(g[k][col]))
k=i;
if(k!=row)
{
for(i=col; i<=m; i++)
swap(g[k][i],g[row][i]);
}
for(i=row+1; i<=n; i++)
{
if(fabs(g[i][col])<eps)
continue;
double t=g[i][col]/g[row][col];
g[i][col]=0.0;
for(j=col+1;j<=m;j++)
g[i][j]-=t*g[row][j];
}
}
for(i=n;i>=1;i--)
{
x[i]=g[i][m];
for(j=i+1;j<=n;j++)
x[i]-=x[j]*g[i][j];
x[i]/=g[i][i];
}
}
int main()
{
int i,j,cs,nn=0;
scanf("%d",&cs);
while(cs--){
scanf("%d%d%d",&n,&m,&k);
memset(g,0,sizeof(g));
int cnt=0;
for(i=0;i<m;i++)
for(j=1;j<=n;j++)
{
cnt++;
add(cnt,i,j,1.0);
if(j==1)
{
add(cnt,i+1,j,-0.25);
add(cnt,1,n-2,-0.75);
}
else if(j==2)
{
add(cnt,i+1,n-2,-0.25);
add(cnt,1,1,-0.25);
add(cnt,1,n-1,-0.5);
}
else if(j==3)
{
add(cnt,i+1,n-1,-0.25);
add(cnt,1,1,-0.25);
add(cnt,1,n-1,-0.25);
add(cnt,1,n,-0.25);
}
else if(j==4)
{
add(cnt,i+1,n,-0.25);
add(cnt,1,n,-0.5);
add(cnt,1,1,-0.25);
}
else
{
add(cnt,i+1,j-3,-0.25);
add(cnt,1,j-3,-0.75);
}
}
gauss(cnt,cnt+1);
printf("Case #%d: %.6lf\n",++nn,x[k]);
}
return 0;
}

161
HDOJ/4327_autoAC.cpp Normal file
View File

@ -0,0 +1,161 @@
#include <cmath>
#include <cstdio>
#include<algorithm>
using namespace std;
const double INF = 1000000000.0;
const int maxn = 1010;
const double eps = 1e-12;
inline double sgn(double x) {return fabs(x)<eps?0:(x>0?1:-1);}
struct Point{
double x,y;
Point(double tx=0,double ty=0){x=tx;y=ty;}
bool operator == (const Point& t) const {
return sgn(x-t.x)==0 && sgn(y-t.y)==0;
}
Point operator - (const Point& t) const {
Point tmp;
tmp.x = x - t.x;
tmp.y = y - t.y;
return tmp;
}
}p[maxn],tmp[maxn],pp[maxn],GP;
struct Seg{Point s,e;};
struct Line {
double a, b, c;
};
double cross(Point a,Point b,Point c){return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);}
inline Point intersect(Point x,Point y,double a,double b,double c){
double u = fabs(a * x.x + b * x.y + c);
double v = fabs(a * y.x + b * y.y + c);
return Point( (x.x * v + y.x * u) / (u + v) , (x.y * v + y.y * u) / (u + v) );
}
int n,ban_tot;
void CUT1(double a,double b,double c){
int i,tot=0;
for(int i = 1; i <= ban_tot; ++i){
if(a*p[i].x + b*p[i].y + c >= eps) pp[++tot] = p[i];
else {
if(a*p[i-1].x + b*p[i-1].y + c > eps){
pp[++tot] = intersect(p[i],p[i-1],a,b,c);
}
if(a*p[i+1].x + b*p[i+1].y + c > eps){
pp[++tot] = intersect(p[i],p[i+1],a,b,c);
}
}
} ban_tot=tot;
pp[tot+1]=pp[1];pp[0]=pp[tot];
memcpy(p,pp,sizeof(pp));
}
void CUT2(double a,double b,double c){
int i,tot=0;
for(int i = 1; i <= ban_tot; ++i){
if(!(a*p[i].x + b*p[i].y + c > eps) ) pp[++tot] = p[i];
else {
if(a*p[i-1].x + b*p[i-1].y + c < -eps){
pp[++tot] = intersect(p[i],p[i-1],a,b,c);
}
if(a*p[i+1].x + b*p[i+1].y + c < -eps){
pp[++tot] = intersect(p[i],p[i+1],a,b,c);
}
}
} ban_tot=tot;
pp[tot+1]=pp[1];pp[0]=pp[tot];
memcpy(p,pp,sizeof(pp));
}
Line Turn(Point s, Point e) {
Line ln;
ln.a = s.y - e.y;
ln.b = e.x - s.x;
ln.c = s.x*e.y - e.x*s.y;
return ln;
}
Line make(Point a,Point b)
{
double x0=(a.x+b.x)/2;
double y0=(a.y+b.y)/2;
Line tmp=Turn(a,b);
Line ans;
ans.a=tmp.b;
ans.b=-tmp.a;
ans.c=tmp.a*y0-tmp.b*x0;
return ans;
}
Line ln[maxn];
inline double PPdis(Point a, Point b) {
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
inline double PLdis(Point p,Point l1,Point l2){
return fabs(cross(p,l1,l2))/PPdis(l1,l2);
}
double calc(Point *p,int n)
{
if(n<3) return 0;
double area=0,V=0;
for(int i=0;i<n;i++) area+=p[(i+1)%n].y*(p[i].x-p[(i+2)%n].x);
area/=2;
area=fabs(area);
double H=10;
int pos=0;
for(int i=0;i<n;i++)
{
if(2-p[i].x-p[i].y<H)
{
H=2-p[i].x-p[i].y;
pos=i;
}
}
V+=area*H;
for(int i=pos+1;;i++)
{
int id1=i%n;
int id2=(i+1)%n;
if(id2==pos) break;
double h=PLdis(p[pos],p[id1],p[id2]);
double s=((2-p[id1].x-p[id1].y-H) + (2-p[id2].x-p[id2].y-H)) * PPdis(p[id1],p[id2])/2;
V+=s*h/3;
}
return V;
}
double ans[110];
int main(){
int t,ca=1,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%lf%lf",&tmp[i].x,&tmp[i].y);
for(int i=1;i<=n;i++)
{
p[1]=Point(0,0);
p[2]=Point(0,1);
p[3]=Point(1,1);
p[4]=Point(1,0);
p[0]=p[4];
p[5]=p[1];
ban_tot=4;
double x0=tmp[i].x,y0=tmp[i].y;
for(int j=1;j<=n;j++)
{
if(i==j) continue;
Line mid_line = make(tmp[i],tmp[j]);
double a=mid_line.a,b=mid_line.b,c=mid_line.c;
if(a*x0+b*y0+c >= eps)
{
CUT1(a,b,c);
}
else
{
CUT2(a,b,c);
}
}
double tmpv=calc(p,ban_tot);
ans[i]=tmpv;
}
printf("Case #%d:\n",ca++);
for(int i=1;i<=n;i++)
{
printf("%.6lf\n",ans[i]);
}
}
return 0;
}

210
HDOJ/4328_autoAC.cpp Normal file
View File

@ -0,0 +1,210 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string.h>
#include <algorithm>
using namespace std;
char map[1010][1010];
int lefts[1010][1010],heights[1010][1010],rights[1010][1010];
int CountSingle(int n,int m,char sing){
int ret=0;
for(int i=1;i<=n;i++){
if(i==1){
int l=0;
for(int j=1;j<=m;j++){
if(map[i][j]!=sing){
heights[i][j]=0;
lefts[i][j]=m+1;
l=j;
}
else{
heights[i][j]=1;
lefts[i][j]=l+1;
}
}
int r=m+1;
for(int j=m;j>=1;j--){
if(map[i][j]!=sing){
rights[i][j]=0;
r=j;
}
else{
rights[i][j]=r-1;
ret=max(ret,heights[i][j]*2+2*(rights[i][j]-lefts[i][j]+1));
}
}
}
else{
int l=0;
for(int j=1;j<=m;j++){
if(map[i][j]!=sing){
heights[i][j]=0;
lefts[i][j]=m+1;
l=j;
}
else{
if(map[i-1][j]==sing){
heights[i][j]=heights[i-1][j]+1;
lefts[i][j]=max(lefts[i-1][j],l+1);
}
else{
heights[i][j]=1;
lefts[i][j]=l+1;
}
}
}
int r=m+1;
for(int j=m;j>=1;j--){
if(map[i][j]!=sing){
rights[i][j]=0;
r=j;
}
else{
if(map[i-1][j]==sing){
rights[i][j]=min(rights[i-1][j],r-1);
ret=max(ret,heights[i][j]*2+2*(rights[i][j]-lefts[i][j]+1));
}
else{
rights[i][j]=r-1;
ret=max(ret,heights[i][j]*2+2*(rights[i][j]-lefts[i][j]+1));
}
}
}
}
}
return ret;
}
int CountCross(int n,int m){
int ret=0;
for(int i=1;i<=n;i++){
if(i==1){
int l=0;
for(int j=1;j<=m;j++){
if(j==1){
heights[i][j]=1;
lefts[i][j]=1;
}
else{
if(map[i][j-1]==map[i][j]){
lefts[i][j]=j;
l=j-1;
heights[i][j]=1;
}
else{
lefts[i][j]=l+1;
heights[i][j]=1;
}
}
}
int r=m+1;
for(int j=m;j>=1;j--){
if(j==m){
heights[i][j]=1;
rights[i][j]=m;
}
else{
if(map[i][j]==map[i][j+1]){
heights[i][j]=1;
rights[i][j]=j;
r=j+1;
}
else{
rights[i][j]=r-1;
heights[i][j]=1;
}
}
ret=max(ret,heights[i][j]*2+2*(rights[i][j]-lefts[i][j]+1));
}
}
else{
int l=0;
for(int j=1;j<=m;j++){
if(j==1){
if(map[i][j]!=map[i-1][j]){
heights[i][j]=heights[i-1][j]+1;
lefts[i][j]=1;
}
else{
heights[i][j]=1;
lefts[i][j]=1;
}
}
else{
if(map[i][j]!=map[i-1][j]){
heights[i][j]=heights[i-1][j]+1;
if(map[i][j]==map[i][j-1]){
lefts[i][j]=j;
l=j-1;
}
else{
lefts[i][j]=max(lefts[i-1][j],l+1);
}
}
else{
heights[i][j]=1;
if(map[i][j]==map[i][j-1]){
lefts[i][j]=j;
l=j-1;
}
else{
lefts[i][j]=l+1;
}
}
}
}
int r=m+1;
for(int j=m;j>=1;j--){
if(j==m){
if(map[i][j]!=map[i-1][j]){
heights[i][j]=heights[i-1][j]+1;
rights[i][j]=m;
}
else{
heights[i][j]=1;
rights[i][j]=m;
}
}
else{
if(map[i][j]!=map[i-1][j]){
heights[i][j]=heights[i-1][j]+1;
if(map[i][j]==map[i][j+1]){
rights[i][j]=j;
r=j+1;
}
else{
rights[i][j]=min(rights[i-1][j],r-1);
}
}
else{
heights[i][j]=1;
if(map[i][j]==map[i][j+1]){
rights[i][j]=j;
r=j+1;
}
else{
rights[i][j]=r-1;
}
}
}
ret=max(ret,heights[i][j]*2+2*(rights[i][j]-lefts[i][j]+1));
}
}
}
return ret;
}
int main(){
int ansR,ansB,ansC,kase=0;
int T,n,m;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
getchar();
for(int i=1;i<=n;i++)
gets(map[i]+1);
ansR=CountSingle(n,m,'R');
ansB=CountSingle(n,m,'B');
ansC=CountCross(n,m);
cout<<"Case #"<<++kase<<": "<<max(ansR,max(ansB,ansC))<<endl;
}
return 0;
}

74
HDOJ/4329_autoAC.cpp Normal file
View File

@ -0,0 +1,74 @@
#include<cstdio>
#include<cstring>
#include<map>
#include<string>
#include<iostream>
using namespace std;
int main(){
int t,T,i,j,k,n;
int sta;
double sum[110],ans;
int all[110];
int ss,num;
string str;
char sss[10010];
map<string,int> m[100];
scanf("%d",&T);
for(t=1;t<=T;t++){
memset(all,0,sizeof(all));
memset(sum,0,sizeof(sum));
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%s",str);
getline(cin,str);
for(j=0;j<str.length() && str[j]==' ';j++);
sta=j;
for(;j<str.length();j++){
if(str[j]==' '){
m[i].insert(make_pair(str.substr(sta,j-sta),1));
all[i]++;
for(;j<str.length() && str[j]==' ';j++);
sta=j;
if(sta==str.length())break;
j--;
}
}
if(sta!=str.length()){
m[i].insert(make_pair(str.substr(sta,j-sta),1));
all[i]++;
}
}
for(i=0;i<n;i++){
scanf("%s",str);
getline(cin,str);
for(j=0;j<str.length() && str[j]==' ';j++);
sta=j;
ss=num=0;
for(;j<str.length();j++){
if(str[j]==' '){
num++;
if(m[i].find(str.substr(sta,j-sta))!=m[i].end()){
ss++;
sum[i]+=(double)ss/num;
}
for(;j<str.length() && str[j]==' ';j++);
sta=j;
if(sta==str.length())break;
j--;
}
}
if(sta!=str.length()){
num++;
if(m[i].find(str.substr(sta,j-sta))!=m[i].end()){
ss++;
sum[i]+=(double)ss/num;
}
}
sum[i]/=all[i];
}
ans=0;
for(i=0;i<n;i++)
ans+=sum[i];
printf("Case #%d: %.6lf\n",t,ans/n);
}
}

231
HDOJ/4330_autoAC.cpp Normal file
View File

@ -0,0 +1,231 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const double eps = 1e-8;
int getNumberOnce(char stage[10][10])
{
bool remove[10][10];
memset(remove, false, sizeof(remove));
for(int i=0;i<8;++i)
{
for(int j=0;j<8;++j)
{
if(i<6)
{
if(stage[i][j])
{
if(stage[i][j] == stage[i+1][j])
{
if(stage[i][j] == stage[i+2][j])
{
remove[i][j] = true;
remove[i+1][j] = true;
remove[i+2][j] = true;
}
}
}
}
if(j<6)
{
if(stage[i][j])
{
if(stage[i][j] == stage[i][j+1])
{
if(stage[i][j] == stage[i][j+2])
{
remove[i][j] = true;
remove[i][j+1] = true;
remove[i][j+2] = true;
}
}
}
}
}
}
int num = 0;
for(int i=0;i<8;++i)
{
for(int j=0;j<8;++j)
{
if(remove[i][j])
{
stage[i][j] = 0;
++ num;
}
}
}
return num;
}
void fallDown(char stage[10][10])
{
for(int j=0;j<8;++j)
{
int k = 7;
for(int i=7;i>=0;--i)
{
if(stage[i][j])
{
stage[k--][j] = stage[i][j];
}
}
for(int i=k;i>=0;--i)
{
stage[i][j] = 0;
}
}
}
int getNumber(char stage[10][10])
{
int num = 0;
while(true)
{
int temp = getNumberOnce(stage);
if(temp == 0)
{
break;
}
num += temp;
fallDown(stage);
}
return num;
}
char stage[10][10];
bool simpleJudge()
{
char temp[10][10];
memcpy(temp, stage, sizeof(temp));
return getNumber(temp) >= 8;
}
struct Point
{
int x, y;
};
int getPow5(int x)
{
int res = 1;
while(x--)
{
res *= 5;
}
return res;
}
double completeJudge(char stage[10][10], int remain)
{
char temp[10][10];
memcpy(temp, stage, sizeof(temp));
int num = getNumberOnce(temp);
if(num >= remain)
{
return 1.0;
}
if(num == 0)
{
return 0.0;
}
remain -= num;
fallDown(temp);
Point stack[8];
int top = 0;
for(int i=0;i<8;++i)
{
for(int j=0;j<8;++j)
{
if(!temp[i][j])
{
stack[top].x = i;
stack[top].y = j;
++ top;
}
}
}
int total = getPow5(top);
double ret = 0.0;
for(int i=0;i<total;++i)
{
int bit = i;
for(int j=0;j<top;++j)
{
temp[stack[j].x][stack[j].y] = '0' + (bit % 5);
bit /= 5;
}
ret += completeJudge(temp, remain);
}
return ret / total;
}
int main()
{
int caseNumber;
scanf("%d", &caseNumber);
for(int cas=1;cas<=caseNumber;++cas)
{
for(int i=0;i<8;++i)
{
scanf("%s", stage[i]);
}
double ans = 0.0;
bool end = false;
for(int i=0;i<8 && !end;++i)
{
for(int j=0;j<8 && !end;++j)
{
if(i!=7)
{
swap(stage[i][j], stage[i+1][j]);
if(simpleJudge())
{
ans = 1.0;
end = true;
}
swap(stage[i][j], stage[i+1][j]);
}
if(j!=7)
{
swap(stage[i][j], stage[i][j+1]);
if(simpleJudge())
{
ans = 1.0;
end = true;
}
swap(stage[i][j], stage[i][j+1]);
}
}
}
if(!end)
{
double temp;
for(int i=0;i<8 && !end;++i)
{
for(int j=0;j<8 && !end;++j)
{
if(i!=7)
{
swap(stage[i][j], stage[i+1][j]);
temp = completeJudge(stage, 8);
ans = max(ans, temp);
swap(stage[i][j], stage[i+1][j]);
}
if(ans > 1.0 - eps)
{
ans = 1.0;
end = true;
}
if(j!=7)
{
swap(stage[i][j], stage[i][j+1]);
temp = completeJudge(stage, 8);
ans = max(ans, temp);
swap(stage[i][j], stage[i][j+1]);
}
if(ans > 1.0 - eps)
{
ans = 1.0;
end = true;
}
}
}
}
printf("Case #%d: %.3lf\n", cas, ans);
}
return 0;
}

125
HDOJ/4331_autoAC.cpp Normal file
View File

@ -0,0 +1,125 @@
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#define MAXN 1005
using namespace std;
int N, G[MAXN][MAXN], c[MAXN], idx;
int dgn[MAXN];
int l[MAXN][MAXN], r[MAXN][MAXN], u[MAXN][MAXN], d[MAXN][MAXN];
struct Node
{
int x, sign, key;
bool operator < (Node temp) const
{
if (x != temp.x) {
return x < temp.x;
}
else {
return sign < temp.sign;
}
}
}p[MAXN<<1];
int lowbit(int x)
{
return x & -x;
}
void addpoint(int a, int b)
{
++idx;
p[idx].x = a, p[idx].sign = 0;
p[idx].key = a;
++idx;
p[idx].x = b, p[idx].sign = 1;
p[idx].key = a;
}
void add(int x)
{
for (int i = x; i <= N; i += lowbit(i)) {
c[i] += 1;
}
}
int sum(int x)
{
int ret = 0;
for (int i = x; i > 0; i -= lowbit(i)) {
ret += c[i];
}
return ret;
}
int solve()
{
int ret = 0;
for (int i = N; i >= 1; --i) {
idx = -1;
for (int j = 1; j <= (N-i+1); ++j) {
if (G[i+j-1][j]) {
addpoint(j, j+min(r[i+j-1][j], d[i+j-1][j])-1);
dgn[j] = j - min(u[i+j-1][j], l[i+j-1][j])+1;
}
}
if (idx == -1) { continue; }
sort(p, p+idx+1);
memset(c, 0, sizeof (c));
for (int k = 0; k <= idx; ++k) {
if (!p[k].sign) {
ret -= sum(p[k].key);
add(dgn[p[k].x]);
}
else {
ret += sum(p[k].key);
}
}
}
for (int j = 2; j <= N; ++j) {
idx = -1;
for (int i = 1; i <= (N-j+1); ++i) {
if (G[i][j+i-1]) {
addpoint(i, i+min(r[i][j+i-1], d[i][j+i-1])-1);
dgn[i] = i - min(u[i][j+i-1], l[i][j+i-1])+1;
}
}
if (idx == -1) { continue; }
sort(p, p+idx+1);
memset(c, 0, sizeof (c));
for (int k = 0; k <= idx; ++k) {
if (!p[k].sign) {
ret -= sum(p[k].key);
add(dgn[p[k].x]);
}
else {
ret += sum(p[k].key);
}
}
}
return ret;
}
int main()
{
int T, ca = 0;
scanf("%d", &T);
while (T--) {
scanf("%d", &N);
memset(d, 0, sizeof (d));
memset(r, 0, sizeof (r));
for (int i = 1; i<= N; ++i) {
for (int j = 1; j <= N; ++j) {
scanf("%d", &G[i][j]);
if (G[i][j]) {
u[i][j] = G[i-1][j] ? u[i-1][j] + 1 : 1;
l[i][j] = G[i][j-1] ? l[i][j-1] + 1 : 1;
}
}
}
for (int i = N; i >= 1; --i) {
for (int j = N; j >= 1; --j) {
if (G[i][j]) {
d[i][j] = G[i+1][j] ? d[i+1][j] + 1 : 1;
r[i][j] = G[i][j+1] ? r[i][j+1] + 1 : 1;
}
}
}
printf("Case %d: %d\n", ++ca, solve());
}
return 0;
}

145
HDOJ/4332_autoAC.cpp Normal file
View File

@ -0,0 +1,145 @@
#include <stdio.h>
#include <string.h>
#include <map>
#include <algorithm>
typedef __int64 LL ;
const LL Mod = 1000000007LL ;
const int m = (1 << 8) ;
const int mm = 70 ;
bool ok[m] ;
int a[8] , N ;
int min[m] ;
struct Matrix{
LL mat[mm][mm] ;
void init(){
memset( mat, 0 ,sizeof(mat) ) ;
}
}M[31] , res ,mid ;
LL ans[mm] ;
bool is_ok(int s){
if( s==0 ) return true ;
if( s==m-1 ) return true ;
int val[8] ;
int i ;
for(i=0;i<8;i++)
if( s&a[i] ) val[i] = 1 ;
else val[i] = 0;
i = 0 ;
while( val[i] ) ++ i ;
for(int ii=0;ii<8;ii++ ){
if( val[ (ii+i)%8 ] == 0 ) continue ;
if( ii+1<8 && val[ (ii+i+1)%8 ] ){
ii ++ ;
}
else return false ;
}
return true ;
}
int getmin( int n ){
int val[8] ;
for(int i=0;i<8;i++) {
if( n&a[i] ) val[i] = 1 ;
else val[i] = 0 ;
}
int v = 0 ;
int res = n ;
for(int ii=0;ii<8;ii++)
if( val[ (2+ii)%8 ] ) v |= a[ ii ] ;
if( res > v ) res = v ;
v = 0 ;
for(int ii=0;ii<8;ii++)
if( val[ (4+ii)%8 ] ) v |= a[ ii ] ;
if( res > v ) res =v ;
v = 0 ;
for(int ii=0;ii<8;ii++)
if( val[ (6+ii)%8 ] ) v |= a[ ii ] ;
if( res > v ) res =v ;
return res ;
}
std::map<int , int> mp ;
bool vis[m + 10][m + 10] ;
void deal(int sou , int to){
int val[8] ;
for(int i=0;i<8;i++){
if( to&a[i] ) val[i] = 1 ;
else val[i] = 0 ;
}
LL v1 , v2 ;
v1 = sou ;
for(int j=0; j<=6;j+=2 ){
v2 = 0 ;
for(int jj=0;jj<8;jj++){
if( val[ (j+jj)%8 ] ) v2 |= a[jj] ;
}
if( ( v1|v2 )==m-1 && ok[ v1&v2 ] && !vis[v1][v2] ){
M[0].mat[ mp[ min[v1] ] ][ mp[ min[v2] ] ] ++ ;
vis[v1][v2] = 1 ;
}
}
}
void build_matrix(){
M[0].init() ;
memset( vis , 0 , sizeof(vis) );
std::map<int, int>::iterator it1 ,it2 ;
for( it1 = mp.begin() ; it1!=mp.end() ; it1++ ){
for( it2=mp.begin() ; it2!=mp.end() ; it2++ ){
deal( it1->first , it2->first ) ;
}
}
M[0].mat[mm-1][mm-1] = 2 ;
for(int ii=1;ii<=30;ii++){
for(int i=0;i<mm;i++){
for(int j=0;j<mm;j++){
mid.mat[i][j] = 0 ;
for(int k=0;k<mm;k++){
mid.mat[i][j] = ( mid.mat[i][j] + M[ii-1].mat[i][k] * M[ii-1].mat[k][j] % Mod ) % Mod ;
}
}
}
M[ii] = mid ;
}
}
void init(){
a[0] = 1 ;
for(int i=1;i<8;i++) a[i] = a[i-1]<<1 ;
for(int i=0;i<m;i++) min[i] = getmin(i) ;
for(int i=0;i<m;i++){
ok[i] = is_ok( i ) ;
mp[ min[i] ] = 1 ;
}
std::map<int,int>::iterator it = mp.begin() ;
int ss = 0 ;
for( it ; it != mp.end(); it ++ ){
it->second = ss ++ ;
}
build_matrix() ;
}
void calc( int n ){
memset( ans , 0 , sizeof(ans) ) ;
int jj = 0 ;
ans[ mm-1 ] = 1 ;
LL CC[mm] ;
while( n ){
if( n&1 ){
for(int i=0;i<mm;i++){
CC[i] = 0;
for(int j=0;j<mm;j++){
CC[i] = ( CC[i] + ans[j] * M[jj].mat[i][j]% Mod ) % Mod ;
}
}
for(int i=0;i<mm;i++) ans[i] = CC[i] ;
}
n >>= 1 ; jj ++ ;
}
}
int main(){
init() ;
int T ;scanf("%d",&T) ;
int cas = 0 ;
while( T-- ){
scanf("%d",&N) ;
calc(N + 1) ;
printf("Case %d: %I64d\n",++cas,ans[0] ) ;
}
return 0 ;
}

55
HDOJ/4333_autoAC.cpp Normal file
View File

@ -0,0 +1,55 @@
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 999999999
using namespace std;
const int MAX=100000+10;
char s[MAX*2];
int ne[MAX];
void get_next(char *a,int len){
int i=-1,j=0;
ne[0]=-1;
while(j<len){
if(i == -1 || a[i] == a[j])ne[++j]=++i;
else i=ne[i];
}
}
void get_extend(char *a,int len){
int k=0,i=1;
ne[0]=len;
while(k+1<len && a[k] == a[k+1])++k;
ne[1]=k;
k=1;
while(++i<len/2){
int maxr=k+ne[k]-1;
ne[i]=min(ne[i-k],max(maxr-i+1,0));
while(i+ne[i]<len && a[ne[i]] == a[i+ne[i]])++ne[i];
if(i+ne[i]>k+ne[k])k=i;
}
}
int main(){
int t,num=0;
cin>>t;
while(t--){
scanf("%s",s);
int len=strlen(s);
get_next(s,len);
int temp=len%(len-ne[len]) == 0?len/(len-ne[len]):1;
for(int i=0;i<=len;++i)s[i+len]=s[i];
get_extend(s,len+len);
int a=0,b=0,c=0;
for(int i=0;i<len;++i){
if(ne[i]>=len)++b;
else if(s[ne[i]]<s[i+ne[i]])++c;
else ++a;
}
cout<<"Case "<<++num<<": "<<a/temp<<' '<<b/temp<<' '<<c/temp<<endl;
}
return 0;
}

91
HDOJ/4334_autoAC.cpp Normal file
View File

@ -0,0 +1,91 @@
#include<stdio.h>
#include<stdlib.h>
int n;
__int64 que[5][210],a[41000],b[41000];
int cmp(const void*a,const void*b)
{
if(*(__int64 *)a-*(__int64 *)b > 0)
return 1;
return -1;
}
void fun()
{
int i,f,ji=0;
for(i=0;i<n;i++)
{
for(f=0;f<n;f++)
{
a[ji]=que[0][i]+que[1][f];
ji++;
}
}
qsort(a,n*n,sizeof(a[0]),cmp);
ji=0;
for(i=0;i<n;i++)
{
for(f=0;f<n;f++)
{
b[ji]=que[2][i]+que[3][f];
ji++;
}
}
qsort(b,n*n,sizeof(b[0]),cmp);
}
int sea(__int64 x)
{
int head=0,tail=n*n-1;
while(1)
{
if(x == a[head]+b[tail])
{
return 1;
}
if(a[head]+b[tail] > x)
{
tail--;
if(-1 == tail)
{
break;
}
}
else
{
head++;
if(n*n == head)
{
break;
}
}
}
return 0;
}
int main()
{
int i,num,f,g;
scanf("%d",&num);
for(i=0;i<num;i++)
{
scanf("%d",&n);
for(f=0;f<5;f++)
{
for(g=0;g<n;g++)
{
scanf("%I64d",&que[f][g]);
}
}
fun();
for(g=0;g<n;g++)
{
if(1 == sea(-que[4][g]))
{
printf("Yes\n");
break;
}
}
if(g == n)
{
printf("No\n");
}
}
return 0;
}

79
HDOJ/4335_autoAC.cpp Normal file
View File

@ -0,0 +1,79 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<cmath>
#define LL unsigned long long
#define eps 1e-6
#define zero(a) fabs(a)<eps
using namespace std;
LL get_eular(LL m){
LL ret=1;
for(LL i=2;i*i<=m;i++)
if(m%i==0){
ret*=i-1;
m/=i;
while(m%i==0){
m/=i;
ret*=i;
}
}
if(m>1)
ret*=m-1;
return ret;
}
LL PowMod(LL a,LL b,LL MOD){
LL ret=1;
while(b){
if(b&1)
ret=(ret*a)%MOD;
a=(a*a)%MOD;
b>>=1;
}
return ret;
}
LL b,p,m,ring[100005];
int main(){
int t,cas=0;
scanf("%d",&t);
while(t--){
scanf("%I64u%I64u%I64u",&b,&p,&m);
printf("Case #%d: ",++cas);
if(p==1){
if(m==18446744073709551615ULL)
printf("18446744073709551616\n");
else
printf("%I64u\n",m+1);
continue;
}
LL i=0,phi=get_eular(p),fac=1,ans=0;
for(i=0;i<=m&&fac<=phi;i++){
if(PowMod(i,fac,p)==b)
ans++;
fac*=i+1;
}
fac=fac%phi;
for(;i<=m&&fac;i++){
if(PowMod(i,fac+phi,p)==b)
ans++;
fac=(fac*(i+1))%phi;
}
if(i<=m){
LL cnt=0;
for(int j=0;j<p;j++){
ring[j]=PowMod(i+j,phi,p);
if(ring[j]==b)
cnt++;
}
LL idx=(m-i+1)/p;
ans+=cnt*idx;
LL remain=(m-i+1)%p;
for(int j=0;j<remain;j++)
if(ring[j]==b)
ans++;
}
printf("%I64u\n",ans);
}
return 0;
}

30
HDOJ/4336_autoAC.cpp Normal file
View File

@ -0,0 +1,30 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n;
double f[1<<20], p[25];
int main()
{
while(scanf("%d", &n)!=EOF)
{
for(int i=0; i<n; i++)
scanf("%lf", &p[i]);
int all=(1<<n)-1;
f[all]=0;
for(int s=all-1; s>=0; s--)
{
double temp=0;
f[s]=1;
for(int i=0; i<n; i++)
{
if(s&(1<<i)) continue;
f[s]+=f[s|(1<<i)]*p[i];
temp+=p[i];
}
f[s]/=temp;
}
printf("%lf\n", f[0]);
}
}

75
HDOJ/4337_autoAC.cpp Normal file
View File

@ -0,0 +1,75 @@
#include <string.h>
#include <iostream>
#include <stdio.h>
using namespace std;
#define V 200
int n,m;
bool c[V][V];
int x[V];
bool flag[V];
void hamilton()
{
int i, k;
bool s[V];
for(i = 0; i < n; i++)
{
x[i] = -1;
s[i] = false;
}
k = 1;
s[0] = true;
x[0] = 0;
while(k >= 0)
{
x[k]++;
while(x[k] < n)
if(!s[x[k]] && c[x[k - 1]][x[k]])
break;
else
x[k]++;
if((x[k] < n) && (k != n - 1))
{
s[x[k]] = true;
k++;
}
else if((x[k] < n) && k == n - 1 && c[x[k]][x[0]])
{
break;
}
else
{
x[k] = -1;
k--;
s[x[k]] = false;
}
}
}
int main()
{
int a,b;
while(cin >> n >> m){
memset(c,0,sizeof(c));
memset(flag,0,sizeof(flag));
memset(x,0,sizeof(x));
for(int i=0;i<m;i++){
cin >> a >> b;
c[a-1][b-1]=c[b-1][a-1]=true;
}
hamilton();
bool f=0;
for(int i=0;i<n;i++){
flag[x[i]]=1;
}
for(int i=0;i<n;i++){
if(!flag[i]) f=1;
}
if(f) cout <<"no solution" <<endl;
else{
for(int i=0;i<n;i++){
if(i==n-1) cout << x[i]+1 <<endl;
else cout << x[i]+1 <<" ";
}
}
}
return 0;
}

186
HDOJ/4338_autoAC.cpp Normal file
View File

@ -0,0 +1,186 @@
#pragma comment(linker, "/STACK:102400000,102400000")
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <stack>
using namespace std;
const int N=200005;
#define Log 22
struct Edge
{
int v;
Edge *nxt;
}memo[N*10],*cur,*h_bef[N],*h_aft[N];
void addEdge(int u,int v,Edge* head[])
{
cur->v=v; cur->nxt=head[u];
head[u]=cur++;
}
int bnum,bn[N];
vector<int> block[N];
bool iscut[N];
bool vis[N];
stack<int> stk;
int dfn[N],low[N],son_root,idx;
int lab[N],id[N],son[N],fa[N];
int dp[N][Log],dep[N];
void tarjan(int pt_u,int pt_pre)
{
stack<int> stk_tar;
stk_tar.push(pt_u); fa[pt_u]=pt_pre;
while(stk_tar.size())
{
int u=stk_tar.top();
int pre=fa[u];
if(dfn[u]==0) dfn[u]=low[u]=++idx,stk.push(u);
Edge* it;
for(it=h_bef[u];it;it=it->nxt)
{
int v=it->v;
if(v==pre) continue;
if(fa[v]==-1)
{
fa[v]=u; stk_tar.push(v);
h_bef[u]=it;
break;
}
else
{
if(fa[v]==u)
{
low[u]=min(low[u],low[v]);
if(dfn[u]<=low[v])
{
if(pre==-1) son_root++;
else iscut[u]=1;
while(1)
{
int top=stk.top(); stk.pop();
block[bnum].push_back(top);
if(top==v) break;
}
block[bnum].push_back(u);
bnum++;
}
}
else low[u]=min(low[u],dfn[v]);
}
}
if(it==NULL) stk_tar.pop();
}
}
void dfs(int u,int pre,int cnt,int iid)
{
dep[u]=dep[ dp[u][0] ]+1;
for(int i=1;i<Log;i++) dp[u][i]= dp[ dp[u][i-1] ][ i-1 ];
id[u]=iid; son[u]=cnt+bn[u];
for(Edge* it=h_aft[u];it;it=it->nxt)
{
int v=it->v;
if(v!=pre)
{
dp[v][0]=u;
dfs(v,u,son[u],iid);
}
}
}
int lca(int u,int v)
{
if(dep[u]<dep[v]) swap(u,v);
for(int st=(1<<(Log-1)),i=Log-1;i>=0;st>>=1,i--)
{
if(st<=dep[u]-dep[v])
{
u=dp[u][i];
}
}
if(u==v) return u;
for(int i=Log-1;i>=0;i--)
{
if(dp[u][i]!=dp[v][i])
{
u=dp[u][i];
v=dp[v][i];
}
}
return dp[u][0];
}
void init()
{
cur=memo;
memset(h_bef,0,sizeof(h_bef));
memset(h_aft,0,sizeof(h_aft));
memset(dp,0,sizeof(dp));
bnum=0; idx=0; son_root=0;
while(stk.size()) stk.pop();
for(int i=0;i<N;i++)
{
block[i].clear();
dfn[i]=0; low[i]=0; lab[i]=-1;
id[i]=i; iscut[i]=0; bn[i]=0;
son[i]=0; dep[i]=0; vis[i]=0;
fa[i]=-1;
}
}
int main()
{
int n,m,Q,t_cnt=0;
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
for(int i=0;i<m;i++)
{
int u,v; scanf("%d%d",&u,&v);
addEdge(u,v,h_bef);
addEdge(v,u,h_bef);
}
for(int i=0;i<n;i++) if(dfn[i]==0)
{
son_root=0;
tarjan(i,-2);
if(son_root>1) iscut[i]=1;
}
int k=0;
for(int i=0;i<n;i++) if(iscut[i])
{
lab[i]=k; bn[k]=1; k++;
}
for(int i=0;i<bnum;i++)
{
for(int j=0;j<(int)block[i].size();j++)
{
int u=block[i][j];
if(iscut[u])
{
addEdge(lab[u],k,h_aft);
addEdge(k,lab[u],h_aft);
}
else lab[u]=k;
}
bn[k]=(int)block[i].size();
k++;
}
for(int i=0;i<k;i++) if(dep[i]==0) dfs(i,-1,0,i);
scanf("%d",&Q);
printf("Case #%d:\n",++t_cnt);
while(Q--)
{
int s,t; scanf("%d%d",&s,&t);
if(s==t) printf("%d\n",n-1);
else if(lab[s]==-1||lab[t]==-1||id[lab[s]]!=id[lab[t]]) printf("%d\n",n);
else
{
s=lab[s]; t=lab[t];
int parent=lca(s,t);
int cnt=son[s]+son[t]-2*son[parent]+bn[parent];
int len=dep[s]+dep[t]-2*dep[parent];
int ans=cnt-len;
printf("%d\n",n-ans);
}
}
puts("");
}
return 0;
}

91
HDOJ/4339_autoAC.cpp Normal file
View File

@ -0,0 +1,91 @@
#include"iostream"
#include"cstdio"
#include"cstring"
using namespace std;
const int N=1000010;
int ltr[N<<2];
char s[2][N];
void build(int l,int r,int k)
{
if(l==r)
{
if(s[0][l]==s[1][l]) ltr[k]=1;
else ltr[k]=0;
return ;
}
int mid=(l+r)>>1;
int l_son=k<<1;
int r_son=l_son+1;
build(l,mid,l_son);
build(mid+1,r,r_son);
ltr[k]=ltr[l_son];
if(ltr[l_son]==mid-l+1) ltr[k]+=ltr[r_son];
}
void update(int l,int r,int z,int aim,char c,int k)
{
if(l==r)
{
s[z][aim]=c;
ltr[k]=s[0][aim]==s[1][aim];
return ;
}
int mid=(l+r)>>1;
int l_son=k<<1;
int r_son=l_son+1;
if(aim<=mid)update(l,mid,z,aim,c,l_son);
else update(mid+1,r,z,aim,c,r_son);
ltr[k]=ltr[l_son];
if(ltr[l_son]==mid-l+1) ltr[k]+=ltr[r_son];
}
int find(int l,int r,int aim,int k)
{
if(l==aim) return ltr[k];
int mid=(l+r)>>1;
int l_son=k<<1;
int r_son=l_son+1;
int ans;
if(aim<=mid)
{
ans=find(l,mid,aim,l_son);
if(ans==mid-aim+1) ans+=find(mid+1,r,mid+1,r_son);
}
else ans=find(mid+1,r,aim,r_son);
return ans;
}
int main()
{
int T,Case;
int i;
int len,q;
int x,a,b;
char c[10];
cin>>T;
for(Case=1;Case<=T;Case++)
{
scanf("%s%s",s[0]+1,s[1]+1);
i=1;
while(s[0][i] && s[1][i]) i++;
len=i-1;
i=1;
build(1,len,1);
cin>>q;
printf("Case %d:\n",Case);
while(q--)
{
scanf("%d",&x);
if(x==1)
{
scanf("%d%d%s",&a,&b,c);
if(b+1>len) continue;
update(1,len,a-1,b+1,c[0],1);
}
else
{
scanf("%d",&b);
if(b+1>len) printf("0\n");
else printf("%d\n",find(1,len,b+1,1));
}
}
}
return 0;
}

69
HDOJ/4340_autoAC.cpp Normal file
View File

@ -0,0 +1,69 @@
#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<algorithm>
#include<map>
#include<iostream>
using namespace std;
const int inf = 100000;
const int maxn = 101;
vector<int> edge[maxn];
int a[maxn],b[maxn];
int f[maxn][2],g[maxn][2];
void dfs(int u,int fa)
{
f[u][0]=0;
g[u][0]=0;
int sz=edge[u].size();
int m1=inf,m2=inf;
bool leaf=true;
for(int i=0;i<sz;i++)
{
int v=edge[u][i];
if(v==fa) continue;
dfs(v,u);
leaf=false;
int tmp=min(f[v][0],g[v][1]);
f[u][0]+=tmp;
m1=min(m1,f[v][1]-tmp);
tmp=min(g[v][0],f[v][1]);
g[u][0]+=tmp;
m2=min(m2,g[v][1]-tmp);
}
if(leaf)
{
f[u][1]=a[u];
f[u][0]=a[u]/2;
g[u][1]=b[u];
g[u][0]=b[u]/2;
}
else
{
f[u][1]=f[u][0] + min(a[u],m1+a[u]/2);
f[u][0]+=a[u]/2;
g[u][1]=g[u][0] + min(b[u],m2+b[u]/2);
g[u][0]+=b[u]/2;
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<=n;i++) edge[i].clear();
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++) scanf("%d",&b[i]);
for(int i=1,u,v;i<n;i++)
{
scanf("%d%d",&u,&v);
edge[u].push_back(v);
edge[v].push_back(u);
}
dfs(1,0);
printf("%d\n",min(f[1][1],g[1][1]));
}
return 0;
}

77
HDOJ/4341_autoAC.cpp Normal file
View File

@ -0,0 +1,77 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
const int sigma_size=26;
const int N=200+100;
const int MAXN=40000+100;
const double eps=1e-8;
const int inf=0x3fffffff;
const int mod=1000000000+7;
#define L(x) (x<<1)
#define R(x) (x<<1|1)
int n,T;
int dp[MAXN],res[MAXN],st[MAXN];
struct node{
int x,y,t,val;
}p[N];
vector<int>G[N];
bool judge(node a,node b){
return a.x*b.y == b.x*a.y;
}
bool cmp(int a,int b){
return p[a].x*p[a].x+p[a].y*p[a].y<p[b].x*p[b].x+p[b].y*p[b].y;
}
int main()
{
int kase=0;
while(~scanf("%d%d",&n,&T)){
for(int i=1;i<=n;i++)
scanf("%d%d%d%d",&p[i].x,&p[i].y,&p[i].t,&p[i].val);
int cnt=0;
for(int i=1;i<=n;i++){
int flag=1;
for(int j=0;j<cnt;j++){
if(!flag)
break;
if(judge(p[i],p[G[j][0]])){
G[j].push_back(i);
flag=0;
}
}
if(!flag)
continue;
G[cnt].clear(); G[cnt++].push_back(i);
}
memset(dp,0,sizeof(dp));
for(int i=0;i<cnt;i++){
sort(G[i].begin(),G[i].end(),cmp);
memcpy(res,dp,sizeof(dp));
memcpy(st,dp,sizeof(dp));
int val=0,t=0;
for(int j=0;j<G[i].size();j++){
int id=G[i][j];
t+=p[id].t; val+=p[id].val;
if(t>T)
break;
for(int k=T;k>=t;k--)
if(res[k]<=res[k-t]+val)
res[k]=res[k-t]+val;
for(int k=t;k<=T;k++)
dp[k]=max(dp[k],res[k]);
memcpy(res,st,sizeof(st));
}
}
printf("Case %d: %d\n",++kase,dp[T]);
}
return 0;
}

20
HDOJ/4342_autoAC.cpp Normal file
View File

@ -0,0 +1,20 @@
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{
int T;
scanf("%d",&T);
long long a;
while(T--)
{
scanf("%I64d",&a);
double temp=ceil((1+sqrt(1+4*a))/2)-1;
long long n=(long long)temp;
long long res1=n+a;
long long res2=n*(n+1)*(2*n+1)/3-3*(n+1)*n/2+n+(n+a-n*n+1)*n;
printf("%I64d %I64d\n",res1,res2);
}
return 0;
}

72
HDOJ/4343_autoAC.cpp Normal file
View File

@ -0,0 +1,72 @@
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int N=100050;
int n,m,opt[20][N*2],x[N*2],cnt;
struct node
{
int l,r;
void read(){ scanf("%d%d",&l,&r); x[cnt++]=l; x[cnt++]=r; }
void print(){ printf("%d %d\n",l,r); }
bool operator <(const node tmp) const{ return l<tmp.l||(l==tmp.l&&r<tmp.r); }
}no[N];
inline int getid(int val,bool f)
{
if(!f) return lower_bound(x,x+cnt,val)-x;
return upper_bound(x,x+cnt,val)-x-1;
}
int calc(int l,int r)
{
if(l>r) return 0;
if(l==cnt||r==-1) return 0;
int ret=0;
for(int i=19;i>=0;i--){
if(opt[i][l]<=r) {
ret+=(1<<i);
l=opt[i][l];
}
}
return ret;
}
int main()
{
while(scanf("%d%d",&n,&m)!=-1)
{
cnt=0;
for(int i=0;i<n;i++){
no[i].read();
}
sort(no,no+n);
sort(x,x+cnt);
cnt=unique(x,x+cnt)-x;
for(int i=0;i<n;i++){
no[i].l=getid(no[i].l,0);
no[i].r=getid(no[i].r,1);
}
x[cnt]=cnt;
int mn=cnt,r=n;
for(int i=cnt-1;i>=0;i--)
{
while(r>0&&no[r-1].l>=i){
r--;
mn=min(mn,no[r].r);
}
opt[0][i]=mn;
}
for(int i=0;i<20;i++) opt[i][cnt]=cnt;
for(int i=1;i<20;i++){
for(int j=0;j<cnt;j++){
opt[i][j]=opt[i-1][opt[i-1][j]];
}
}
while(m--)
{
int l,r;
scanf("%d%d",&l,&r);
l=getid(l,0); r=getid(r,1);
printf("%d\n",calc(l,r));
}
}
return 0;
}

142
HDOJ/4344_autoAC.cpp Normal file
View File

@ -0,0 +1,142 @@
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
using namespace std;
#define Times 10
typedef __int64 LL;
map<LL,int>m;
LL Random(LL n)
{
return ((double)rand()/RAND_MAX*n+0.5);
}
LL multi(LL a,LL b,LL mod)
{
LL ans=0;
while(b)
{
if(b&1)
{
b--;
ans=(ans+a)%mod;
}
else
{
b/=2;
a=(a+a)%mod;
}
}
return ans;
}
LL Pow(LL a,LL b,LL mod)
{
LL ans=1;
while(b)
{
if(b&1)
{
b--;
ans=multi(ans,a,mod);
}
else
{
b/=2;
a=multi(a,a,mod);
}
}
return ans;
}
bool witness(LL a,LL n)
{
LL d=n-1;
while(!(d&1))
d>>=1;
LL t=Pow(a,d,n);
while(d!=n-1 && t!=1 && t!=n-1)
{
t=multi(t,t,n);
d<<=1;
}
return t==n-1 || d&1;
}
bool miller_rabin(LL n)
{
if(n==2)
return true;
if(n<2||!(n&1))
return false;
for(int i=1;i<=Times;i++)
{
LL a=Random(n-2)+1;
if(!witness(a,n))
return false;
}
return true;
}
LL gcd(LL a,LL b)
{
if(b==0)
return a;
return gcd(b,a%b);
}
LL pollard_rho(LL n,LL c)
{
LL x,y,d,i=1,k=2;
x=Random(n-1)+1;
y=x;
while(1)
{
i++;
x=(multi(x,x,n)+c)%n;
d=gcd(y-x,n);
if(1<d&&d<n)
return d;
if(y==x)
return n;
if(i==k)
{
y=x;
k<<=1;
}
}
}
void find(LL n,LL c)
{
if(n==1)
return ;
if(miller_rabin(n))
{
m[n]++;
return ;
}
LL p=n;
while(p>=n)
p=pollard_rho(p,c--);
find(p,c);
find(n/p,c);
}
int main()
{
int t;
cin>>t;
while(t--)
{
LL n;
cin>>n;
m.clear();
find(n,2013724);
if(m.size()==1)
cout<<1<<" "<<n/m.begin()->first<<endl;
else
{
LL ans=0;
map<LL,int>::iterator it=m.begin();
for(;it!=m.end();it++)
ans+=Pow(it->first,it->second,n);
cout<<m.size()<<" "<<ans<<endl;
}
}
return 0;
}

48
HDOJ/4345_autoAC.cpp Normal file
View File

@ -0,0 +1,48 @@
#include<stdio.h>
#include<string.h>
const int MAXN=1100;
int prime[MAXN+1];
int getPrime()
{
memset(prime,0,sizeof(prime));
for(int i=2;i<=MAXN;i++)
{
if(!prime[i]) prime[++prime[0]]=i;
for(int j=1;j<=prime[0]&&prime[j]<=MAXN/i;j++)
{
prime[prime[j]*i]=1;
if(i%prime[j]==0)break;
}
}
return prime[0];
}
long long dp[MAXN][MAXN];
long long DP(int n,int i)
{
if(dp[n][i]!=-1)return dp[n][i];
if(prime[i]>n)
{
dp[n][i]=1;
return dp[n][i];
}
int k=0;
dp[n][i]=0;
while(k<=n)
{
dp[n][i]+=DP(n-k,i+1);
if(k==0)k=prime[i];
else k*=prime[i];
}
return dp[n][i];
}
int main()
{
getPrime();
memset(dp,-1,sizeof(dp));
int n;
while(scanf("%d",&n)!=EOF)
{
printf("%I64d\n",DP(n,1));
}
return 0;
}

47
HDOJ/4346_autoAC.cpp Normal file
View File

@ -0,0 +1,47 @@
#include<cstdio>
#include<cstring>
using namespace std;
#define mod 1000000007
int t;
long long mod_pow(int a,int n,int p)
{
long long ret=1;
long long A=a;
while(n){
if (n & 1)
ret=(ret*A)%p;
A=(A*A)%p;
n>>=1;
}
return ret;
}
int main(){
scanf("%d",&t);
while( t -- ){
char str[808];
scanf("%s",str);
int len = strlen(str);
int unknown = 0, r_num = 0;
for(int i = 0; i < len; i ++){
if(str[i] == '?') unknown ++;
else if(str[i] == 'R') r_num ++;
}
long long unbea = (r_num == 0);
for(int i = 0; i < len; i ++){
if(str[i] == 'R' || str[i] == '?'){
int x = (str[i] == 'R');
unbea = (unbea + (x == r_num) ) % mod;
for(int dis = 1; dis + i < len; dis += 2){
int y = x;
for(int sta = i + dis; sta < len; sta += dis){
y += (str[sta] == 'R');
if(str[sta] == 'G') break;
unbea = (unbea + (y == r_num) )% mod;
}
}
}
}
printf("%I64d\n",mod_pow(2,unknown,mod) - unbea);
}
return 0;
}

76
HDOJ/4347_autoAC.cpp Normal file
View File

@ -0,0 +1,76 @@
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=55555,K=5;
const int inf=0x3f3f3f3f;
#define sqr(x) (x)*(x)
int k,n,idx;
struct point
{
int x[K];
bool operator < (const point &u) const
{
return x[idx]<u.x[idx];
}
}po[N];
typedef pair<double,point>tp;
priority_queue<tp>nq;
struct kdTree
{
point pt[N<<2];
int son[N<<2];
void build(int l,int r,int rt=1,int dep=0)
{
if(l>r) return;
son[rt]=r-l;
son[rt*2]=son[rt*2+1]=-1;
idx=dep%k;
int mid=(l+r)/2;
nth_element(po+l,po+mid,po+r+1);
pt[rt]=po[mid];
build(l,mid-1,rt*2,dep+1);
build(mid+1,r,rt*2+1,dep+1);
}
void query(point p,int m,int rt=1,int dep=0)
{
if(son[rt]==-1) return;
tp nd(0,pt[rt]);
for(int i=0;i<k;i++) nd.first+=sqr(nd.second.x[i]-p.x[i]);
int dim=dep%k,x=rt*2,y=rt*2+1,fg=0;
if(p.x[dim]>=pt[rt].x[dim]) swap(x,y);
if(~son[x]) query(p,m,x,dep+1);
if(nq.size()<m) nq.push(nd),fg=1;
else
{
if(nd.first<nq.top().first) nq.pop(),nq.push(nd);
if(sqr(p.x[dim]-pt[rt].x[dim])<nq.top().first) fg=1;
}
if(~son[y]&&fg) query(p,m,y,dep+1);
}
}kd;
void print(point &p)
{
for(int j=0;j<k;j++) printf("%d%c",p.x[j],j==k-1?'\n':' ');
}
int main()
{
while(scanf("%d%d",&n,&k)!=EOF)
{
for(int i=0;i<n;i++) for(int j=0;j<k;j++) scanf("%d",&po[i].x[j]);
kd.build(0,n-1);
int t,m;
for(scanf("%d",&t);t--;)
{
point ask;
for(int j=0;j<k;j++) scanf("%d",&ask.x[j]);
scanf("%d",&m); kd.query(ask,m);
printf("the closest %d points are:\n", m);
point pt[20];
for(int j=0;!nq.empty();j++) pt[j]=nq.top().second,nq.pop();
for(int j=m-1;j>=0;j--) print(pt[j]);
}
}
return 0;
}

109
HDOJ/4348_autoAC.cpp Normal file
View File

@ -0,0 +1,109 @@
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
const int maxn = 100010;
int n, m;
struct Node {
long long d, td;
int timelabel;
Node() {};
Node(long long _d, long long _td, int t) {
d = _d; td = _td; timelabel = t;
}
};
vector<Node> bit[maxn];
long long a[maxn], sum[maxn] = {0};
inline int lowbit(int x)
{
return x & (-x);
}
inline void update(int x, long long w, int timelabel)
{
int size;
long long tw = w * x;
while (x <= n) {
size = bit[x].size();
bit[x].push_back(bit[x][size-1]);
size++;
bit[x][size-1].d += w;
bit[x][size-1].td += tw;
bit[x][size-1].timelabel = timelabel;
x += lowbit(x);
}
}
inline long long getsum(int x, int timelabel)
{
long long sd = 0, std = 0;
int size, ox = x;
while (x > 0) {
size = bit[x].size();
while (bit[x][size-1].timelabel > timelabel) {
size--;
}
sd += bit[x][size-1].d;
std += bit[x][size-1].td;
x -= lowbit(x);
}
return (ox + 1) * sd - std;
}
inline long long getans(int x, int y, int timelabel)
{
return sum[y] - sum[x-1] + getsum(y, timelabel) - getsum(x - 1, timelabel);
}
inline void back(int timelabel)
{
int size;
for (int i = 1; i <= n; ++i) {
size = bit[i].size();
while (bit[i][size-1].timelabel > timelabel) {
bit[i].pop_back();
size--;
}
}
}
void build()
{
for (int i = 1; i <= n; ++i) {
bit[i].clear();
bit[i].push_back(Node(0LL, 0LL, 0));
}
}
int main()
{
int cas = 0;
while (scanf("%d%d", &n, &m) != EOF) {
if (cas++) printf("\n");
for (int i = 1; i <= n; ++i) {
scanf("%lld\n", &a[i]);
sum[i] = sum[i-1] + a[i];
}
char op[3];
int x, y, z;
int timelabel = 0;
build();
while (m--) {
scanf("%s", op);
if (op[0] == 'Q') {
scanf("%d%d", &x, &y);
printf("%lld\n", getans(x, y, timelabel));
} else if (op[0] == 'C') {
scanf("%d%d%d", &x, &y, &z);
timelabel++;
update(x, z, timelabel);
update(y + 1, -z, timelabel);
} else if (op[0] == 'H') {
scanf("%d%d%d", &x, &y, &z);
printf("%lld\n", getans(x, y, z));
} else {
scanf("%d", &x);
timelabel = x;
back(x);
}
}
}
return 0;
}

24
HDOJ/4349_autoAC.cpp Normal file
View File

@ -0,0 +1,24 @@
#include"cstdlib"
#include"cstdio"
#include"cstring"
#include"cmath"
#include"stack"
#include"algorithm"
#include"iostream"
#define ll __int64
using namespace std;
int main()
{
int n;
while(cin>>n)
{
int cnt=0;
while(n)
{
if(n%2==1) cnt++;
n/=2;
}
printf("%d\n",1<<cnt);
}
return 0;
}

41
HDOJ/4350_autoAC.cpp Normal file
View File

@ -0,0 +1,41 @@
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
int ans[10000][53];
int main(){
int T, op, left, right;
scanf("%d", &T);
for(int t=1; t<=T; t++){
for(int i=1; i<=52; i++)
scanf("%d", &ans[0][i]);
scanf("%d %d %d", &op, &left, &right);
int len = 1;
while(1){
int tag = 1;
for(int i=left; i<=right; i++)
ans[len][tag++] = ans[len-1][i];
for(int i=1; i<left; i++)
ans[len][tag++] = ans[len-1][i];
for(int i=right+1; i<=52; i++)
ans[len][tag++] = ans[len-1][i];
bool flag = true;
for(int i=1; i<=52; i++)
if(ans[0][i] != ans[len][i]){
flag = false;
break;
}
if(flag){
tag = op%len;
printf("Case #%d:", t);
for(int i=1; i<=52; i++)
printf(" %d", ans[tag][i]);
printf("\n");
break;
}
len++;
}
}
return 0;
}

125
HDOJ/4351_autoAC.cpp Normal file
View File

@ -0,0 +1,125 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
typedef struct
{
int l,r,sum;
int lsum,rsum,midsum;
}Tree;
Tree tree[500005];
int a[100005];
int c[2000][2000];
int ans,rans;
int Count(int t)
{
if (t==0) return 0;
int s=0;
while(t)
{
s+=t%10;
t/=10;
}
s%=9;
if (s==0) return 9;
return s;
}
int Pre()
{
int i,j,x,y;
for (i=0;i<1024;i++)
{
for (j=i;j<1024;j++)
{
c[i][j]=0;
for (x=0;x<10;x++)
{
if ((i & (1<<x))==0) continue;
for (y=0;y<10;y++)
{
if ((j & (1<<y))==0) continue;
c[i][j]=(c[i][j]|(1<<Count(x+y)));
}
}
c[j][i]=c[i][j];
}
}
}
void Build(int t,int l,int r)
{
tree[t].l=l;
tree[t].r=r;
if (l==r)
{
tree[t].sum=a[l];
tree[t].lsum=(1<<a[l]);
tree[t].rsum=(1<<a[l]);
tree[t].midsum=(1<<a[l]);
return;
}
int mid=(l+r)/2;
Build(2*t+1,l,mid);
Build(2*t+2,mid+1,r);
tree[t].sum=Count(tree[2*t+1].sum+tree[2*t+2].sum);
tree[t].lsum=(tree[2*t+1].lsum | c[1<<tree[2*t+1].sum][tree[2*t+2].lsum]);
tree[t].rsum=(tree[2*t+2].rsum | c[tree[2*t+1].rsum][1<<tree[2*t+2].sum]);
tree[t].midsum=(tree[2*t+1].midsum | tree[2*t+2].midsum | c[tree[2*t+1].rsum][tree[2*t+2].lsum]);
}
void Query(int t,int x,int y)
{
int l,r;
l=tree[t].l;
r=tree[t].r;
if (l==x && r==y)
{
ans=(ans | tree[t].midsum | c[rans][tree[t].lsum]);
rans=(c[rans][1<<tree[t].sum] | tree[t].rsum);
return;
}
int mid=(l+r)/2;
if (mid>=x) Query(2*t+1,x,min(y,mid));
if (mid<y) Query(2*t+2,max(x,mid+1),y);
}
int main()
{
int T,i,j,n,x,y,m,s,cnt=1;
Pre();
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for (i=0;i<n;i++)
{
scanf("%d",&a[i]);
a[i]=Count(a[i]);
}
Build(0,0,n-1);
scanf("%d",&m);
printf("Case #%d:\n",cnt++);
while(m--)
{
scanf("%d%d",&x,&y);
ans=rans=0;
Query(0,x-1,y-1);
s=0;
for (i=9;i>=0;i--)
{
if (ans>=(1<<i))
{
ans-=(1<<i);
if (s==0) printf("%d",i);
else printf(" %d",i);
s++;
if (s==5) break;
}
}
for (i=s;i<5;i++)
{
printf(" -1");
}
printf("\n");
}
if (T>0) printf("\n");
}
return 0;
}

81
HDOJ/4352_autoAC.cpp Normal file
View File

@ -0,0 +1,81 @@
#include<cstdio>
#include<cstring>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 20;
ll dp[N][10][1<<10][11];
int has[1<<10];
int ne[1<<10][10];
int bit[N],K;
int go(int mask,int number){
int pos = -1;
for(int i = number;i <=9;i++)
if(mask & (1<<i)){
pos = i;
break;
}
if(pos == -1)mask |= 1<<number;
else{
mask ^= 1<<pos;
mask |= 1<<number;
}
return mask;
}
void init(){
memset(dp,-1,sizeof(dp));
for(int i=0;i<1<<10;i++){
has[i] = 0;
for(int j=0;j<10;j++)
if(i&(1<<j))has[i]++;
}
for(int i = 0;i < 1<<10;i++)
for(int j = 0;j < 10;j++)
ne[i][j] = go(i,j);
for(int i = 0;i < 15;i++){
for(int j=0;j<10;j++){
}
}
}
ll dfs(int pos,int number,int mask,bool isZero,bool flag){
if(pos == 0)return has[mask] == K;
if(flag && ~dp[pos][number][mask][K])return dp[pos][number][mask][K];
ll ans = 0;
int u = flag ? 9:bit[pos];
for(int d = 0;d <= u;d++){
if(isZero && d == 0)ans += dfs(pos-1,d,0,1,flag || d < u);
else{
ans += dfs(pos-1,d,ne[mask][d],0,flag || d < u);
}
}
if(flag)dp[pos][number][mask][K] = ans ;
return ans;
}
ll solve(ll n){
int len = 0;
while(n){
bit[++len] = n % 10;
n /= 10;
}
return dfs(len,0,0,1,0);
}
int main(){
init();
ll L,R;
int T;cin >> T;
for(int cas = 1;cas <= T;cas++){
cin >> L >> R >> K;
cout<<"Case #"<<cas<<": "<<solve(R) - solve(L - 1)<<endl;
}
return 0;
}

62
HDOJ/4353_autoAC.cpp Normal file
View File

@ -0,0 +1,62 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
struct point{
int x, y;
point() {}
point(int a, int b) : x(a), y(b){}
void input(){
scanf("%d%d", &x, &y);
}
friend point operator - (const point &a, const point &b){
return point(a.x - b.x, a.y - b.y);
}
friend bool operator < (const point &a, const point &b){
return a.x < b.x;
}
};
const int maxn = 200 + 10, maxm = 500 + 10;
point po[maxn], mi[maxm];
int det(point a, point b){
return a.x * b.y - a.y * b.x;
}
int num[maxn][maxn];
int n, m, cs;
void init(point a, point b, int &cnt){
int x1 = a.x, x2 = b.x;
for (int i = 0; i < m; i++)
if (mi[i].x >= x1 && mi[i].x < x2)
if (det(mi[i] - a, b - a) > 0)
cnt += 1;
}
int main(){
scanf("%d", &cs);
for (int kase = 1; kase <= cs; ++kase){
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++)
po[i].input();
for (int i = 0; i < m; i++)
mi[i].input();
sort(po, po + n);
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
init(po[i], po[j], num[i][j] = 0);
printf("Case #%d: ", kase);
double ans = -1;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
for (int k = j + 1; k < n; k++){
int cnt = num[i][k] - num[i][j] - num[j][k];
if (!cnt) continue;
double area = det(po[j] - po[i], po[k] - po[i]) / 2.0;
double tmp = area / (double)cnt;
if (ans == -1 || ans > tmp) ans = tmp;
}
if (ans == -1) puts("-1");
else printf("%.6lf\n", ans);
}
return 0;
}

108
HDOJ/4354_autoAC.cpp Normal file
View File

@ -0,0 +1,108 @@
#include<cstdio>
#include<cstring>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
const int C = 5005;
const int N = 2005;
const int M = 1005;
int cnt[N];
int vis[C];
int dp[C][2];
int c, n, k, m;
vector<int>v[N];
struct city
{
int x, y;
bool operator<(const city &a) const
{
return x<a.x;
}
}ct[C];
int min(int x, int y)
{
if(x==-1) return y;
return x<y?x:y;
}
void dfs(int s)
{
vis[s] = 1;
dp[s][1] = 1;
dp[s][0] = 0;
for(int i=0; i<v[s].size(); i++)
{
int ss = v[s][i];
if(vis[ss]==1 || cnt[ss]==0) continue;
dfs(ss);
dp[s][1] += dp[ss][0];
dp[s][0] += max(dp[ss][1], dp[ss][0]);
}
}
bool ok(int l, int r)
{
int res = 0;
memset(vis, 0, sizeof(vis));
for(int i = l; i<=r; i++)
if(vis[ ct[i].y ]==0)
{
dfs( ct[i].y );
res += max( dp[ ct[i].y ][0], dp[ ct[i].y ][1] );
}
if(res>=k) return true;
else return false;
}
int solve()
{
int kk, l, r;
kk = l = r = 1;
cnt[ ct[1].y ]++;
int ans = -1;
while(r<=c)
{
if(kk>=k && ok(l, r))
{
ans = min(ans, ct[r].x - ct[l].x);
cnt[ ct[l].y ]--;
if( cnt[ ct[l].y ]==0 ) kk--;
l++;
}
else
{
r++;
cnt[ ct[r].y ]++;
if( cnt[ ct[r].y ]==1 ) kk++;
}
}
return ans;
}
int main()
{
int t, tt=0, i, x, y;
scanf("%d", &t);
while(t--)
{
scanf("%d%d%d%d", &c, &n, &k, &m);
for(i=1; i<=n; i++) v[i].clear();
memset(cnt, 0, sizeof(cnt));
memset(vis, 0, sizeof(vis));
for(i=1; i<=c; i++) scanf("%d%d", &ct[i].x, &ct[i].y);
sort(ct+1, ct+1+c);
for(i=1; i<=m; i++)
{
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
}
printf("Case #%d: %d\n", ++tt, solve());
}
return 0;
}

47
HDOJ/4355_autoAC.cpp Normal file
View File

@ -0,0 +1,47 @@
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int N = 10e4+1;
const float eps = 1e-2;
struct po
{
double x;
double w;
} point[N];
int main()
{
int t;
scanf("%d",&t);
for(int cases = 1; cases<=t; cases++)
{
int n;
scanf("%d",&n);
for(int i=1; i<=n; i++)
scanf("%lf %lf",&point[i].x,&point[i].w);
double low = point[1].x;
double high = point[n].x;
double sum1 = 10;
double sum2 = 0;
while(fabs(sum1-sum2)>eps)
{
double mid1 = low + (high-low)/3;
double mid2 = high - (high-low)/3;
sum1 = sum2 = 0;
for(int i=1; i<=n; i++)
{
sum1 += pow((fabs(point[i].x - mid1)),3)*point[i].w;
sum2 += pow((fabs(point[i].x - mid2)),3)*point[i].w;
}
if(sum1<sum2)
high = mid2;
else if(sum1>sum2)
low = mid1;
else if(sum1==sum2)
low = high;
}
printf("Case #%d: %.0lf\n",cases,sum1);
}
return 0;
}

58
HDOJ/4356_autoAC.cpp Normal file
View File

@ -0,0 +1,58 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define MOD 1000000007
#define N 2010
using namespace std;
int n,ans;
int a[N],w[N];
int f[N][N];
int fac[N];
void init(){
fac[0]=1;
int i,j;
for (i=1;i<=n;i++)
fac[i]=(1ll*fac[i-1]*i)%MOD;
for (i=0;i<=n;i++) f[1][i]=n-i;
for (i=1;i<=n;i++){
for (j=0;j<w[i];j++) f[i+1][j]=f[i][j]-1;
for (j=w[i];j<=n;j++) f[i+1][j]=f[i][j];
}
}
int dfs(int k){
if (k==n) return 0;
int tp=(1ll*fac[n-k]*f[k][w[k]])%MOD;
int tp1=dfs(k+1);
if (w[k]>=a[k]) ans=(1ll*ans+tp1)%MOD;
return (1ll*tp1+tp)%MOD;
}
void work(){
int i,j,k;
for (i=1;i<=n;i++){
if (a[i]>w[i]) ans=(1ll*ans+1ll*f[i][a[i]-1]*fac[n-i])%MOD;
else ans=(1ll*ans+1ll*f[i][w[i]]*fac[n-i])%MOD;
for (j=i+1;j<=n;j++){
k=f[i][w[i]]*f[i][a[j]-1]-min(f[i][w[i]],f[i][a[j]-1]);
ans=(1ll*ans+1ll*k*fac[n-i-1])%MOD;
}
}
}
int main(){
int test;
scanf("%d",&test);
for (int cas=1;cas<=test;cas++){
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d",&w[i]);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
ans=0;
init();
work();
dfs(1);
printf("Case #%d: %d\n",cas,ans);
}
return 0;
}

27
HDOJ/4357_autoAC.cpp Normal file
View File

@ -0,0 +1,27 @@
#include<iostream>
#include<string>
using namespace std;
int t;
string a,b;
int main()
{
cin>>t;
for(int cas=1;cas<=t;cas++){
cin>>a>>b;
int len=a.length();
cout<<"Case #"<<cas<<": ";
if(len>2){
int n=0,m=0;
for(int i=0;i<len;i++){
n+=a[i];
m+=b[i];
}
if((n&1)==(m&1)) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
else{
if(((a[0]-b[0]+26)%26)==((a[1]-b[1]+26)%26)&&(a[0]-b[0])%2==0||((a[0]-b[1]+26)%26)==((a[1]-b[0]+26)%26)&&(a[1]-b[0]+1)%2==0) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
}

155
HDOJ/4358_autoAC.cpp Normal file
View File

@ -0,0 +1,155 @@
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <deque>
#include <map>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
#define print(x) cout<<x<<endl
#define input(x) cin>>x
#define SIZE 100100
struct BIT
{
int baum[SIZE];
void init()
{
memset(baum,0,sizeof(baum));
}
inline int lowbit(int x)
{
return x&(-x);
}
void add(int x,int val)
{
while(x<SIZE)
{
baum[x]+=val;
x+=lowbit(x);
}
}
int sum(int x)
{
int res=0;
while(x>0)
{
res+=baum[x];
x-=lowbit(x);
}
return res;
}
int sum(int l,int r)
{
return sum(r)-sum(l-1);
}
};
struct query
{
int l,r,id;
query(){}
query(int il,int ir,int iid)
{
l=il;r=ir;id=iid;
}
friend bool operator < (const query& a,const query& b)
{
return a.r<b.r;
}
};
int n,k,q;
int w[SIZE];
vector<int> pl[SIZE];
vector<int> g[SIZE];
int lson[SIZE],rson[SIZE],val[SIZE];
int cnt,ind;
void dfs(int now,int father)
{
lson[now]=rson[now]=++ind;
val[now]=w[now];
for(int i=0;i<(int)g[now].size();i++)
{
int next=g[now][i];
if(next!=father)
{
dfs(next,now);
rson[now]=rson[next];
}
}
}
int main()
{
int T,a,b;
BIT bit;
query ask[SIZE];
int ans[SIZE];
map<int,int> mp;
input(T);
int cas=1;
while(T--)
{
bit.init();
cnt=ind=0;
mp.clear();
memset(ans,0,sizeof(ans));
for(int i=0;i<SIZE;i++) g[i].clear();
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%d",&w[i]);
if(!mp[w[i]])
{
mp[w[i]]=++cnt;
pl[cnt].clear();
}
w[i]=mp[w[i]];
}
for(int i=1;i<n;i++)
{
scanf("%d%d",&a,&b);
g[a].push_back(b);
g[b].push_back(a);
}
dfs(1,-1);
scanf("%d",&q);
for(int i=0;i<q;i++)
{
scanf("%d",&a);
ask[i]=query(lson[a],rson[a],i);
}
sort(ask,ask+q);
int ptr=0;
for(int i=1;i<=n;i++)
{
int v=val[i];
pl[v].push_back(i);
int sz=pl[v].size();
if(sz>=k)
{
if(sz==k)
{
bit.add(pl[v][sz-k],1);
}
if(sz>k)
{
bit.add(pl[v][sz-k-1],-2);
bit.add(pl[v][sz-k],1);
}
}
while(ask[ptr].r==i)
{
int id=ask[ptr].id;
ans[id]=bit.sum(ask[ptr].l,ask[ptr].r);
ptr++;
}
}
printf("Case #%d:\n",cas++);
for(int i=0;i<q;i++)
{
printf("%d\n",ans[i]);
}
if(T) puts("");
}
return 0;
}

60
HDOJ/4359_autoAC.cpp Normal file
View File

@ -0,0 +1,60 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#define inf 0x3f3f3f3f
#define Inf 0x3FFFFFFFFFFFFFFFLL
#define eps 1e-9
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int maxn=365;
const int mod=1000000000+7;
ll dp[maxn][maxn],C[maxn][maxn];
void Init()
{
memset(C,0,sizeof(C));
C[0][0]=1;
for(int i=1;i<maxn;++i)
{
C[i][0]=1;
for(int j=1;j<=i;++j)
{
C[i][j]=C[i-1][j-1]+C[i-1][j];
if(C[i][j]>=mod) C[i][j]-=mod;
}
}
}
ll f(int n,int d)
{
if(n==1&&d>=1) return 1;
if(n==1||d==0) return 0;
if(dp[n][d]!=-1) return dp[n][d];
ll &ans=dp[n][d];
ans=0;
ans=(ans+f(n-1,d-1)*n*2)%mod;
for(int k=1;k<=n-2;++k)
ans=(ans+(f(k,d-1)*f(n-k-1,d-1)%mod*C[n-2][k]%mod)*n)%mod;
return ans;
}
int main()
{
Init();
memset(dp,0xff,sizeof(dp));
int t,tcase=0,n,d;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&d);
ll ans=f(n,d)-f(n,d-1);
ans=(ans+mod)%mod;
printf("Case #%d: %I64d\n",++tcase,ans);
}
return 0;
}

100
HDOJ/4360_autoAC.cpp Normal file
View File

@ -0,0 +1,100 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long lld;
const int mn=3333;
const int mm=33333;
const lld oo=1e15;
int reach[mm], ne[mm], flow[mm], ch[mm];
lld head[mn], que[mn], dis[mn][4], cnt[mn][4], inque[mn];
int n, edge;
void addedge(int u, int v, int c1, int c2, char c)
{
ch[edge]=c, reach[edge]=v, flow[edge]=c1, ne[edge]=head[u], head[u]=edge++;
ch[edge]=c, reach[edge]=u, flow[edge]=c2, ne[edge]=head[v], head[v]=edge++;
}
int find(char c)
{
if(c=='L') return 0;
else if(c=='O') return 1;
else if(c=='V') return 2;
else return 3;
}
bool spfa()
{
int l=0, h=0;
memset(inque,0,sizeof(inque));
for(int i=1; i<=n; i++)
for(int j=0; j<4; j++) dis[i][j]=oo, cnt[i][j]=0;
inque[1]=1;
dis[1][0]=0;
que[l++]=1;
while(l!=h)
{
int u=que[h++];
if(h==mn) h=0;
inque[u]=0;
for(int i=head[u]; i>=0; i=ne[i])
{
int s=find(ch[i]), v=reach[i], val=flow[i];
if(dis[v][(s+1)%4]>=dis[u][s]+val)
{
if(dis[v][(s+1)%4]==dis[u][s]+val)
{
if(cnt[u][s]+1>cnt[v][(s+1)%4]) cnt[v][(s+1)%4]=cnt[u][s]+1;
else continue;
}
else
{
dis[v][(s+1)%4]=dis[u][s]+val;
cnt[v][(s+1)%4]=cnt[u][s]+1;
}
if(!inque[v])
{
inque[v]=1;
que[l++]=v;
if(l==mn) l=0;
}
}
}
}
if(dis[n][0]==oo||!cnt[n][0]) return false;
else return true;
}
int main()
{
int m, T, tcase=0;
cin >> T;
while(T--)
{
cin >> n >> m;
edge=0;
memset(head,-1,sizeof(head));
int mp[4]={0,0,0,0}, ct=0;
while(m--)
{
int u, v, val, se;
char sh[3];
scanf("%d%d%d%s",&u,&v,&val,sh);
addedge(u,v,val,val,sh[0]);
if(n==1&&u==1&&v==1)
{
se=find(sh[0]);
if(!mp[se]) ct++, mp[se]=val;
else mp[se]=min(mp[se],val);
}
}
if(ct==4)
{
lld sum=mp[0]+mp[1]+mp[2]+mp[3];
printf("Case %d: Cute Sangsang, Binbin will come with a donkey after travelling %I64d meters and finding %d LOVE strings at last.\n",++tcase,sum,ct/4);
continue;
}
bool ok=spfa();
if(!ok) printf("Case %d: Binbin you disappoint Sangsang again, damn it!\n",++tcase);
else printf("Case %d: Cute Sangsang, Binbin will come with a donkey after travelling %I64d meters and finding %d LOVE strings at last.\n",++tcase,dis[n][0],cnt[n][0]/4);
}
return 0;
}

11
HDOJ/4361_autoAC.cpp Normal file
View File

@ -0,0 +1,11 @@
#include <cstdio>
#include <cstdlib>
int main()
{
srand(1121139700);
int caseNumber;
scanf("%d", &caseNumber);
while(caseNumber --)
if(rand() & 1) printf("alive!\n");
else printf("dead!\n");
}

111
HDOJ/4362_autoAC.cpp Normal file
View File

@ -0,0 +1,111 @@
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
#include <cstdio>
using namespace std;
const int Ni = 1005;
const int inf=1<<27;
struct node{
int x,w;
bool operator < (const node &a) const
{
return x<a.x;
}
}arr[52][Ni];
struct qnode{
int val,x;
bool operator < (const qnode &a) const
{
if(val>a.val) return 1;
if(val==a.val&&x>a.x) return 1;
return 0;
}
};
int dp[52][Ni];
int n,m;
inline void cInit()
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&arr[i][j].x);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&arr[i][j].w);
}
}
for(i=0;i<n;i++)
{
sort(arr[i],arr[i]+m);
}
}
int main()
{
int cs,x,i,j,ans;
cin>>cs;
while(cs--)
{
scanf("%d%d%d",&n,&m,&x);
cInit();
for(j=0;j<m;j++)
{
dp[0][j]=x-arr[0][j].x;
if(dp[0][j]<0) dp[0][j]=-dp[0][j];
dp[0][j]+=arr[0][j].w;
}
for(i=1;i<n;i++)
{
priority_queue<qnode> ql,qr;
qnode qn;
for(j=0;j<m;j++)
{
qn.val=dp[i-1][j]+arr[i-1][j].x;
qn.x=arr[i-1][j].x;
qr.push(qn);
}
int k=0;
for(j=0;j<m;j++)
{
dp[i][j]=inf;
while(k<m&&arr[i-1][k].x<=arr[i][j].x)
{
qn.val=dp[i-1][k]-arr[i-1][k].x;
qn.x=arr[i-1][k].x;
ql.push(qn);
k++;
}
if(!ql.empty())
{
qn=ql.top();
dp[i][j]=min(dp[i][j],qn.val+arr[i][j].x+arr[i][j].w);
}
if(!qr.empty())
{
qn=qr.top();
while(!qr.empty()&&qn.x<arr[i][j].x)
{
qr.pop();
qn=qr.top();
}
if(qn.x>=arr[i][j].x)
dp[i][j]=min(dp[i][j],qn.val-arr[i][j].x+arr[i][j].w);
}
}
}
ans=inf;
for(i=0;i<m;i++)
{
if(ans>dp[n-1][i])
ans=dp[n-1][i];
}
printf("%d\n",ans);
}
return 0;
}

70
HDOJ/4363_autoAC.cpp Normal file
View File

@ -0,0 +1,70 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#define inf 1<<29
#define LL long long
#define MOD 1000000007
using namespace std;
int dp[41][41][5][5][5][5][2];
int get_dp(int x,int y,int l,int r,int u,int d,int k){
if(dp[x][y][l][r][u][d][k]!=-1)
return dp[x][y][l][r][u][d][k];
dp[x][y][l][r][u][d][k]=0;
if((x==1&&k==0)||(y==1&&k)){
for(int i=1;i<5;i++)
if(i!=l&&i!=r&&i!=u&&i!=d)
dp[x][y][l][r][u][d][k]++;
return dp[x][y][l][r][u][d][k];
}
dp[x][y][l][r][u][d][k]=0;
if(!k){
for(int i=1;i<x;i++)
for(int j=1;j<5;j++){
if(j!=u&&j!=l&&j!=r)
dp[x][y][l][r][u][d][k]=(dp[x][y][l][r][u][d][k]+get_dp(x-i,y,l,r,j,d,1))%MOD;
if(j!=d&&j!=l&&j!=r)
dp[x][y][l][r][u][d][k]=(dp[x][y][l][r][u][d][k]+get_dp(i,y,l,r,u,j,1))%MOD;
}
int t=0;
for(int i=1;i<=4;i++)
if(i!=u&&i!=l&&i!=r)
for(int j=1;j<=4;j++)
if(j!=d&&j!=l&&j!=r&&j!=i)
t++;
dp[x][y][l][r][u][d][k]=(dp[x][y][l][r][u][d][k]+MOD-t*(x-1))%MOD;
for(int i=1;i<5;i++)
if(i!=l&&i!=r&&i!=u&&i!=d)
dp[x][y][l][r][u][d][k]++;
}
else{
for(int i=1;i<y;i++)
for(int j=1;j<5;j++){
if(j!=l&&j!=d&&j!=u)
dp[x][y][l][r][u][d][k]=(dp[x][y][l][r][u][d][k]+get_dp(x,y-i,j,r,u,d,0))%MOD;
if(j!=r&&j!=d&&j!=u)
dp[x][y][l][r][u][d][k]=(dp[x][y][l][r][u][d][k]+get_dp(x,i,l,j,u,d,0))%MOD;
}
int t=0;
for(int i=1;i<=4;i++)
if(i!=l&&i!=u&&i!=d)
for(int j=1;j<=4;j++)
if(j!=r&&j!=u&&j!=d&&j!=i)
t++;
dp[x][y][l][r][u][d][k]=(dp[x][y][l][r][u][d][k]+MOD-t*(y-1))%MOD;
for(int i=1;i<5;i++)
if(i!=l&&i!=r&&i!=u&&i!=d)
dp[x][y][l][r][u][d][k]++;
}
return dp[x][y][l][r][u][d][k]%MOD;
}
int main(){
memset(dp,-1,sizeof(dp));
int t;
scanf("%d",&t);
while(t--){
int x,y;
scanf("%d%d",&x,&y);
printf("%d\n",get_dp(x,y,0,0,0,0,0));
}
return 0;
}

63
HDOJ/4364_autoAC.cpp Normal file
View File

@ -0,0 +1,63 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
int ans[5][5]={{2,3,1,1},{1,2,3,1},{1,1,2,3},{3,1,1,2}};
int matrix[5][5];
int a[5][5];
void multi()
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
for(int k=0;k<4;k++)
{
int t=matrix[k][j];
t<<=1;
if(t>0xFF)//225
t=(t^0x1B)%(0xFF+1);
if(ans[i][k]==1)
{
a[i][j]^=matrix[k][j];
}
else if(ans[i][k]==2)
{
a[i][j]^=t;
}
else if(ans[i][k]==3)
{
t^=matrix[k][j];
a[i][j]^=t;
}
}
}
}
}
int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
memset(a,0,sizeof(a));
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
scanf("%X",&matrix[i][j]);
multi();
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(j!=0)
cout<<' ';
printf("%02X",a[i][j]);
}
puts("");
}
if(cas != 0)
puts("");
}
return 0;
}

90
HDOJ/4365_autoAC.cpp Normal file
View File

@ -0,0 +1,90 @@
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<cstring>
#include<vector>
#include<string>
#define LL long long
#define MOD 100000007
using namespace std;
class Point
{
public:
int x,y;
bool operator == (const Point &tmp) const{
return (x==tmp.x && y==tmp.y);
}
}p[2024];
int midx,midy;
bool cmp(const Point &a,const Point &b){
if(a.x != b.x)
return a.x < b.x;
return a.y < b.y;
}
void Solve1( int x, int y , int cnt )
{
if( x > midx)
{
x = (midx<<1) - x;
}
if( y > midy ) y = (midy<<1) - y;
if( x > y ) swap( x ,y );
p[cnt].x = x; p[cnt].y = y;
}
void Solve2( int x, int y , int cnt )
{
if( x > midx )
{
int d = x - midx - 1;
x = midx - d;
}
if( y > midy )
{
int d = y - midy - 1;
y = midy - d;
}
if( x > y ) swap( x , y );
p[cnt].x = x ; p[cnt].y = y;
}
LL Pow( LL k , LL a )
{
LL ans = 1;
while( k )
{
if( k &1 ) ans = (ans*a)%MOD;
a = (a*a)%MOD;
k >>= 1;
}
return ans;
}
int main( )
{
int n,m,k,x,y;
while( scanf( "%d %d %d",&n,&m,&k )==3 )
{
midx = (n+1) /2;midy = midx;
for( int i = 0; i < m ; i ++ )
{
scanf( "%d %d",&x,&y );
x += 1 ; y += 1;
if( n & 1 ) Solve1( x ,y , i );
else Solve2( x , y , i );
}
LL L = ( n + 1 )/2;
LL ans = L*L/2 + (L + 1) /2;
sort(p,p+m,cmp);
ans -= unique(p,p+m)-p;
if( ans > 0 )
{
ans = Pow( ans , (LL)k );
printf( "%I64d\n",ans );
}
else printf( "0\n" );
}
return 0;
}

116
HDOJ/4366_autoAC.cpp Normal file
View File

@ -0,0 +1,116 @@
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <map>
#include <stack>
#include <algorithm>
#define lson l, m , rt << 1
#define rson m + 1, r, rt << 1 | 1
#define maxn 50010
using namespace std;
struct people{
int id,loty,abty;
bool operator < (const people& cmp)const {
return abty > cmp.abty;
}
}emp[maxn];
int Max[maxn << 2],t,n,m,ans[maxn],L[maxn],R[maxn],head[maxn],tot,ed;
map <int, int> mp;
struct Edge{
int v,next;
}edge[maxn];
void add(int u,int v){
edge[ed].v = v;
edge[ed].next = head[u];
head[u] = ed ++;
}
void dfs(int root){
bool vis[maxn];
memset(vis,0,sizeof(vis));
tot = 0; stack <int> ss;
while(!ss.empty()){ss.pop();}
ss.push(root); vis[root] = true; L[root] = tot ++;
while(!ss.empty()){
int now = ss.top();
bool flag = false;
for(int i = head[now]; i != - 1; i = edge[i].next){
int x = edge[i].v;
if(!vis[x]){
flag = true;
vis[x] = true;
L[x] = tot ++;
ss.push(x);
head[now] = edge[i].next;
break;
}
}
if(flag) continue;
if(vis[now]){
R[now] = tot;
ss.pop();
}
}
}
int query(int L, int R, int l, int r, int rt){
if(L > R) return -1;
if(L <= l && r <= R){
return Max[rt];
}int m = (l + r) >> 1;
int ll = -1, rr = -1;
if(L <= m) ll = query(L,R,lson);
if(R > m) rr = query(L,R,rson);
return max(ll,rr);
}
void update(int &pos, int &val, int l, int r, int rt){
if(l == r){
Max[rt] = val; return ;
}int m = (l + r) >> 1;
if(pos <= m) update(pos,val,lson);
else update(pos,val,rson);
Max[rt] = max(Max[rt << 1], Max[rt << 1 | 1]);
}
void pre(){
mp.clear(); mp[-1] = -1; tot = ed = 0;
memset(L,0,sizeof(L)); memset(R,0,sizeof(R)); memset(ans,-1,sizeof(ans));
memset(head,-1,sizeof(head)); memset(Max,-1,sizeof(Max));
for(int i = 1,fa; i < n; i ++){
scanf("%d%d%d",&fa,&emp[i].loty, &emp[i].abty);
add(fa,i);
mp[emp[i].loty] = i;
emp[i].id = i;
}
dfs(0);
sort(emp + 1,emp + n);
}
void showans(){
int fired;
while(m --){
scanf("%d",&fired);
printf("%d\n",ans[fired]);
}
}
int main(){
scanf("%d",&t);
while(t --){
scanf("%d%d",&n,&m);
pre();
for(int i = 1, j; i < n; i = j){
j = i;
while(j < n && emp[j].abty == emp[i].abty){
int id = emp[j].id;
int lo = query(L[id] + 1,R[id] - 1,0,tot - 1, 1);
ans[id] = mp[lo];
j ++;
}
j = i;
while(j < n && emp[j].abty == emp[i].abty){
int id = emp[j].id;
update(L[id],emp[j].loty,0,tot - 1, 1);
j ++;
}
}
showans();
}
return 0;
}

164
HDOJ/4367_autoAC.cpp Normal file
View File

@ -0,0 +1,164 @@
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;
typedef long long LL;
const int N=210;
const int M=41000;
const int MOD=1000000007;
const double eps=1e-8;
const double PI=acos(-1.0);
const double PI2=PI*2;
struct Point
{
double x,y;
LL index;
double angle;
inline void input()
{
scanf("%lf%lf",&x,&y);
}
}point[N],temp[N];
int n;
bool visit[M];
LL fib[M];
LL fk[M];
int num[N][N];
int left[N][N];
int right[N][N];
inline Point operator-(const Point &a, const Point &b)
{
Point c;
c.x=a.x-b.x;
c.y=a.y-b.y;
return c;
}
inline double operator*(const Point &a, const Point &b)
{
return a.x*b.y-a.y*b.x;
}
inline bool operator==(const Point &a, const Point &b)
{
return a.x==b.x&&a.y==b.y;
}
inline int cross(const Point &a, const Point &b, const Point &o)
{
return (a-o)*(b-o)>0? 1:-1;
}
inline int cross(const int &a, const int &b, const int &o)
{
return cross(point[a],point[b],point[o]);
}
inline double positiveAtan(const Point &a, const Point &o)
{
double res=atan2(a.y-o.y,a.x-o.x);
if(res<0)
res+=PI2;
return res;
}
bool operator<(const Point &a, const Point &b)
{
return a.angle<b.angle;
}
int abs1(int x)
{
return x<0? -x:x;
}
int getAngleNumber(int a,int b,int c)
{
if(point[c].y<point[b].y&&point[a].y>=point[b].y)
return n-abs1(right[b][c]-right[b][a]+2)+3;
return abs1(right[b][a]-right[b][c])+2;
}
int getTriangleNumber(int a, int b, int c)
{
return n-left[a][b]-left[b][c]-left[c][a]+getAngleNumber(a,b,c)+getAngleNumber(b,c,a)+getAngleNumber(c,a,b)-6;
}
LL quick_mod(LL a,LL b)
{
LL ans=1;
a%=MOD;
while(b)
{
if(b&1)
{
ans=ans*a%MOD;
b--;
}
b>>=1;
a=a*a%MOD;
}
return ans;
}
LL solve(int x)
{
if(visit[x])
return fk[x];
visit[x]=true;
fib[0]=x;
fib[1]=x;
for(int i=2;i<=x;++i)
fib[i]=(fib[i-1]*fib[i-2])%MOD;
return fk[x]=fib[x]+1;
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=0;i<n;++i)
{
point[i].input();
temp[i]=point[i];
temp[i].index=i;
}
memset(left,0,sizeof(left));
memset(right,0,sizeof(right));
for(int i=0;i<n;++i)
{
for(int j=i+1;j<n;++j)
{
for(int k=0;k<n;++k)
{
if(k!=i&&k!=j)
{
if(cross(k,j,i)<0)
++left[i][j];
else if(cross(k,i,j)<0)
++left[j][i];
}
}
}
for(int j=0;j<n;++j)
{
if(temp[j].index==i)
temp[j].angle=-1e100;
else
temp[j].angle=positiveAtan(temp[j],point[i]);
}
sort(temp,temp+n);
int cnt=0;
for(int j=0;j<n;++j)
right[i][temp[j].index]=++cnt;
}
memset(num,0,sizeof(num));
for(int i=0;i<n;++i)
{
for(int j=i+1;j<n;++j)
{
for(int k=0;k<n;++k)
{
if(k==i||k==j) continue;
if(cross(point[k], point[j], point[i]) < 0)
num[i][j]+=getAngleNumber(j,k,i)-getTriangleNumber(i,j,k);
}
}
}
LL ans=1;
for(int i=0;i<n;++i)
for(int j=i+1;j<n;++j)
ans=(ans*solve(num[i][j]))%MOD;
printf("%I64d\n", ans);
}
return 0;
}

63
HDOJ/4368_autoAC.cpp Normal file
View File

@ -0,0 +1,63 @@
#include<stdio.h>
#include<math.h>
#define eps 1e-5
#define PI 3.1415926535897932384626
int n,a[10001];
double angle;
int i,j,k,maxtop;
double dis[10001],t,top,ans,low,h;
int main()
{
while(~scanf("%d%lf",&n,&angle))
{
if(angle<-eps) for(i=n-1;i>=0;i--) scanf("%d",&a[i]);
else for(i=0;i<n;i++) scanf("%d",&a[i]);
if(angle<-eps) angle=-angle;
angle=PI*angle/180.0;
t=-1;ans=0;
for(i=0;i<n;i++)
{
dis[i]=((i+1)*tan(angle)+a[i]*1.0)/sqrt(tan(angle)*tan(angle)+1);
if(t<dis[i]){t=dis[i];maxtop=i;}
}
for(i=0;i<maxtop;i++)
{
top=a[i];
for(j=i+1;j<=maxtop;j++)
{
if(dis[j]>dis[i]+eps)
{
if(dis[j]-sin(angle)>dis[i]+eps)
{
top-=(j-i-1)*1.0*tan(angle);
ans+=(j-i-1)*(j-i-1)*0.5*tan(angle);
}
else
{
top-=(a[i]-a[j])*1.0;
ans+=(a[i]-a[j])*(a[i]-a[j])*0.5/tan(angle);
}
for(k=i+1;k<j;k++)
ans+=(top-a[k])*1.0;
i=j-1;
break;
}
}
}
for(i=n-1;i>maxtop;i--)
{
for(j=i-1;j>=maxtop;j--)
{
h=i-j;
low=h*tan(angle)-(a[j+1]-a[i]);
top=(h-1)*tan(angle)-(a[j+1]-a[i]);
ans+=(top+low)/2.0;
if(dis[j]>dis[i]+eps)
break;
}
i=j+1;
}
printf("%.2lf\n",ans);
}
return 0;
}

71
HDOJ/4370_autoAC.cpp Normal file
View File

@ -0,0 +1,71 @@
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=330;
int cost[MAXN][MAXN];
int dist[MAXN];
int que[MAXN];
bool vis[MAXN];
void SPFA(int start,int n)
{
int front=0,rear=0;
for(int v=1;v<=n;v++)
{
if(v==start)
{
dist[v]=INF;
vis[v]=false;
}
else if(cost[start][v]!=INF)
{
dist[v]=cost[start][v];
que[rear++]=v;
vis[v]=true;
}
else
{
dist[v]=INF;
vis[v]=false;
}
}
while(front!=rear)
{
int u=que[front++];
for(int v=1;v<=n;v++)
{
if(dist[v]>dist[u]+cost[u][v])
{
dist[v]=dist[u]+cost[u][v];
if(!vis[v])
{
vis[v]=true;
que[rear++]=v;
if(rear>=MAXN) rear=0;
}
}
}
vis[u]=false;
if(front>=MAXN)front=0;
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
SPFA(1,n);
int ans=dist[n];
int loop1=dist[1];
SPFA(n,n);
int loopn=dist[n];
ans=min(ans,loop1+loopn);
printf("%d\n",ans);
}
return 0;
}

23
HDOJ/4371_autoAC.cpp Normal file
View File

@ -0,0 +1,23 @@
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int test,n,m,tmin,t,i,k;
scanf("%d",&test);
for(k=1;k<=test;k++)
{
tmin=1000000;
scanf("%d %d",&n,&m);
for(i=0;i<m;i++)
{
scanf("%d",&t);
if(tmin>t)
tmin=t;
}
n=n/tmin;
if(n&1) printf("Case #%d: Bob\n",k);
else printf("Case #%d: Alice\n",k);
}
return 0;
}

38
HDOJ/4372_autoAC.cpp Normal file
View File

@ -0,0 +1,38 @@
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
typedef long long LL;
const int N=2005;
const LL MOD=1000000007;
LL C[N][N];
LL S[N][N];
void Init()
{
int i,j;
for(i=0;i<N;i++)
{
C[i][0]=1;
C[i][i]=1;
S[i][0]=0;
S[i][i]=1;
for(j=1;j<i;j++)
{
C[i][j]=(C[i-1][j]%MOD+C[i-1][j-1]%MOD)%MOD;
S[i][j]=((i-1)%MOD*S[i-1][j]%MOD+S[i-1][j-1]%MOD);
}
}
}
int main()
{
LL t,n,f,b,ans;
Init();
scanf("%I64d",&t);
while(t--)
{
scanf("%I64d%I64d%I64d",&n,&f,&b);
ans=C[f+b-2][f-1]%MOD*S[n-1][f+b-2]%MOD;
printf("%I64d\n",ans);
}
return 0;
}

159
HDOJ/4373_autoAC.cpp Normal file
View File

@ -0,0 +1,159 @@
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<map>
#include<set>
#include<cmath>
#include<cstring>
#include<iomanip>
using namespace std;
#define i64 __int64
#define MM(name,what) memset(name,what,sizeof(name))
const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 10e-9;
const double pi = acos(-1.0);
const int maxn = 100111;
const i64 mod = 364875103;
const i64 mod1 = 97;
const i64 mod2 = 3761599;
const int maxc = 111;
i64 gcd(i64 _a, i64 _b)
{
if (!_a || !_b)
{
return max(_a, _b);
}
i64 _t;
while ((_t = _a % _b))
{
_a = _b;
_b = _t;
}
return _b;
}
i64 ext_gcd (i64 _a, i64 _b, i64 &_x, i64 &_y)
{
if (!_b)
{
_x = 1;
_y = 0;
return _a;
}
i64 _d = ext_gcd (_b, _a % _b, _x, _y);
i64 _t = _x;
_x = _y;
_y = _t - _a / _b * _y;
return _d;
}
i64 invmod (i64 _a, i64 _p)
{
i64 _ans, _y;
ext_gcd (_a, _p, _ans, _y);
_ans < 0 ? _ans += _p : 0;
return _ans;
}
i64 dp[maxn];
i64 inv[maxn];
i64 c[maxc][maxc];
i64 n,m;
i64 ans;
i64 a[21];
i64 cx;
void init()
{
for(int i=1;i<maxn;i++)
{
inv[i]=invmod(i,mod2);
}
MM(c,0);
for(int i=0;i<maxc;i++)
{
c[i][i]=1;
c[i][0]=1;
}
for(int i=1;i<maxc;i++)
{
for(int j=1;j<maxc;j++)
{
c[i][j]=c[i-1][j-1]+c[i-1][j];
c[i][j]%=mod1;
}
}
cx = invmod(mod1,mod2);
return ;
}
i64 lucas(i64 x,i64 y,i64 p=mod1)
{
if(y>x) return 0;
i64 re=1;
i64 dx = x%p;
i64 dy = y%p;
re*=c[dx][dy];
dx = x/p;
dy = y/p;
if(dx || dy)
{
re*=lucas(dx,dy);
}
re%=p;
return re;
}
i64 find(int x)
{
i64 t1 = lucas(n+x-1,x);
i64 t2 = dp[x];
i64 temp = cx;
temp *= t2-t1;
temp %= mod2;
temp = (temp+mod2)%mod2;
temp *= mod1;
temp += t1;
temp %= mod;
return temp;
}
void start()
{
dp[0]=1;
i64 now,temp;
for(int i=1;i<=m;i++)
{
now = n+i-1;
dp[i]=dp[i-1]*now;
dp[i]%=mod2;
dp[i]*=inv[i];
dp[i]%=mod2;
}
return ;
}
int main()
{
init();
int k,T;
cin>>T;
for(int tt=1;tt<=T;tt++)
{
cin>>n>>m;
start();
cin>>k;
for(int i=1;i<=k;i++)
{
cin>>a[i];
a[i]++;
}
ans=1;
i64 pre = m;
i64 now;
for(int i=k;i>=1;i--)
{
now = pre - a[i]+1;
ans *= find(now);
ans %= mod;
pre = a[i]-1;
}
cout<<"Case #"<<tt<<": "<<ans<<endl;
}
return 0;
}

99
HDOJ/4374_autoAC.cpp Normal file
View File

@ -0,0 +1,99 @@
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#define eps 1e-5
#define MAXN 105
#define MAXM 11111
#define INF 1000000000
#define lch(x) x<<1
#define rch(x) x<<1|1
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int n, m, x, t;
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;
}
int dp[MAXN][MAXM];
int sum[MAXN][MAXM];
int lmx[4 * MAXM];
int rmx[4 * MAXM];
int a[MAXN][MAXM];
int q[MAXM];
int main()
{
while(scanf("%d%d%d%d", &n, &m, &x, &t) != EOF)
{
for(int i = 1; i <= n + 2; i++)
for(int j = 1; j <= m; j++)
sum[i][j] = 0, dp[i][j] = -INF, a[i][j] = 0;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
a[i][j] = in();
sum[i][j] = sum[i][j - 1] + a[i][j];
}
int tmp = 0;
for(int i = x; i <= x + t; i++)
{
tmp += a[1][i];
dp[1][i] = tmp;
dp[2][i] = dp[1][i] + a[2][i];
}
tmp = 0;
for(int i = x; i >= x - t; i--)
{
tmp += a[1][i];
dp[1][i] = tmp;
dp[2][i] = dp[1][i] + a[2][i];
}
for(int i = 3; i <= n + 1; i++)
{
for(int j = 1; j <= m; j++)
{
lmx[j] = dp[i - 1][j] - sum[i - 1][j];
rmx[j] = dp[i - 1][j] + sum[i - 1][j - 1];
}
int head = 1, rear = 0;
for(int j = 1; j <= m; j++)
{
while(head <= rear && lmx[q[rear]] <= lmx[j]) rear--;
q[++rear] = j;
while(head <= rear && q[head] < j - t) head++;
dp[i][j] = max(lmx[q[head]] + sum[i - 1][j] + a[i][j], dp[i][j]);
}
head = 1, rear = 0;
for(int j = m; j >= 1; j--)
{
while(head <= rear && rmx[q[rear]] <= rmx[j]) rear--;
q[++rear] = j;
while(head <= rear && q[head] > j + t) head++;
dp[i][j] = max(rmx[q[head]] - sum[i - 1][j - 1] + a[i][j], dp[i][j]);
}
}
int ans = -INF;
for(int i = 1; i <= m; i++)
if(dp[n + 1][i] > ans) ans = dp[n + 1][i];
printf("%d\n", ans);
}
return 0;
}

114
HDOJ/4375_autoAC.cpp Normal file
View File

@ -0,0 +1,114 @@
#include<iostream>
#include<sstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int H, W, N;
#define next nex
char lr[1000100];
int next[1000100][4];
int dir[1000100];
const int inf = 1e9 + 7;
char maz[1010][1010];
int vis[1010][1010][4];
int sx, sy, ex, ey;
#define mp make_pair
int cas;
int tot;
struct Edge
{
int x, y, nex;
};
Edge edge[4000010];
int head[1000010];
void addEdge(int s, int x, int y)
{
edge[tot].x = x, edge[tot].y = y;
edge[tot].nex = head[s];
head[s] = tot++;
}
bool bfs()
{
memset(head, -1, sizeof(head));
tot = 0;
while(sx >= 0 && sx < H && sy >= 0 && sy < W && vis[sx][sy][0] != cas && maz[sx][sy] != '#')
{
addEdge(0, sx, sy);
vis[sx][sy][0] = cas;
if(sx == ex && sy == ey) return true;
sx += dx[0], sy += dy[0];
}
for(int i = 0; i <= N; i++)
{
for(int j = head[i]; j + 1; j = edge[j].nex)
{
int nx = edge[j].x, ny = edge[j].y;
for(int d = 0; d < 4; d++)
{
if(next[i][d] == inf) continue;
int tx = nx + dx[d], ty = ny + dy[d];
while(tx >= 0 && tx < H && ty >= 0 && ty < W && vis[tx][ty][d] != cas && maz[tx][ty] != '#')
{
addEdge(next[i][d], tx, ty);
vis[tx][ty][d] = cas;
if(tx == ex && ty == ey) return true;
tx += dx[d], ty += dy[d];
}
}
}
}
return false;
}
void init()
{
dir[0] = 0;
for(int i = 0; i < N; i++)
dir[i + 1] = (lr[i] == 'L') ? (dir[i] + 3) % 4 : (dir[i] + 1) % 4;
int last[4] = {inf, inf, inf, inf};
for(int i = N; i >= 0; i--)
{
for(int j = 0; j < 4; j++)
next[i][j] = last[j];
last[dir[i]] = i;
}
return;
}
int main()
{
cas = 0;
memset(vis, 0, sizeof(vis));
while(~scanf("%d %d %d", &H, &W, &N))
{
cas++;
scanf("%s", lr);
init();
for(int i = 0; i < H; i++)
{
scanf("%s", maz[i]);
for(int j = 0; j < W; j++)
if(maz[i][j] == 'S') sx = i, sy = j;
else if(maz[i][j] == 'E') ex = i, ey = j;
}
if(bfs()) puts("Yes");
else puts("No");
}
return 0;
}

171
HDOJ/4376_autoAC.cpp Normal file
View File

@ -0,0 +1,171 @@
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define MAXL 22
#define KIND 9
#define MAXNODE 42
typedef long long LL;
vector<int> A, B, RA, RB;
char subA[MAXL], subB[MAXL];
int N, en[MAXNODE], suffix[MAXNODE], q[MAXNODE], child[MAXNODE][KIND];
LL dp[MAXL][3][3][3][3][MAXNODE][1 << 2];
int change(char ch) {
return ch - '1';
}
void Trie() {
memset(en, 0, sizeof(en));
memset(child, 0, sizeof(child));
N = 1;
for(int i = 0; i < 2; ++i) {
char* s = i == 0? subA: subB;
int p = 1;
for (int j = 0; s[j]; ++j) {
int ch = change(s[j]);
if (!child[p][ch]) child[p][ch] = ++N;
p = child[p][ch];
}
en[p] |= 1 << i;
}
}
void AC() {
int ql = 0, qr = 0;
for (int i = 0; i < KIND; ++i)
if (child[1][i]) {
suffix[child[1][i]] = 1;
q[qr++] = child[1][i];
}
else child[1][i] = 1;
while (ql < qr) {
int u = q[ql++];
for (int i = 0; i < KIND; ++i)
if (child[u][i]) {
suffix[child[u][i]] = child[suffix[u]][i];
q[qr++] = child[u][i];
}
else child[u][i] = child[suffix[u]][i];
}
for (int i = 1; i <= N; ++i)
for (int j = i; j != 1; j = suffix[j]) en[i] |= en[j];
}
int cal1(int pos, int val, int pre, vector<int>& vec) {
if(pos >= vec.size()) return 2;
if(pre != 0) return pre;
if(val > vec[pos]) return 2;
if(val < vec[pos]) return 1;
return 0;
}
int cal2(int pos, int val, int pre, vector<int>& vec) {
if(pos >= vec.size()) return 2;
if(val > vec[pos]) return 2;
if(val < vec[pos]) return 1;
return pre;
}
int main() {
int T;
scanf("%d", &T);
for(int cas = 1; cas <= T; ++cas) {
LL a, b;
cin >> a >> b;
scanf("%s%s", subA, subB);
LL sub = 0, rsub = 0;
for(int i = 0; subA[i]; ++i)
sub = sub * 10 + subA[i] - '0';
for(int i = 0; subB[i]; ++i)
rsub = rsub * 10 + subB[i] - '0';
reverse(subB, subB + strlen(subB));
Trie();
AC();
A.clear();
B.clear();
RA.clear();
RB.clear();
LL ta = a, tb = b;
while(ta) {
RA.push_back(ta % 10);
ta /= 10;
}
while(tb) {
RB.push_back(tb % 10);
tb /= 10;
}
A = RA;
B = RB;
reverse(A.begin(), A.end());
reverse(B.begin(), B.end());
int l = B.size();
memset(dp, 0, sizeof(dp));
for(int j = 1; j < 10; ++j) {
int s1 = cal1(0, j, 0, A);
int s2 = cal1(0, j, 0, B);
int rs1 = cal2(0, j, 0, RA);
int rs2 = cal2(0, j, 0, RB);
int v = child[1][j - 1];
++dp[0][s1][s2][rs1][rs2][v][en[v]];
}
for(int i = 0; i + 1 < l; ++i) {
for(int s1 = 0; s1 < 3; ++s1) for(int s2 = 0; s2 < 3; ++s2) for(int rs1 = 0; rs1 < 3; ++rs1) for(int rs2 = 0; rs2 < 3; ++rs2)
for(int u = 1; u <= N; ++u)
for(int mask = 0; mask < (1 << 2); ++mask) if(dp[i][s1][s2][rs1][rs2][u][mask]) {
for(int val = 1; val < 10; ++val) {
int ns1 = cal1(i + 1, val, s1, A);
int ns2 = cal1(i + 1, val, s2, B);
int nrs1 = cal2(i + 1, val, rs1, RA);
int nrs2 = cal2(i + 1, val, rs2, RB);
int v = child[u][val - 1];
dp[i + 1][ns1][ns2][nrs1][nrs2][v][mask | en[v]] += dp[i][s1][s2][rs1][rs2][u][mask];
}
}
}
LL res = 0;
int la = A.size(), lb = B.size();
for(int len = la; len < lb - 1; ++len) {
for(int s1 = 0; s1 < 3; ++s1) for(int s2 = 0; s2 < 3; ++s2) for(int rs1 = 0; rs1 < 3; ++rs1) for(int rs2 = 0; rs2 < 3; ++rs2)
for(int u = 1; u <= N; ++u)
for(int mask = 1; mask < (1 << 2); ++mask)
res += dp[len][s1][s2][rs1][rs2][u][mask];
}
if(la != lb) {
for(int s1 = 0; s1 < 3; ++s1) for(int s2 = 0; s2 < 3; ++s2) for(int rs1 = 0; rs1 < 3; ++rs1) for(int rs2 = 0; rs2 < 3; ++rs2)
for(int u = 1; u <= N; ++u)
for(int mask = 1; mask < (1 << 2); ++mask) {
bool flaga = false;
if(mask == 1 || mask == 3) {
if(s1 == 0 || s1 == 2) flaga = true;
}
if(mask == 2 || mask == 3) {
if(rs1 == 0 || rs1 == 2) flaga = true;
}
if(flaga)
res += dp[la - 1][s1][s2][rs1][rs2][u][mask];
bool flagb = false;
if(mask == 1 || mask == 3) {
if(s2 == 0 || s2 == 1) flagb = true;
}
if(mask == 2 || mask == 3) {
if(rs2 == 0 || rs2 == 1) flagb = true;
}
if(flagb)
res += dp[lb - 1][s1][s2][rs1][rs2][u][mask];
}
} else {
for(int s1 = 0; s1 < 3; ++s1) for(int s2 = 0; s2 < 3; ++s2) for(int rs1 = 0; rs1 < 3; ++rs1) for(int rs2 = 0; rs2 < 3; ++rs2)
for(int u = 1; u <= N; ++u)
for(int mask = 1; mask < (1 << 2); ++mask) {
bool flag = false;
if(mask == 1 || mask == 3) {
if((s1 == 0 || s1 == 2) && (s2 == 0 || s2 == 1)) flag = true;
}
if(mask == 2 || mask == 3) {
if((rs1 == 0 || rs1 == 2) && (rs2 == 0 || rs2 == 1)) flag = true;
}
if(flag) res += dp[la - 1][s1][s2][rs1][rs2][u][mask];
}
}
printf("Case #%d: ", cas);
cout << res << endl;
}
return 0;
}

35
HDOJ/4377_autoAC.cpp Normal file
View File

@ -0,0 +1,35 @@
#include<stdio.h>
#include<math.h>
int T,n;
int i,j,qujian[100001],began,t,res;
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
t=(int)(sqrt(n)+0.99999);
res=t*t-n;
for(i=0;i<t;i++) qujian[i]=t;
for(i=0;i<t;i++)
if(qujian[i]<res+1)
{
qujian[i]=1;
res-=(t-1);
}
else
{
qujian[i]-=res;
break;
}
began=0;
for(i=0;i<t;i++)
{
began+=qujian[i];
for(j=0;j<qujian[i];j++)
if(i==t-1&&j==qujian[i]-1) printf("%d\n",began-j);
else printf("%d ",began-j);
}
}
return 0;
}

36
HDOJ/4379_autoAC.cpp Normal file
View File

@ -0,0 +1,36 @@
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
unsigned int a,b,k,n,l,last;
unsigned int mod;
bool v[21000000];
int main()
{
while(cin>>n>>l>>a>>b>>mod)
{
for(unsigned int i=1;i<=n;i++)
v[i]=false;
unsigned int ans=0;unsigned int max=0,t;
last=b;
for(unsigned int i=1;i<=n;i++)
{
t=(last+a)%mod;
if(t*2<=l)
{
ans++;v[i]=true;
if(t>max)max=t;
}
last=t;
}
last=b;
for(unsigned int i=1;i<=n;i++)
{
t=(last+a)%mod;
if((!v[i])&&t+max<=l){ans++;break;}
last=t;
}
cout<<ans<<endl;
}
return 0;
}

92
HDOJ/4380_autoAC.cpp Normal file
View File

@ -0,0 +1,92 @@
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long LL;
const int N=105;
const int M=1005;
struct Point
{
LL x,y;
}P1[N],P2[M];
int f[N][N][N];
int n,m;
LL cross(Point a,Point b,Point s)
{
LL x1=a.x-s.x,y1=a.y-s.y;
LL x2=b.x-s.x,y2=b.y-s.y;
return x1*y2-x2*y1;
}
int dis2(Point a,Point b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*((a.y-b.y));
}
int cmp(Point a,Point b)
{
if(cross(a,b,P1[0])>0)
return 1;
else return 0;
}
void init()
{
int i,t;
for(t=0,i=0;i<n;i++){
if((P1[i].y-P1[t].y)<0 || (P1[i].y-P1[t].y==0 && P1[i].x-P1[t].x<0))
t=i;
}
swap(P1[0],P1[t]);
sort(P1+1,P1+n,cmp);
}
int cal(int x,int y,int z)
{
int i;
int sum=0;
LL a,b,c;
for (i=0;i<m;i++)
{
a=cross(P1[y],P2[i],P1[x]);
b=cross(P1[z],P2[i],P1[y]);
c=cross(P1[x],P2[i],P1[z]);
if ((a>=0 && b>=0 && c>=0) || (a<=0 && b<=0 && c<=0))
{
sum++;
}
}
return sum;
}
int main()
{
int i,j,k;
int CA=0;
int ans=0;
while (scanf("%d%d",&n,&m)!=EOF)
{
ans=0;
memset(P1,0,sizeof(P1));
memset(P2,0,sizeof(P2));
memset(f,0,sizeof(f));
for (i=0;i<n;i++)
scanf("%I64d%I64d",&P1[i].x,&P1[i].y);
for (i=0;i<m;i++)
scanf("%I64d%I64d",&P2[i].x,&P2[i].y);
init();
for (i=2;i<n;i++){
for (j=1;j<i;j++){
f[0][j][i]=cal(0,j,i);
if (f[0][j][i]%2!=0) ans++;
}
}
for (i=2;i<n;i++)
for (j=1;j<i;j++)
for (k=j+1;k<i;k++){
if (cross(P1[k],P1[i],P1[j])>0)
f[j][k][i]=f[0][j][k]+f[0][k][i]-f[0][j][i];
else
f[j][k][i]=f[0][j][i]-(f[0][j][k]+f[0][k][j]);
if (f[j][k][i]%2!=0) ans++;
}
printf("Case %d: ",++CA);
printf("%d\n",ans);
}
}

74
HDOJ/4381_autoAC.cpp Normal file
View File

@ -0,0 +1,74 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int maxn = 2005;
struct node{
int aa,cor;
node(){}
node(int _aa,int _cor){
aa = _aa; cor = _cor;
}
}x1[maxn],x2[maxn];
int n,m;
int dp1[maxn],dp2[maxn];
int sumx1,sumx2;
bool cmp(const node &p,const node &q){
return p.aa < q.aa;
}
int min(int a,int b)
{
return a<b?a:b;
}
int main()
{
int cas,ta=1;
scanf("%d",&cas);
while(cas--){
int i,j;
scanf("%d%d",&n,&m);
sumx1 = sumx2 = 1;
for(i=0; i<m; i++){
int key,yy,z;
scanf("%d%d%d",&key,&yy,&z);
if(key == 1){
x1[sumx1++] = node(yy,z);
}else{
x2[sumx2++] = node(n+1-yy,z);
}
}
sort(x1+1,x1+sumx1,cmp);
sort(x2+1,x2+sumx2,cmp);
memset(dp1,0x3f,sizeof(dp1));
memset(dp2,0x3f,sizeof(dp2));
dp1[0] = dp2[0] = 0;
for(i=1; i<sumx1; i++){
for(j=x1[i].aa; j>=x1[i].cor; j--){
dp1[j] = min(dp1[j],dp1[j-x1[i].cor]+1);
}
}
for(i=1; i<sumx2; i++){
for(j=x2[i].aa; j>=x2[i].cor; j--){
dp2[j] = min(dp2[j],dp2[j-x2[i].cor]+1);
}
}
int ans = 0,anssum = 0,tmp;
for(i=1; i<=n; i++){
for(j=0; j<=i; j++){
tmp = dp1[j] + dp2[i-j];
if(tmp <= m){
if(ans != i){
ans = i; anssum = tmp;
}else if(tmp < anssum){
anssum = tmp;
}
}
}
}
printf("Case %d: %d %d\n",ta++,ans,anssum);
}
return 0;
}

2
HDOJ/4382_autoAC.cpp Normal file
View File

@ -0,0 +1,2 @@
SET|ADD C1|C2, C1|C2|IntegerNumber
MUL C1|C2, IntegerNumber

94
HDOJ/4383_autoAC.cpp Normal file
View File

@ -0,0 +1,94 @@
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<deque>
#include<map>
#include<queue>
#define iinf 2000000000
#define linf 1000000000000000000LL
#define dinf 1e200
#define eps 1e-5
#define lng long long
#define sqr(a) ((a)*(a))
#define pi 3.14159265359
#define ff(i,xi,n) for(int i=xi;i<=(int)(n);++i)
#define ffd(i,xi,n) for(int i=xi;i>=(int)(n);--i)
#define ffl(i,r) for(int i=head[r];i!=-1;i=edge[i].next)
#define cc(i,j) memset(i,j,sizeof(i))
#define two(x) ((lng)1<<(x))
#define N 11111
#define M 1000000
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define Mod n
#define Pmod(x) (x%Mod+Mod)%Mod
using namespace std;
typedef vector<int> vi;
typedef vector<string> vs;
typedef unsigned int uint;
typedef unsigned lng ulng;
template<class T> inline void checkmax(T &x,T y){if(x<y) x=y;}
template<class T> inline void checkmin(T &x,T y){if(x>y) x=y;}
template<class T> inline T Min(T x,T y){return (x>y?y:x);}
template<class T> inline T Max(T x,T y){return (x<y?y:x);}
template<class T> T gcd(T a,T b){return (a%b)==0?b:gcd(b,a%b);}
template<class T> T lcm(T a,T b){return a*b/gcd(a,b);}
template<class T> T Abs(T a){return a>0?a:(-a);}
template<class T> inline T lowbit(T n){return (n^(n-1))&n;}
template<class T> inline int countbit(T n){return (n==0)?0:(1+countbit(n&(n-1)));}
template<class T> inline bool isPrimeNumber(T n)
{if(n<=1)return false;for (T i=2;i*i<=n;i++) if (n%i==0) return false;return true;}
template<class T> inline T Minmin(T a,T b,T c,T d){return Min(Min(a,b),Min(c,d));}
int dp[N][2][2],ini[N],head[N],tot,n;
struct pp
{
int v,next;
}edge[N*2];
inline void add(int u,int v)
{
edge[tot].v=v;
edge[tot].next=head[u];
head[u]=tot++;
}
void dfs(int r,int pre)
{
int clo=ini[r],rev=1-clo;
int sum=0,dps[3]={};
dps[1]=dps[2]=iinf;
ffl(i,r)
{
int v=edge[i].v;
if(v==pre) continue;
dfs(v,r);
sum+=dp[v][clo][0];
dps[2]=Min(dps[1]+dp[v][rev][1],dps[2]+dp[v][rev][0]);
dps[1]=Min(dps[0]+dp[v][rev][1],dps[1]+dp[v][rev][0]);
dps[0]+=dp[v][rev][0];
}
dp[r][clo][0]=Minmin(sum,dps[0]+2,dps[1]+2,dps[2]+2);
dp[r][clo][1]=Min(dps[1]+1,dps[0]+1);
dp[r][rev][0]=Minmin(sum+1,dps[0]+1,dps[1]+1,dps[2]+1);
dp[r][rev][1]=Min(dps[0],dps[1]);
}
int main()
{
int o=0;
while(scanf("%d",&n)==1)
{
cc(head,-1);
tot=0;
ff(i,2,n)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
ff(i,1,n)
scanf("%d",ini+i);
dfs(1,-1);
printf("Case %d: %d\n",++o,Min(dp[1][0][0],dp[1][1][0]));
}
return 0;
}

103
HDOJ/4385_autoAC.cpp Normal file
View File

@ -0,0 +1,103 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define clr(x)memset(x,0,sizeof(x))
#define min(a,b)(a)<(b)?(a):(b)
#define INF 0x1f1f1f1f
#define N 21
struct node
{
int x,y;
}p[N],be;
int dis(node a,node b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int g[N][N];
int dp[1<<N];
int pre[1<<N];
struct ans
{
int fi,se,num;
}res[N];
bool cmp(ans a,ans b)
{
return a.fi<b.fi;
}
int main()
{
int ca=1,n,i,j,k,t;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&be.x,&be.y);
scanf("%d",&n);
p[n]=be;
for(i=0;i<n;i++)
scanf("%d%d",&p[i].x,&p[i].y);
memset(dp,INF,sizeof(dp));
pre[0]=0;
int st=1<<n;
dp[0]=0;
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
g[i][j]=dis(p[i],p[j]);
printf("Case %d:\n",ca++);
int stat,tmp,ss;
for(i=0;i<st;i++)
{
for(j=0;j<n;j++)
{
if((1<<j)&i)
continue;
stat=i|(1<<j);
tmp=dp[i]+g[n][j]*2;
if(tmp<dp[stat])
{
dp[stat]=tmp;
pre[stat]=i;
}
for(k=j+1;k<n;k++)
{
if((1<<k)&i)
continue;
ss=stat|(1<<k);
tmp=dp[i]+g[n][j]+g[j][k]+g[n][k];
if(tmp<dp[ss])
{
dp[ss]=tmp;
pre[ss]=i;
}
}
}
}
st-=1;
printf("%d\n",dp[st]);
int top=0;
while(st)
{
stat=pre[st]^st;
st=pre[st];
res[top].num=0;
for(i=0;i<n;i++)
if((1<<i)&stat)
{
res[top].num++;
if(res[top].num==1)
res[top].fi=i+1;
else res[top].se=i+1;
}
top++;
}
sort(res,res+top,cmp);
for(i=0;i<top;i++)
{
if(res[i].num==1)
printf("%d%c",res[i].fi,i==top-1?'\n':' ');
else
printf("%d %d%c",res[i].fi,res[i].se,i==top-1?'\n':' ');
}
}
return 0;
}

27
HDOJ/4386_autoAC.cpp Normal file
View File

@ -0,0 +1,27 @@
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
{
int t;
int a,b,c,d,max,count=0;
double p,ans;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
if(a>b) max=a;
else max=b;
if(max<b) max=b;
if(max<c) max=c;
if(max<d) max=d;
if(max-(a+b+c+d-max)>=0)
{
printf("Case %d: -1\n",++count);
continue;
}
p=(a+b+c+d)*1.0/2;
ans=sqrt((p-a)*(p-b)*(p-c)*(p-d));
printf("Case %d: %.6lf\n",++count,ans);
}
}

23
HDOJ/4387_autoAC.cpp Normal file
View File

@ -0,0 +1,23 @@
#include <cstdio>
#include <algorithm>
using namespace std;
int n,k;
int main(void)
{
int cas = 1;
while(scanf("%d %d",&n,&k)!=EOF){
printf("Case %d: ",cas++);
int r = n - 2 * k;
int ans = 1;
bool sig = true;
if(r&1){
if(k == 1) sig = false;
else if(r == 1) ans = k;
}
if(sig)
printf("Alice %d\n",ans);
else
puts("Bob");
}
return 0;
}

45
HDOJ/4388_autoAC.cpp Normal file
View File

@ -0,0 +1,45 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int getOneNumber(int x)
{
int count = 0;
for(int i=0;(1<<i)<=x;++i)
{
if(x & (1 << i))
{
++ count;
}
}
return count;
}
int main()
{
int caseNumber;
int n, m, a;
scanf("%d", &caseNumber);
for(int cas=1;cas<=caseNumber;++cas)
{
scanf("%d", &n);
int a = 0;
while(n--)
{
scanf("%d", &m);
if(!(getOneNumber(m) & 1))
{
++ a;
}
}
printf("Case %d: ", cas);
if(a & 1)
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
return 0;
}

5
HDOJ/4389_autoAC.cpp Normal file
View File

@ -0,0 +1,5 @@
Here is a function f(x):
int f ( int x ) {
if ( x == 0 ) return 0;
return f ( x / 10 ) + x % 10;
}

103
HDOJ/4390_autoAC.cpp Normal file
View File

@ -0,0 +1,103 @@
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <ctime>
#include <queue>
#include <map>
#include <sstream>
#define CL(arr, val) memset(arr, val, sizeof(arr))
#define REP(i, n) for((i) = 0; (i) < (n); ++(i))
#define FOR(i, l, h) for((i) = (l); (i) <= (h); ++(i))
#define FORD(i, h, l) for((i) = (h); (i) >= (l); --(i))
#define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) x < y ? x : y
#define Max(x, y) x < y ? y : x
#define E(x) (1 << (x))
const double eps = 1e-4;
typedef long long LL;
const int inf = ~0u>>2;
using namespace std;
const int N = 110;
const int M = 1000010;
const int MOD = 1e9 + 7;
int c[N][N];
int k[M], t[M];
int num, n;
int prime[N*20];
bool vis[N*20];
int cnt;
void get_prime() {
CL(vis, true);
int i, j;
for(i = 2; i < N*20; ++i) {
for(j = i*i; j < N*20; j += i) {
vis[j] = false;
}
}
cnt = 0;
for(i = 2; i < N*20; ++i) {
if(vis[i]) prime[cnt++] = i;
}
}
void init() {
get_prime();
CL(c, 0);
int i , j;
for(i = 0; i < N; ++i) c[i][0] = c[i][i] = 1;
for(i = 2; i < N; ++i) {
for(j = 1; j < i; ++j) {
c[i][j] = (c[i-1][j] + c[i-1][j-1])%MOD;
}
}
}
LL solve(int x) {
int m = n, flag = 1, i;
for(i = 0; i < n; ++i) {
if(((1<<i)&x) == 0) {
flag *= -1; m--;
}
}
LL tmp = 1;
for(i = 0; i < num; ++i) {
tmp = tmp*LL(c[k[i]+m-1][m-1])%MOD;
}
return tmp*flag;
}
int main() {
init();
int x, i, j;
LL ans;
while(~scanf("%d", &n)) {
CL(t, 0);
for(i = 0; i < n; ++i) {
scanf("%d", &x);
for(j = 0; prime[j]*prime[j] <= x; ++j) {
if(x%prime[j] == 0) {
while(x%prime[j] == 0) {
t[prime[j]]++; x /= prime[j];
}
}
}
if(x != 1) t[x]++;
}
num = 0;
for(i = 2; i < M; ++i) {
if(t[i] != 0) {
k[num++] = t[i];
}
}
ans = 0;
for(i = (1<<n)-1; i >= 1; --i) {
ans = (ans + solve(i))%MOD;
}
cout << (ans%MOD + MOD)%MOD << endl;
}
return 0;
}

79
HDOJ/4391_autoAC.cpp Normal file
View File

@ -0,0 +1,79 @@
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map>
using namespace std;
#define for if(0); else for
const int N=65537*2;
struct SegTree{
int a[N<<1],mi[N<<1],mx[N<<1];
int Q;
void init(int n,int c[]){
for(Q=1;Q<=n+2;Q<<=1);
memset(a,0,sizeof(a));
for(int i=Q+0;i<Q+n;i++) a[i]=mi[i]=mx[i]=c[i-Q];
for(int i=Q-1;i>=1;i--) pushup(i);
}
void pushup(int rt) {
a[rt]=a[rt<<1]==a[rt<<1|1]?a[rt<<1]:-1;
mi[rt]=min(mi[rt<<1],mi[rt<<1|1]);
mx[rt]=max(mx[rt<<1],mx[rt<<1|1]);
}
void update_one(int rt,int x) {
a[rt]=mi[rt]=mx[rt]=x;
}
void pushdown(int rt) {
if(a[rt]!=-1) {
update_one(rt<<1,a[rt]);
update_one(rt<<1|1,a[rt]);
}
}
void update(int L,int R,int l,int r,int rt,int c) {
if(a[rt]==c) return;
if(L<=l && r<=R) {
update_one(rt,c);
return;
}
pushdown(rt);
if(rt>=Q) return;
int m=(l+r)>>1;
if(L<=m) update(L,R,l,m,rt<<1,c);
if(m<R) update(L,R,m+1,r,rt<<1|1,c);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt,int c) {
int ret=0;
if(L<=l && r<=R) {
if(c>=mi[rt]&&c<=mx[rt]){
int m=(l+r)>>1;
if(a[rt]!=-1) return (a[rt]==c)?r-l+1:0;
else return query(L,R,l,m,rt<<1,c)+query(L,R,m+1,r,rt<<1|1,c);
}else return 0;
}
if(rt>=Q) return ret;
pushdown(rt);
int m=(l+r)>>1;
if(L<=m) ret+=query(L,R,l,m,rt<<1,c);
if(m<R) ret+=query(L,R,m+1,r,rt<<1|1,c);
return ret;
}
}st;
int a[N];
int n,m;
int main() {
while(scanf("%d%d",&n,&m)!=EOF) {
for(int i=0;i<n;i++) scanf("%d",&a[i]);
st.init(n,a);
for(int i=0;i<m;i++) {
int cmd,l,r,c;
scanf("%d%d%d%d",&cmd,&l,&r,&c);
if(cmd==1){
st.update(l,r,0,st.Q-1,1,c);
}else {
printf("%d\n",st.query(l,r,0,st.Q-1,1,c));
}
}
}
return 0;
}

126
HDOJ/4392_autoAC.cpp Normal file
View File

@ -0,0 +1,126 @@
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const double eps=1e-8;
int pr[1231];
struct A{vector<int> a;double s;}e,te;
struct P{int p[100],n;double s;}p,da;
long long mx;
int pt1(A &e,int n){
double k=1;
for(int i=0;i<n;i++){
k*=pow(pr[i]+0.0,e.a[i]+0.0);
if(k>p.s*1.00000001)return 0;
}
return 1;
}
void cf(P &p,int k){
p.p[p.n]=0;
for(int i=0;i<p.n;i++)
p.p[i]*=k;
for(int i=0;i<p.n;i++)
p.p[i+1]+=p.p[i]/10,p.p[i]%=10;
while(p.p[p.n]){
p.p[p.n+1]=p.p[p.n]/10;
p.p[p.n]%=10;
p.n++;
}
}
P qiu(A &a,int n){
P p;
p.n=1;
p.p[0]=1;
for(int i=0;i<n;i++)
for(int q=0;q<a.a[i];q++)
cf(p,pr[i]);
return p;
}
int pt(P a,P b){
if(a.n!=b.n){
return a.n<b.n;
}
for(int i=a.n;i--;){
if(a.p[i]!=b.p[i])
return a.p[i]<b.p[i];
}
return 1;
}
int s[123],x[123];
int n;
long long js(A &e,int n){
long long da=1;
for(int i=0;i<n;i++)
da*=e.a[i]+1;
return da;
}
void dfs(int rt,A& e,long long k){
if(rt>n)return ;
if(k>=mx){
if(!pt1(e,rt))return ;
if(k>mx||(k==mx&& pt(qiu(e,rt),da))){
if(!pt(qiu(e,rt),p))return ;
mx=k;
da=qiu(e,rt);
}
}
int ss=s[rt];
if(rt)ss=min(ss,e.a[rt-1]);
for(int i=ss;i>=x[rt];i--){
e.a[rt]=i;
dfs(rt+1,e,k*(e.a[rt]+1));
}
}
int gs(int k){ return 2*(log(pr[n]+0.0)/log(k+0.0))-1;}
int gx(int k){ return (log(pr[n]+0.0)/log(k+0.0));}
void qiu(){
for(n=1;;n++){
e.a.clear();
for(int i=0;i<n;i++)
s[i]=gs(pr[i]),x[i]=gx(pr[i]);
if(x[0]<3)x[0]=1;
for(int i=0;i<n;i++)
e.a.push_back(1);
if(!pt1(e,n))break;
}
if(n<15)
for(int i=0;i<n;i++)
x[i]=1;
e.a.clear();
for(int i=0;i<n;i++)e.a.push_back(0);
dfs(0,e,1);
}
int main(){
int ind=0;
for(int i=2;i<1e3;i++){
if(pr[i]==0){
pr[ind++]=i;
for(int q=i*i;q<1e3;q+=i)
pr[q]=1;
}
}
string s;
while(cin>>s){
if(s=="1"){
puts("1 1");
continue;
}
mx=-1;
reverse(s.begin(),s.end());
p.n=s.size();
p.s=0;
for(int i=0;i<s.size();i++){
p.p[i]=s[i]-'0';
p.s+=p.p[i]*pow(10.0,i);
}
qiu();
p=da;
for(int i=p.n;i--;)
cout<<p.p[i];
cout<<' '<<mx;cout<<endl;
}
}

91
HDOJ/4393_autoAC.cpp Normal file
View File

@ -0,0 +1,91 @@
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<string>
#define Min(a,b) a<b?a:b
#define Max(a,b) a>b?a:b
#define CL(a,num) memset(a,num,sizeof(a));
#define maxn 60100
#define eps 1e-6
#define inf 9999999
using namespace std;
struct node
{
int f;
int s;
int id;
}p[maxn] ;
int vis[maxn];
int cmp(const node a,const node b)
{
if( a.s != b.s) return a.s > b.s;
else
if(a.f != b.f) return a.f > b.f ;
else
return a.id < b.id;
}
int main()
{
int t,i,j,n,k,cnt;
int cas = 0 ;
scanf("%d",&t);
while(t--)
{
memset(vis,0,sizeof(vis));
int mx = -1;
scanf("%d",&n);
for(i = 1;i <=n;i++ )
{
scanf("%d%d",&p[i].f,&p[i].s);
p[i].id = i;
if(mx < p[i].f)
{
mx = p[i].f ;
k = i;
}
}
printf("Case #%d:\n",++cas);
printf("%d",k);
vis[k] = 1 ;
if(n > 510)cnt = 510;
else cnt = n;
for(i = 1;i <= cnt - 1;i++)
{
mx = -1;
for(j = 1;j <= n;j++)
{
if(!vis[j])
{
p[j].f +=p[j].s;
if(mx < p[j].f)
{
k = j;
mx = p[j].f;
}
}
}
printf(" %d",k);
vis[k] = 1;
}
if(cnt < n)
{
sort(p + 1,p+1+n,cmp);
for(i = 1; i <= n;i++)
{
if(!vis[p[i].id])
{
printf(" %d",p[i].id);
vis[p[i].id] = 1;
}
}
}
printf("\n");
}
return 0;
}

71
HDOJ/4394_autoAC.cpp Normal file
View File

@ -0,0 +1,71 @@
#include<stdio.h>
__int64 T,n,tag;
__int64 ans,mod[11]={0,1},dig[11];
__int64 i,j,k,t;
struct node
{
__int64 id,val;
}que[100000001];
__int64 wei(__int64 x)
{
__int64 z=1;
if(!x) return 0;
while(x/=10) z++;
return z;
}
__int64 cmp(__int64 a, __int64 b, __int64 k)
{
while(k--)
{
if(a%10!=b%10) return 0;
a/=10;b/=10;
}
return 1;
}
__int64 bfs(__int64 x)
{
__int64 t=0,pop=0,push=1,i,j,bl,ans,sw,k=0;
que[0].id=que[0].val=0;ans=sw=0x7fffffff;
while(pop<push)
{
t=que[pop].val;
k=que[pop++].id+1;
if(cmp(t*t,x,wei(x)))
{
ans=t<ans?t:ans;
sw=wei(ans);
}
if(wei(t)>=wei(x)) return 0;
if(k>sw) continue;
for(i=0;i<10;i++)
{
if(cmp((i*mod[k]+t)*(i*mod[k]+t),x%mod[k+1],k))
{
if(cmp((i*mod[k]+t)*(i*mod[k]+t),x,wei(x)))
{
ans=(i*mod[k]+t)<ans?(i*mod[k]+t):ans;
sw=wei(ans);
continue;
}
if(k>=sw) continue;
que[push].val=i*mod[k]+t;
que[push++].id=k;
}
}
}
if(ans==0x7fffffff) return 0;
return ans;
}
int main()
{
scanf("%I64d",&T);
for(i=2;i<=10;i++) mod[i]=mod[i-1]*10;
while(T--)
{
scanf("%I64d",&tag);
ans=bfs(tag);
if(ans) printf("%I64d\n",ans);
else printf("None\n");
}
return 0;
}

65
HDOJ/4395_autoAC.cpp Normal file
View File

@ -0,0 +1,65 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 2e5 + 2e4;
const int shift = 2e5;
bool reach[N];
int w[205];
inline int read(){
double x;
scanf("%lf",&x);
if(x > 0)
return x * 10000 + 1e-8;
else
return x * 10000 - 1e-8;
}
int main()
{
int T,n;
scanf("%d",&T);
while(T--)
{
memset(reach,false,sizeof(reach));
int goal = read();
scanf("%d",&n);
for(int i=0;i<n;i++)
w[i] = read();
sort(w,w+n);
reach[ 0 + shift ] = true;
for(int i=0;i<n;i++)
{
bool f = w[i] > 0;
if(f)
{
for(int j=N-1-w[i];j>=0;j--)
if(reach[j])
reach[ j + w[i] ] = true;
}
else
{
for(int j=-w[i];j<N;j++)
if(reach[j])
reach[ j + w[i] ] = true;
}
}
int i = goal + shift , j = 0;
int ans = -1;
while(1)
{
if(i-j >= 0 && reach[i-j])
{
ans = i - j;
break;
}
if(i+j < N && reach[i+j])
{
ans = i + j;
break;
}
++j;
}
printf("%.4f\n",(ans-shift)/10000.0);
}
return 0;
}

86
HDOJ/4396_autoAC.cpp Normal file
View File

@ -0,0 +1,86 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 5005;
const int M = 200005;
const int NN = 505;
const int Lum = 10;
const int INF = 0x3f3f3f3f;
int vis[N][NN], d[N][NN], en;
int head[M];
int n, m, s, t, k;
struct node {
int to, dis, next;
}edge[M];
void addEdge(int u,int v,int x) {
edge[en].to = v;
edge[en].next = head[u];
edge[en].dis = x;
head[u] = en++;
edge[en].to = u;
edge[en].next = head[v];
edge[en].dis = x;
head[v] = en++;
}
struct Node{
int u, m;
};
void SPFA() {
queue<Node> Q;
for(int i = 1; i <= n; i++) {
for (int j = 0; j <= NN; j++) {
d[i][j] = INF;
vis[i][j] = 0;
}
}
d[s][0] = 0;
vis[s][0] = 1;
Q.push((Node){s, 0});
while(!Q.empty()) {
int u = Q.front().u;
int m = Q.front().m;
vis[u][m] = 0;
Q.pop();
int temp = m + Lum;
if (temp >= NN) continue;
if (temp > k) temp = k;
for(int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].to;
if(d[u][m] + edge[i].dis < d[v][temp]) {
d[v][temp] = d[u][m] + edge[i].dis;
if(!vis[v][temp]) {
Q.push((Node){v, temp});
vis[v][temp] = 1;
}
}
}
}
}
void input() {
int u, v, c;
for (int i = 0; i < m; i++) {
scanf("%d %d %d", &u, &v, &c);
addEdge(u, v, c);
}
scanf("%d %d %d", &s, &t, &k);
}
int main() {
while (scanf("%d %d", &n, &m) == 2) {
en = 0;
memset(head, -1, sizeof(head));
input();
if (k % 10) {
k = 10 * ((k / 10) + 1);
}
SPFA();
if (d[t][k] == INF) {
printf("-1\n");
} else printf("%d\n", d[t][k]);
}
return 0;
}

159
HDOJ/4397_autoAC.cpp Normal file
View File

@ -0,0 +1,159 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
struct node{
int a[59];
void init()
{
for(int i=0;i<59;i++) a[i]=i;
}
void chface(int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,int a8)
{
int tmp= a[a0];
a[a0]=a[a6],a[a6]=a[a8],a[a8]=a[a2],a[a2]=tmp;
tmp = a[a1];
a[a1]=a[a3],a[a3]=a[a7],a[a7]=a[a5],a[a5]=tmp;
}
void chline(int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,int a8,int a9,int a10,int a11)
{
int tmp = a[a0];
a[a0]=a[a9],a[a9]=a[a6],a[a6]=a[a3],a[a3]=tmp;
tmp = a[a1];
a[a1]=a[a10],a[a10]=a[a7],a[a7]=a[a4],a[a4]=tmp;
tmp = a[a2];
a[a2]=a[a11],a[a11]=a[a8],a[a8]=a[a5],a[a5]=tmp;
}
bool check()
{
for(int i=0;i<59;i++)
if(a[i]!=i) return false;
return true;
}
void U()
{
chface(36,37,38, 39,40,41, 42,43,44);
chline(2,1,0, 29,28,27, 20,19,18 ,11,10,9);
}
void u()
{
chface(38,37,36, 41,40,39, 44,43,42);
chline(9,10,11,18,19,20,27,28,29,0,1,2);
}
void R()
{
chface(9,10,11,12,13,14,15,16,17);
chline(44,41,38,18,21,24,53,50,47,8,5,2);
}
void r()
{
chface(11,10,9, 14,13,12, 17,16,15);
chline(2,5,8,47,50,53,24,21,18,38,41,44);
}
void F()
{
chface(0,1,2,3,4,5,6,7,8);
chline(42,43,44 ,9,12,15, 47,46,45, 35,32,29);
}
void f()
{
chface(2,1,0, 5,4,3, 8,7,6);
chline(29,32,35,45,46,47,15,12,9,44,43,42);
}
void D()
{
chface(45,46,47,48,49,50,51,52,53);
chline(6,7,8 ,15,16,17, 24,25,26, 33,34,35);
}
void d()
{
chface(47,46,45, 50,49,48, 53,52,51);
chline(35,34,33,26,25,24,17,16,15,8,7,6);
}
void L()
{
chface(27,28,29,30,31,32,33,34,35);
chline(0,3,6, 45,48,51, 26,23,20, 36,39,42);
}
void l()
{
chface(29,28,27,32,31,30,35,34,33);
chline(42,39,36,20,23,26,51,48,45,6,3,0);
}
void B()
{
chface(18,19,20,21,22,23,24,25,26);
chline(38,37,36, 27,30,33, 51,52,53, 17,14,11);
}
void b()
{
chface(20,19,18, 23,22,21,26,25,24);
chline(11,14,17,53,52,51,33,30,27,36,37,38);
}
void X()
{
chline(5,4,3, 32,31,30, 23,22,21, 14,13,12);
}
void x()
{
chline(12,13,14,21,22,23,30,31,32,3,4,5);
}
void Y()
{
chline(7,4,1, 43,40,37, 19,22,25, 52,49,46);
}
void y()
{
chline(46,49,52,25,22,19,37,40,43,1,4,7);
}
void Z()
{
chline(41,40,39,28,31,34, 48,49,50, 16,13,10);
}
void z()
{
chline(10,13,16,50,49,48,34,31,28,39,40,41);
}
};
int main()
{
char a[2009];
node t;
bool ou = false;
while(~scanf("%s",a))
{
if(ou) printf("\n");
ou = true;
t.init();
int len = strlen(a);
for(int i=0;i<len;i++)
{
if(a[i]=='U') t.U();
else if(a[i]=='R') t.R();
else if(a[i]=='F') t.F();
else if(a[i]=='D') t.D();
else if(a[i]=='L') t.L();
else if(a[i]=='B') t.B();
else if(a[i]=='X') t.X();
else if(a[i]=='Y') t.Y();
else if(a[i]=='Z') t.Z();
else if(a[i]=='u') t.u();
else if(a[i]=='r') t.r();
else if(a[i]=='f') t.f();
else if(a[i]=='d') t.d();
else if(a[i]=='l') t.l();
else if(a[i]=='b') t.b();
else if(a[i]=='x') t.x();
else if(a[i]=='y') t.y();
else if(a[i]=='z') t.z();
}
if(t.check())
printf("Yes\n");
else
printf("No\n");
}
return 0;
}

88
HDOJ/4398_autoAC.cpp Normal file
View File

@ -0,0 +1,88 @@
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
using namespace std;
const int MAXN=100010;
int Ti[MAXN];
int ne[MAXN];
map<int,int>mp;
struct Node
{
int next_id;
int ti;
};
struct classcomp
{
bool operator()(const Node &a,const Node &b)const
{
return a.next_id<b.next_id;
}
};
multiset<Node,classcomp>T_info;
multiset<Node>::iterator it_n;
set<int>Te;
set<int>::iterator it;
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==2)
{
for(int i=1;i<=n;i++)
scanf("%d",&Ti[i]);
mp.clear();
for(int i=n;i>=1;i--)
{
if(mp[Ti[i]])
ne[i]=mp[Ti[i]];
else ne[i]=n+1;
mp[Ti[i]]=i;
}
Te.clear();
T_info.clear();
for(int i=1;i<=m;i++)
{
if(!mp[i])mp[i]=n+1;
Node temp;
temp.next_id=mp[i];
temp.ti=i;
T_info.insert(temp);
Te.insert(i);
}
int ans=0;
for(int i=1;i<=n;i++)
{
it=Te.find(Ti[i]);
if(it!=Te.end())
{
Node temp;
temp.next_id=i;
temp.ti=Ti[i];
T_info.erase(temp);
temp.next_id=ne[i];
T_info.insert(temp);
}
else
{
ans++;
it_n=T_info.end();
it_n--;
if(ne[i]<(*it_n).next_id)
{
Te.erase((*it_n).ti);
T_info.erase(it_n);
Te.insert(Ti[i]);
Node temp;
temp.next_id=ne[i];
temp.ti=Ti[i];
T_info.insert(temp);
}
}
}
printf("%d\n",ans);
}
return 0;
}