Powered By HC TECH : AutoACer Engine

5400-5504(END by now)
This commit is contained in:
KiritoTRw 2016-09-13 04:55:33 +08:00 committed by GitHub
parent 6d0ead9b6a
commit b39288d6ef
87 changed files with 8119 additions and 0 deletions

61
HDOJ/5400_autoAC.cpp Normal file
View File

@ -0,0 +1,61 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAXN= 100002;
int a[MAXN], m, n, b1, b2, len[MAXN], ch[MAXN];
long long ans;
int main()
{
int i;
while(scanf("%d%d%d", &n, &b1, &b2) != -1)
{
for(i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
}
ans = 0;
len[1] = 1;
for(i = 2; i <= n; i++)
{
ch[i] = a[i] - a[i-1];
if(i == 2)
{
if(a[2]-a[1] == b1 || a[2] - a[1] == b2)
len[i] = 2;
else len[i] = 1;
}
else
{
if(ch[i] == ch[i-1])
{
if(ch[i] == b1 || ch[i] == b2)
len[i] = len[i-1] + 1;
else
len[i] = 1;
}
else if(ch[i] == b2 && ch[i-1] == b1)
{
len[i] = len[i-1] + 1;
}
else if(ch[i] == b1 || ch[i] == b2)
{
len[i] = 2;
}
else
{
len[i] = 1;
}
}
}
for(i = 1; i<= n; i++)
{
ans += len[i];
}
printf("%I64d\n", ans);
}
return 0;
}

51
HDOJ/5401_autoAC.cpp Normal file
View File

@ -0,0 +1,51 @@
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
#include<string.h>
#include<set>
#include<map>
#include<queue>
#include<math.h>
#define ll long long
#define pi acos(-1)
#define PLL pair<ll,ll>
#define PLLL pair<PLL,ll>
#define MP make_pair
using namespace std;
const ll mod = 1e9+7;
const ll maxn= 1000+10;
ll m,a[maxn],b[maxn],c[maxn],d[maxn],l[maxn],ans[maxn],s[maxn],cc[maxn],dd[maxn];
map<PLLL,ll> ma;
ll calc(ll p,ll x,ll y){
if(p==0) return 0;
PLLL r=MP(MP(p,x),y);
if(ma.find(r)!=ma.end()) return ma[r];
if(x<s[a[p]]&&y<s[a[p]]) return ma[r]=(calc(a[p],x,y)%mod);
else if(x<s[a[p]]&&y>=s[a[p]]) return ma[r]=(calc(a[p],x,c[p])+calc(b[p],y-s[a[p]],d[p])+l[p])%mod;
else if(x>=s[a[p]]&&y<s[a[p]]) return ma[r]=(calc(a[p],y,c[p])+calc(b[p],x-s[a[p]],d[p])+l[p])%mod;
else return ma[r]=(calc(b[p],x-s[a[p]],y-s[a[p]])%mod);
}
ll solve(ll p,ll id){
if(p==0) return 0;
if(id<s[a[p]])
return ((calc(a[p],id,c[p])+l[p])%mod*(s[b[p]]%mod)%mod+(dd[p]+solve(a[p],id))%mod)%mod;
else{
id=(id-s[a[p]]);
return ((calc(b[p],id,d[p])+l[p])%mod*(s[a[p]]%mod)%mod+(cc[p]+solve(b[p],id))%mod)%mod;
}
}
int main(){
while(scanf("%lld",&m)!=EOF){
s[0]=1; a[0]=b[0]=c[0]=d[0]=cc[0]=dd[0]=l[0]=0; ma.clear();
for(ll i=1;i<=m;i++) s[i]=ans[i]=0;
for(ll i=1;i<=m;i++){
scanf("%lld%lld%lld%lld%lld",&a[i],&b[i],&c[i],&d[i],&l[i]); s[i]=s[a[i]]+s[b[i]];
cc[i]=solve(a[i],c[i])%mod,dd[i]=solve(b[i],d[i])%mod;
ans[i]=((cc[i]*(s[b[i]]%mod)%mod)+(dd[i]*(s[a[i]]%mod)%mod)+l[i]%mod*(s[a[i]]%mod)%mod*(s[b[i]]%mod)%mod+(ans[a[i]]+ans[b[i]])%mod)%mod;
printf("%lld\n",ans[i]);
}
}
return 0;
}

105
HDOJ/5402_autoAC.cpp Normal file
View File

@ -0,0 +1,105 @@
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<algorithm>
#include<functional>
using namespace std;
typedef long long LL;
const LL base = 1e9 + 7;
const int maxn = 105;
LL T, n, m, a[maxn][maxn], sum, x, y;
inline void read(int &x)
{
char ch;
while ((ch = getchar())<'0' || ch>'9');
x = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') x = x * 10 + ch - '0';
}
void get()
{
x = 1; y = 2;
for (int i = 1; i <= n;i++)
for (int j = 1; j <= m; j++)
if (((i + j) & 1) && a[x][y] > a[i][j]) x = i, y = j;
}
int main()
{
while (scanf("%lld%lld", &n, &m) !=EOF)
{
sum = 0;
for (int i = 1; i <= n;i++)
for (int j = 1; j <= m; j++)
{
scanf("%lld", &a[i][j]);
sum += a[i][j];
}
if (n & 1 || m & 1)
{
printf("%lld\n", sum);
if (n & 1)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j < m; j++)
if (i & 1) printf("R"); else printf("L");
if (i < n) printf("D"); else printf("\n");
}
}
else
{
for (int i = 1; i <= m; i++)
{
for (int j = 1; j < n; j++)
if (i & 1) printf("D"); else printf("U");
if (i < m) printf("R"); else printf("\n");
}
}
}
else
{
get();
printf("%lld\n", sum - a[x][y]);
for (int i = 1; i <= n; i += 2)
{
if (x == i || x == i + 1)
{
for (int j = 1; j < y; j++)
{
if (j & 1) printf("D"); else printf("U");
printf("R");
}
if (y < m) printf("R");
for (int j = y + 1; j <= m; j++)
{
if (j & 1) printf("U"); else printf("D");
if (j < m) printf("R");
}
if (i < n - 1) printf("D");
}
else if (i < x)
{
for (int j = 1; j < m; j++) printf("R");
printf("D");
for (int j = 1; j < m; j++) printf("L");
printf("D");
}
else
{
for (int j = 1; j < m; j++) printf("L");
printf("D");
for (int j = 1; j < m; j++) printf("R");
if (i < n - 1) printf("D");
}
}
printf("\n");
}
}
return 0;
}

182
HDOJ/5405_autoAC.cpp Normal file
View File

@ -0,0 +1,182 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
#define maxn 200007
#define inf 1000000000
#define ll long long
ll mod = 1000000007;
struct Node{
Node *fa,*ch[2];
bool rev,root;
ll val,s1,s2,s3,s4;
};
Node pool[maxn];
Node *nil,*tree[maxn];
int cnt = 0;
void init(){
cnt = 1;
nil = tree[0] = pool;
nil->ch[0] = nil->ch[1] = nil;
nil->s1 = nil->s2 = nil->val = nil->s3 = nil->s4 = 0;
}
Node *newnode(ll val,Node *f){
pool[cnt].fa = f;
pool[cnt].ch[0]=pool[cnt].ch[1]=nil;
pool[cnt].rev = false;
pool[cnt].root = true;
pool[cnt].val = val;
pool[cnt].s1 = 0;
pool[cnt].s2 = 0;
pool[cnt].s3 = 0;
pool[cnt].s4 = 0;
return &pool[cnt++];
}
void update_rev(Node *x){
if(x == nil) return ;
x->rev = !x->rev;
swap(x->ch[0],x->ch[1]);
}
void update(Node *x){
if(x == nil) return ;
x->s2 = x->s1 + x->val;
x->s4 = x->s3;
if(x->ch[0] != nil){
x->s2 += x->ch[0]->s2;
if(x->s2 >= mod)
x->s2 -= mod;
x->s4 += x->ch[0]->s4;
x->s4 %= mod;
}
if(x->ch[1] != nil){
x->s2 += x->ch[1]->s2;
if(x->s2 >= mod)
x->s2 -= mod;
x->s4 += x->ch[1]->s4;
x->s4 %= mod;
}
}
void pushdown(Node *x){
if(x->rev != false){
update_rev(x->ch[0]);
update_rev(x->ch[1]);
x->rev = false;
}
}
void push(Node *x){
if(!x->root) push(x->fa);
pushdown(x);
}
void rotate(Node *x){
Node *f = x->fa, *ff = f->fa;
int t = (f->ch[1] == x);
if(f->root)
x->root = true, f->root = false;
else ff->ch[ff->ch[1] == f] = x;
x->fa = ff;
f->ch[t] = x->ch[t^1];
x->ch[t^1]->fa = f;
x->ch[t^1] = f;
f->fa = x;
update(f);
}
void splay(Node *x){
push(x);
Node *f, *ff;
while(!x->root){
f = x->fa,ff = f->fa;
if(!f->root)
if((ff->ch[1]==f)&&(f->ch[1] == x)) rotate(f);
else rotate(x);
rotate(x);
}
update(x);
}
Node *access(Node *x){
Node *y = nil,*z;
while(x != nil){
splay(x);
z = x->ch[1];
x->ch[1]->root = true;
(x->ch[1] = y)->root = false;
update(z);
x->s1 += z->s2;
x->s3 += (z->s2*z->s2);
x->s1 %= mod;
x->s3 %= mod;
x->s1 -= y->s2;
x->s3 -= y->s2*y->s2;
x->s1 %= mod;
x->s3 %= mod;
update(x);
y = x;
x = x->fa;
}
return y;
}
void be_root(Node *x){
access(x);
splay(x);
update_rev(x);
}
int value[maxn];
vector<int> head[maxn];
void dfs(int u,int f){
tree[u]->fa = tree[f];
for(int i = 0;i < head[u].size() ;i++){
int v = head[u][i];
if(v == f) continue;
dfs(v,u);
tree[u]->s1 += tree[v]->s2;
tree[u]->s1 %= mod;
tree[u]->s3 += tree[v]->s2*tree[v]->s2;
tree[u]->s3 %= mod;
}
update(tree[u]);
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
for(int i = 1;i <= n; i++)
scanf("%d",&value[i]);
for(int i = 0;i <= n; i++)
head[i].clear();
int u,v;
for(int i = 1;i < n; i++){
scanf("%d%d",&v,&u);
head[v].push_back(u);
head[u].push_back(v);
}
init();
for(int i=1;i <= n;i++)
tree[i] = newnode(value[i],nil);
dfs(1,0);
ll total = 0;
for(int i = 1;i <= n; i++)
total += value[i];
total %= mod;
int t;
for(int i = 0;i < m; i++){
scanf("%d%d%d",&t,&u,&v);
if(t == 1){
be_root(tree[u]);
access(tree[u]);
total = total + v - tree[u]->val;
total %= mod;
tree[u]->val = v;
update(tree[u]);
}
else {
be_root(tree[u]);
access(tree[v]);
splay(tree[v]);
ll ans = total*total%mod-tree[v]->s4;
ans = (ans%mod+mod)%mod;
printf("%I64d\n",ans);
}
}
}
return 0;
}

457
HDOJ/5406_autoAC.cpp Normal file
View File

@ -0,0 +1,457 @@
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <vector>
#include <cstring>
#include <map>
#include <set>
#include <unordered_map>
#include <queue>
#include <sstream>
using namespace std;
const int MAXN = 1005;
struct A{
int H, D;
bool operator<(const A rhs)const{
if(H != rhs.H)
return H > rhs.H;
else
return D < rhs.D;
}
}arr[MAXN];
int N;
int dval[MAXN], dvn;
#define MAXP 2010
#define MAXQ 1000005
#define MAXI 0x7fffffff
#define MAXC 0x0fffffff
#define BIGM MAXC
struct EDGE{
int st, ed;
int flow;
int low;
int upf;
int cost;
int hnext;
int tnext;
int hprev;
int tprev;
char set;
}edge[MAXQ+MAXP];
int ecnt;
int ecnt2;
int sece;
bool search_method;
struct NODE{
int head;
int tail;
int d;
int pi;
int pred;
int depth;
}node[MAXP];
int ncnt;
struct LOOP{
int e;
int prev;
int next;
}loop[MAXP+3];
int lcnt;
int addedge(int a, int b, int low, int upf, int cost, int &cnt)
{
edge[cnt].st = a;
edge[cnt].ed = b;
edge[cnt].low = low;
edge[cnt].upf = upf;
edge[cnt].cost = cost;
edge[cnt].hnext = -1;
edge[cnt].tnext = -1;
edge[cnt].hprev = -1;
edge[cnt].tprev = -1;
cnt++;
return (cnt - 1);
}
void insert_tree(int j)
{
int a, b;
a = edge[j].st;
b = edge[j].ed;
edge[j].hprev = -1;
edge[j].hnext = node[a].head;
if(node[a].head >= 0)
edge[node[a].head].hprev = j;
node[a].head = j;
edge[j].tprev = -1;
edge[j].tnext = node[b].tail;
if(node[b].tail >= 0)
edge[node[b].tail].tprev = j;
node[b].tail = j;
}
void delete_tree(int j)
{
int a, b;
a = edge[j].st;
b = edge[j].ed;
if(edge[j].hprev >= 0)
{
edge[edge[j].hprev].hnext = edge[j].hnext;
}else{
node[a].head = edge[j].hnext;
}
if(edge[j].hnext >= 0)
{
edge[edge[j].hnext].hprev = edge[j].hprev;
}
edge[j].hprev = -1;
edge[j].hnext = -1;
if(edge[j].tprev >= 0)
{
edge[edge[j].tprev].tnext = edge[j].tnext;
}else{
node[b].tail = edge[j].tnext;
}
if(edge[j].tnext >= 0)
{
edge[edge[j].tnext].tprev = edge[j].tprev;
}
edge[j].tprev = -1;
edge[j].tnext = -1;
}
void M_init()
{
int i, j;
node[0].d = 0;
node[0].head = -1;
node[0].tail = -1;
for(i=0;i<ecnt;i++)
{
edge[i].flow = edge[i].low;
edge[i].set = 'L';
node[edge[i].st].d -= edge[i].low;
node[edge[i].ed].d += edge[i].low;
}
ecnt2 = ecnt;
for(i=1;i<=ncnt;i++)
{
if(node[i].d > 0)
{
j = addedge(i, 0, 0, MAXC, BIGM, ecnt2);
edge[j].flow = node[i].d;
}else{
j = addedge(0, i, 0, MAXC, BIGM, ecnt2);
edge[j].flow = -node[i].d;
}
edge[j].set = 'T';
insert_tree(j);
}
}
void calc_pi(int root, int val, int dep, int pred)
{
int j;
node[root].pi = val;
node[root].depth = dep;
node[root].pred = pred;
for(j=node[root].head;j!=-1;j=edge[j].hnext)
{
if(j != pred)
{
calc_pi(edge[j].ed, val - edge[j].cost, dep + 1, j);
}
}
for(j=node[root].tail;j!=-1;j=edge[j].tnext)
{
if(j != pred)
{
calc_pi(edge[j].st, val + edge[j].cost, dep + 1, j);
}
}
}
int calc_dual1()
{
int i, ret = -1, dual0 = -1, dual;
for(i=0;i<ecnt2;i++)
{
dual = edge[i].cost - node[edge[i].st].pi + node[edge[i].ed].pi;
if((edge[i].set == 'L' && dual < 0)||(edge[i].set == 'U' && dual > 0))
{
dual = labs(dual);
if(dual > dual0)
{
ret = i;
dual0 = dual;
}
}
}
return ret;
}
int calc_dual2()
{
int i, dual;
for(i=sece;i<ecnt2;i++)
{
dual = edge[i].cost - node[edge[i].st].pi + node[edge[i].ed].pi;
if((edge[i].set == 'L' && dual < 0)||(edge[i].set == 'U' && dual > 0))
{
sece = i + 1;
return i;
}
}
for(i=0;i<sece;i++)
{
dual = edge[i].cost - node[edge[i].st].pi + node[edge[i].ed].pi;
if((edge[i].set == 'L' && dual < 0)||(edge[i].set == 'U' && dual > 0))
{
sece = i + 1;
return i;
}
}
return -1;
}
bool augment(int rda)
{
int p, q;
int pl, ql, pp, qp, maxflow;
bool flagp, flagq, flag, ret;
lcnt = 0;
if(edge[rda].set == 'L')
{
p = edge[rda].st;
q = edge[rda].ed;
}else{
q = edge[rda].st;
p = edge[rda].ed;
}
loop[lcnt].e = rda;
loop[lcnt].prev = 1;
loop[lcnt].next = 2;
pl = 1;
ql = 2;
loop[pl].e = -1;
loop[pl].next = 0;
loop[pl].prev = -1;
loop[ql].e = -1;
loop[ql].next = -1;
loop[ql].prev = 0;
lcnt = 3;
maxflow = edge[rda].upf - edge[rda].low;
while(p != q)
{
if(node[p].depth > node[q].depth)
{
flagp = true;
flagq = false;
}else if(node[p].depth < node[q].depth)
{
flagp = false;
flagq = true;
}else{
flagp = true;
flagq = true;
}
if(flagp)
{
pp = node[p].pred;
if(edge[pp].ed == p)
{
maxflow = min(maxflow, edge[pp].upf - edge[pp].flow);
p = edge[pp].st;
}else{
maxflow = min(maxflow, edge[pp].flow - edge[pp].low);
p = edge[pp].ed;
}
loop[pl].e = pp;
loop[pl].prev = lcnt;
loop[lcnt].e = -1;
loop[lcnt].next = pl;
loop[lcnt].prev = -1;
pl = lcnt;
lcnt++;
}
if(flagq)
{
qp = node[q].pred;
if(edge[qp].st == q)
{
maxflow = min(maxflow, edge[qp].upf - edge[qp].flow);
q = edge[qp].ed;
}else{
maxflow = min(maxflow, edge[qp].flow - edge[qp].low);
q = edge[qp].st;
}
loop[ql].e = qp;
loop[ql].next = lcnt;
loop[lcnt].e = -1;
loop[lcnt].next = -1;
loop[lcnt].prev = ql;
ql = lcnt;
lcnt++;
}
}
flag = false;
while(loop[pl].next >= 0)
{
pl = loop[pl].next;
ql = loop[pl].e;
if(ql == -1)
break;
if(ql == rda)
{
ret = (flag == (edge[rda].set == 'L'));
}
if(edge[ql].st == p)
{
edge[ql].flow += maxflow;
if(!flag && (edge[ql].flow == edge[ql].upf))
{
edge[ql].set = 'U';
flag = true;
delete_tree(ql);
}else{
edge[ql].set = 'T';
}
p = edge[ql].ed;
}else{
edge[ql].flow -= maxflow;
if(!flag && (edge[ql].flow == edge[ql].low))
{
edge[ql].set = 'L';
flag = true;
delete_tree(ql);
}else{
edge[ql].set = 'T';
}
p = edge[ql].st;
}
}
return ret;
}
bool NSMCalc(int &cost)
{
int rda, i;
M_init();
calc_pi(0, 0, 0, -1);
rda = (search_ ? calc_dual1() : calc_dual2());
while(rda >= 0)
{
int j;
insert_tree(rda);
if(augment(rda))
{
i = edge[rda].st;
j = edge[rda].ed;
calc_pi(i, node[j].pi + edge[rda].cost, node[j].depth + 1, rda);
}else{
i = edge[rda].ed;
j = edge[rda].st;
calc_pi(i, node[j].pi - edge[rda].cost, node[j].depth + 1, rda);
}
rda = (search_ ? calc_dual1() : calc_dual2());
}
for(i=ecnt;i<ecnt2;i++)
{
if(edge[i].flow > 0)
return false;
}
cost = 0;
for(i=0;i<ecnt;i++)
{
cost += edge[i].flow * edge[i].cost;
}
return true;
}
void init_graph(int pointnum, bool method)
{
int i;
ncnt = pointnum;
ecnt = 0;
sece = 0;
for(i=0;i<=ncnt;i++)
{
node[i].d = 0;
node[i].head = -1;
node[i].tail = -1;
node[i].pred = -1;
node[i].pi = 0;
node[i].depth = 0;
}
search_method = method;
}
inline char getc(){
static const int BUFLEN = 1 << 15;
static char B[BUFLEN], *S = B, *T = B;
if(S == T){
S = B;
T = S + fread(B, 1, BUFLEN, stdin);
}
return (S == T) ? 0 : *(S ++);
}
int ReadInt(){
char ch;
do ch = getc(); while(!isdigit(ch));
int aa = ch - '0';
for(ch = getc(); isdigit(ch); ch = getc())
aa = aa * 10 + ch - '0';
return aa;
}
int calc(){
int i, j;
int ret, mxv;
init_graph(N * 2 + 3, false);
addedge(0, 1, 0, 2, 0, ecnt);
for(i = 1; i <= N; i ++){
addedge(1, i + 1, 0, 1, 0, ecnt);
addedge(i + N + 1, N * 2 + 2, 0, 1, 0, ecnt);
addedge(i + 1, i + N + 1, 0, 1, -1, ecnt);
}
for(i = 1; i <= N; i ++){
mxv = 0x3fffffff;
for(j = i + 1; j <= N; j ++){
if(arr[j].D < arr[i].D || arr[j].D >= mxv)
continue;
addedge(i + N + 1, j + 1, 0, 1, 0, ecnt);
addedge(i + 1, j + 1, 0, 1, 0, ecnt);
mxv = arr[j].D;
}
}
node[0].d = 2;
node[N * 2 + 2].d = -2;
NSMCalc(ret);
return - ret;
}
bool deal1(){
int i;
for(i = 1; i <= N; i ++){
if(arr[i + 1].D < arr[i].D)
return false;
}
printf("%d\n", N);
return true;
}
void domain(){
int i, ans = 0;
N = ReadInt();
for(i = 1; i <= N; i ++){
arr[i].H = ReadInt();
arr[i].D = ReadInt();
}
sort(arr + 1, arr + N + 1);
dvn = 0;
for(i = 1; i <= N; i ++){
dval[dvn ++] = arr[i].D;
}
sort(dval, dval + dvn);
dvn = unique(dval, dval + dvn) - dval;
for(i = 1; i <= N; i ++){
arr[i].D = lower_bound(dval, dval + dvn, arr[i].D) - dval + 1;
}
ans = calc();
printf("%d\n", ans);
}
int main(){
int T = ReadInt();
while(T --){
domain();
}
return 0;
}

47
HDOJ/5407_autoAC.cpp Normal file
View File

@ -0,0 +1,47 @@
#include <iostream>
using namespace std;
#define prt(k) cerr<<#k" = "<<k<<endl
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
const int N = 1000100;
ll p[N];
ll a[N];
bool ok(ll n)
{
int t = p[n];
while (n % t==0 && n > 1) n /= t;
return n==1;
}
ll pmod(ll a, ll n)
{
ll ret = 1;
for (; n; n>>=1, a=a*a%mod) if (n & 1)
ret = ret * a % mod;
return ret;
}
ll inv(ll a)
{
return pmod(a, mod - 2);
}
int main()
{
for (int i=1;i<N;i++) p[i]=i;
for (int i=2;i<N;i++) if (p[i]==i) {
for (int j=i+i;j<N;j+=i) p[j] = i;
}
a[0] = 1;
for (int i=1;i<N;i++) {
if (ok(i))
a[i] = a[i-1] * p[i] % mod;
else
a[i] = a[i-1];
}
int n; int re; cin>>re;
while (re--) {
scanf("%d", &n);
n++;
ll ans = a[n] * inv(n) % mod;
printf("%I64d\n", ans);
}
}

77
HDOJ/5409_autoAC.cpp Normal file
View File

@ -0,0 +1,77 @@
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN = 100005;
struct NODE;
struct EDGE{
NODE *a, *b;
EDGE *next;
int u;
bool bridge;
}edge[MAXN << 1];
struct NODE{
EDGE *head;
NODE *par;
int DFN, low;
int mv;
}node[MAXN];
int ecnt, dep, n, m;
void Tarjan(NODE *x){
x->DFN = x->low = ++ dep;
for(EDGE *j = x->head; j; j = j->next){
NODE *p = j->b;
if(p == x->par)
continue;
if(!p->DFN){
p->par = x;
Tarjan(p);
}
j->bridge = p->low > x->DFN;
x->low = min(x->low, p->low);
x->mv = max(x->mv, p->mv);
j->u = j->b->mv;
}
}
void addedge(NODE *a, NODE *b, int& ecnt){
EDGE *j = &edge[ecnt ++];
j->a = a;
j->b = b;
j->next = a->head;
a->head = j;
j->bridge = false;
}
void domain(){
int i, u, v;
scanf("%d%d", &n, &m);
ecnt = 0;
for(i = 0; i <= n; i ++){
node[i].mv = i;
node[i].par = nullptr;
node[i].DFN = 0;
node[i].head = nullptr;
}
for(i = 0; i < m; i ++){
scanf("%d%d", &u, &v);
addedge(node + u, node + v, ecnt);
addedge(node + v, node + u, ecnt);
}
dep = 0;
Tarjan(node + n);
for(i = 0; i < ecnt; ){
u = 0;
if(edge[i].bridge)
u = edge[i].u;
i ++;
if(edge[i].bridge)
u = edge[i].u;
i ++;
printf("%d %d\n", u, u > 0 ? u + 1 : 0);
}
}
int main(){
int T;
scanf("%d", &T);
while(T --)
domain();
return 0;
}

30
HDOJ/5410_autoAC.cpp Normal file
View File

@ -0,0 +1,30 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1005;
int w[N], a[N], b[N];
int dp[N][2005][2];
int main(){
int t; scanf("%d", &t);
while (t--){
int m, n; scanf("%d %d", &m, &n);
for (int i = 1; i <= n; i++){
scanf("%d %d %d", w + i, a + i, b + i);
}
memset(dp, 0, sizeof dp);
for (int i = 1; i <= n; i++){
for (int j = 0; j < w[i]; j++){
dp[i][j][0] = max(dp[i - 1][j][0], dp[i - 1][j][1]);
dp[i][j][1] = 0;
}
for (int j = w[i]; j <= m; j++){
dp[i][j][0] = max(dp[i - 1][j][0], dp[i - 1][j][1]);
dp[i][j][1] = max(dp[i][j - w[i]][0] + a[i] + b[i], dp[i][j - w[i]][1] + a[i]);
}
}
int ans = max(dp[n][m][0], dp[n][m][1]);
printf("%d\n", ans);
}
return 0;
}

62
HDOJ/5411_autoAC.cpp Normal file
View File

@ -0,0 +1,62 @@
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<ctime>
#include<vector>
using namespace std;
const int base = 2015;
const int maxn = 100005;
int T, n, m, k, kk;
struct Matrix
{
#define size 55
int m[size][size];
Matrix() { memset(m, 0, sizeof(m)); }
void operator =(const Matrix&b) { memcpy(m, b.m, sizeof(m)); }
Matrix operator *(const Matrix&b) {
Matrix c;
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
for (int k = 0; k < size; k++)
c.m[i][k] = (c.m[i][k] + m[i][j] * b.m[j][k]) % base;
return c;
}
Matrix get(int x)
{
Matrix a, b = *this;
for (int f = 0; x; x >>= 1)
{
if (x & 1)
if (f) a = a * b; else a = b, f = 1;
b = b * b;
}
return a;
}
};
int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%d%d", &n, &m);
Matrix a, b;
for (int i = 1; i <= n + 1; i++) a.m[i][n + 1] = 1;
for (int i = 1; i <= n; i++)
{
scanf("%d", &k);
for (int j = 1; j <= k; j++)
{
scanf("%d", &kk);
a.m[i][kk] = 1;
}
}
for (int i = 1; i <= n + 1; i++) b.m[1][i] = 1;
a = a.get(m);
b = b * a;
if (m == 1) printf("%d\n", n + 1);
else printf("%d\n", b.m[1][n + 1]);
}
return 0;
}

192
HDOJ/5412_autoAC.cpp Normal file
View File

@ -0,0 +1,192 @@
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <climits>
#include <ctype.h>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <deque>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
const double eps = 1e-9;
const double PI = acos(-1.00);
const double e = exp(1.0);
#define INF 0x3f3f3f3f
#define N 600010
#define M 100010
struct treap
{
int key,wht,count,sz,ch[2];
} tp[N*15];
int tree[N<<1];
int nodecount,root;
int IDX(int l,int r)
{
return l+r | l!=r;
}
void init()
{
tp[0].sz=0;
tp[0].wht=-INF;
nodecount=0;
root=0;
}
void update(int x)
{
tp[x].sz=tp[tp[x].ch[0]].sz+tp[x].count+tp[tp[x].ch[1]].sz;
}
void rotate(int &x,int t)
{
int y=tp[x].ch[t];
tp[x].ch[t]=tp[y].ch[!t];
tp[y].ch[!t]=x;
update(x);
update(y);
x=y;
}
void insert(int &x,int t)
{
if(! x)
{
x=++nodecount;
tp[x].key=t;
tp[x].wht=rand();
tp[x].count=1;
tp[x].ch[0]=tp[x].ch[1]=0;
}
else if(tp[x].key==t) tp[x].count++;
else
{
int k=tp[x].key<t;
insert(tp[x].ch[k],t);
if(tp[x].wht<tp[tp[x].ch[k]].wht) rotate(x,k);
}
update(x);
}
void erase(int &x,int t)
{
if(tp[x].key==t)
{
if(tp[x].count==1)
{
if(! tp[x].ch[0] && ! tp[x].ch[1])
{
x=0;
return;
}
rotate(x,tp[tp[x].ch[0]].wht<tp[tp[x].ch[1]].wht);
erase(x,t);
}
else tp[x].count--;
}
else erase(tp[x].ch[tp[x].key<t],t);
update(x);
}
int select(int x,int t)
{
if(! x) return 0;
if(tp[x].key>t) return select(tp[x].ch[0],t);
return tp[x].count+tp[tp[x].ch[0]].sz+select(tp[x].ch[1],t);
}
int a[N],b[N],ord[M][5],lb;
int n,m,tt;
int search(int x)
{
int l=1,r=b[0],mid;
while (l<=r)
{
mid=(l+r)>>1;
if(b[mid]==x) return mid;
if(b[mid]<x) l=mid+1;
else r=mid-1;
}
}
void treeinsert(int l,int r,int i,int x)
{
insert(tree[IDX(l,r)],x);
if(l==r) return;
int m=(l+r)>>1;
if(i<=m) treeinsert(l,m,i,x);
else treeinsert(m+1,r,i,x);
}
void treedel(int l,int r,int i,int x)
{
erase(tree[IDX(l,r)],x);
if(l==r) return;
int m=(l+r)>>1;
if(i<=m) treedel(l,m,i,x);
else treedel(m+1,r,i,x);
}
int query(int l,int r,int x,int y,int k)
{
if(l==r) return l;
int m=(l+r)>>1;
int ans=select(tree[IDX(l,m)],y)-select(tree[IDX(l,m)],x);
if(ans>=k) return query(l,m,x,y,k);
return query(m+1,r,x,y,k-ans);
}
int main ()
{
while (~scanf("%d",&n))
{
b[0]=1;
lb=0;
memset(tree,0,sizeof(tree));
init();
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
b[++lb]=a[i];
}
scanf("%d",&m);
for(int i=1; i<=m; i++)
{
int op;
int x,y,c;
scanf("%d",&op);
if(op == 2)
{
scanf("%d %d %d",&x,&y,&c);
ord[i][1]=1;
ord[i][2]=x;
ord[i][3]=y;
ord[i][4]=c;
}
else
{
scanf("%d %d",&x,&y);
ord[i][1]=2;
ord[i][2]=x;
ord[i][3]=y;
b[++lb]=y;
}
}
sort(b+1,b+1+lb);
for(int i=1; i<=lb; i++)
if(b[i]!=b[b[0]]) b[++b[0]]=b[i];
for(int i=1; i<=n; i++)
{
a[i]=search(a[i]);
treeinsert(1,b[0],a[i],i);
}
for(int i=1; i<=m; i++)
{
if(ord[i][1]==1)
printf("%d\n",b[query(1,b[0],ord[i][2]-1,ord[i][3],ord[i][4])]);
else
{
treedel(1,b[0],a[ord[i][2]],ord[i][2]);
a[ord[i][2]]=search(ord[i][3]);
treeinsert(1,b[0],a[ord[i][2]],ord[i][2]);
}
}
}
return 0;
}

97
HDOJ/5413_autoAC.cpp Normal file
View File

@ -0,0 +1,97 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
#include <limits>
using namespace std;
typedef long long LL;
const int MAXN=20000 + 10;
const int INF = numeric_limits<int>::max();
const LL LL_INF= numeric_limits<LL>::max();
int bit[MAXN][MAXN/32+100];
struct Edge {
int to,next;
Edge(){}
Edge(int _to,int _next):to(_to),next(_next){}
}e[MAXN*10],e2[MAXN*10];
int head[MAXN],tot,du[MAXN];
int head2[MAXN],tot2;
int n,m;
void init(){
for(int i=1;i<=n;i++)head2[i]=head[i]=-1,du[i]=0;
tot2=0;
tot=0;
}
void AddEdge(int u,int v){
e[tot]=Edge(v,head[u]);
head[u]=tot++;
}
void AddEdge1(int u,int v){
e2[tot2]=Edge(v,head2[u]);
head2[u]=tot2++;
}
int que[MAXN],front,rear;
void _or(int posi,int posj){
for(int i=0;i<=n/32;i++)bit[posi][i]|=bit[posj][i];
}
void top_bfs(){
front=rear=0;
memset(bit,0,sizeof(bit));
for(int i=1;i<=n;i++){
bit[i][i/32]=(1<<(i%32));
if(du[i]==0){
que[front++]=i;
--du[i];
}
}
while(front!=rear){
int u=que[rear++];
for(int i=head[u];~i;i=e[i].next){
int v=e[i].to;
AddEdge1(v,u);
if(--du[v]==0)que[front++]=v;
}
}
}
template <class T>
void Rd(T &tmp){
char ch;
while((ch=getchar())&&!isdigit(ch));
tmp=0;
do {
tmp=(tmp<<3)+(tmp<<1)+(ch^48);
}while((ch=getchar())&&isdigit(ch));
return ;
}
int main()
{
int T;
Rd(T);
while(T--){
Rd(n),Rd(m);
init();
for(int i=0,x,y;i<m;i++){
Rd(x),Rd(y);
AddEdge(x,y);
++du[y];
}
top_bfs();
int ans=0;
for(int i=0;i<front;i++){
int u=que[i];
for(int j=head2[u];~j;j=e2[j].next){
int v=e2[j].to;
if(bit[u][v/32]&(1<<(v%32)))++ans;
_or(u,v);
}
}
printf("%d\n",ans);
}
return 0;
}

52
HDOJ/5414_autoAC.cpp Normal file
View File

@ -0,0 +1,52 @@
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn=100000+100;
char s[maxn];
char ts[maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s%s",s,ts);
int n1=strlen(s);
int n2=strlen(ts);
int cur=0;
for(int i=0;i<n2;i++)
{
if(ts[i]==s[cur])
cur++;
if(cur==n1)
break;
}
if(cur==n1)
{
int a1=1,a2=1;
for(int j=1;j<n1;j++)
{
if(s[j]==s[0])
a1++;
else
break;
}
for(int j=1;j<n2;j++)
{
if(ts[j]==ts[0])
a2++;
else
break;
}
if(a2>a1||s[0]!=ts[0])
printf("No\n");
else
printf("Yes\n");
}
else
printf("No\n");
}
return 0;
}

70
HDOJ/5416_autoAC.cpp Normal file
View File

@ -0,0 +1,70 @@
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 1e6+10;
typedef long long LL;
int dp[maxn];
int head[maxn],ip;
LL cnt[maxn];
int n;
struct nod{
int to,next,w;
}edge[maxn];
bool vis[maxn];
void init(){
ip=0;
memset(head,-1,sizeof(head));
memset(cnt,0,sizeof(cnt));
memset(vis,0,sizeof(vis));
}
void add(int u,int v,int w){
edge[ip].to=v;
edge[ip].w=w;
edge[ip].next=head[u];
head[u]=ip++;
}
int mmax;
void dfs(int u,int pre,int val){
for(int i=head[u];i!=-1;i=edge[i].next){
int v= edge[i].to;
if(v==pre) continue;
if(edge[i].w^val>mmax)
mmax = edge[i].w^val;
cnt[edge[i].w^val]++;
dfs(v,u,val^edge[i].w);
}
}
int main()
{
int t,m;
scanf("%d",&t);
while(t--){
init();
scanf("%d",&n);
for(int i=0;i<n-1;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
mmax = 0;
dfs(1,1,0);
scanf("%d",&m);
cnt[0]++;
for(int i=0;i<m;i++){
int s;
scanf("%d",&s);
LL ans = 0;
for(int j=0;j<maxn;j++){
if((s^j)==j) ans=ans+(cnt[j]*(cnt[j]-1));
else ans = ans=ans+cnt[j]*cnt[s^j];
}
ans=ans/2;
if(s==0) ans=ans+n;
printf("%I64d\n",ans);
}
}
return 0;
}

21
HDOJ/5417_autoAC.cpp Normal file
View File

@ -0,0 +1,21 @@
#include <iostream>
using namespace std;
int main()
{
int x, y, w, n;
while(cin>>x>>y>>w>>n)
{
n -= 1;
int ans, tmp1, tmp2;
if(w > x)
ans = (x+y)*n;
else
{
tmp1 = n % ((int)(x/w)+1);
tmp2 = n / (x/w+1);
ans = tmp2*(x+y)+w*tmp1;
}
cout<<ans<<endl;
}
return 0;
}

63
HDOJ/5418_autoAC.cpp Normal file
View File

@ -0,0 +1,63 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
#include <limits>
using namespace std;
typedef long long LL;
const int MAXN=20;
const int INF = numeric_limits<int>::max();
int dp[1<<MAXN][MAXN];
int dis[MAXN][MAXN];
int main()
{
int T,n,m;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
for(int i=0;i<=n;i++)
for(int j=0;j<=n;j++)dis[i][j]=INF;
for(int i=0,x,y,w;i<m;i++){
scanf("%d%d%d",&x,&y,&w);
--x;--y;
dis[x][y]=dis[y][x]=min(dis[x][y],w);
}
if(n==1){
puts("0");
continue;
}
for(int k=0;k<n;k++)
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
if(dis[i][k]!=INF&&dis[k][j]!=INF)dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
--n;
int tot=(1<<n);
for(int s=0;s<tot;s++){
for(int i=0;i<n;i++){
if(s&(1<<i)){
if(s==(1<<i))dp[s][i]=dis[0][i+1];
else {
dp[s][i]=INF;
for(int j=0;j<n;j++){
if(i!=j&&(s&(1<<j))){
dp[s][i]=min(dp[s][i],dp[s^(1<<i)][j]+dis[i+1][j+1]);
}
}
}
}
}
}
int ans=INF;
for(int i=0;i<n;i++)ans=min(ans,dp[tot-1][i]+dis[0][i+1]);
printf("%d\n",ans);
}
return 0;
}

46
HDOJ/5419_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
#define LL __int64
LL dp[50100] ;
int a[50100] ;
LL gcd(LL a,LL b) {
return b == 0 ? a : gcd(b,a%b) ;
}
int main() {
int t , n , m ;
int i , l , r ;
LL x , y , temp ;
scanf("%d", &t) ;
while( t-- ) {
scanf("%d %d", &n, &m) ;
memset(dp,0,sizeof(dp)) ;
for(i = 1 ; i <= n ; i++)
scanf("%d", &a[i]) ;
while( m-- ) {
scanf("%d %d", &l, &r) ;
dp[l]++ ;
dp[r+1]-- ;
}
x = 0 ;
y = (LL)n*(n-1)*(n-2)/6 ;
for(i = 1 ; i <= n ; i++) {
dp[i] += dp[i-1] ;
if( dp[i] > 2 )
x += dp[i]*(dp[i]-1)*(dp[i]-2)/6*a[i] ;
}
if( x == 0 || y == 0 ) {
printf("0\n") ;
continue ;
}
temp = gcd(x,y) ;
x /= temp ;
y /= temp ;
if( y == 1 )
printf("%I64d\n", x ) ;
else
printf("%I64d/%I64d\n", x, y) ;
}
return 0 ;
}

124
HDOJ/5420_autoAC.cpp Normal file
View File

@ -0,0 +1,124 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
#include <limits>
using namespace std;
typedef long long LL;
const int MAXN=100000+1000;
const int MAXM=MAXN*20;
const int INF = numeric_limits<int>::max();
const LL LL_INF= numeric_limits<LL>::max();
struct Edge {
int to,next;
Edge(){}
Edge(int _to,int _next):to(_to),next(_next){}
}e[MAXM<<3];
int head[MAXM],tot;
void init(){
memset(head,-1,sizeof(head));
tot=0;
}
void AddEdge(int u,int v){
e[tot]=Edge(v,head[u]);
head[u]=tot++;
}
typedef pair<int,int> ii;
vector <ii> t[MAXN<<2];
int d[MAXN],L[MAXN],R[MAXN],dep[MAXN],ncnt,rid[MAXN],n,Begin[MAXN<<2];
void dfs(int u,int d){
L[u]=++ncnt;
rid[ncnt]=u;
dep[u]=d;
for(int i=head[u];~i;i=e[i].next)
dfs(e[i].to,d+1);
R[u]=ncnt;
}
#define lson L,mid,rt<<1
#define rson mid+1,R,rt<<1|1
void build(int L,int R,int rt){
t[rt].resize(R-L+1);
if(L==R){
Begin[rt]=n+1;
t[rt][0]=make_pair(dep[rid[L]],rid[L]);
++n;
AddEdge(n,rid[L]);
return ;
}
int mid=(L+R)>>1;
build(lson);build(rson);
merge(t[rt<<1].begin(),t[rt<<1].end(),t[rt<<1|1].begin(),t[rt<<1|1].end(),t[rt].begin());
Begin[rt]=n+1;
for(int i=1;i<R-L+1;i++)AddEdge(n+1+i,n+i);
for(int i=0;i<R-L+1;i++)AddEdge(n+1+i,t[rt][i].second);
n+=R-L+1;
}
void connect(int L,int R,int rt,int l,int r,int x,int d){
if(l<=L&&R<=r){
int pos=lower_bound(t[rt].begin(),t[rt].end(),make_pair(d,1<<30))-t[rt].begin()-1;
if(~pos)AddEdge(x,Begin[rt]+pos);
return ;
}
int mid=(L+R)>>1;
if(l<=mid)connect(lson,l,r,x,d);
if(r>mid)connect(rson,l,r,x,d);
}
int dfn[MAXM],low[MAXM],sta[MAXM],top,times,bleg[MAXM],cnt[MAXM];
void tarjan(int u){
low[u]=dfn[u]=++times;
sta[++top]=u;
for(int i=head[u];~i;i=e[i].next){
int v=e[i].to;
if(!dfn[v]){
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(!bleg[v])
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u]){
bleg[0]++;
cnt[bleg[0]]=0;
do{
bleg[sta[top]]=bleg[0];
cnt[bleg[0]]+=(sta[top]<=ncnt);
}while(sta[top--]!=u);
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
init();
for(int i=2,x;i<=n;i++){
scanf("%d",&x);
AddEdge(x,i);
}
ncnt=0;
dfs(1,0);
init();
build(1,ncnt,1);
for(int i=1,x,y;i<=ncnt;i++){
scanf("%d%d",&x,&y);
connect(1,ncnt,1,L[x],R[x],i,dep[x]+y);
}
memset(dfn,0,sizeof(dfn));
memset(bleg,0,sizeof(bleg));
top=times=0;
for(int i=1;i<=n;i++)if(!dfn[i])tarjan(i);
LL ans=0;
for(int i=1;i<=bleg[0];i++){
ans+=(LL)cnt[i]*(cnt[i]-1)/2;
}
printf("%I64d\n",ans);
}
return 0;
}

73
HDOJ/5421_autoAC.cpp Normal file
View File

@ -0,0 +1,73 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std ;
typedef long long LL ;
#define clr( a , x ) memset ( a , x , sizeof a )
const int MAXN = 200005 ;
struct Palindromic_Tree {
int nxt[MAXN][26] ;
int fail[MAXN] ;
int len[MAXN] ;
int S[MAXN] , L , R ;
int num[MAXN] ;
int last[2] ;
int p ;
LL tot ;
int newnode ( int l ) {
for ( int i = 0 ; i < 26 ; ++ i ) nxt[p][i] = 0 ;
num[p] = 0 ;
len[p] = l ;
return p ++ ;
}
void init ( int n ) {
p = 0 ;
newnode ( 0 ) ;
newnode ( -1 ) ;
clr ( S , -1 ) ;
L = n ;
R = n - 1 ;
last[0] = last[1] = 1 ;
fail[0] = 1 ;
tot = 0 ;
}
int get_fail ( int v , bool d ) {
if ( d ) while ( S[R - len[v] - 1] != S[R] ) v = fail[v] ;
else while ( S[L + len[v] + 1] != S[L] ) v = fail[v] ;
return v ;
}
void add ( int c , bool d ) {
if ( d ) S[++ R] = c ;
else S[-- L] = c ;
int cur = get_fail ( last[d] , d ) ;
if ( !nxt[cur][c] ) {
int now = newnode ( len[cur] + 2 ) ;
fail[now] = nxt[get_fail ( fail[cur] , d )][c] ;
nxt[cur][c] = now ;
num[now] = num[fail[now]] + 1 ;
}
last[d] = nxt[cur][c] ;
if ( len[last[d]] == R - L + 1 ) last[d ^ 1] = last[d] ;
tot += num[last[d]] ;
}
} ;
Palindromic_ T ;
int n ;
void solve () {
int op ;
char c ;
T.init ( n ) ;
for ( int i = 0 ; i < n ; ++ i ) {
scanf ( "%d" , &op ) ;
if ( op <= 2 ) {
scanf ( " %c" , &c ) ;
T.add ( c - 'a' , op - 1 ) ;
}
if ( op == 3 ) printf ( "%d\n" , T.p - 2 ) ;
if ( op == 4 ) printf ( "%I64d\n" , T.tot ) ;
}
}
int main () {
while ( ~scanf ( "%d" , &n ) ) solve () ;
return 0 ;
}

19
HDOJ/5422_autoAC.cpp Normal file
View File

@ -0,0 +1,19 @@
#include <iostream>
using namespace std;
int main(){
int n,m,a,b,sign;
while(cin>>n>>m){
sign=0;
while(m--){
cin>>a>>b;
if((a==1&&b==n)||(b==1&&a==n))
sign=1;
}
n=n*(n-1)/2;
if(sign)
cout<<"1 "<<n<<endl;
else
cout<<"1 1"<<endl;
}
return 0;
}

44
HDOJ/5423_autoAC.cpp Normal file
View File

@ -0,0 +1,44 @@
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int s[1005][1005],sum[1005],dis[1005];
int n;
void dfs(int x,int num){
int i;
dis[x]=num;
for(i=1;i<=n;i++)
if(s[x][i]&&!dis[i])
dfs(i,num+1);
}
int main(){
int i,a,b,ans;
while(scanf("%d",&n)!=EOF){
memset(s,0,sizeof(s));
memset(sum,0,sizeof(sum));
memset(dis,0,sizeof(dis));
for(i=1;i<=n-1;i++){
scanf("%d%d",&a,&b);
s[a][b]=s[b][a]=1;
}
dfs(1,1);
for(i=1;i<=n;i++)
sum[dis[i]]++;
ans=0;
for(i=1;i<=n;i++){
if(sum[i]==1)
ans++;
else{
if(sum[i+1]==0)
ans=n;
break;
}
}
if(ans==n)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

47
HDOJ/5424_autoAC.cpp Normal file
View File

@ -0,0 +1,47 @@
#include <iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n,cnt,x,y,deg[1001];
bool flag,vis[1001],Map[1001][1001];
void dfs(int u)
{
if(cnt==n) {flag=1;return;}
for(int i=1;i<=n&&!flag;++i)
if(!vis[i]&&Map[u][i])
{
vis[i]=1;
++cnt;
dfs(i);
--cnt;
vis[i]=0;
}
}
int main()
{
while(~scanf("%d",&n))
{
flag=0;
cnt=1;
memset(deg,0,sizeof(deg));
memset(vis,0,sizeof(vis));
memset(Map,0,sizeof(Map));
for(int i=0;i<n;++i){
scanf("%d%d",&x,&y);
if(x!=y&&!Map[x][y])
{
Map[x][y]=Map[y][x]=1;
++deg[x],++deg[y];
}
}
int s=1,tot=0;
for(int i=1;i<=n;++i)
if(deg[i]==1) {s=i;++tot;}
if(tot>2) {puts("NO");continue;}
vis[s]=1;
dfs(s);
if(flag) puts("YES");
else puts("NO");
}
return 0;
}

53
HDOJ/5425_autoAC.cpp Normal file
View File

@ -0,0 +1,53 @@
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
#define MAXN 100005
vector<int>vec[MAXN];
queue<int>q;
int n,d[MAXN];
inline double f(double a,double b)
{
return a * b / (a + b);
}
double solve()
{
double ans = 0;
for(int i = n - 1;i >= max(n - 100,0);i--)
for(int j = n - 1;j > i;j--)
ans += f(d[i],d[j]) / (pow(2.0,n - i) - (n + 1) / pow(2.0,i));
return ans;
}
int main()
{
while(scanf("%d",&n) != EOF)
{
for(int i = 0;i <= n;i++)vec[i].clear();
for(int i = 1;i < n;i++)
{
int u;
scanf("%d",&u);
vec[u - 1].push_back(i);
}
d[0] = 1;
q.push(0);
while(!q.empty())
{
int u = q.front();
q.pop();
for(int i = 0;i < vec[u].size();i++)
{
int v = vec[u][i];
d[v] = d[u] + 1;
q.push(v);
}
}
sort(d,d + n);
double ans = solve();
printf("%.6f\n",ans);
}
}

52
HDOJ/5427_autoAC.cpp Normal file
View File

@ -0,0 +1,52 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
struct node
{
char s[110];
int a;
}p[1000],t;
int T,n;
int main()
{
while(~scanf("%d",&T))
{
while(T--)
{
scanf("%d",&n);
getchar();
for(int i=0;i<n;i++)
{
gets(p[i].s);
}
for(int i=0;i<n;i++)
{
int sum=0;
int len=strlen(p[i].s);
for(int j=len-4;j<len;j++)
{
sum*=10;
sum+=p[i].s[j]-'0';
}
p[i].s[len-5]='\0';
p[i].a=sum;
}
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(p[j].a<p[j+1].a)
{
t=p[j];
p[j]=p[j+1];
p[j+1]=t;
}
}
}
for(int i=0;i<n;i++)
printf("%s\n",p[i].s);
}
}
}

37
HDOJ/5428_autoAC.cpp Normal file
View File

@ -0,0 +1,37 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=40000;
int T;
int n;
long long a[100],x,cnt;
long long vis[maxn];
int main()
{
scanf("%d",&T);
while(T--){
memset(vis,0,sizeof(vis));
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%I64d",&a[i]);
cnt=0;
for(int i=0;i<n;i++){
x=a[i];
for(long long j=2;j*j<=x;j++){
while(x%j==0){
x/=j;
vis[cnt++]=j;
}
}
if(x>1)
vis[cnt++]=x;
}
sort(vis,vis+cnt);
if(cnt<2)
printf("-1\n");
else
printf("%I64d\n",vis[0]*vis[1]);
}
return 0;
}

309
HDOJ/5429_autoAC.cpp Normal file
View File

@ -0,0 +1,309 @@
#include <iostream>
#include <string.h>
#include<stdio.h>
#include<iostream>
using namespace std;
#define DIGIT 4
#define DEPTH 10000
#define MAX 100
typedef int bignum_t[MAX+1];
int read(bignum_ a,istream& is=cin){
char buf[MAX*DIGIT+1],ch;
int i,j;
memset((void*)a,0,sizeof(bignum_t));
if (!(is>>buf)) return 0;
for (a[0]=strlen(buf),i=a[0]/2-1;i>=0;i--)
ch=buf[i],buf[i]=buf[a[0]-1-i],buf[a[0]-1-i]=ch;
for (a[0]=(a[0]+DIGIT-1)/DIGIT,j=strlen(buf);j<a[0]*DIGIT;buf[j++]='0');
for (i=1;i<=a[0];i++)
for (a[i]=0,j=0;j<DIGIT;j++)
a[i]=a[i]*10+buf[i*DIGIT-1-j]-'0';
for (;!a[a[0]]&&a[0]>1;a[0]--);
return 1;
}
void write(const bignum_ a,ostream& os=cout){
int i,j;
for (os<<a[i=a[0]],i--;i;i--)
for (j=DEPTH/10;j;j/=10)
os<<a[i]/j%10;
}
int comp(const bignum_ a,const bignum_ b){
int i;
if (a[0]!=b[0])
return a[0]-b[0];
for (i=a[0];i;i--)
if (a[i]!=b[i])
return a[i]-b[i];
return 0;
}
int comp(const bignum_ a,const int b){
int c[12]={1};
for (c[1]=b;c[c[0]]>=DEPTH;c[c[0]+1]=c[c[0]]/DEPTH,c[c[0]]%=DEPTH,c[0]++);
return comp(a,c);
}
int comp(const bignum_ a,const int c,const int d,const bignum_ b){
int i,t=0,O=-DEPTH*2;
if (b[0]-a[0]<d&&c)
return 1;
for (i=b[0];i>d;i--){
t=t*DEPTH+a[i-d]*c-b[i];
if (t>0) return 1;
if (t<O) return 0;
}
for (i=d;i;i--){
t=t*DEPTH-b[i];
if (t>0) return 1;
if (t<O) return 0;
}
return t>0;
}
void add(bignum_ a,const bignum_ b){
int i;
for (i=1;i<=b[0];i++)
if ((a[i]+=b[i])>=DEPTH)
a[i]-=DEPTH,a[i+1]++;
if (b[0]>=a[0])
a[0]=b[0];
else
for (;a[i]>=DEPTH&&i<a[0];a[i]-=DEPTH,i++,a[i]++);
a[0]+=(a[a[0]+1]>0);
}
void add(bignum_ a,const int b){
int i=1;
for (a[1]+=b;a[i]>=DEPTH&&i<a[0];a[i+1]+=a[i]/DEPTH,a[i]%=DEPTH,i++);
for (;a[a[0]]>=DEPTH;a[a[0]+1]=a[a[0]]/DEPTH,a[a[0]]%=DEPTH,a[0]++);
}
void sub(bignum_ a,const bignum_ b){
int i;
for (i=1;i<=b[0];i++)
if ((a[i]-=b[i])<0)
a[i+1]--,a[i]+=DEPTH;
for (;a[i]<0;a[i]+=DEPTH,i++,a[i]--);
for (;!a[a[0]]&&a[0]>1;a[0]--);
}
void sub(bignum_ a,const int b){
int i=1;
for (a[1]-=b;a[i]<0;a[i+1]+=(a[i]-DEPTH+1)/DEPTH,a[i]-=(a[i]-DEPTH+1)/DEPTH*DEPTH,i++);
for (;!a[a[0]]&&a[0]>1;a[0]--);
}
void sub(bignum_ a,const bignum_ b,const int c,const int d){
int i,O=b[0]+d;
for (i=1+d;i<=O;i++)
if ((a[i]-=b[i-d]*c)<0)
a[i+1]+=(a[i]-DEPTH+1)/DEPTH,a[i]-=(a[i]-DEPTH+1)/DEPTH*DEPTH;
for (;a[i]<0;a[i+1]+=(a[i]-DEPTH+1)/DEPTH,a[i]-=(a[i]-DEPTH+1)/DEPTH*DEPTH,i++);
for (;!a[a[0]]&&a[0]>1;a[0]--);
}
void mul(bignum_ c,const bignum_ a,const bignum_ b){
int i,j;
memset((void*)c,0,sizeof(bignum_t));
for (c[0]=a[0]+b[0]-1,i=1;i<=a[0];i++)
for (j=1;j<=b[0];j++)
if ((c[i+j-1]+=a[i]*b[j])>=DEPTH)
c[i+j]+=c[i+j-1]/DEPTH,c[i+j-1]%=DEPTH;
for (c[0]+=(c[c[0]+1]>0);!c[c[0]]&&c[0]>1;c[0]--);
}
void mul(bignum_ a,const int b){
int i;
for (a[1]*=b,i=2;i<=a[0];i++){
a[i]*=b;
if (a[i-1]>=DEPTH)
a[i]+=a[i-1]/DEPTH,a[i-1]%=DEPTH;
}
for (;a[a[0]]>=DEPTH;a[a[0]+1]=a[a[0]]/DEPTH,a[a[0]]%=DEPTH,a[0]++);
for (;!a[a[0]]&&a[0]>1;a[0]--);
}
void mul(bignum_ b,const bignum_ a,const int c,const int d){
int i;
memset((void*)b,0,sizeof(bignum_t));
for (b[0]=a[0]+d,i=d+1;i<=b[0];i++)
if ((b[i]+=a[i-d]*c)>=DEPTH)
b[i+1]+=b[i]/DEPTH,b[i]%=DEPTH;
for (;b[b[0]+1];b[0]++,b[b[0]+1]=b[b[0]]/DEPTH,b[b[0]]%=DEPTH);
for (;!b[b[0]]&&b[0]>1;b[0]--);
}
void div(bignum_ c,bignum_ a,const bignum_ b){
int h,l,m,i;
memset((void*)c,0,sizeof(bignum_t));
c[0]=(b[0]<a[0]+1)?(a[0]-b[0]+2):1;
for (i=c[0];i;sub(a,b,c[i]=m,i-1),i--)
for (h=DEPTH-1,l=0,m=(h+l+1)>>1;h>l;m=(h+l+1)>>1)
if (comp(b,m,i-1,a)) h=m-1;
else l=m;
for (;!c[c[0]]&&c[0]>1;c[0]--);
c[0]=c[0]>1?c[0]:1;
}
void div(bignum_ a,const int b,int& c){
int i;
for (c=0,i=a[0];i;c=c*DEPTH+a[i],a[i]=c/b,c%=b,i--);
for (;!a[a[0]]&&a[0]>1;a[0]--);
}
void sqrt(bignum_ b,bignum_ a){
int h,l,m,i;
memset((void*)b,0,sizeof(bignum_t));
for (i=b[0]=(a[0]+1)>>1;i;sub(a,b,m,i-1),b[i]+=m,i--)
for (h=DEPTH-1,l=0,b[i]=m=(h+l+1)>>1;h>l;b[i]=m=(h+l+1)>>1)
if (comp(b,m,i-1,a)) h=m-1;
else l=m;
for (;!b[b[0]]&&b[0]>1;b[0]--);
for (i=1;i<=b[0];b[i++]>>=1);
}
int length(const bignum_ a){
int t,ret;
for (ret=(a[0]-1)*DIGIT,t=a[a[0]];t;t/=10,ret++);
return ret>0?ret:1;
}
int digit(const bignum_ a,const int b){
int i,ret;
for (ret=a[(b-1)/DIGIT+1],i=(b-1)%DIGIT;i;ret/=10,i--);
return ret%10;
}
int zeronum(const bignum_ a){
int ret,t;
for (ret=0;!a[ret+1];ret++);
for (t=a[ret+1],ret*=DIGIT;!(t%10);t/=10,ret++);
return ret;
}
void comp(int* a,const int l,const int h,const int d){
int i,j,t;
for (i=l;i<=h;i++)
for (t=i,j=2;t>1;j++)
while (!(t%j))
a[j]+=d,t/=j;
}
void convert(int* a,const int h,bignum_ b){
int i,j,t=1;
memset(b,0,sizeof(bignum_t));
for (b[0]=b[1]=1,i=2;i<=h;i++)
if (a[i])
for (j=a[i];j;t*=i,j--)
if (t*i>DEPTH)
mul(b,t),t=1;
mul(b,t);
}
void combination(bignum_ a,int m,int n){
int* t=new int[m+1];
memset((void*)t,0,sizeof(int)*(m+1));
comp(t,n+1,m,1);
comp(t,2,m-n,-1);
convert(t,m,a);
delete []t;
}
void permutation(bignum_ a,int m,int n){
int i,t=1;
memset(a,0,sizeof(bignum_t));
a[0]=a[1]=1;
for (i=m-n+1;i<=m;t*=i++)
if (t*i>DEPTH)
mul(a,t),t=1;
mul(a,t);
}
#define SGN(x) ((x)>0?1:((x)<0?-1:0))
#define ABS(x) ((x)>0?(x):-(x))
int read(bignum_ a,int &sgn,istream& is=cin){
char str[MAX*DIGIT+2],ch,*buf;
int i,j;
memset((void*)a,0,sizeof(bignum_t));
if (!(is>>str)) return 0;
buf=str,sgn=1;
if (*buf=='-') sgn=-1,buf++;
for (a[0]=strlen(buf),i=a[0]/2-1;i>=0;i--)
ch=buf[i],buf[i]=buf[a[0]-1-i],buf[a[0]-1-i]=ch;
for (a[0]=(a[0]+DIGIT-1)/DIGIT,j=strlen(buf);j<a[0]*DIGIT;buf[j++]='0');
for (i=1;i<=a[0];i++)
for (a[i]=0,j=0;j<DIGIT;j++)
a[i]=a[i]*10+buf[i*DIGIT-1-j]-'0';
for (;!a[a[0]]&&a[0]>1;a[0]--);
if (a[0]==1&&!a[1]) sgn=0;
return 1;
}
struct bignum{
bignum_t num;
int sgn;
public:
inline bignum(){memset(num,0,sizeof(bignum_t));num[0]=1;sgn=0;}
inline bignum& operator=(const bignum& a){memcpy(num,a.num,sizeof(bignum_t));sgn=a.sgn;return *this;}
inline bignum& operator=(const int a){memset(num,0,sizeof(bignum_t));num[0]=1;sgn=SGN(a);add(num,sgn*a);return *this;};
inline bignum& operator+=(const bignum& a){if(sgn==a.sgn)add(num,a.num);else if(sgn&&a.sgn){int ret=comp(num,a.num);if(ret>0)sub(num,a.num);else if(ret<0){bignum_ t;
memcpy(t,num,sizeof(bignum_t));memcpy(num,a.num,sizeof(bignum_t));sub(num,t);sgn=a.sgn;}else memset(num,0,sizeof(bignum_t)),num[0]=1,sgn=0;}else if(!sgn)memcpy(num,a.num,sizeof(bignum_t)),sgn=a.sgn;return *this;}
inline bignum& operator+=(const int a){if(sgn*a>0)add(num,ABS(a));else if(sgn&&a){int ret=comp(num,ABS(a));if(ret>0)sub(num,ABS(a));else if(ret<0){bignum_ t;
memcpy(t,num,sizeof(bignum_t));memset(num,0,sizeof(bignum_t));num[0]=1;add(num,ABS(a));sgn=-sgn;sub(num,t);}else memset(num,0,sizeof(bignum_t)),num[0]=1,sgn=0;}else if(!sgn)sgn=SGN(a),add(num,ABS(a));return *this;}
inline bignum operator+(const bignum& a){bignum ret;memcpy(ret.num,num,sizeof(bignum_t));ret.sgn=sgn;ret+=a;return ret;}
inline bignum operator+(const int a){bignum ret;memcpy(ret.num,num,sizeof(bignum_t));ret.sgn=sgn;ret+=a;return ret;}
inline bignum& operator-=(const bignum& a){if(sgn*a.sgn<0)add(num,a.num);else if(sgn&&a.sgn){int ret=comp(num,a.num);if(ret>0)sub(num,a.num);else if(ret<0){bignum_ t;
memcpy(t,num,sizeof(bignum_t));memcpy(num,a.num,sizeof(bignum_t));sub(num,t);sgn=-sgn;}else memset(num,0,sizeof(bignum_t)),num[0]=1,sgn=0;}else if(!sgn)add(num,a.num),sgn=-a.sgn;return *this;}
inline bignum& operator-=(const int a){if(sgn*a<0)add(num,ABS(a));else if(sgn&&a){int ret=comp(num,ABS(a));if(ret>0)sub(num,ABS(a));else if(ret<0){bignum_ t;
memcpy(t,num,sizeof(bignum_t));memset(num,0,sizeof(bignum_t));num[0]=1;add(num,ABS(a));sub(num,t);sgn=-sgn;}else memset(num,0,sizeof(bignum_t)),num[0]=1,sgn=0;}else if(!sgn)sgn=-SGN(a),add(num,ABS(a));return *this;}
inline bignum operator-(const bignum& a){bignum ret;memcpy(ret.num,num,sizeof(bignum_t));ret.sgn=sgn;ret-=a;return ret;}
inline bignum operator-(const int a){bignum ret;memcpy(ret.num,num,sizeof(bignum_t));ret.sgn=sgn;ret-=a;return ret;}
inline bignum& operator*=(const bignum& a){bignum_ t;mul(t,num,a.num);memcpy(num,t,sizeof(bignum_t));sgn*=a.sgn;return *this;}
inline bignum& operator*=(const int a){mul(num,ABS(a));sgn*=SGN(a);return *this;}
inline bignum operator*(const bignum& a){bignum ret;mul(ret.num,num,a.num);ret.sgn=sgn*a.sgn;return ret;}
inline bignum operator*(const int a){bignum ret;memcpy(ret.num,num,sizeof(bignum_t));mul(ret.num,ABS(a));ret.sgn=sgn*SGN(a);return ret;}
inline bignum& operator/=(const bignum& a){bignum_ t;div(t,num,a.num);memcpy(num,t,sizeof(bignum_t));sgn=(num[0]==1&&!num[1])?0:sgn*a.sgn;return *this;}
inline bignum& operator/=(const int a){int t;div(num,ABS(a),t);sgn=(num[0]==1&&!num[1])?0:sgn*SGN(a);return *this;}
inline bignum operator/(const bignum& a){bignum ret;bignum_ t;memcpy(t,num,sizeof(bignum_t));div(ret.num,t,a.num);ret.sgn=(ret.num[0]==1&&!ret.num[1])?0:sgn*a.sgn;return ret;}
inline bignum operator/(const int a){bignum ret;int t;memcpy(ret.num,num,sizeof(bignum_t));div(ret.num,ABS(a),t);ret.sgn=(ret.num[0]==1&&!ret.num[1])?0:sgn*SGN(a);return ret;}
inline bignum& operator%=(const bignum& a){bignum_ t;div(t,num,a.num);if (num[0]==1&&!num[1])sgn=0;return *this;}
inline int operator%=(const int a){int t;div(num,ABS(a),t);memset(num,0,sizeof(bignum_t));num[0]=1;add(num,t);return t;}
inline bignum operator%(const bignum& a){bignum ret;bignum_ t;memcpy(ret.num,num,sizeof(bignum_t));div(t,ret.num,a.num);ret.sgn=(ret.num[0]==1&&!ret.num[1])?0:sgn;return ret;}
inline int operator%(const int a){bignum ret;int t;memcpy(ret.num,num,sizeof(bignum_t));div(ret.num,ABS(a),t);memset(ret.num,0,sizeof(bignum_t));ret.num[0]=1;add(ret.num,t);return t;}
inline bignum& operator++(){*this+=1;return *this;}
inline bignum& operator--(){*this-=1;return *this;};
inline int operator>(const bignum& a){return sgn>0?(a.sgn>0?comp(num,a.num)>0:1):(sgn<0?(a.sgn<0?comp(num,a.num)<0:0):a.sgn<0);}
inline int operator>(const int a){return sgn>0?(a>0?comp(num,a)>0:1):(sgn<0?(a<0?comp(num,-a)<0:0):a<0);}
inline int operator>=(const bignum& a){return sgn>0?(a.sgn>0?comp(num,a.num)>=0:1):(sgn<0?(a.sgn<0?comp(num,a.num)<=0:0):a.sgn<=0);}
inline int operator>=(const int a){return sgn>0?(a>0?comp(num,a)>=0:1):(sgn<0?(a<0?comp(num,-a)<=0:0):a<=0);}
inline int operator<(const bignum& a){return sgn<0?(a.sgn<0?comp(num,a.num)>0:1):(sgn>0?(a.sgn>0?comp(num,a.num)<0:0):a.sgn>0);}
inline int operator<(const int a){return sgn<0?(a<0?comp(num,-a)>0:1):(sgn>0?(a>0?comp(num,a)<0:0):a>0);}
inline int operator<=(const bignum& a){return sgn<0?(a.sgn<0?comp(num,a.num)>=0:1):(sgn>0?(a.sgn>0?comp(num,a.num)<=0:0):a.sgn>=0);}
inline int operator<=(const int a){return sgn<0?(a<0?comp(num,-a)>=0:1):(sgn>0?(a>0?comp(num,a)<=0:0):a>=0);}
inline int operator==(const bignum& a){return (sgn==a.sgn)?!comp(num,a.num):0;}
inline int operator==(const int a){return (sgn*a>=0)?!comp(num,ABS(a)):0;}
inline int operator!=(const bignum& a){return (sgn==a.sgn)?comp(num,a.num):1;}
inline int operator!=(const int a){return (sgn*a>=0)?comp(num,ABS(a)):1;}
inline int operator[](const int a){return digit(num,a);}
friend inline istream& operator>>(istream& is,bignum& a){read(a.num,a.sgn,is);return is;}
friend inline ostream& operator<<(ostream& os,const bignum& a){if(a.sgn<0)os<<'-';write(a.num,os);return os;}
friend inline bignum sqrt(const bignum& a){bignum ret;bignum_ t;memcpy(t,a.num,sizeof(bignum_t));sqrt(ret.num,t);ret.sgn=ret.num[0]!=1||ret.num[1];return ret;}
friend inline bignum sqrt(const bignum& a,bignum& b){bignum ret;memcpy(b.num,a.num,sizeof(bignum_t));sqrt(ret.num,b.num);ret.sgn=ret.num[0]!=1||ret.num[1];b.sgn=b.num[0]!=1||ret.num[1];return ret;}
inline int length(){return ::length(num);}
inline int zeronum(){return ::zeronum(num);}
inline bignum C(const int m,const int n){combination(num,m,n);sgn=1;return *this;}
inline bignum P(const int m,const int n){permutation(num,m,n);sgn=1;return *this;}
};
#define N 105
bignum s[N];
int main()
{
int t,n,i,k;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=k=0;i<n;i++)
{
cin>>s[i];
if(s[i]==0)
k++;
}
if(k>0&&k<n)
{
puts("No");
continue;
}
if(n<3)
{
puts("Yes");
continue;
}
for(i=1;i<n-1;i++)
if(s[i-1]*s[i+1]!=s[i]*s[i])
break;
if(i<n-1)
puts("No");
else
puts("Yes");
}
return 0;
}

31
HDOJ/5430_autoAC.cpp Normal file
View File

@ -0,0 +1,31 @@
#include<stdio.h>
#define LL long long
LL fun(LL m)
{
LL res=m;
for(LL i=2;i*i<=m;i++)
{
if(m%i==0)
{
res=(res*(i-1))/i;
while(m%i==0)
{
m/=i;
}
}
}
if(m>1) res=(res*(m-1))/m;
return res;
}
int main()
{
LL n;
int T;
scanf ("%d",&T);
while(T--)
{
scanf("%lld",&n);
printf("%lld\n",fun(n+1));
}
return 0;
}

81
HDOJ/5431_autoAC.cpp Normal file
View File

@ -0,0 +1,81 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char str[20010];
int bits[32];
int save[32][20010];
int tmp[32];
int findindex(int i,int key){
int l=1,r=save[i][0];
int m,ans=0;
while(l<=r){
int m=(l+r)/2;
if(save[i][m]<=key){
ans=m;
l=m+1;
}
else r=m-1;
}
return ans;
}
int main(){
int kase,now,T,K;
scanf("%d",&kase);
for(int i=1;i<32;i++) bits[i]=(1<<i);
while(kase--){
scanf("%s",str);
int len=strlen(str);
for(int i=0;i<31;i++) save[i][0]=0;
for(int i=0;i<len;i++){
now=0;
for(int j=i;j<=min(i+29,len-1);j++){
now=(now<<1)+(str[j]-'A');
save[j-i+1][++save[j-i+1][0]]=now;
}
}
for(int i=1;i<=30;i++) sort(save[i]+1,save[i]+save[i][0]+1);
for(int i=1;i<=30;i++) {
save[i][0]=unique(save[i]+1,save[i]+save[i][0]+1)-(save[i]+1);
}
scanf("%d",&T); int ans,ansbit;
while(T--){
scanf("%d",&K);
ans=-1;
for(int i=1;i<=30;i++){
if(K>bits[i]-save[i][0]){
K-=(bits[i]-save[i][0]);
continue;
}
int l=0,r=bits[i]-1;
while(l<=r){
int m=(l+r)>>1;
int pos=findindex(i,m);
if(K>m+1-pos) l=m+1;
else if(K<m+1-pos) r=m-1;
else{
ans=m;
while(ans==save[i][pos]&&pos!=0){
ans--; pos--;
}
break;
}
}
if(ans!=-1){
ansbit=i;
break;
}
}
int cnt=0;
while(ansbit--){
tmp[cnt++]=ans%2;
ans/=2;
}
while(cnt--)
printf("%c",tmp[cnt]+'A');
puts("");
}
}
return 0;
}

45
HDOJ/5432_autoAC.cpp Normal file
View File

@ -0,0 +1,45 @@
#include<map>
#include<set>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int MX = 1e5 + 5;
double A[MX], B[MX];
int main() {
int T, n;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%lf", &A[i]);
}
for(int i = 1; i <= n; i++) {
scanf("%lf", &B[i]);
}
double L = 0, R = 1000, m;
for(int i = 1; i <= 40; i++) {
m = (L + R) / 2;
double s, sum1 = 0, sum2 = 0;
for(int j = 1; j <= n; j++) {
if(A[j] <= m) s = 0;
else s = B[j] * B[j] * (A[j] - m) * (A[j] - m) * (A[j] - m) / (3.0 * A[j] * A[j]);
sum1 += s;
sum2 += B[j] * B[j] * A[j] / 3.0 - s;
}
if(sum1 < sum2) R = m;
else L = m;
}
printf("%d\n", (int)m);
}
return 0;
}

69
HDOJ/5433_autoAC.cpp Normal file
View File

@ -0,0 +1,69 @@
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 55;
const double eps = 1e-6;
const double inf = 1e20;
const int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int N, M, K, Sx, Sy, Ex, Ey;
bool done[maxn][maxn][maxn];
char G[maxn][maxn];
double dp[maxn][maxn][maxn];
struct State {
int x, y, k;
double d;
State(int x = 0, int y = 0, int k = 0, double d = 0): x(x), y(y), k(k), d(d) {}
bool operator < (const State& u) const { return d > u.d; }
};
void init () {
scanf("%d%d%d", &N, &M, &K);
for (int i = 1; i <= N; i++)
scanf("%s", G[i]+1);
scanf("%d%d%d%d", &Sx, &Sy, &Ex, &Ey);
}
bool Dijkstra () {
for (int i = 1; i <= N; i++)
for (int j = 1; j <= M; j++)
for (int k = 0; k <= K; k++) dp[i][j][k] = inf;
memset(done, false, sizeof(done));
dp[Sx][Sy][K] = 0;
priority_queue<State> Q;
Q.push(State(Sx, Sy, K, 0));
while (!Q.empty()) {
State u = Q.top();
Q.pop();
int x = u.x, y = u.y, k = u.k;
if (done[x][y][k]) continue;
done[x][y][k] = true;
for (int i = 0; i < 4; i++) {
int p = x + dir[i][0];
int q = y + dir[i][1];
int t = k - 1;
if (p <= 0 || p > N || q <= 0 || q > M || t <= 0 || G[p][q] == '#') continue;
double d = 1.0 * abs(G[x][y] - G[p][q]) / k;
if (dp[p][q][t] > dp[x][y][k] + d) {
dp[p][q][t] = dp[x][y][k] + d;
Q.push(State(p, q, t, dp[p][q][t]));
}
}
}
double ans = inf;
for (int i = 1; i <= K; i++)
ans = min(ans, dp[Ex][Ey][i]);
if (fabs(ans-inf) < eps) return false;
printf("%.2lf\n", ans);
return true;
}
int main () {
int cas;
scanf("%d", &cas);
while (cas--) {
init();
if (!Dijkstra()) printf("No Answer\n");
}
return 0;
}

101
HDOJ/5434_autoAC.cpp Normal file
View File

@ -0,0 +1,101 @@
#include<map>
#include<set>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
typedef long long LL;
const int matMX = 128 + 5;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
struct Mat {
int m, n;
LL S[matMX][matMX];
Mat(int a, int b) {
m = a;
n = b;
memset(S, 0, sizeof(S));
}
Mat(int a, int b, LL w[][matMX]) {
m = a;
n = b;
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
S[i][j] = w[i][j];
}
}
}
};
Mat mat_mul(Mat A, Mat B) {
Mat C(A.m, B.n);
for(int i = 0; i < A.m; i++) {
for(int j = 0; j < B.n; j++) {
for(int k = 0; k < A.n; k++) {
C.S[i][j] = (C.S[i][j] + A.S[i][k] * B.S[k][j]) % mod;
}
}
}
return C;
}
Mat Blank(int m, int n) {
Mat ret(m, n);
for(int i = 0; i < m; i++) {
ret.S[i][i] = 1;
}
return ret;
}
Mat mat_pow(Mat A, LL b) {
Mat ret = Blank(A.m, A.n);
while(b) {
if(b & 1) ret = mat_mul(ret, A);
A = mat_mul(A, A);
b >>= 1;
}
return ret;
}
LL S[matMX][matMX];
void DFS(int m, int cnt, int x, int y) {
if(cnt == m) {
S[x][y] = 1;
return;
}
DFS(m, cnt + 1, x << 1, y << 1);
DFS(m, cnt + 1, x << 1 | 1, y << 1 | 1);
if(cnt) {
if((x & 1) ^ (y & 1) == 0) {
DFS(m, cnt + 1, x << 1, y << 1 | 1);
DFS(m, cnt + 1, x << 1 | 1, y << 1);
} else if((x & 1) && !(y & 1)) {
DFS(m, cnt + 1, x << 1 | 1, y << 1);
} else {
DFS(m, cnt + 1, x << 1, y << 1 | 1);
}
} else {
DFS(m, cnt + 1, x << 1 | 1, y << 1);
DFS(m, cnt + 1, x << 1, y << 1 | 1);
}
}
int main() {
int m, n;
while(~scanf("%d%d", &n, &m)) {
DFS(m, 0, 0, 0);
Mat A(1 << m, 1 << m, S), B(1 << m, 1);
memset(B.S, 0, sizeof(B.S));
B.S[0][0] = 1;
Mat ret = mat_mul(mat_pow(A, n), B);
LL ans = 0;
for(int i = 0; i < (1 << m); i++) {
ans += ret.S[i][0];
ans %= mod;
}
printf("%I64d\n", ans);
}
return 0;
}

65
HDOJ/5435_autoAC.cpp Normal file
View File

@ -0,0 +1,65 @@
#include <cstdio>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define getmid(l,r) ((l) + ((r) - (l)) / 2)
#define MEM(a,b) memset(a,b,sizeof(a))
#define MP(a,b) make_pair(a,b)
#define PB push_back
typedef long long ll;
typedef pair<int,int> pii;
const double eps = 1e-8;
const int INF = (1 << 30) - 1;
const int MAXN = 100010;
const ll mod = 1e9 + 7;
int T,LA,LB;
char A[MAXN],B[MAXN];
int dp[100010][16];
int Solve(char *s,int L,int p,int f,int xsum){
if(p > L) return xsum;
if(f){
int len = L - p + 1;
int res = 0;
for(int i = 0; i < 16; ++i)
res = (res + 1ll * (xsum ^ i) * dp[len][i]) % mod;
return res;
}
int top = s[p] - '0';
int res = 0;
for(int i = 0; i <= top; ++i)
res = (res + 1ll * Solve(s,L,p + 1,f | (i < top),xsum ^ i)) % mod;
return res;
}
void Pre(){
dp[0][0] = 1;
for(int i = 1; i < 100010; ++i)
for(int j = 0; j < 16; ++j)
for(int k = 0; k < 10; ++k)
dp[i][j] = (dp[i][j] + 1ll * dp[i - 1][j ^ k]) % mod;
}
int main(){
Pre();
scanf("%d",&T);
for(int tt = 1; tt <= T; ++tt){
scanf("%s%s",A + 1,B + 1);
LA = strlen(A + 1);
LB = strlen(B + 1);
int tmp = 0;
for(int i = 1; i <= LA; ++i) tmp ^= (A[i] - '0');
int ansA = Solve(A,LA,1,0,0);
int ansB = Solve(B,LB,1,0,0);
printf("Case #%d: %d\n",tt,(int)
(((1ll * ansB - ansA + tmp) % mod + mod) % mod));
}
return 0;
}

79
HDOJ/5437_autoAC.cpp Normal file
View File

@ -0,0 +1,79 @@
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#define maxn 150005
using namespace std;
struct node
{
char name[205];
int v, tp;
friend bool operator <(node a,node b)
{
if(a.v == b.v) return a.tp > b.tp;
return a.v < b.v;
}
};
node fri[maxn];
struct node1
{
int t, r;
}ask[maxn];
bool cmp(node1 a, node1 b)
{
return a.t < b.t;
}
int put[maxn], wen[105];
int main()
{
int t, k, m, p, i, j, ok, now;
scanf("%d", &t);
while(t--)
{
scanf("%d %d %d", &k, &m, &p);
for(i = 0;i < k;i++)
{
scanf("%s %d",fri[i].name, &fri[i].v);
fri[i].tp = i;
}
ask[0].t = -1;
for(i = 0;i < m;i++)
scanf("%d %d", &ask[i].t, &ask[i].r);
for(i = 0;i < p;i++)
scanf("%d", &wen[i]);
priority_queue<node>q;
sort(ask, ask + m, cmp);
now = 0;
ok = 0;
for(i = 1;i <= k;i++)
{
q.push(fri[i - 1]);
if(i == ask[now].t)
{
for(j = 1;j <= ask[now].r&&(!q.empty());j++)
{
node x = q.top();
q.pop();
put[ok++] = x.tp;
}
now++;
}
}
while(!q.empty())
{
node x = q.top();
q.pop();
put[ok++] = x.tp;
}
for(i = 0;i < p;i++)
printf("%s%c", fri[put[wen[i] - 1]].name, i==p-1?'\n':' ');
}
}

70
HDOJ/5438_autoAC.cpp Normal file
View File

@ -0,0 +1,70 @@
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e4 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const int M = 1e5 + 10;
int n, m;
int deg[N], val[N];
vector<int> G[N];
bool vis[N];
void dfs(int u, int &cnt, long long& sum) {
sum += val[u];
++cnt;
vis[u] = true;
for(int i = 0; i < G[u].size(); ++i) {
int v = G[u][i];
if(vis[v]) continue;
dfs(v, cnt, sum);
}
}
int main() {
ios_base::sync_with_stdio(0);
int t; scanf("%d", &t);
while(t--) {
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i) {
scanf("%d", val + i);
G[i].clear();
vis[i] = false;
deg[i] = 0;
}
for(int i = 1; i <= m; ++i) {
int u, v; scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
++deg[u], ++deg[v];
}
queue<int> q;
for(int i = 1; i <= n; ++i)
if(deg[i] < 2) q.push(i);
while(q.size()) {
int u = q.front(); q.pop();
deg[u] = 0; vis[u] = true;
for(int i = 0; i < G[u].size(); ++i) {
int v = G[u][i];
if(--deg[v] < 2 && !vis[v]) q.push(v);
}
}
long long ans = 0;
for(int i = 1; i <= n; ++i) {
if(vis[i]) continue;
int cnt = 0; long long sum = 0;
dfs(i, cnt, sum);
if(cnt & 1) ans += sum;
}
printf("%I64d\n", ans);
}
return 0;
}

77
HDOJ/5439_autoAC.cpp Normal file
View File

@ -0,0 +1,77 @@
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <set>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
const int MOD = (int)1e9+7;
const int INF = (int)1e9;
const int MAXN = 438744;
int a[MAXN+5];
int b[MAXN+5];
int sum[MAXN+5];
int dp[MAXN+5];
int pre[MAXN+5];
int sz,val;
int add(int x,int y){
x+=y;
if(x>=MOD)x-=MOD;
return x;
}
int po(int x,int n){
int ans=1;
int temp=x;
while(n){
if(n&1)ans=(ll)ans*temp%MOD;
temp=(ll)temp*temp%MOD;
n>>=1;
}
return ans;
}
int main()
{
val=po(2,MOD-2);
a[1]=1;
a[2]=2;
a[3]=2;
int sz=3;
for(int i=3;;i++){
int cnt=a[i];
while(cnt){
a[++sz]=i;
cnt--;
if(sz>=MAXN)break;
}
if(sz>=MAXN)break;
}
for(int i=1;i<=MAXN;i++){
sum[i]=sum[i-1]+a[i];
if(sum[i]>=INF)break;
}
for(int i=1;i<=MAXN;i++){
dp[i]=add(dp[i-1],(ll)( add(add(add(pre[i-1],1),pre[i-1]),a[i]) )*a[i]%MOD*i%MOD*val%MOD);
pre[i]=add(pre[i-1],a[i]);
}
int t;scanf("%d",&t);
while(t--){
int n;scanf("%d",&n);
int l=1,r=MAXN;
int pos;
while(l<=r){
int mid=l+r>>1;
if(n<=sum[mid]){
pos=mid;
r=mid-1;
}
else l=mid+1;
}
int x=n-sum[pos-1];
int ans=add(dp[pos-1],(ll)( add(add(add(pre[pos-1],1),pre[pos-1]),x) )*x%MOD*pos%MOD*val%MOD);
printf("%d\n",ans);
}
return 0;
}

145
HDOJ/5440_autoAC.cpp Normal file
View File

@ -0,0 +1,145 @@
#include <cstdio>
#include <cstring>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1 * 1e5 + 5;
ll S;
int N, M, L[10], X[10], head, rear, ans;
map<ll, int> C;
set<ll> T;
struct State {
ll s;
int f, c;
State(ll s = 0, int f = 0, int c = 0): s(s), f(f), c(c) {}
}Q[maxn], cur;
inline ll idx(int* c) {
ll ret = 0;
for (int i = 0; i < 12; i++)
ret = ret * (N+1) + c[i];
return ret;
}
inline void reidx(ll s, int* c) {
for (int i = 11; i >= 0; i--) {
c[i] = s % (N+1);
s /= (N+1);
}
}
void init () {
C.clear();
T.clear();
scanf("%d%d", &N, &M);
int x, cnt[15];
memset(cnt, 0, sizeof(cnt));
for (int i = 1; i <= N; i++) {
scanf("%d", &x);
if (x == 12) x = 0;
cnt[x]++;
}
S = idx(cnt);
for (int i = 0; i < M; i++)
scanf("%d%d", &L[i], &X[i]);
}
bool dfs1 (int d, int* c, int* t, int n, int m, int v) {
if (d > 12 || n > m) return false;
if (n == 0 && m == 0) {
ll tmp = idx(t);
if (tmp == S) {
ans = cur.c + 1;
return true;
}
if (!C.count(tmp)) {
C[tmp] = cur.c + 1;
if (cur.c < 4) Q[rear++] = State(tmp, v, cur.c+1);
}
return false;
}
for (int i = min(c[d], n); i >= 0; i--) {
if (n-i > m-c[d]) break;
t[(d-X[v]+12)%12] += i;
t[d] += (c[d] - i);
if (dfs1(d+1, c, t, n-i, m-c[d], v)) return true;
t[(d-X[v]+12)%12] -= i;
t[d] -= (c[d] - i);
}
return false;
}
bool dfs2 (int d, int* c, int* t, int n, int m, int v) {
if (d > 12 || n > m) return false;
if (n == 0 && m == 0) {
ll tmp = idx(t);
if (C.count(tmp)) {
ans = C[tmp] + cur.c + 1;
return true;
}
if (!T.count(tmp) && cur.c < 2) {
T.insert(tmp);
Q[rear++] = State(tmp, v, cur.c+1);
}
return false;
}
for (int i = min(c[d], n); i >= 0; i--) {
if (n-i > m-c[d]) break;
t[(d+X[v]+12)%12] += i;
t[d] += (c[d] - i);
if (dfs2(d+1, c, t, n-i, m-c[d], v)) return true;
t[(d+X[v]+12)%12] -= i;
t[d] -= (c[d] - i);
}
return false;
}
int bfs() {
int cnt[15], pos[15];
memset(cnt, 0, sizeof(cnt));
cnt[0] = N;
ll k = idx(cnt);
if (S == k) return 0;
C[k] = 0;
head = rear = 0;
for (int i = 0; i < M; i++) {
int x = (-X[i] + 12) % 12;
cnt[x] += L[i];
cnt[0] -= L[i];
ll tmp = idx(cnt);
if (!C.count(tmp)) {
if (tmp == S) return 1;
Q[rear++] = State(tmp, i, 1);
C[tmp] = 1;
}
cnt[x] -= L[i];
cnt[0] += L[i];
}
while (head < rear) {
cur = Q[head++];
reidx(cur.s, cnt);
for (int i = M-1; i >= cur.f; i--) {
memset(pos, 0, sizeof(pos));
if (dfs1(0, cnt, pos, L[i], N, i))
return ans;
}
}
head = rear = 0;
Q[rear++] = State(S, 0, 0);
while (head < rear) {
cur = Q[head++];
reidx(cur.s, cnt);
for (int i = M-1; i >= cur.f; i--) {
memset(pos, 0, sizeof(pos));
if (dfs2(0, cnt, pos, L[i], N, i)) return ans;
}
}
return -1;
}
int main () {
int cas;
scanf("%d", &cas);
while (cas--) {
init();
printf("%d\n", bfs());
}
return 0;
}

78
HDOJ/5441_autoAC.cpp Normal file
View File

@ -0,0 +1,78 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAX 20007
using namespace std;
int fa[MAX],num[MAX],t,n,m,q,pp[MAX];
struct Query
{
int x,id;
bool operator < ( const Query& a )const
{
return x < a.x;
}
}a[MAX];
struct Edge
{
int u,v,w;
bool operator < ( const Edge& a ) const
{
return w < a.w;
}
}e[MAX*5];
void init ( )
{
for ( int i = 1 ; i <= n ; i++ )
{
num[i] = 1;
fa[i] = i;
}
}
int _find ( int x )
{
return fa[x] == x? x : fa[x] = _find ( fa[x] );
}
void _union ( int x , int y )
{
x = _find ( x );
y = _find ( y );
if ( y < x ) swap ( x , y );
fa[y] = x;
num[x] += num[y];
}
int main ( )
{
scanf ( "%d" ,&t );
while ( t-- )
{
int ans = 0;
scanf ( "%d%d%d" , &n , &m , &q );
init ( );
for ( int i = 0 ; i < m ; i++ )
scanf ( "%d%d%d" , &e[i].u , &e[i].v , &e[i].w );
sort ( e , e+m );
int j = 0;
for ( int i = 0 ; i < q ; i++ )
{
a[i].id = i;
scanf ( "%d" , &a[i].x );
}
sort ( a , a+q );
for ( int i = 0 ; i < q ; i++ )
{
while ( j < m && e[j].w <= a[i].x )
{
int u = _find ( e[j].u );
int v = _find ( e[j].v );
j++;
if ( u == v ) continue;
ans += (num[u]+num[v])*(num[u]+num[v]-1)-num[u]*(num[u]-1) - num[v]*(num[v]-1);
_union ( u , v );
}
pp[a[i].id] = ans;
}
for ( int i = 0 ; i < q ; i++ )
printf ( "%d\n" , pp[i] );
}
}

90
HDOJ/5442_autoAC.cpp Normal file
View File

@ -0,0 +1,90 @@
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
#define maxn 200007
int wwa[maxn],wwb[maxn],wwv[maxn],wws[maxn];
int cmp(int *r,int a,int b,int l){
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(int *r,int *sufix,int n,int m){
int i,j,p,*x=wwa,*y=wwb,*t;
for(i=0;i<m;i++)wws[i]=0;
for(i=0;i<n;i++)wws[x[i]=r[i]]++;
for(i=1;i<m;i++)wws[i]+=wws[i-1];
for(i=n-1;i>=0;i--)sufix[--wws[x[i]]]=i;
for(j=1,p=1;p<n;j*=2,m=p){
for(p=0,i=n-j;i<n;i++)y[p++]=i;
for(i=0;i<n;i++)
if(sufix[i]>=j)
y[p++]=sufix[i]-j;
for(i=0;i<n;i++)wwv[i]=x[y[i]];
for(i=0;i<m;i++)wws[i]=0;
for(i=0;i<n;i++)wws[wwv[i]]++;
for(i=1;i<m;i++)wws[i]+=wws[i-1];
for(i=n-1;i>=0;i--)
sufix[--wws[wwv[i]]]=y[i];
for(t=x,x=y,y=t,p=1,x[sufix[0]]=0,i=1;i<n;i++)
x[sufix[i]]=cmp(y,sufix[i-1],sufix[i],j)?p-1:p++;
}
return;
}
int r[maxn];
int rank1[maxn],height[maxn],sufix[maxn];
void calheight(int *r,int *sufix,int n){
int i,j,k=0;
for(i=0;i<=n;i++) rank1[sufix[i]]=i;
for(i=0;i<n;height[rank1[i++]]=k)
for(k?k--:0,j=sufix[rank1[i]-1];r[i+k]==r[j+k];k++);
return;
}
struct Node{
int p,c;
};
int comp(Node a,Node b){
if(a.p == b.p) return a.c < b.c;
return a.p < b.p;
}
Node ans[maxn];
char word[maxn];
int main(){
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
scanf("%s",word);
for(int i = 0;i < n; i++){
r[i]=r[i+n] = word[i];
}
int len =n*4+1;
r[n*2] = '#';
for(int i = 0;i < n; i++){
r[i+n*2+1] = r[i+n*3+1] = word[n-i-1];
}
r[len] = 0;
da(r,sufix,len+1,328);
calheight(r,sufix,len);
int cnt = 0;
ans[cnt++].p = sufix[len];
for(int i = len-1;i > 1; i--){
if(height[i+1] < n) break;
if(sufix[i]>=n&&sufix[i]<=n*2) continue;
if(sufix[i]>=n*3+1)continue;
ans[cnt++].p = sufix[i];
}
for(int i = 0;i < cnt; i++){
if(ans[i].p > 2*n){
ans[i].c =1;
ans[i].p = n-(ans[i].p-2*n-1);
}
else {
ans[i].p++;
ans[i].c = 0;
}
}
sort(ans,ans+cnt,comp);
printf("%d %d\n",ans[0].p,ans[0].c);
}
return 0;
}

46
HDOJ/5443_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e3 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
int n, q, dp[2][N][20];
int RMQ(int l, int r) {
int k = 31 - __builtin_clz(r - l + 1);
int Max = max(dp[1][l][k], dp[1][r - (1 << k) + 1][k]);
return Max;
}
int main() {
int t; scanf("%d", &t);
while(t--) {
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
scanf("%d", &dp[0][i][0]);
dp[1][i][0] = dp[0][i][0];
}
for(int j = 1; (1 << j) <= n; ++j) {
for(int i = 1; i + (1 << j) - 1 <= n; ++i) {
dp[0][i][j] = min(dp[0][i][j - 1],
dp[0][i + (1 << j - 1)][j - 1]);
dp[1][i][j] = max(dp[1][i][j - 1],
dp[1][i + (1 << j - 1)][j - 1]);
}
}
scanf("%d", &q);
while(q--) {
int x, y; scanf("%d%d", &x, &y);
printf("%d\n", RMQ(x, y));
}
}
return 0;
}

80
HDOJ/5444_autoAC.cpp Normal file
View File

@ -0,0 +1,80 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1100;
struct tree{
int v, l, r, f;
tree(){}
tree(int v, int l, int r, int f):v(v), l(l), r(r), f(f){}
} t[maxn<<2];
int T, n;
int pt, sz;
int st[maxn];
int a[maxn];
int ps[maxn];
bool v[maxn];
void work(int x){
int pos = 0, ed = ps[x];
while(1){
if (pt > ed) break;
v[a[pt]] = true;
if (t[pos].v > a[pt]){
if (t[pos].r == -1){
st[a[pt]] = sz;
t[sz] = tree(a[pt++], -1, -1, pos);
t[pos].r = sz;
pos = sz++;
}
else pos = t[pos].r;
}
else{
if (t[pos].l == -1){
st[a[pt]] = sz;
t[sz] = tree(a[pt++], -1, -1, pos);
t[pos].l = sz;
pos = sz++;
}
else pos = t[pos].l;
}
}
}
char s[1100];
void output(int x){
int len = 0, px;
while(1){
px = x;
x = t[x].f;
if (x == -1) break;
if (t[x].l == px) s[len++] = 'W';
else s[len++] = 'E';
}
for (int i = 0, j = len-1; i < j; i++, j--)
swap(s[i], s[j]);
s[len] = 0;
printf("%s\n", s);
}
int main()
{
scanf("%d", &T);
while(T--){
scanf("%d", &n);
for (int i = 0; i < n; i++){
v[i+1] = false;
scanf("%d", &a[i]);
ps[a[i]] = i;
}
sz = 0, pt = 1;
t[sz++] = tree(a[0], -1, -1, -1);
v[a[0]] = true;
for (int i = 1; i <= n; i++)
if (!v[i]){ work(i); }
int q, qq;
scanf("%d", &q);
for (int i = 0; i < q; i++){
scanf("%d", &qq);
output(st[qq]);
}
}
return 0;
}

90
HDOJ/5445_autoAC.cpp Normal file
View File

@ -0,0 +1,90 @@
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <functional>
#include <cassert>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ff(i, n) for(int i=0,END=(n);i<END;i++)
#define fff(i, n, m) for(int i=(n),END=(m);i<=END;i++)
#define dff(i, n, m) for(int i=(n),END=(m);i>=END;i--)
#define travel(e, u) for(int e=first[u], v=vv[first[u]]; ~e; e=nxt[e])
#define mid ((l+r)/2)
#define bit(n) (1ll<<(n))
#define clr(a, b) memset(a, b, sizeof(a))
#define debug(x) cout << #x << " = " << x << endl;
#define ls (rt << 1)
#define rs (ls | 1)
#define lson l, m, ls
#define rson m + 1, r, rs
void work();
int main() {
work();
return 0;
}
const int maxn = 222;
const int maxv = 5e4 + 5 + maxn;
const int inf = 0x3f3f3f3f;
int n, m, p;
int dp[maxv];
void zero_one_pack(int v, int c, int V) {
dff(i, V, v) {
dp[i] = min(dp[i], dp[i - v] + c);
}
}
void zero_one_pack2(int v, int c, int V) {
dff(i, V, v) {
dp[i] = max(dp[i], dp[i - v] + c);
}
}
void multi_pack(int v, int c, int num, int V, bool flag = true) {
int k = 1;
while (num) {
if (flag)
zero_one_pack(v * k, c * k, V);
else
zero_one_pack2(v * k, c * k, V);
num -= k;
k += k;
if (k > num) k = num;
}
}
void input() {
scanf("%d%d%d", &n, &m, &p);
clr(dp, inf); dp[0] = 0;
ff (i, n) {
int v, c, num;
scanf("%d%d%d", &v, &c, &num);
multi_pack(v, c, num, p + 100);
}
int V = *min_element(dp + p, dp + p + 100);
clr(dp, 0);
ff (i, m) {
int v, c, num;
scanf("%d%d%d", &v, &c, &num);
multi_pack(c, v, num, 50000, false);
}
int ans = inf;
dff(i, 50000, 0) if (dp[i] >= V) {
ans = min(ans, i);
}
if (ans == inf) {
puts("TAT");
} else {
printf("%d\n", ans);
}
}
void work() {
int T; scanf("%d", &T);
fff(cas, 1, T) {
input();
}
}

81
HDOJ/5446_autoAC.cpp Normal file
View File

@ -0,0 +1,81 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cassert>
using namespace std;
#define debug 0
const int maxn = 101000;
typedef long long ll;
ll A[maxn], inv[maxn];
int res[20];
int p[20];
void gcd(ll a, ll b, ll d, ll& x, ll& y){
if (!b) { d = a; x = 1; y = 0; }
else{ gcd(b, a%b, d, y, x); y -= x*(a/b); }
}
int cal(long long n, long long m, int p){
A[0] = A[1] = 1;
inv[0] = inv[1] = 1;
for (int i = 2; i <= p; i++){
inv[i] = (long long)(p - p/i) * inv[p % i] % p;
A[i] = A[i-1] * i % p;
}
for (int i = 2; i < p; i++){
inv[i] = inv[i] * inv[i-1] % p;
}
if (debug){
for (int i = 0; i < p; i++){
assert(inv[i] * A[i] % p == 1);
printf("%lld %lld\n", inv[i], A[i]);
}
}
int ret = 1;
while(n&&m){
int a = n % p, b = m % p;
if (a < b) return 0;
n /= p, m /= p;
ret = (long long)ret * A[a] % p * inv[b] % p * inv[a-b] % p;
}
return ret;
}
ll mul(ll a, ll b, ll p){
ll ret = 0;
for (; a; a>>=1){
if (a&1){
ret += b;
if (ret >= p) ret -= p;
}
b <<= 1;
if (b >= p) b -= p;
}
return ret;
}
ll crt(int n, int* a, int *m){
ll M = 1, d, y, x = 0;
for (int i = 0; i < n; i++) M *= m[i];
for (int i = 0; i < n; i++){
ll w = M / m[i];
gcd(m[i], w, d, d, y);
int sign;
if (y < 0) sign = -1;
else sign = 1;
x = (x + mul(y*sign, w*a[i]%M, M) * sign) % M;
}
return (x + M) % M;
}
int T;
int main()
{
scanf("%d", &T);
while(T--){
ll n, m; int k;
scanf("%I64d %I64d %d", &n, &m, &k);
for (int i = 0; i < k; i++){
scanf("%d", &p[i]);
res[i] = cal(n, m, p[i]);
}
long long ans = crt(k, res, p);
printf("%I64d\n", ans);
}
return 0;
}

49
HDOJ/5448_autoAC.cpp Normal file
View File

@ -0,0 +1,49 @@
#include <cstdio>
typedef long long LL;
const int maxn = 100010, mod = 1000000007, inv2 = 500000004;
int t, n, pw1[maxn], pw2[maxn], x, y, pre1x, pre1y, pre2x, pre2y, ans;
int mod_add(int x, int y)
{
x += y;
if(x >= mod)
x -= mod;
return x;
}
int mod_sub(int x, int y)
{
x -= y;
if(x < 0)
x += mod;
return x;
}
int det(int x1, int y1, int x2, int y2)
{
return mod_sub((LL)x1 * y2 % mod, (LL)x2 * y1 % mod);
}
int main()
{
scanf("%d", &t);
pw1[0] = 1;
for(int i = 1; i < maxn; ++i)
pw1[i] = (LL)pw1[i - 1] * 2 % mod;
pw2[0] = 1;
for(int i = 1; i < maxn; ++i)
pw2[i] = (LL)pw2[i - 1] * inv2 % mod;
while(t--)
{
scanf("%d", &n);
pre1x = pre1y = pre2x = pre2y = ans = 0;
for(int i = 1; i <= n; ++i)
{
scanf("%d%d", &x, &y);
ans = mod_add(ans, (LL)pw1[i - 1] * det(x, y, pre2x, pre2y) % mod);
ans = mod_add(ans, (LL)(i == n ? inv2 : pw1[n - i - 1]) * det(pre1x, pre1y, x, y) % mod);
pre1x = mod_add(pre1x, (LL)x * pw1[i] % mod);
pre1y = mod_add(pre1y, (LL)y * pw1[i] % mod);
pre2x = mod_add(pre2x, (LL)x * pw2[i] % mod);
pre2y = mod_add(pre2y, (LL)y * pw2[i] % mod);
}
printf("%d\n", ans);
}
return 0;
}

95
HDOJ/5449_autoAC.cpp Normal file
View File

@ -0,0 +1,95 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 50005;
int N, E, first[maxn], jump[maxn<<1], linker[maxn<<1];
ll sz[maxn], up[maxn], dn[maxn], f[20][maxn], dep[maxn];
void addEdge(int u, int v) {
jump[E] = first[u];
linker[E] = v;
first[u] = E++;
}
void dfs (int u, int far, int d) {
sz[u] = 1;
dep[u] = d;
f[0][u] = far;
for (int i = first[u]; i + 1; i = jump[i]) {
int v = linker[i];
if (v == far) continue;
dfs(v, u, d + 1);
sz[u] += sz[v];
}
}
void dfs (int u) {
if (f[0][u] != -1) {
up[u] = up[f[0][u]] + 2 * sz[u] - 1;
dn[u] = dn[f[0][u]] + 2 * (N-sz[u]) - 1;
} else
up[u] = dn[u] = 0;
for (int i = first[u]; i + 1; i = jump[i]) {
int v = linker[i];
if (v == f[0][u]) continue;
dfs(v);
}
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
int d = dep[u] - dep[v];
for (int i = 0; i < 20; i++) if ((1<<i)&d)
u = f[i][u];
if (u == v) return u;
for (int i = 19; i >= 0; i--) {
if (f[i][u] != f[i][v]) {
u = f[i][u];
v = f[i][v];
}
}
return f[0][u];
}
void init () {
E = 0;
memset(first, -1, sizeof(first));
int u, v;
scanf("%d", &N);
for (int i = 1; i < N; i++) {
scanf("%d%d", &u, &v);
addEdge(u, v);
addEdge(v, u);
}
dfs(0, -1, 0);
dfs(0);
for (int k = 1; k < 20; k++) {
for (int i = 0; i < N; i++)
f[k][i] = f[k-1][f[k-1][i]];
}
}
ll get (int u, int v) {
int l = lca(u, v);
return up[u] - up[l] + dn[v] - dn[l];
}
void solve () {
int q, p, u, v;
scanf("%d", &q);
while (q--) {
ll ans = 0;
scanf("%d%d", &p, &u);
while (p--) {
scanf("%d", &v);
ans += get(u, v);
u = v;
}
printf("%lld.0000\n", ans);
}
}
int main () {
int cas;
scanf("%d", &cas);
while (cas--) {
init();
solve();
if (cas) printf("\n");
}
return 0;
}

91
HDOJ/5450_autoAC.cpp Normal file
View File

@ -0,0 +1,91 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 105;
const int maxm = 15;
const int maxs = 200000;
const int mod = 10007;
int N, P, dp[maxn][maxs];
bool exist[maxn];
int encode (int* c, int n) {
int ret = 0;
for (int i = 0; i < n; i++)
ret = ret * 3 + c[i];
return ret;
}
void decode(int* c, int n, int s) {
for (int i = n-1; i >= 0; i--) {
c[i] = s % 3;
s /= 3;
}
}
void move(int* c, int n) {
for (int i = n; i > 0; i--)
c[i] = c[i-1];
c[0] = 0;
}
void init () {
scanf("%d%d", &N, &P);
memset(exist, false, sizeof(exist));
int k, x;
scanf("%d", &k);
for (int i = 0; i < k; i++) {
scanf("%d", &x);
exist[x] = true;
}
}
void add(int& u, int v) { u = (u + v) % mod; }
int solve () {
int n = P + 2, cur[maxm], nxt[maxm], idx[3];
idx[0] = 1, idx[1] = P, idx[2] = P + 2;
memset(dp, 0, sizeof(dp));
for (int i = 0; i < n; i++) cur[i] = 2;
int e = encode(cur, n);
dp[0][e] = 1;
for (int i = 0; i < N; i++) {
for (int s = 0; s <= e; s++) {
if (dp[i][s] == 0) continue;
decode(cur, n, s);
memcpy(nxt, cur, sizeof(cur));
move(nxt, n);
if (exist[i+1]) {
nxt[0] = 2;
if (nxt[n] == 2) add(dp[i+1][encode(nxt, n)], dp[i][s]);
continue;
}
if (nxt[n] == 2) add(dp[i+1][encode(nxt, n)], dp[i][s]);
nxt[0] = 1;
for (int j = 0; j < 3; j++) {
int& v = nxt[idx[j]];
if (v < 2) {
v++;
if (nxt[n] == 2) add(dp[i+1][encode(nxt, n)], dp[i][s]);
v--;
}
}
nxt[0] = 2;
for (int j = 0; j < 3; j++) {
for (int k = j+1; k < 3; k++) {
int& u = nxt[idx[j]];
int& v = nxt[idx[k]];
if (u < 2 && v < 2) {
u++, v++;
if (nxt[n] == 2) add(dp[i+1][encode(nxt, n)], dp[i][s]);
u--, v--;
}
}
}
}
}
return dp[N][e];
}
int main () {
int cas;
scanf("%d", &cas);
for (int kcas = 1; kcas <= cas; kcas++) {
init();
printf("Case #%d: %d\n", kcas, solve());
}
return 0;
}

74
HDOJ/5451_autoAC.cpp Normal file
View File

@ -0,0 +1,74 @@
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#define LL long long
#define FOR(i,x,y) for(int i = x;i < y;i ++)
#define IFOR(i,x,y) for(int i = x;i > y;i --)
#define N 2
int MOD;
LL p;
struct Matrix{
LL mat[N][N];
Matrix operator *(const Matrix& a) const{
Matrix c;
memset(c.mat,0,sizeof(c.mat));
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
for(int k=0;k<N;k++){
LL ssss = ((mat[i][k]*a.mat[k][j])%MOD+MOD)%MOD;
c.mat[i][j] = ((c.mat[i][j]+ssss)%MOD+MOD)%MOD;
}
}
}
return c;
}
Matrix operator ^(LL n) const{
Matrix c;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
c.mat[i][j]=(i==j);
}
}
Matrix a = *this;
while(n)
{
if(n%2) c=c*a;
a=a*a;
n=n>>1;
}
return c;
}
};
Matrix ss;
LL quickpow(LL a,LL n,int m){
LL ret = 1;
while(n){
if(n&1) ret = (ret*a)%m;
a = (a*a)%m;
n >>= 1;
}
return ret;
}
int main(){
int T,tCase = 0;
scanf("%d",&T);
ss.mat[0][0] = 10; ss.mat[0][1] = -1; ss.mat[1][0] = 1; ss.mat[1][1] = 0;
while(T--){
printf("Case #%d: ",++tCase);
LL x;
scanf("%I64d%d",&x,&MOD);
p = (MOD-1)*(MOD+1);
LL r = quickpow(2,x,p);
Matrix st;
st.mat[0][0] = 10; st.mat[0][1] = -1; st.mat[1][0] = 1; st.mat[1][1] = 0;
st = st^r;
LL aaaa = (((st.mat[0][0]*10)%MOD+MOD)%MOD + ((st.mat[0][1]*2)%MOD+MOD)%MOD)%MOD;
if(!aaaa){
printf("%d\n",MOD-1);
}
else
printf("%I64d\n",aaaa-1);
}
return 0;
}

123
HDOJ/5452_autoAC.cpp Normal file
View File

@ -0,0 +1,123 @@
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn = 80010;
const int maxm = 26;
int dp[2*maxn][maxm];
bool vis[maxn];
struct edge
{
int from, to;
int next;
} e[2*maxn];
int tot,head[maxn];
int cnt;
int num[maxn];
void init()
{
memset(head,-1,sizeof(head));
memset(vis,false,sizeof(vis));
memset(num,0,sizeof(num));
cnt = 0;
}
void addedge(int u, int v)
{
e[cnt].from = u;
e[cnt].to = v;
e[cnt].next = head[u];
head[u] = cnt++;
}
int ver[2*maxn], R[2*maxn], first[maxn];
void dfs(int u ,int dep)
{
vis[u] = true;
ver[++tot] = u;
first[u] = tot;
R[tot] = dep;
for(int k=head[u]; k!=-1; k=e[k].next)
if( !vis[e[k].to] )
{
int v = e[k].to;
dfs(v, dep+1);
ver[++tot] = u;
R[tot] = dep;
}
}
void ST(int n)
{
for(int i=1; i<=n; i++)
dp[i][0] = i;
for(int j=1; (1<<j)<=n; j++)
{
for(int i=1; i+(1<<j)-1<=n; i++)
{
int a = dp[i][j-1] , b = dp[i+(1<<(j-1))][j-1];
dp[i][j] = R[a]<R[b]?a:b;
}
}
}
int RMQ(int l,int r)
{
int k=0;
while((1<<(k+1))<=r-l+1)
k++;
int a = dp[l][k], b = dp[r-(1<<k)+1][k];
return R[a]<R[b]?a:b;
}
int LCA(int u ,int v)
{
int x = first[u] , y = first[v];
if(x > y) swap(x,y);
int res = RMQ(x,y);
return ver[res];
}
int DFS(int u,int fa)
{
for(int i = head[u]; ~i; i = e[i].next)
{
int v = e[i].to;
if(v == fa)
continue;
DFS(v, u);
num[u]+=num[v];
}
return 0;
}
int main()
{
int t;
int cas = 0;
int n, m;
scanf("%d",&t);
while(t--)
{
init();
scanf("%d%d",&n,&m);
int u, v;
for(int i = 0; i < n-1; i++)
{
scanf("%d%d",&u,&v);
addedge(u, v);
addedge(v, u);
}
for(int i = n; i <= m; i++)
{
scanf("%d%d",&u,&v);
int tt = LCA(u, v);
num[u]++;
num[v]++;
num[tt]-=2;
}
DFS(1, 1);
int ans = INF;
for(int i = 2; i <= n; i++)
{
ans = min(ans, num[i]+1);
}
printf("Case #%d: %d\n",++cas,ans);
}
return 0;
}

157
HDOJ/5454_autoAC.cpp Normal file
View File

@ -0,0 +1,157 @@
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef __int64 LL;
const int maxn = 300005;
struct tree
{
LL suml[maxn << 2], sumr[maxn << 2], sum[maxn << 2];
LL add[maxn << 2];
inline void pushup(int id, int L, int R)
{
LL mid = L + R >> 1;
sum[id] = sum[id << 1] + sum[id << 1 | 1];
suml[id] = suml[id << 1] + (mid - L + 1)*sum[id << 1 | 1] + suml[id << 1 | 1];
sumr[id] = sumr[id << 1 | 1] + sumr[id << 1] + (R - mid)*sum[id << 1];
}
inline void pushdown(int id, int L, int R)
{
int mid = L + R >> 1;
if (add[id])
{
LL ln = mid - L + 1;
LL rn = R - mid;
add[id << 1] += add[id];
add[id << 1 | 1] += add[id];
sum[id << 1] += add[id] * ln;
sum[id << 1 | 1] += add[id] * rn;
suml[id << 1] += add[id] * (1 + ln)*ln >> 1;
suml[id << 1 | 1] += add[id] * (1 + rn)*rn >> 1;
sumr[id << 1] += add[id] * (1 + ln)*ln >> 1;
sumr[id << 1 | 1] += add[id] * (1 + rn)*rn >> 1;
add[id] = 0;
}
}
void build()
{
memset(sum, 0, sizeof(sum));
memset(suml, 0, sizeof(suml));
memset(sumr, 0, sizeof(sumr));
memset(add, 0, sizeof(add));
}
LL queL(int id, int L, int R, int l, int r)
{
if (l <= L&&R <= r) return (L - l)*sum[id] + suml[id];
else
{
pushdown(id, L, R);
int mid = L + R >> 1;
LL res = 0;
if (l <= mid) res += queL(id << 1, L, mid, l, r);
if (mid<r) res += queL(id << 1 | 1, mid + 1, R, l, r);
return res;
}
}
LL queR(int id, int L, int R, int l, int r)
{
if (l <= L&&R <= r) return (r - R)*sum[id] + sumr[id];
else
{
pushdown(id, L, R);
int mid = L + R >> 1;
LL res = 0;
if (l <= mid) res += queR(id << 1, L, mid, l, r);
if (mid<r) res += queR(id << 1 | 1, mid + 1, R, l, r);
return res;
}
}
LL que(int id, int L, int R, int l, int r)
{
if (l <= L&&R <= r) return sum[id];
else
{
pushdown(id, L, R);
int mid = L + R >> 1;
LL res = 0;
if (l <= mid) res += que(id << 1, L, mid, l, r);
if (mid<r) res += que(id << 1 | 1, mid + 1, R, l, r);
return res;
}
}
void op(int id, int L, int R, int l, int r)
{
if (l <= L&&R <= r)
{
LL sn = R - L + 1;
add[id]++;
sum[id] += sn;
suml[id] += (1 + sn)*sn >> 1;
sumr[id] += (1 + sn)*sn >> 1;
}
else
{
pushdown(id, L, R);
int mid = L + R >> 1;
if (l <= mid) op(id << 1, L, mid, l, r);
if (mid<r) op(id << 1 | 1, mid + 1, R, l, r);
pushup(id, L, R);
}
}
}tz, tf;
int main()
{
int T;
scanf("%d", &T);
int ks = 0;
while (T--)
{
printf("Case #%d:\n", ++ks);
tz.build();
tf.build();
int n, m;
scanf("%d %d", &n, &m);
while (m--)
{
int op;
scanf("%d", &op);
if (op == 1)
{
int l, r;
scanf("%d %d", &l, &r);
tf.op(1, 1, n << 1, l, r);
}
else if (op == 2)
{
int l, r;
scanf("%d %d", &l, &r);
l += n;
r += n;
tz.op(1, 1, n << 1, l, r);
}
else
{
int x1, y1, x2, y2;
scanf("%d %d %d %d", &x1, &x2, &y1, &y2);
LL ans = 0;
int A, B, C, D;
A = x1 - y1 + n;
B = x1 - y2 + n;
C = x2 - y2 + n;
D = x2 - y1 + n;
if (D >= max(A, C) + 1) ans += tz.queR(1, 1, n << 1, max(A, C) + 1, D);
if (B <= min(A, C) - 1) ans += tz.queL(1, 1, n << 1, B, min(A, C) - 1);
ans += tz.que(1, 1, n << 1, min(A, C), max(A, C))*(min(y2 - y1, x2 - x1) + 1);
A = x1 + y1;
B = x1 + y2;
C = x2 + y2;
D = x2 + y1;
if (A <= min(B, D) - 1) ans += tf.queL(1, 1, n << 1, A, min(B, D) - 1);
if (C >= max(B, D) + 1) ans += tf.queR(1, 1, n << 1, max(B, D) + 1, C);
ans += tf.que(1, 1, n << 1, min(B, D), max(B, D))*(min(y2 - y1, x2 - x1) + 1);
printf("%I64d\n", ans);
}
}
}
return 0;
}

92
HDOJ/5455_autoAC.cpp Normal file
View File

@ -0,0 +1,92 @@
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#include<vector>
#define LL long long
#define MAXN 100010
using namespace std;
string s1;
int len,k,fg,cnt,ans,fg2;
int main(){
int T,cas,i;
scanf("%d",&T);
getchar();
for(cas=1;cas<=T;cas++)
{
char s2[MAXN];
getline(cin,s1);
len=s1.length();
fg=0;
for(i=0;i<len;i++)
{
if(s1[i]=='c')
{
fg=1;
k=i;
break;
}
}
if(fg)
{
cnt=0;
for(i=k;i<len;i++)
{
s2[cnt++]=s1[i];
}
for(i=0;i<k;i++)
{
s2[cnt++]=s1[i];
}
}
else
{
ans=(len+1)/2;
printf("Case #%d: %d\n",cas,ans);
continue;
}
ans=1;
fg2=1;
int shuf=0;
for(i=1;i<len;i++)
{
if(s2[i]=='f')
{
shuf++;
}
else
{
if(s2[i]=='c')
{
if(shuf<2)
{
printf("Case #%d: -1\n",cas);
break;
}
else
{
ans++;
shuf=0;
}
}
else
{
printf("Case #%d: -1\n",cas);
break;
}
}
}
if(i==len&&shuf<2)
{
printf("Case #%d: -1\n",cas);
continue;
}
if(i==len)
{
printf("Case #%d: %d\n",cas,ans);
}
}
return 0;
}

77
HDOJ/5456_autoAC.cpp Normal file
View File

@ -0,0 +1,77 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <list>
#include <cstdlib>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cassert>
#define ALL(a) a.begin(), a.end()
#define clr(a, x) memset(a, x, sizeof a)
#define X first
#define Y second
#define pb push_back
#define lowbit(x) (x&(-x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define rep1(i,x,y) for(int i=x;i<=y;i++)
#define rep(i,n) for(int i=0;i<(int)n;i++)
using namespace std;
const double eps = 1e-6;
typedef long long LL;
typedef long long ll;
typedef pair<int, int> pii;
const int oo =0x3f3f3f3f;
const int N = 510;
const int cnt[]= {6,2,5,5,4,5,6,3,7,6};
ll d[N][2][8];
int M,n;
bool vis[N][2][8];
ll dp(int i,int j,int s)
{
if(vis[i][j][s]) return d[i][j][s];
vis[i][j][s] = 1;
d[i][j][s] = 0;
if(i == 0)
{
return d[i][j][s] = (j==0 && s==7);
}
rep(p,10) rep(q,10)
{
int nj = (p-j >= q ? 0 : 1);
int a = p, b = q , c = nj*10+p-j-q;
if((s&1) && c!=0) continue;
if((s&2) && b!=0) continue;
int nee = cnt[a]+((s&2) ? 0:cnt[b])+((s&1) ? 0 : cnt[c]);
if(nee > i) continue;
int vv[8]= {0};
rep(t,8)
{
int ts = 0;
if((t&1) && (c>0)) ts|=1;
if((t&2) && (b>0)) ts|=2;
if((t&4) && (a>0) && nj==0 && i==nee) ts|=4;
if(!vv[ts]) d[i][j][s] = (d[i][j][s]+dp(i-nee,nj,s|ts))%M,vv[ts]=1;
}
}
return d[i][j][s];
}
int main()
{
int T,kase=1;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&M);
memset(vis,false,sizeof(vis));
printf("Case #%d: %I64d\n",kase++,dp(n-3,0,0));
}
return 0;
}

340
HDOJ/5457_autoAC.cpp Normal file
View File

@ -0,0 +1,340 @@
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#include <functional>
#include <numeric>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
#define eps 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LLINF 1LL<<60
#define speed std::ios::sync_with_stdio(false);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef complex<ld> point;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef vector<int> vi;
#define CLR(x,y) memset(x,y,sizeof(x))
#define CPY(x,y) memcpy(x,y,sizeof(x))
#define clr(a,x,size) memset(a,x,sizeof(a[0])*(size))
#define cpy(a,x,size) memcpy(a,x,sizeof(a[0])*(size))
#define debug(a) cout << #a" = " << (a) << endl;
#define debugarry(a, n) for (int i = 0; i < (n); i++) { cout << #a"[" << i << "] = " << (a)[i] << endl; }
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define lowbit(x) (x&(-x))
#define MID(x,y) (x+((y-x)>>1))
#define ls (idx<<1)
#define rs (idx<<1|1)
#define lson ls,l,mid
#define rson rs,mid+1,r
template<class T>
inline bool read(T &n)
{
T x = 0, tmp = 1;
char c = getchar();
while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
if(c == EOF) return false;
if(c == '-') c = getchar(), tmp = -1;
while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
n = x*tmp;
return true;
}
template <class T>
inline void write(T n)
{
if(n < 0)
{
putchar('-');
n = -n;
}
int len = 0,data[20];
while(n)
{
data[len++] = n%10;
n /= 10;
}
if(!len) data[len++] = 0;
while(len--) putchar(data[len]+48);
}
const int MAXN=10240;
const int MAXM=40000;
struct ISAP
{
struct nedge
{
int u,v,next;
ll c;
} e[MAXM*2];
int head[MAXN],tot,n;
int ss,tt;
void Resize(int n)
{
this->n=n;
}
void Clearall()
{
tot=1;
CLR(head,0);
}
void addedge(int u,int v,ll w)
{
e[++tot].v=v;
e[tot].u=u;
e[tot].c=w;
e[tot].next=head[u];
head[u]=tot;
e[++tot].v=u;
e[tot].u=v;
e[tot].c=0;
e[tot].next=head[v];
head[v]=tot;
}
int dep[MAXN],gap[MAXN];
void bfs()
{
CLR(dep,-1);
CLR(gap,0);
queue<int> que;
gap[0]=1;
dep[tt]=0;
que.push(tt);
while(!que.empty())
{
int u=que.front();
que.pop();
for(int i=head[u]; i; i=e[i].next)
if(dep[e[i].v]==-1)
{
que.push(e[i].v);
dep[e[i].v]=dep[u]+1;
++gap[dep[e[i].v]];
}
}
}
int res,cur[MAXN],num[MAXN];
int top;
int MaxFlow(int s,int t)
{
this->ss=s;
this->tt=t;
bfs();
top=res=0;
memcpy(cur,head,sizeof(head));
int u=ss,i;
while(dep[ss]<n)
{
if(u==tt)
{
int temp=INF;
int inser;
for(i=0; i<top; i++)
if(temp>e[num[i]].c)
{
temp=e[num[i]].c;
inser=i;
}
for(i=0; i<top; i++)
{
e[num[i]].c-=temp;
e[num[i]^1].c+=temp;
}
res+=temp;
top=inser;
u=e[num[top]].u;
}
if(u!=tt && gap[dep[u]-1]==0)
break;
for(i=cur[u]; i; i=e[i].next)
if(e[i].c != 0 && dep[u]==dep[e[i].v]+1)
break;
if(i)
{
cur[u]=i;
num[top++]=i;
u=e[i].v;
}
else
{
int mi=n;
for(i=head[u]; i; i=e[i].next)
if(e[i].c>0 && mi>dep[e[i].v])
{
mi=dep[e[i].v];
cur[u]=i;
}
--gap[dep[u]];
dep[u]=mi+1;
++gap[dep[u]];
if(u!=ss)
u=e[num[--top]].u;
}
}
return res;
}
void print()
{
puts("GRAPH:");
for(int i=0; i<=n; i++)
{
if(!head[i])
continue;
printf("%d ->",i);
for(int j=head[i]; j; j=e[j].next)
printf("%d(%I64d) ",e[j].v,e[j].c);
putchar('\n');
}
}
}g;
static int tim;
struct Node
{
Node *ln[2];
int cost[2],idx;
bool f;
void init()
{
f=0;
idx=tim++;
memset(ln,0,sizeof ln);
memset(cost,INF,sizeof cost);
}
};
struct Trie
{
Node T[MAXN];
int cnt,pos[MAXN];
Node *rt;
Node* get()
{
T[cnt].init();
return &T[cnt++];
}
void init()
{
cnt=0;
rt=get();
}
void insert(int *s,int j)
{
Node *p=rt;
for(int i=0; i<8; i++)
{
int v=s[i];
if(p->ln[v]==NULL) p->ln[v]=get();
p=p->ln[v];
}
p->f=1;
pos[j]=p->idx;
}
void find(char *s,int cost)
{
Node *p=rt;
for(int i=0; s[i]; i++)
{
int v=s[i]-'0';
if(p->ln[v]==NULL) return;
if(s[i+1]==0) p->cost[v]=min(p->cost[v],cost);
else p=p->ln[v];
}
}
} pre,su;
int n,m,ss,tt;
int a[9];
char str[9],x[9];
void dfs(Node *p,int flag)
{
if(flag)
{
if(p->ln[0])
{
g.addedge(p->ln[0]->idx,p->idx,p->cost[0]);
dfs(p->ln[0],1);
}
if(p->ln[1])
{
g.addedge(p->ln[1]->idx,p->idx,p->cost[1]);
dfs(p->ln[1],1);
}
}
else
{
if(p->ln[0])
{
g.addedge(p->idx,p->ln[0]->idx,p->cost[0]);
dfs(p->ln[0],0);
}
if(p->ln[1])
{
g.addedge(p->idx,p->ln[1]->idx,p->cost[1]);
dfs(p->ln[1],0);
}
}
}
void build()
{
for(int i=1; i<=n; i++)
g.addedge(pre.pos[i],su.pos[i],INF);
dfs(pre.rt,0);
dfs(su.rt ,1);
}
int main()
{
int T,cas=1;
read(T);
while(T--)
{
scanf("%d %d",&n,&m);
tim=0;
pre.init();su.init();g.Clearall();
for(int i=1,x; i<=n; i++)
{
scanf("%d",&x);
for(int j=0; j<8; j++)
if(x&(1<<7-j)) a[j]=1;
else a[j]=0;
pre.insert(a,i);
for(int j=0; j<8; j++)
if(x&(1<<j)) a[j]=1;
else a[j]=0;
su.insert(a,i);
}
g.Resize(tim);
ss=0;
tt=su.rt->idx;
for(int i=1,cost; i<=m; i++)
{
scanf("%s %s %d",str,x,&cost);
if(str[0]=='P') pre.find(x,cost);
else
{
int len=strlen(x);
reverse(x,x+len);
su.find(x,cost);
}
}
build();
ll ans=g.MaxFlow(ss,tt);
printf("Case #%d: %lld\n",cas++,ans>=INF?-1:ans);
}
return 0;
}

310
HDOJ/5458_autoAC.cpp Normal file
View File

@ -0,0 +1,310 @@
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#include <functional>
#include <numeric>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
#define eps 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LLINF 1LL<<62
#define speed std::ios::sync_with_stdio(false);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef complex<ld> point;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef vector<int> vi;
#define CLR(x,y) memset(x,y,sizeof(x))
#define CPY(x,y) memcpy(x,y,sizeof(x))
#define clr(a,x,size) memset(a,x,sizeof(a[0])*(size))
#define cpy(a,x,size) memcpy(a,x,sizeof(a[0])*(size))
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define lowbit(x) (x&(-x))
#define MID(x,y) (x+((y-x)>>1))
#define getidx(l,r) (l+r | l!=r)
#define ls getidx(l,mid)
#define rs getidx(mid+1,r)
#define lson l,mid
#define rson mid+1,r
#define root 1,n
template<class T>
inline bool read(T &n)
{
T x = 0, tmp = 1;
char c = getchar();
while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
if(c == EOF) return false;
if(c == '-') c = getchar(), tmp = -1;
while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
n = x*tmp;
return true;
}
template <class T>
inline void write(T n)
{
if(n < 0)
{
putchar('-');
n = -n;
}
int len = 0,data[20];
while(n)
{
data[len++] = n%10;
n /= 10;
}
if(!len) data[len++] = 0;
while(len--) putchar(data[len]+48);
}
const int MAXN=30005;
const int MAXM=100005;
const int MAXQ=100005;
int t,n,m,q;
struct edge
{
int a,b;
edge(){}
edge(int aa,int bb):a(aa),b(bb){}
bool operator < (const edge &o) const
{
if(a == o.a)
{
return b < o.b;
}
return a < o.a;
}
};
multiset<edge> S;
multiset<edge> V;
struct Ans
{
int kind;
int a;
int b;
int val;
}ans[MAXQ];
struct Edge
{
int u,v,w;
int next;
}e[MAXN<<1];
int head[MAXN],tot;
int siz[MAXN],p[MAXN],son[MAXN],dep[MAXN];
int dfn[MAXN],top[MAXN],tim;
int sum[MAXN<<1],la[MAXN<<1];
int bingchafa[MAXN];
int getfather(int x)
{
if(bingchafa[x] == x) return x;
return bingchafa[x] = getfather(bingchafa[x]);
}
void init()
{
CLR(head,-1);CLR(sum,0);
tot=tim=0;
for(int i=1;i<=n;i++) bingchafa[i] = i;
S.clear();
V.clear();
}
void addedge(int u,int v,int w)
{
e[tot].u=u;e[tot].v=v;e[tot].w=w;
e[tot].next=head[u];head[u]=tot++;
}
void dfs(int u,int fa=0,int depth=0)
{
siz[u]=1;son[u]=-1;
p[u]=fa;dep[u]=depth;
for(int i=head[u];~i;i=e[i].next)
{
int v=e[i].v;
if(v==p[u]) continue;
dfs(v,u,depth+1);
siz[u]+=siz[v];
if(son[u]==-1 || siz[v]>siz[son[u]])
son[u]=v;
}
}
void getid(int u,int fa)
{
dfn[u]=++tim;top[u]=fa;
if(son[u]!=-1)
getid(son[u],fa);
for(int i=head[u];~i;i=e[i].next)
{
int v=e[i].v;
if(v==p[u] || v==son[u]) continue;
getid(v,v);
}
}
void pushup(int l,int r)
{
int idx=getidx(l,r),mid=MID(l,r);
sum[idx]=0;
if(!la[ls])
sum[idx]+=sum[ls];
if(!la[rs])
sum[idx]+=sum[rs];
}
void pushdown(int l,int r)
{
int idx=getidx(l,r),mid=MID(l,r);
if(!la[idx]) return;
la[ls]=la[idx];
la[rs]=la[idx];
}
void build(int l,int r)
{
int idx=getidx(l,r);
la[idx]=0;
if(l==r)
{
sum[idx]=1;
return;
}
int mid=MID(l,r);
build(l,mid);
build(mid+1,r);
pushup(l,r);
}
void update2(int l,int r,int L,int R)
{
int idx=getidx(l,r);
if(L==l && r==R)
{
la[idx]=1;
sum[idx]=0;
return;
}
pushdown(l,r);
int mid=MID(l,r);
if(R<=mid)
update2(l,mid,L,R);
else if(L>mid)
update2(mid+1,r,L,R);
else
update2(l,mid,L,mid),update2(mid+1,r,mid+1,R);
pushup(l,r);
}
int query(int l,int r,int L,int R)
{
int idx=getidx(l,r);
if(la[idx]) return 0;
if(L==l && r==R)
{
return sum[idx];
}
int mid=MID(l,r);
if(R<=mid)
return query(lson,L,R);
if(L>mid)
return query(rson,L,R);
return query(lson,L,mid)+query(rson,mid+1,R);
}
void change(int u,int v)
{
while(top[u]!=top[v])
{
if(dep[top[u]]>dep[top[v]])
swap(u,v);
update2(1,tim,dfn[top[v]],dfn[v]);
v=p[top[v]];
}
if(u==v) return;
if(dep[u]>dep[v])
swap(u,v);
update2(1,tim,dfn[son[u]],dfn[v]);
}
int query(int u,int v)
{
int ans=0;
while(top[u]!=top[v])
{
if(dep[top[u]]>dep[top[v]])
swap(u,v);
ans+=query(1,tim,dfn[top[v]],dfn[v]);
v=p[top[v]];
}
if(u==v) return ans;
if(dep[u]>dep[v])
swap(u,v);
ans+=query(1,tim,dfn[u]+1,dfn[v]);
return ans;
}
int main()
{
scanf("%d",&t);
for(int cas = 1;cas <= t;cas++)
{
scanf("%d%d%d",&n,&m,&q);
init();
for(int i=1;i<=m;i++)
{
edge tmp;
scanf("%d%d",&tmp.a,&tmp.b);
if(tmp.a > tmp.b) swap(tmp.a,tmp.b);
S.insert(tmp);
}
for(int i=1;i<=q;i++)
{
scanf("%d%d%d",&ans[i].kind,&ans[i].a,&ans[i].b);
if(ans[i].a > ans[i].b) swap(ans[i].a,ans[i].b);
if(ans[i].kind == 1)
{
multiset<edge>::iterator Pos = S.find(edge(ans[i].a,ans[i].b));
S.erase(Pos);
}
}
for(multiset<edge>::iterator It = S.begin();It != S.end();++It)
{
if(getfather(It->a) != getfather(It->b))
{
bingchafa[getfather(It->a)] = getfather(It->b);
V.insert(*It);
}
}
for(multiset<edge>::iterator It = V.begin();It != V.end();++It)
{
addedge(It->a,It->b,1);
addedge(It->b,It->a,1);
}
dfs(1);getid(1,1);
build(1,tim);
for(multiset<edge>::iterator It = S.begin();It != S.end();++It)
if(V.find(*It) == V.end())
{
change(It->a,It->b);
}
for(int i=q;i>=1;i--)
{
if(ans[i].kind == 1)
change(ans[i].a,ans[i].b);
else if(ans[i].kind == 2)
ans[i].val = query(ans[i].a,ans[i].b);
}
printf("Case #%d:\n",cas);
for(int i=1;i<=q;i++)
if(ans[i].kind == 2)
printf("%d\n",ans[i].val);
}
return 0;
}

46
HDOJ/5459_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
using namespace std;
const int maxn = 201315;
const int mod = 530600414;
typedef long long LL;
LL dp[maxn];
LL dist[maxn];
LL cnt[maxn];
LL fibo[maxn];
void init() {
fibo[1] = 1, fibo[2] = 2;
cnt[1] = 1, cnt[2] = 0;
for (int i = 3; i < maxn; i ++) {
fibo[i] = (fibo[i-1] % mod + fibo[i-2] % mod) % mod;
cnt[i] = (cnt[i-1] % mod + cnt[i-2] % mod) % mod;
}
dist[1] = dist[2] = 0, dist[3] = 2;
for (int i = 4; i < maxn; i ++) {
dist[i] = (dist[i-1] % mod + dist[i-2] % mod + (fibo[i-1] % mod * cnt[i-2] % mod) % mod) % mod;
}
dp[4] = dp[1] = dp[2] = dp[3] = 0;
for (int i = 5; i < maxn; i ++) {
dp[i] = (dp[i-2] % mod + dp[i-1] % mod
+ (cnt[i-1] % mod * dist[i-2] % mod) % mod
+ (cnt[i-2] % mod * ((fibo[i-1] % mod * cnt[i-1] % mod) % mod - dist[i-1] % mod) % mod) % mod) % mod;
}
}
int main() {
init();
int cas = 1, t, n;
scanf("%d", &t);
while (t --) {
scanf("%d", &n);
printf("Case #%d: ", cas ++);
printf("%I64d\n", dp[n]);
}
return 0;
}

59
HDOJ/5460_autoAC.cpp Normal file
View File

@ -0,0 +1,59 @@
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
const int maxn = (1<<8)+5;
const int limit = 32 * 13 * 13;
typedef long long ll;
typedef map<int,ll>::iterator iter;
int N, Q, X[20], multi[maxn], ct[maxn];
map<int, ll> G[maxn];
int bitcount(int s) { return s == 0 ? 0 : bitcount(s>>1) + (s&1); }
void add (int u, int s, ll k) {
if (s < 0) return;
G[u][s] += k;
}
void merge(int u, int p, int q) {
for (iter i = G[p].begin(); i != G[p].end(); i++) {
for (iter j = G[q].begin(); j != G[q].end(); j++) {
ll k = 1LL * i->second * j->second;
add(u, i->first + j->first, k * 2);
add(u, i->first * j->first, k * 2);
add(u, i->first - j->first, k);
add(u, j->first - i->first, k);
if (i->first && j->first % i->first == 0)
add(u, j->first / i->first, k);
if (j->first && i->first % j->first == 0)
add(u, i->first / j->first, k);
}
}
}
ll solve () {
ll ret = 0;
for (int i = 1; i < (1<<N); i++) {
int cbit = bitcount(i);
for (int u = i; u; u = (u-1)&i) {
int v = u^i;
if (u < v) break;
merge(i, u, v);
}
if (G[i].count(Q))
ret += G[i][Q] * cbit * cbit;
}
return ret;
}
int main () {
int cas;
scanf("%d", &cas);
for (int kcas = 1; kcas <= cas; kcas++) {
scanf("%d%d", &N, &Q);
for (int i = 0; i < (1<<N); i++) G[i].clear();
for (int i = 0; i < N; i++) {
scanf("%d", &X[i]);
G[1<<i][X[i]] = 1;
}
printf("Case #%d: %lld\n", kcas, solve());
}
return 0;
}

47
HDOJ/5461_autoAC.cpp Normal file
View File

@ -0,0 +1,47 @@
#include<iostream>
#include<cstdio>
#include<utility>
#include<algorithm>
using namespace std;
typedef long long LL;
typedef pair<LL,int> PLL;
PLL x[5],y[5];
bool cmp1(const PLL &x,const PLL &y) {return x.first>y.first;}
bool cmp2(const PLL &x,const PLL &y) {return x.first<y.first;}
int main()
{
int T;
scanf("%d",&T);
int n,a,b;
LL z;
for(int k=1;k<=T;k++)
{
scanf("%d%d%d",&n,&a,&b);
for(int i=0;i<n;i++)
{
scanf("%lld",&z);
x[4]=make_pair(z*z,i);
y[4]=make_pair(z,i);
if(i<=4)
{
x[i]=x[4];
y[i]=y[4];
}else
{
if(a>0)
sort(x,x+5,cmp1);
else sort(x,x+5,cmp2);
if(b>0)
sort(y,y+5,cmp1);
else sort(y,y+5,cmp2);
}
}
LL ans=-1e20;
for(int i=0;i<min(n,2);i++)
for(int j=0;j<min(n,2);j++)
if(x[i].second!=y[j].second)
ans=max(ans,x[i].first*a+b*y[j].first);
printf("Case #%d: %lld\n",k,ans);
}
return 0;
}

204
HDOJ/5462_autoAC.cpp Normal file
View File

@ -0,0 +1,204 @@
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
const int maxn = 1005;
const double eps = 1e-7;
struct Point
{
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) { }
bool operator < (const Point &a) const {
return a.x < x || (a.x == x && a.y < y);
}
};
typedef Point Vector;
struct Line
{
Point P;
Vector v;
double ang;
Line() {}
Line(Point P, Vector v) : P(P), v(v) { ang = atan2(v.y, v.x); }
bool operator < (const Line &L) const {
return ang < L.ang;
}
};
Vector operator + (Vector A, Vector B)
{
return Vector(A.x + B.x, A.y + B.y);
}
Vector operator - (Vector A, Vector B)
{
return Vector(A.x - B.x, A.y - B.y);
}
Vector operator * (Vector A, double p)
{
return Vector(A.x * p, A.y * p);
}
Vector operator / (Vector A, double p)
{
return Vector(A.x / p, A.y / p);
}
int dcmp(double x)
{
if(fabs(x) < eps) return 0;
else return x < 0 ? -1 : 1;
}
double Dot(Vector A, Vector B)
{
return A.x * B.x + A.y * B.y;
}
double Length(Vector A)
{
return sqrt(Dot(A, A));
}
double Angle(Vector A, Vector B)
{
return acos(Dot(A, B) / Length(A) / Length(B));
}
double Cross(Vector A, Vector B)
{
return A.x * B.y - A.y * B.x;
}
double Area2(Point A, Point B, Point C)
{
return Cross(B - A, C - A);
}
double PolyonArea(Point *p, int n)
{
double area = 0;
for(int i = 1; i < n-1; i++)
area += Cross(p[i] - p[0], p[i+1] - p[0]);
return area / 2;
}
Vector Rotate(Vector A, double rad)
{
return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
}
Vector Normal(Vector A)
{
double L = Length(A);
return Vector(-A.y / L, A.x / L);
}
bool OnLeft(Line L, Point p)
{
return Cross(L.v, p - L.P) > 0;
}
Point GetIntersection(Line a, Line b)
{
Vector u = a.P - b.P;
double t = Cross(b.v, u) / Cross(a.v, b.v);
return a.P + a.v * t;
}
int ConvexHull(Point *p, int n, Point *ch)
{
sort(p, p+n);
int m = 0;
for(int i = 0; i < n; i++) {
while(m > 1 && Cross(ch[m-1] - ch[m-2], p[i] - ch[m-2]) <= 0) m--;
ch[m++] = p[i];
}
int k = m;
for(int i = n-2; i >= 0; i--) {
while(m > k && Cross(ch[m-1] - ch[m-2], p[i] - ch[m-2]) <= 0) m--;
ch[m++] = p[i];
}
if(n > 1) m--;
return m;
}
Point p[maxn];
Line q[maxn];
int HalfplaneInersection(Line* L, int n, Point* poly)
{
sort(L, L+n);
int first, last;
q[first = last = 0] = L[0];
for(int i = 1; i < n; i++) {
while(first < last && !OnLeft(L[i], p[last - 1])) last--;
while(first < last && !OnLeft(L[i], p[first])) first++;
q[++last] = L[i];
if(fabs(Cross(q[last].v, q[last-1].v)) < eps) {
last--;
if(OnLeft(q[last], L[i].P)) q[last] = L[i];
}
if(first < last) p[last-1] = GetIntersection(q[last-1], q[last]);
}
while(first < last && !OnLeft(q[first], p[last-1])) last--;
if(last - first <= 1) return 0;
p[last] = GetIntersection(q[last], q[first]);
int m = 0;
for(int i = first; i <= last; i++) poly[m++] = p[i];
return m;
}
double PolygonArea(Point *p, int n) {
double area = 0;
for(int i = 1; i < n-1; i++)
area += Cross(p[i] - p[0], p[i+1] - p[0]);
return fabs(area / 2);
}
Point poly[1005];
Point g[105][2005];
Point P2[10];
Line line[105];
double twox[105][2005];
double twoy[105][2005];
double xx[105][2005];
double yy[105][2005];
int n, m;
void work()
{
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++) {
twox[i][0] = twoy[i][0] = xx[i][0] = yy[i][0] = 0;
for(int j = 1; j <= m; j++) {
scanf("%lf%lf", &g[i][j].x, &g[i][j].y);
twox[i][j] = g[i][j].x * g[i][j].x;
twoy[i][j] = g[i][j].y * g[i][j].y;
twox[i][j] += twox[i][j-1];
twoy[i][j] += twoy[i][j-1];
xx[i][j] = g[i][j].x + xx[i][j-1];
yy[i][j] = g[i][j].y + yy[i][j-1];
}
}
for(int i = 1; i <= n; i++) {
int cnt = 0;
for(int j = 1; j <= n; j++) {
if(i == j) continue;
double a = 0, b = 0, c = 0;
a = 2 * (xx[i][m] - xx[j][m]);
b = 2 * (yy[i][m] - yy[j][m]);
c += twox[j][m] + twoy[j][m];
c -= twox[i][m] + twoy[i][m];
Point P;
Vector v(b, -a);
if(fabs(a) > fabs(b)) P = Point(-c / a, 0);
else P = Point(0, -c / b);
line[cnt++] = Line(P, v);
}
for(int j = 0; j < 4; j++) {
line[cnt++] = Line(P2[j], P2[(j+1)%4] - P2[j]);
}
int t = HalfplaneInersection(line, cnt, poly);
double area = PolygonArea(poly, t);
int ans = area + 0.5;
printf("%d%c", ans, i == n ? '\n' : ' ');
}
}
int main()
{
P2[0] = Point(0, 0);
P2[1] = Point(4095, 0);
P2[2] = Point(4095, 4095);
P2[3] = Point(0, 4095);
int _;
scanf("%d", &_);
for(int i = 1; i <= _; i++) {
printf("Case #%d: ", i);
work();
}
return 0;
}

34
HDOJ/5463_autoAC.cpp Normal file
View File

@ -0,0 +1,34 @@
#include<cstdio>
#include<cstring>
const int maxn=1100;
int T;
int n,m,r;
int vis[maxn];
int main()
{
scanf("%d",&T);
while(T--){
scanf("%d",&n);
memset(vis,0,sizeof(vis));
int ans=0;
for(int i=0;i<n;i++){
scanf("%d %d",&r,&m);
vis[r]+=m;
}
for(int i=0;i<=500;i++){
if(vis[i]>0){
if(vis[i]%64==0){
ans+=vis[i]/64;
}
else{
ans+=vis[i]/64+1;
}
}
}
if(!ans%36)
printf("%d\n",ans/36);
else
printf("%d\n",ans/36+1);
}
return 0;
}

31
HDOJ/5464_autoAC.cpp Normal file
View File

@ -0,0 +1,31 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
const int N = 1005;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
int n, p, a[N], f[N][N];
int main() {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &p);
memset(f, 0, sizeof(f));
f[0][0] = 1;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
a[i] %= p;
a[i] = (a[i] + p) % p;
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < p; j++) {
f[i][j] = (f[i][j] + f[i - 1][j]) % MOD;
f[i][(j + a[i]) % p] = (f[i][(j + a[i]) % p] + f[i - 1][j]) % MOD;
}
}
printf("%d\n", f[n][0]);
}
return 0;
}

117
HDOJ/5465_autoAC.cpp Normal file
View File

@ -0,0 +1,117 @@
#pragma comment(linker, "/STACK:1677721600")
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cassert>
#include <iostream>
#include <algorithm>
#define pb push_back
#define mp make_pair
#define LL long long
#define lson lo,mi,rt<<1
#define rson mi+1,hi,rt<<1|1
#define Min(a,b) ((a)<(b)?(a):(b))
#define Max(a,b) ((a)>(b)?(a):(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(int i=(a); i<=(b); i++)
#define dec(i,a,b) for(int i=(a); i>=(b); i--)
using namespace std;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double ee = exp(1.0);
const int inf = 0x3f3f3f3f;
const int maxn = 500 + 10;
const double pi = acos(-1.0);
const LL iinf = 0x3f3f3f3f3f3f3f3f;
int readT()
{
char c;
int ret = 0,flg = 0;
while(c = getchar(), (c < '0' || c > '9') && c != '-');
if(c == '-') flg = 1; else ret = c ^ 48;
while( c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c ^ 48);
return flg ? - ret : ret;
}
int c[maxn][maxn];
int a[maxn][maxn];
int n, m;
int lowbit(int x)
{
return x & -x;
}
void update(int x, int y, int num)
{
for (int i = x; i <= n; i += lowbit(i))
{
for (int j = y; j <= m; j += lowbit(j))
{
c[i][j] ^= num;
}
}
}
int query(int x, int y)
{
int res = 0;
int t = x;
for (int i = x; i > 0; i -= lowbit(i))
{
for (int j = y; j > 0; j -= lowbit(j))
{
res ^= c[i][j];
}
}
return res;
}
int sum(int x0, int y0, int x1, int y1)
{
x0--, y0--;
return query(x1, y1) ^ query(x0, y1) ^ query(x1, y0) ^ query(x0, y0);
}
int main()
{
int ncase = readT();
while (ncase--)
{
mem(c, 0);
n = readT();
m = readT();
int q = readT();
rep(i, 1, n)
{
rep(j, 1, m)
{
a[i][j] = readT();
update(i, j, a[i][j]);
}
}
while (q--)
{
int op = readT();
if (op == 1)
{
int x1 = readT();
int y1 = readT();
int x2 = readT();
int y2 = readT();
int ans = sum(x1, y1, x2, y2);
puts(ans ? "Yes" : "No");
}
else
{
int x = readT();
int y = readT();
int num =readT();
update(x, y, num ^ a[x][y]);
a[x][y] = num;
}
}
}
return 0;
}

236
HDOJ/5467_autoAC.cpp Normal file
View File

@ -0,0 +1,236 @@
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <vector>
using namespace std;
typedef long long LL;
#define mp(x, y) make_pair(x, y)
const int maxn = 300005;
const int maxm = 300005;
const int INF = 0x3f3f3f3f;
const int mod = 1e9+7;
struct node *null;
struct node
{
int size, rev;
LL val, sum;
node *fa, *ch[2];
void init()
{
rev = val = sum = 0;
fa = ch[0] = ch[1] = null;
}
inline bool d()
{
return fa->ch[1] == this;
}
inline bool isroot()
{
return fa == null || fa->ch[0] != this && fa->ch[1] != this;
}
inline void setc(node *p, int d)
{
ch[d] = p;
p->fa = this;
}
inline void pushup()
{
sum = ch[0]->sum * ch[1]->sum % mod * ((val - 1) * val / 2) % mod;
}
inline void flip()
{
if(this == null) return;
swap(ch[0], ch[1]);
rev ^= 1;
}
inline void pushdown()
{
if(rev) {
ch[0]->flip();
ch[1]->flip();
rev = 0;
}
}
inline void go()
{
if(!isroot()) fa->go();
pushdown();
}
void rot()
{
node *f = fa, *ff = fa->fa;
int c = d(), cc = fa->d();
f->setc(ch[!c], c);
this->setc(f, !c);
if(ff->ch[cc] == f) ff->setc(this, cc);
else this->fa = ff;
f->pushup();
}
node* splay()
{
go();
while(!isroot()) {
if(!fa->isroot()) d() == fa->d() ? fa->rot() : rot();
rot();
}
pushup();
return this;
}
node* access()
{
for(node *p = this, *q = null; p != null; q = p, p = p->fa) {
p->splay()->setc(q, 1);
p->pushup();
}
return splay();
}
inline void makeroot()
{
access()->flip();
}
node* getroot() {
node *x;
for(x = access(); x->pushdown(), x->ch[0] != null; x = x->ch[0]);
return x;
}
void cut()
{
access();
ch[0]->fa = null;
ch[0] = null;
pushup();
}
void cut(node *o)
{
makeroot();
o->cut();
}
void link(node *p)
{
makeroot();
fa = p;
}
}*Node[maxn], pool[maxm], *tail;
struct ope
{
int kk, u, v;
ope(int kk = 0, int u = 0, int v = 0) : kk(kk), u(u), v(v) {}
}op[maxn];
struct Edge
{
int v;
Edge *next;
}*H[maxn], *edges, E[maxn];
vector<int> to[maxn];
LL res[maxn];
int T[maxn];
int a[maxn];
int n, m;
void addedges(int u, int v)
{
edges->v = v;
edges->next = H[u];
H[u] = edges++;
}
node* newnode(int val)
{
tail->init();
tail->val = val;
tail->sum = tail->val * (tail->val - 1) / 2;
return tail++;
}
void init()
{
tail = pool;
null = newnode(1);
null->sum = 1;
edges = E;
memset(H, 0, sizeof H);
memset(T, 0, sizeof T);
memset(res, -1, sizeof res);
}
bool add(int u, int v)
{
if(u == v) return 0;
if(Node[u]->getroot() == Node[v]->getroot()) return 0;
Node[u]->link(Node[v]);
return true;
}
bool del(int u, int v)
{
if(u == v) return 0;
if(Node[u]->getroot() != Node[v]->getroot()) return 0;
Node[u]->makeroot();
Node[v]->access();
if(Node[v]->ch[0] != Node[u] || Node[u]->ch[1] != null) return 0;
else Node[v]->cut(Node[u]);
return 1;
}
LL query(node *a, node *b)
{
a->access();
for(a = null; b != null; a = b, b = b->fa) {
b->splay();
if(b->fa == null) return b->ch[1]->sum * a->sum % mod * (b->val * (b->val - 1) / 2) % mod;
b->setc(a, 1);
b->pushup();
}
}
void modify(int u, LL val)
{
Node[u]->access();
Node[u]->val = val;
Node[u]->pushup();
}
void dfs(int ti)
{
LL tmp = 0;
int flag = 0;
if(op[ti].kk == 1) flag = add(op[ti].u, op[ti].v);
if(op[ti].kk == 2) flag = del(op[ti].u, op[ti].v);
if(op[ti].kk == 4) {
if(Node[op[ti].u]->getroot() != Node[op[ti].v]->getroot()) res[ti] = 0;
else res[ti] = query(Node[op[ti].u], Node[op[ti].v]);
}
if(op[ti].kk == 5) {
tmp = a[op[ti].u];
a[op[ti].u] = op[ti].v;
modify(op[ti].u, op[ti].v);
}
for(Edge *e = H[ti]; e; e = e->next) dfs(e->v);
if(op[ti].kk == 2 && flag) add(op[ti].u, op[ti].v);
if(op[ti].kk == 1 && flag) del(op[ti].u, op[ti].v);
if(op[ti].kk == 5) {
a[op[ti].u] = tmp;
modify(op[ti].u, tmp);
}
}
void work()
{
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++) to[i].clear();
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
for(int i = 1; i <= n; i++) Node[i] = newnode(a[i]);
for(int i = 1; i <= m; i++) {
int kk, u, v;
scanf("%d", &kk);
if(kk == 3) scanf("%d", &u), T[i] = u;
else scanf("%d%d", &u, &v), T[i] = i-1;
op[i] = ope(kk, u, v);
}
for(int i = 1; i <= m; i++) addedges(T[i], i);
dfs(0);
for(int i = 1; i <= m; i++) if(res[i] != -1) printf("%lld\n", res[i]);
}
int main()
{
int _;
scanf("%d", &_);
while(_--) {
init();
work();
}
return 0;
}

166
HDOJ/5468_autoAC.cpp Normal file
View File

@ -0,0 +1,166 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<sstream>
#include<string>
#include<bitset>
using namespace std;
typedef long long LL;
const LL LINF = (1LL <<63);
const int INF = 1 << 31;
const int NS = 100010;
const int MS = 19;
const int MOD = 1000000007;
const int EDGE_MAX = NS;
struct graphEdge
{
int pst;
int next;
};
struct ForwardStart
{
int top;
int head[EDGE_MAX];
graphEdge edge[EDGE_MAX << 1];
void init(int len)
{
top = 0;
memset(head, -1, sizeof(int) * len);
}
void addEdge(int u, int v)
{
edge[top].pst = v;
edge[top].next = head[u];
head[u] = top++;
}
void printAll()
{
printf("top = %d\n", top);
for(int i = 1; i < EDGE_MAX; i++)
{
if(-1 != head[i])
{
printf("head[%2d]'son:%2d", i, edge[head[i]].pst);
for(int j = edge[head[i]].next; j != -1; j = edge[j].next)
{
printf(",%2d", edge[j].pst);
}
puts("");
}
}
}
}cTree;
bitset<NS> isPrime;
vector<int> fac[NS];
int miu[NS];
void prepare()
{
isPrime.set();
isPrime[1] = false;
miu[1] = 1;
for(int i = 2; i < NS; i++)
{
if(isPrime[i])
{
for(int j = i; j < NS; j+=i)
{
isPrime[j] = false;
int k = j / i;
if(k % i)
{
miu[j] = -miu[k];
}
else
{
miu[j] = 0;
}
fac[j].push_back(i);
}
}
else
{
if(miu[i] != 0)
{
for(int j = i; j < NS; j+=i)
{
fac[j].push_back(i);
}
}
}
}
}
int n;
int val[NS];
int ans[NS];
int sz[NS];
int dp[NS];
void dfs(int rt, int fa)
{
vector<int> temp;
sz[rt] = 1;
int value = val[rt];
int len = fac[value].size();
for(int i = 0; i < len; i++)
{
int d = fac[value][i];
int cnt = dp[d];
temp.push_back(cnt);
dp[d] += 1;
}
for(int i = cTree.head[rt]; i != -1; i = cTree.edge[i].next)
{
int cson = cTree.edge[i].pst;
if(cson == fa) continue;
dfs(cson, rt);
sz[rt] += sz[cson];
}
ans[rt] = sz[rt];
for(int i = 0; i < len; i++)
{
int d = fac[value][i];
int cnt = dp[d] - temp[i];
if(cnt > 0)
{
ans[rt] += miu[d] * cnt;
}
}
return ;
}
int main()
{
prepare();
int nCase = 1;
while(~scanf("%d", &n))
{
cTree.init(n + n + 2);
int u, v;
for(int i = 1; i < n; i++)
{
scanf("%d %d", &u, &v);
cTree.addEdge(u, v);
cTree.addEdge(v, u);
}
for(int i = 1; i <= n; i++)
{
scanf("%d", &val[i]);
if(val[i] < 0)
{
val[i] = - val[i];
}
}
memset(dp, 0, sizeof(dp));
dfs(1, -1);
printf("Case #%d:", nCase++);
for(int i = 1; i <= n; i++)
{
printf(" %d", ans[i]);
}
printf("\n");
}
return 0;
}

77
HDOJ/5469_autoAC.cpp Normal file
View File

@ -0,0 +1,77 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
#define N 10010
vector<int>G[N];
int n,len;
char value[N],an[N];
int dis[N];
int f[N];
bool visit[N];
void init() {
for(int i = 1; i <= n; i++) G[i].clear();
memset(dis,0,sizeof(dis));
memset(visit,0,sizeof(visit));
}
void dfs(int u) {
int ma = -10000000;
visit[u] = 1;
for(int i = 0; i < G[u].size(); i++)
if(!visit[G[u][i]]) {
dfs(G[u][i]);
f[G[u][i]] = u;
ma = max(ma,dis[G[u][i]]);
}
if(ma == -10000000)
dis[u] = 1;
else
dis[u] = ma+1;
}
bool dfs1(int u,int x,int len1) {
visit[u] = 1;
if(x > len) return 1;
if(dis[u] > len1)
for(int i = 0; i < G[u].size(); i++) {
if(value[G[u][i]] == an[x] && !visit[G[u][i]]) {
if(dfs1(G[u][i],x+1,len1-1)) return 1;
}
}
else {
if(value[f[u]] == an[x] && !visit[f[u]]) {
return dfs1(f[u],x+1,len1-1);
}
}
return 0;
}
int main() {
int t;scanf("%d",&t);
int ca = 1;
while(t--) {
scanf("%d",&n);
init();
int x,y;
for(int i = 1; i < n; i++) {
scanf("%d%d",&x,&y);
G[x].push_back(y);
G[y].push_back(x);
}
scanf("%s",value+1);
scanf("%s",an+1);
dfs(1); bool flag = 0;
len = strlen(an+1);
for(int i = 1; i <= n; i++)
if(value[i] == an[1]) {
memset(visit,0,sizeof(visit));
if(dfs1(i,2,len-1)) {
printf("Case #%d: Find\n",ca++);
flag = 1;
break;
}
}
if(!flag) printf("Case #%d: Impossible\n",ca++);
}
return 0;
}

114
HDOJ/5470_autoAC.cpp Normal file
View File

@ -0,0 +1,114 @@
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
const int N = 100000;
const int C = 26;
char s[N + 1];
struct State {
int length;
int first;
State* parent;
State* go[C];
State* extend(State*, int);
};
int state_count;
State states[N << 1];
State* new_state(int length) {
State* state = states + (state_count ++);
state->length = length;
state->first = -1;
state->parent = NULL;
memset(state->go, 0, sizeof(state->go));
return state;
}
State* State::extend(State* start, int token) {
State *p = this;
State *np = new_state(length + 1);
while (p && !p->go[token]) {
p->go[token] = np;
p = p->parent;
}
if (!p) {
np->parent = start;
} else {
State *q = p->go[token];
if (p->length + 1 == q->length) {
np->parent = q;
} else {
State *nq = new_state(p->length + 1);
memcpy(nq->go, q->go, sizeof(q->go));
nq->parent = q->parent;
np->parent = q->parent = nq;
while (p && p->go[token] == q) {
p->go[token] = nq;
p = p->parent;
}
}
}
return np;
}
int cost[C], a, b, match[N + 1], queue[N + 1];
State* end[N + 1];
long long minimum[N + 1];
int main() {
int T;
scanf("%d", &T);
for (int t = 1; t <= T; ++ t) {
scanf("%s", s);
for (int i = 0; i < C; ++ i) {
scanf("%d", cost + i);
}
scanf("%d%d", &a, &b);
int n = strlen(s);
state_count = 0;
end[0] = new_state(0);
for (int i = 0; i < n; ++ i) {
end[i + 1] = end[i]->extend(end[0], s[i]- 'a');
}
for (int i = 0; i < n; ++ i) {
for (State* p = end[i + 1]; p && !~p->first; p = p->parent) {
p->first = i;
}
}
State* p = end[0];
for (int i = 0, l = 0; i < n; ++ i) {
int c = s[i] - 'a';
while (!p->go[c]) {
p = p->parent;
l = std::min(l, p->length);
}
p = p->go[c];
l ++;
while (p->first > i - l) {
l --;
if (p->parent->length == l) {
p = p->parent;
}
}
match[i + 1] = l;
}
minimum[0] = 0;
int head = 0;
int tail = 0;
queue[tail ++] = 0;
for (int i = 1; i <= n; ++ i) {
minimum[i] = minimum[i - 1] + cost[s[i - 1] - 'a'];
while (head < tail && queue[head] < i - match[i]) {
head ++;
}
if (head < tail) {
int j = queue[head];
minimum[i] = std::min(minimum[i], minimum[j] + (long long)a * (i - j) + b + b);
}
#define VALUE(i) (minimum[i] - (long long)a * (i))
while (head < tail && VALUE(queue[tail - 1]) > VALUE(i)) {
tail --;
}
#undef VALUE
queue[tail ++] = i;
}
std::cout << "Case #" << t << ": " << minimum[n] << std::endl;;
}
return 0;
}

110
HDOJ/5471_autoAC.cpp Normal file
View File

@ -0,0 +1,110 @@
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1024 + 5;
const int mod = 1e9 + 7;
struct Rectangle {
int x1, y1, x2, y2, c;
Rectangle (int x1 = 0, int y1 = 0, int x2 = 0, int y2 = 0, int c = 0): x1(x1), y1(y1), x2(x2), y2(y2), c(c) {}
void read() { scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &c); }
int size() { return (x2-x1+1)*(y2-y1+1); }
bool in(const Rectangle& u) const {
return x1 <= u.x1 && u.x2 <= x2 && y1 <= u.y1 && u.y2 <= y2;
}
}R[15];
int N, M, H, W, cc, all, ct[15][maxn], sz[15][maxn];
vector<int> X, Y, C, G[15];
int bitcount(int x) { return x == 0 ? 0 : bitcount(x>>1) + (x&1); }
void init () {
scanf("%d%d%d%d", &H, &W, &M, &N);
X.clear(), Y.clear(), C.clear();
for (int i = 0; i < N; i++) {
R[i].read();
X.push_back(R[i].x1-1);
X.push_back(R[i].x2);
Y.push_back(R[i].y1-1);
Y.push_back(R[i].y2);
C.push_back(R[i].c);
}
X.push_back(0);
X.push_back(H);
Y.push_back(0);
Y.push_back(W);
sort(X.begin(), X.end());
sort(Y.begin(), Y.end());
sort(C.begin(), C.end());
int cx = unique(X.begin(), X.end()) - X.begin();
int cy = unique(Y.begin(), Y.end()) - Y.begin();
cc = unique(C.begin(), C.end()) - C.begin();
int id[15], rk[15];
for (int i = 0; i < cc; i++) G[i].clear();
for (int i = 0; i < N; i++) {
id[i] = lower_bound(C.begin(), C.begin()+cc, R[i].c) - C.begin();
rk[i] = G[id[i]].size();
G[id[i]].push_back(i);
}
all = 0;
memset(ct, 0, sizeof(ct));
memset(sz, 0, sizeof(sz));
for (int x = 1; x < cx; x++) {
for (int y = 1; y < cy; y++) {
int t = cc, s = 0;
Rectangle tmp = Rectangle(X[x-1]+1, Y[y-1]+1, X[x], Y[y]);
for (int i = 0; i < N; i++) {
if (R[i].in(tmp)) {
if (id[i] < t)
s = 0, t = id[i];
if (t == id[i])
s |= (1<<rk[i]);
}
}
if (t == cc) all += tmp.size();
else {
int add = tmp.size();
for (int j = s; j; j = (j-1)&s)
ct[t][j] += add;
}
}
}
for (int c = 0; c < cc; c++) {
int s = (1<<G[c].size());
for (int i = 0; i < s; i++) {
for (int j = i; j; j = (j-1)&i)
sz[c][i] += ct[c][j] * (bitcount(j)&1 ? 1 : -1);
}
}
}
ll pow_mod(ll x, ll n) {
ll ret = 1;
while (n) {
if (n&1) ret = ret * x % mod;
x = x * x % mod;
n >>= 1;
}
return ret;
}
int solve () {
ll ret = pow_mod(M, all);
for (int i = 0; i < cc; i++) {
int t = 0, n = (1<<G[i].size());
for (int s = 0; s < n; s++) {
int si = bitcount(s)&1 ? -1 : 1;
t = ((t + si * pow_mod(C[i]-1, sz[i][s]) * pow_mod(C[i], sz[i][n-1]-sz[i][s])) % mod + mod) % mod;
}
ret = ret * t % mod;
}
return ret;
}
int main () {
int cas;
scanf("%d", &cas);
for (int kcas = 1; kcas <= cas; kcas++) {
init();
printf("Case #%d: %d\n", kcas, solve());
}
return 0;
}

93
HDOJ/5473_autoAC.cpp Normal file
View File

@ -0,0 +1,93 @@
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
struct Point {
ll x, y;
Point(ll x = 0, ll y = 0): x(x), y(y) {}
void read() { scanf("%lld%lld", &x, &y); }
bool operator < (const Point& u) const { return x < u.x || (x == u.x && y < u.y); }
bool operator == (const Point& u) const { return !(*this < u) && !(u < *this); }
bool operator != (const Point& u) const { return !(*this == u); }
bool operator > (const Point& u) const { return u < *this; }
bool operator <= (const Point& u) const { return *this < u || *this == u; }
bool operator >= (const Point& u) const { return *this > u || *this == u; }
Point operator + (const Point& u) { return Point(x + u.x, y + u.y); }
Point operator - (const Point& u) { return Point(x - u.x, y - u.y); }
Point operator * (const double u) { return Point(x * u, y * u); }
Point operator / (const double u) { return Point(x / u, y / u); }
ll operator ^ (const Point& u) { return x*u.y - y*u.x; }
};
typedef Point Vector;
ll Cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; }
ll getArea (Point* p, int n) {
ll ret = 0;
for (int i = 0; i < n-1; i ++)
ret += Cross(p[i]-p[0], p[i+1]-p[0]);
return ret < 0 ? -ret : ret;
}
int getConvexHull (Point* p, int n, Point* ch) {
sort(p, p + n);
int m = 0;
for (int i = 0; i < n; i++) {
while (m > 1 && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-1]) <= 0) m--;
ch[m++] = p[i];
}
int k = m;
for (int i = n-2; i >= 0; i--) {
while (m > k && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-1]) <= 0) m--;
ch[m++] = p[i];
}
if (n > 1) m--;
return m;
}
const int maxn = 105;
int N, K;
ll dp[maxn][maxn];
Point P[maxn], Q[maxn];
ll solve () {
ll allarea = getArea(P, N);
if (K >= N) return allarea;
ll ret = 0;
bool vis[maxn];
memset(vis, 0, sizeof(vis));
int ti = min(5 * (N / K), N);
for (int t = 0; t < ti; t++) {
int s = rand() % N;
while (vis[s]) s = rand() % N;
vis[s] = 1;
memset(dp, 0, sizeof(dp));
dp[0][0] = allarea;
for (int i = 1; i <= N; i++) {
int u = (i + s) % N;
ll sum = 0;
for (int j = i-1; j >= 0; j--) {
int v = (j + s) % N;
int p = (v + 1) % N;
ll tmp = ((P[p]-P[u])^(P[v]-P[u]));
if (tmp < 0) tmp = -tmp;
sum += tmp;
for (int x = K; x > 0; x--)
dp[i][x] = max(dp[i][x], dp[j][x-1]-sum);
}
}
ret = max(ret, dp[N][K]);
}
return ret;
}
int main () {
int cas;
scanf("%d", &cas);
srand((int)time(NULL));
for (int kcas = 1; kcas <= cas; kcas++) {
scanf("%d%d", &N, &K);
for (int i = 0; i < N; i++) Q[i].read();
N = getConvexHull(Q, N, P);
printf("Case #%d: %lld\n", kcas, solve());
}
return 0;
}

83
HDOJ/5475_autoAC.cpp Normal file
View File

@ -0,0 +1,83 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn=155555;
ll sum[maxn*4+4];
int add;
int a[maxn];
int L,R;
int MOD;
int p;
void pushup(int rt){
sum[rt]=sum[rt<<1]*sum[rt<<1|1]%MOD;
}
void build(int l,int r,int rt){
if(l==r){
sum[rt]=1;
return ;
}
int m=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void update(int l,int r,int rt){
if(l==r){
sum[rt]=add;
return ;
}
int m=(l+r)>>1;
if(p<=m)
update(lson);
else
update(rson);
pushup(rt);
}
ll query(int l,int r,int rt){
ll ret=1;
if(L<=l&&R>=r)
return sum[rt];
int m=(l+r)>>1;
if(L<=m)
ret*=query(lson)%MOD;
if(R>m)
ret*=query(rson)%MOD;
return ret%MOD;
}
int main()
{
int n,m;
int op,u,q;
int t;
scanf("%d",&t);
for(int case1=1;case1<=t;case1++){
printf("Case #%d:\n",case1);
scanf("%d%d",&q,&MOD);
build(1,q,1);
for(int i=1;i<=q;i++){
scanf("%d%d",&op,&u);
if(op==1){
p=i;
add=u;
update(1,q,1);
L=1;
R=i;
printf("%I64d\n",query(1,q,1)%MOD);
}
else{
p=u;
add=1;
update(1,q,1);
L=1;
R=i;
printf("%I64d\n",query(1,q,1)%MOD);
}
}
}
return 0;
}

33
HDOJ/5476_autoAC.cpp Normal file
View File

@ -0,0 +1,33 @@
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<map>
#define pi (2*acos(0))
#define maxn 1000000
using namespace std;
int main()
{
int t;
scanf("%d", &t);
for (int k=1;k<=t;k++)
{
double ax, ay, bx, by, cx, cy;
scanf("%lf%lf%lf%lf%lf%lf", &ax, &ay, &bx, &by, &cx, &cy);
double t1 = (ax - bx) * (ax - bx) + (ay - by) * (ay - by);
double ab = sqrt(t1);
double t2 = (bx - cx) * (bx - cx) + (by - cy) * (by - cy);
double bc = sqrt(t2);
double h = sqrt(t1 - t2/4);
double angle = acos(bc/(2.0*ab));
double d = bc / sin(angle);
double ans = 0;
ans = d * angle;
ans = ans + h;
printf("Case #%d: %.4f\n", k, ans);
}
return 0;
}

51
HDOJ/5479_autoAC.cpp Normal file
View File

@ -0,0 +1,51 @@
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
char s[1010];
int l[1010],r[1010];
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%s",s);
mem0(l);
mem0(r);
int len=strlen(s);
for(int i=len-1;i>=0;i--){
if(i==len-1)
r[i]=(s[i]==')');
else
r[i]=r[i+1]+(s[i]==')');
}
for(int i=0;i<len;i++){
if(i==0)
l[i]=(s[i]=='(');
else
l[i]=l[i-1]+(s[i]=='(');
}
int ans=INF;
for(int i=0;i<len;i++)
ans=min(ans,l[i]+r[i]-1);
printf("%d\n",ans);
}
return 0;
}

97
HDOJ/5481_autoAC.cpp Normal file
View File

@ -0,0 +1,97 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 110000
#define pr pair<int,int>
#define lson l,mid,no<<1
#define rson mid+1,r,no<<1|1
#define mod 1000000007ll
using namespace std;
typedef long long ll;
int dis[N<<1],len;
pr L[N];
ll sum[N<<3],cov[N<<3];
ll P[N],S[N];
void Pushdown(int no,int len)
{
if(cov[no])
{
cov[no<<1]+=cov[no];
cov[no<<1|1]+=cov[no];
sum[no<<1]+=cov[no]*(len-(len>>1));
sum[no<<1|1]+=cov[no]*(len>>1);
cov[no]=0;
}
}
void Pushup(int no)
{
sum[no]=sum[no<<1]+sum[no<<1|1];
}
void update(int l,int r,int no,int st,int en)
{
if(st<=l&&r<=en)
{
cov[no]++;
sum[no]+=r-l+1;
}
else
{
int mid=l+r>>1;
Pushdown(no,r-l+1);
if(en<=mid) update(lson,st,en);
else if(st>mid) update(rson,st,en);
else update(lson,st,en),update(rson,st,en);
Pushup(no);
}
}
int query(int l,int r,int no,int k)
{
if(l==r)
return sum[no];
else
{
int mid=l+r>>1;
Pushdown(no,r-l+1);
if(k<=mid) return query(lson,k);
else return query(rson,k);
}
}
int main()
{
int c,T;
int n,i,l,r;
scanf("%d",&T);
P[1]=1;
for(i=2;i<N;i++)
P[i]=P[i-1]*2%mod;
for(i=1;i<N;i++)
S[i]=(S[i-1]+P[i])%mod;
for(c=1;c<=T;c++)
{
memset(sum,0,sizeof(sum));
memset(cov,0,sizeof(cov));
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d%d",&L[i].first,&L[i].second);
dis[i+i-1]=L[i].first;
dis[i+i]=L[i].second;
}
sort(dis+1,dis+n+n+1);
len=unique(dis+1,dis+n+n+1)-dis-1;
for(i=1;i<=n;i++)
{
l=lower_bound(dis+1,dis+len+1,L[i].first)-dis;
r=lower_bound(dis+1,dis+len+1,L[i].second)-dis;
if(l<=r-1)
update(1,len-1,1,l,r-1);
}
ll ans=0;
for(i=1;i<len;i++)
{
ans=(ans+(S[n]-S[n-query(1,len-1,1,i)]+mod)%mod*(((ll)dis[i+1]-dis[i]+mod)%mod)%mod)%mod;
}
printf("%I64d\n",ans);
}
return 0;
}

138
HDOJ/5483_autoAC.cpp Normal file
View File

@ -0,0 +1,138 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
#define scan(x) scanf("%d",&(x))
#define scan2(x,y) scanf("%d%d",&(x),&(y))
#define scan3(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))
#define scan4(x,y,z,k) scanf("%d%d%d%d",&(x),&(y),&(z),&(k))
const int maxn = 3003;
const int maxm = maxn * maxn;
const int inf = 1000000000;
int n, m;
__int64 mst;
int map[maxn][maxn];
int dp[maxn][maxn], best[maxn][maxn];
int dis[maxn], pre[maxn];
bool vis[maxn];
vector<int> edge[maxn];
#include<iostream>
int minz(int a, int b)
{
return a < b ? a : b;
}
void init()
{
int i, j;
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
map[i][j] = dp[i][j] = inf;
edge[i].clear();
vis[i] = 0;
pre[i] = -1;
dis[i] = inf;
}
}
void input()
{
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
int w;scan(w);
map[i][j] = map[j][i] = w;
}
}
}
void prim()
{
int i, j, k;
for(i = 1; i < n; i++)
{
dis[i] = map[0][i];
pre[i] = 0;
}
dis[0] = inf;
vis[0] = 1;
pre[0] = -1;
mst = 0;
for(i = 0; i < n-1; i++)
{
k = 0;
for(j = 1; j < n; j++)
if(!vis[j] && dis[k] > dis[j])
k = j;
vis[k] = 1;
mst += dis[k];
if(pre[k] != -1)
edge[k].push_back(pre[k]),
edge[pre[k]].push_back(k);
for(j = 1; j < n; j++)
if(!vis[j] && dis[j] > map[k][j] )
dis[j] = map[k][j], pre[j] = k;
}
}
int dfs1(int u, int fa, int rt)
{
int i;
for(i = 0; i < edge[u].size(); i++)
{
int v = edge[u][i];
if(v == fa) continue;
dp[rt][u] = minz(dp[rt][u], dfs1(v, u, rt));
}
if(fa != rt) dp[rt][u] = minz(dp[rt][u], map[rt][u]);
return dp[rt][u];
}
int dfs2(int u, int fa, int rt)
{
int i;
int ans = dp[u][rt];
for(i = 0; i < edge[u].size(); i++)
{
int v = edge[u][i];
if(v == fa) continue;
ans = minz(ans, dfs2(v, u, rt));
}
return ans;
}
void solve()
{
int i,j;
for(i = 0; i < n; i++)
dfs1(i, -1, i);
for(i = 0; i < n; i++)
for(j = 0; j < edge[i].size(); j++)
{
int v = edge[i][j];
best[i][v] = best[v][i] = dfs2(v, i, i);
}
}
void query()
{
int ans =0 ;
for(int i=0;i<n;i++){
int x=i;
for(int j=0;j<edge[i].size() ; j++){
int y=edge[i][j];
if(x<y) continue;
int sum = mst - map[x][y] + best[x][y];
if(sum != mst){
ans++;
}
}
}
printf("%d\n",ans);
}
int main(){
int T;scan(T);
while(T--){
scan(n);
m=n-1;
init();
input();
prim();
solve();
query();
}
}

144
HDOJ/5486_autoAC.cpp Normal file
View File

@ -0,0 +1,144 @@
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 1000006
#define mod 1000000007
#define eps 1e-9
#define e exp(1.0)
#define PI acos(-1)
const double EP = 1E-10 ;
int Num;
const ll inf=999999999;
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
vector<int> Q;
map<int,int> H;
struct node
{
int x,y;
};
node p[maxn];
int b[maxn];
vector<int> Q1[maxn];
int main()
{
int t=read();
for(int cas=1;cas<=t;cas++)
{
int n=read();
H.clear();
for(int i=1;i<=n;i++)
{
p[i].x=read(),p[i].y=read();
Q.push_back(p[i].x);
Q.push_back(p[i].y);
}
sort(Q.begin(),Q.end());
Q.erase(unique(Q.begin(),Q.end()),Q.end());
int len = Q.size();
for(int i=0;i<len;i++)
H[Q[i]]=i;
for(int i=0;i<len;i++)
Q1[i].clear(),b[i]=0;
Q.clear();
for(int i=1;i<=n;i++)
{
Q1[H[p[i].x]].push_back(H[p[i].y]);
}
for(int i=0;i<len;i++)
{
sort(Q1[i].begin(),Q1[i].end());
Q1[i].erase(unique(Q1[i].begin(),Q1[i].end()),Q1[i].end());
}
for(int i=0;i<len;i++)
{
for(int j=0;j<Q1[i].size();j++)
b[Q1[i][j]]++;
}
int ans1 = 0,ans2 = 0,ans3 = 0;
for(int i=0;i<len;i++)
{
if(Q1[i].size()==0)continue;
if(Q1[i].size()==1)
{
if(b[Q1[i][0]]==1)
ans3++;
}
else
{
int flag = 1;
for(int j=0;j<Q1[i].size();j++)
{
if(b[Q1[i][j]]!=1)
{
flag = 0;
break;
}
}
if(flag)
ans1++;
}
}
for(int i=0;i<len;i++)
Q1[i].clear(),b[i]=0;
for(int i=1;i<=n;i++)
{
Q1[H[p[i].y]].push_back(H[p[i].x]);
}
for(int i=0;i<len;i++)
{
sort(Q1[i].begin(),Q1[i].end());
Q1[i].erase(unique(Q1[i].begin(),Q1[i].end()),Q1[i].end());
}
for(int i=0;i<len;i++)
{
for(int j=0;j<Q1[i].size();j++)
b[Q1[i][j]]++;
}
for(int i=0;i<len;i++)
{
if(Q1[i].size()==0)continue;
if(Q1[i].size()==1)
{
if(b[Q1[i][0]]==1)
ans3++;
}
else
{
int flag = 1;
for(int j=0;j<Q1[i].size();j++)
{
if(b[Q1[i][j]]!=1)
{
flag = 0;
break;
}
}
if(flag)
ans2++;
}
}
printf("Case #%d: %d %d %d\n",cas,ans1,ans2,ans3/2);
}
}

104
HDOJ/5487_autoAC.cpp Normal file
View File

@ -0,0 +1,104 @@
#pragma warning(disable:4996)
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
using namespace std;
#define maxn 1005
int n1, m1, k1;
int n2, m2, k2;
bool tar1[maxn];
bool tar2[maxn];
int go1[maxn][26];
int go2[maxn][26];
int prestate[maxn*maxn];
char prechar[maxn*maxn];
bool vis[maxn*maxn];
bool check(int x)
{
return tar1[x / n2] != tar2[x%n2];
}
int nextState(int x, int c)
{
return go1[x / n2][c] * n2 + go2[x%n2][c];
}
int main()
{
int T; cin >> T; int ca = 0;
while (T--)
{
memset(go1, -1, sizeof(go1));
memset(go2, -1, sizeof(go2));
memset(tar1, 0, sizeof(tar1));
memset(tar2, 0, sizeof(tar2));
int accept;
int ui, vi;
char ci[3];
scanf("%d%d%d", &n1, &m1, &k1);
for (int i = 0; i < k1; ++i){
scanf("%d", &accept);
tar1[accept] = true;
}
for (int i = 0; i < m1; ++i){
scanf("%d%d%s", &ui, &vi, ci);
go1[ui][ci[0] - 'a'] = vi;
}
for (int i = 0; i <= n1; ++i){
for (int k = 0; k < 26; ++k){
if (go1[i][k] == -1) go1[i][k] = n1;
}
}
scanf("%d%d%d", &n2, &m2, &k2);
for (int i = 0; i < k2; ++i){
scanf("%d", &accept);
tar2[accept] = true;
}
for (int i = 0; i < m2; ++i){
scanf("%d%d%s", &ui, &vi, ci);
go2[ui][ci[0] - 'a'] = vi;
}
for (int i = 0; i <= n2; ++i){
for (int k = 0; k < 26; ++k){
if (go2[i][k] == -1) go2[i][k] = n2;
}
}
++n1; ++n2;
int ans = -1;
memset(vis, 0, sizeof(vis));
queue<int> Q;
Q.push(0);
vis[0] = true;
while (!Q.empty())
{
int state = Q.front(); Q.pop();
if (check(state)){
ans = state;
break;
}
for (int k = 0; k < 26; ++k){
int nstate = nextState(state, k);
if (!vis[nstate]){
Q.push(nstate);
vis[nstate] = true;
prestate[nstate] = state;
prechar[nstate] = k;
}
}
}
if (-1 == ans){
printf("Case #%d: 0\n", ++ca);
continue;
}
string ts;
while (ans != 0){
ts.push_back(char('a' + prechar[ans]));
ans = prestate[ans];
}
reverse(ts.begin(), ts.end());
printf("Case #%d: %s\n", ++ca, ts.c_str());
}
return 0;
}

103
HDOJ/5489_autoAC.cpp Normal file
View File

@ -0,0 +1,103 @@
#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
using namespace std ;
const int maxn = 1e5+10 ;
int a[maxn] , b[maxn] ;
int c[maxn] ;
pair<int , int> pre[maxn] ;
int len_b = 0 ;
int len_a = 0 ;
int find_b(int l , int r , int v)
{
while(l<=r)
{
int mid = (l+r)>> 1 ;
if(b[mid]>=v)
l = mid+1 ;
else r = mid - 1 ;
}
return l;
}
int find_a(int l , int r ,int v)
{
while(l<=r)
{
int mid = (l + r) >> 1 ;
if(a[mid]<=v)
l = mid+1 ;
else r = mid - 1 ;
}
return l ;
}
int find_ans_a(int l , int r , int v)
{
while(l<=r)
{
int mid = (l+r)>>1 ;
if(a[mid]<=v)
l = mid + 1 ;
else r = mid - 1 ;
}
return r ;
}
int find_ans_b(int l , int r , int v)
{
while(l<=r)
{
int mid = (l+r) >> 1 ;
if(b[mid] >= v)
l = mid + 1 ;
else r = mid - 1 ;
}
return r ;
}
void debuga()
{
for(int i = 1;i <= len_a;i++)
printf("%d%c" , a[i] , i == len_ ?'\n' :' ') ;
}
int main()
{
int t ;
scanf("%d" , &t) ;
int cas = 0 ;
while(t--)
{
int n , l ;
scanf("%d%d" , &n , &l) ;
for(int i = 1;i <= n;i++)
a[i] = b[i] = 0 ;
for(int i = 1;i <= n;i++)
scanf("%d" , &c[i]) ;
len_a = len_b = 0 ;
for(int j = n;j > l;j--)
{
int pos = find_b(1 , len_ , c[j]) ;
pre[j] = make_pair(pos , b[pos]) ;
if(!b[pos])len_b++ ;
b[pos] = c[j] ;
}
int ans = len_ ;
for(int i = 1;i <= n-l;i++)
{
int pos = find_a(1 , len_ , c[i]) ;
if(a[pos] == 0)len_a++ ;
a[pos] = c[i] ;
pair<int , int> tmp = pre[i+l] ;
if(tmp.second == 0)len_b--;
else b[tmp.first] = tmp.second ;
int pos_1 = find_ans_b(1 , len_ , c[i]) ;
ans = max(pos+pos_ , ans) ;
if(tmp.second != 0)
{
int pos_2 = find_ans_a(1 , len_ , tmp.second) ;
ans = max(ans , pos_2 + tmp.first) ;
}
}
printf("Case #%d: " , ++cas) ;
printf("%d\n" , ans) ;
}
return 0 ;
}

85
HDOJ/5490_autoAC.cpp Normal file
View File

@ -0,0 +1,85 @@
#include <iostream>
#include <cstdio>
#include <cstdlib>
#define LL long long
#define MOD 1000000007
using namespace std;
LL b1, q, a1, d, n, m;
LL ans, dis[10005];
void input()
{
ans = 0;
scanf("%I64d%I64d%I64d%I64d%I64d%I64d", &b1, &q, &a1, &d, &n, &m);
}
void exgcd(LL a, LL b, LL &x, LL &y, LL &d)
{
if (b == 0)
{
x = 1;
y = 0;
d = a;
}
else
{
exgcd(b, a%b, y, x, d);
y -= a/b*x;
}
}
LL modDiv(LL a, LL b)
{
LL x, y, d;
exgcd(b, MOD, x, y, d);
x = (x+MOD) % MOD;
x = (x*a/d) % MOD;
return x;
}
LL quickPow(LL x, LL n)
{
LL a = 1;
while (n)
{
a *= n&1 ? x : 1;
a %= MOD;
n >>= 1 ;
x *= x;
x %= MOD;
}
return a;
}
void work()
{
LL now = b1, tmp;
now %= MOD;
for (int i = 1; i <= n; ++i)
{
tmp = modDiv(now, q-1);
now = now+tmp;
now %= MOD;
dis[i] = (a1+(i-1)*d%MOD-tmp)%MOD;
dis[i] = (dis[i]+MOD)%MOD;
}
LL qt = 1;
ans += qt*dis[n];
for (int i = n-1; i > 0; --i)
{
qt = modDiv((qt*((n+m-1)-i)%MOD+MOD)%MOD, n-i);
ans += (qt*dis[i])%MOD;
ans %= MOD;
}
tmp = quickPow(q, (m-1)%(MOD-1))%MOD;
ans += now*tmp%MOD;
ans %= MOD;
printf("%I64d\n", ans);
}
int main()
{
int T;
scanf("%d", &T);
for (int times = 1; times <= T; ++times)
{
printf("Case #%d: ", times);
input();
work();
}
return 0;
}

112
HDOJ/5491_autoAC.cpp Normal file
View File

@ -0,0 +1,112 @@
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 300006
#define mod 1000000007
#define eps 1e-9
#define e exp(1.0)
#define PI acos(-1)
const double EP = 1E-10 ;
int Num;
const ll inf=999999999;
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int get(ll x)
{
int c =0 ;
while (x >0)
{
if((x &1) ==1)
c++; ;
x >>=1 ;
}
return c ;
}
ll change(ll x,int num)
{
for(int i=0;i<132;i++)
{
if((x&(1LL<<i))==0)
{
x|=(1LL<<i);
num--;
}
if(num==0)
break;
}
return x;
}
ll change2(ll x,int K,int s1,ll P)
{
int l =0;
for(int i=0;i<132;i++)
{
if((x&(1LL<<i))==0)
{
x|=(1LL<<i);
l = i;
break;
}
}
int num = K+1;
for(int i = l-1;i>=0;i--)
{
x-=(1LL<<i);
num--;
if(num==s1)
break;
}
if(x>P*2)return P;
return x;
}
int main()
{
int t;scanf("%d",&t);
for(int cas = 1;cas <= t;cas ++)
{
ll a=read(),s1=read(),s2=read();
ll ans = a+1;
printf("Case #%d: ",cas);
int len = get(ans);
if(len<=s2&&len>=s1)
{
printf("%I64d\n",ans);
continue;
}
if(len<s1)
{
printf("%I64d\n",change(ans,s1-len));
continue;
}
if(len>s2)
{
while(len>s2)
{
ans = change2(ans,len,s1,a);
len = get(ans);
}
printf("%I64d\n",ans);
}
}
}

97
HDOJ/5492_autoAC.cpp Normal file
View File

@ -0,0 +1,97 @@
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
#include <string>
#include <ctime>
#include <list>
#include <bitset>
typedef unsigned char byte;
#define pb push_back
#define input_fast std::ios::sync_with_stdio(false);std::cin.tie(0)
#define pi acos(-1)
using namespace std;
const int maxn = 30 + 10;
int dp[maxn][maxn][2000];
int g[maxn][maxn] , n , m , px ,dir[2][2] = {0,1,1,0},tans;
int cal(int x,int y)
{
return px*x - y*y;
}
bool inmap(int x,int y)
{
return x <= n && x >= 1 && y <= m && y >= 1;
}
void initiation()
{
tans = 1 << 29;
scanf("%d%d",&n,&m);
for(int i = 1 ; i <= n ; ++ i)
for(int j = 1 ; j <= m ; ++ j)
scanf("%d",&g[i][j]);
px = n + m - 1;
memset(dp,-1,sizeof(dp));
dp[1][1][g[1][1]] = g[1][1]*g[1][1];
}
void updata(int &x ,int v)
{
if(x==-1) x=v;
else x = min(x,v);
}
int solve()
{
for(int i = 1 ; i <= n ; ++ i)
for(int j = 1 ; j <= m ; ++ j)
{
for(int z = 0 ; z <= 1900 ; ++ z)
{
if(dp[i][j][z] == -1) continue;
for(int k = 0 ; k < 2 ; ++ k)
{
int newx = i + dir[k][0];
int newy = j + dir[k][1];
if(!inmap(newx,newy)) continue;
int newval = dp[i][j][z] + g[newx][newy]*g[newx][newy];
updata(dp[newx][newy][z+g[newx][newy]],newval);
}
}
}
int ans = 1 << 30;
for(int i = 0 ; i <= 1900 ; ++ i) if(dp[n][m][i] != -1) ans = min(ans , px*dp[n][m][i] - i*i);
return ans;
}
void dfs(int x,int y,int s1,int s2)
{
if(x==n&&y==m)
{
tans =min(tans,s1*px-s2*s2);
}
else
{
for(int k = 0 ; k < 2 ; ++ k)
{
int newx = x + dir[k][0];
int newy = y + dir[k][1];
if(!inmap(newx,newy)) continue;
dfs(newx,newy,s1+g[newx][newy]*g[newx][newy],s2+g[newx][newy]);
}
}
}
int main(int argc,char *argv[])
{
int Case,cas=1;
scanf("%d",&Case);
while(Case--)
{
initiation();
printf("Case #%d: %d\n",cas++,solve());
}
return 0;
}

132
HDOJ/5493_autoAC.cpp Normal file
View File

@ -0,0 +1,132 @@
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200006
#define mod 1000000007
#define eps 1e-9
#define e exp(1.0)
#define PI acos(-1)
#define lowbit(x) (x)&(-x)
const double EP = 1E-10 ;
int Num;
const ll inf=999999999;
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,val1[maxn],val2[maxn];
void add(int * vc,int u ,int v)
{
while(u <= n )
{
vc[u] += v;
u += lowbit(u);
}
}
int query(int * vc,int u)
{
int res=0;
while(u)
{
res += vc[u];
u -= lowbit(u);
}
return res;
}
int RankGet(int * vc,int k)
{
int L = 1 , R = n;
while(L < R)
{
int mid = L + ((R-L)>>1);
int res = query(vc,mid);
if(res == k) R = mid;
else if(res < k) L = mid + 1;
else R = mid - 1;
}
return L;
}
struct kkk
{
int x,y;
};
bool cmp(kkk aa,kkk bb)
{
return aa.x<bb.x;
}
kkk p[maxn];
int ans[maxn];
vector<int> v1,v2;
int main()
{
int t=read();
for(int cas=1;cas<=t;cas++)
{
n=read();
for(int i=1;i<=n;i++)
{
p[i].x=read(),p[i].y=read();
p[i].y++;
}
sort(p+1,p+n+1,cmp);
int flag=1;
memset(val1,0,4*(n+2));memset(val2,0,4*(n+2));
for(int i = 1 ; i <= n ; ++ i)
{
add(val1,i,1);
add(val2,i,1);
}
for(int i=1;i<=n;i++)
{
if(p[i].y>n-i+1)
{
flag=0;
break;
}
int p1 = RankGet(val1,p[i].y);
int p2 = n-RankGet(val2,p[i].y) + 1;
if(p1<p2)
{
ans[p1]=p[i].x;
add(val1,p1,-1);
add(val2,n-p1+1,-1);
}
else
{
ans[p2]=p[i].x;
add(val1,p2,-1);
add(val2,n-p2+1,-1);
}
}
printf("Case #%d:",cas);
if(!flag)
{
printf(" impossible\n");
}
else
{
for(int i=1;i<=n;i++)
printf(" %d",ans[i]);
printf("\n");
}
}
}

44
HDOJ/5494_autoAC.cpp Normal file
View File

@ -0,0 +1,44 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
#define ll long long
#define CL(a) memset(a,0,sizeof(a))
int main ()
{
int T,n,m;
int a[1111],b[1111];
cin>>T;
while (T--)
{
cin>>n>>m;
for (int i=0; i<n; i++)
cin>>a[i];
for (int i=0; i<n; i++)
cin>>b[i];
sort(a, a+n);
sort(b, b+n);
int suma=0,sumb=0;
for (int i=1; i<=m; i++)
{
suma+=a[i-1];
sumb+=b[n-i];
}
if (suma>sumb)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}

34
HDOJ/5495_autoAC.cpp Normal file
View File

@ -0,0 +1,34 @@
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
#define maxn 100007
int a[maxn],b[maxn],vis[maxn];
int dfs(int x){
vis[x] = 1;
if(vis[b[a[x]]]) return 1;
return dfs(b[a[x]])+1;
}
int main(){
int t,n,u;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i = 1;i <= n; i++){
scanf("%d",&u);
a[u] = i;
}
for(int i = 1;i <= n; i++)
scanf("%d",&b[i]);
memset(vis,0,sizeof(vis));
int ans = n;
for(int i = 1;i <= n; i++){
if(vis[i] == 0) {
if(dfs(i) > 1) ans--;
}
}
printf("%d\n",ans);
}
return 0;
}

39
HDOJ/5496_autoAC.cpp Normal file
View File

@ -0,0 +1,39 @@
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long LL;
const LL mod = 1000000007;
const int maxn = 100005;
int b[maxn],a[maxn];
int n,m;
LL cnt[maxn];
LL B[maxn];
int main() {
int t;
cin >> t;
B[0] = 1;
for (int i = 1;i < maxn;i++) B[i] = (B[i - 1] * 2) % mod;
while (t--) {
memset(cnt, 0, sizeof(cnt));
cin >> n;
for (int i = 0;i < n;i++) {
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(b, b + n);
m = unique(b, b + n) - b;
LL ret = 0;
for (int i = 0;i < n;i++) {
int x = lower_bound(b, b + m, a[i]) - b;
ret = ((ret - ((cnt[x] * (LL)a[i]) % mod * B[n - i - 1])%mod)%mod+mod)%mod;
ret = (ret + B[n - 1] * (LL)a[i]) % mod;
cnt[x] = (cnt[x] + B[i])%mod;
}
cout << ret << endl;
}
return 0;
}

60
HDOJ/5497_autoAC.cpp Normal file
View File

@ -0,0 +1,60 @@
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
int n, m, a[N], b[N], c[N];
void add(int *b, int i, int v) {
for(; i <= n; i += i & -i) b[i] += v;
}
int sum(int *b, int i) {
int ret = 0;
for(; i; i -= i & -i) ret += b[i];
return ret;
}
inline int read() {
int c, x;
while(c = getchar(), !isdigit(c));
x = c - '0';
while(c = getchar(), isdigit(c)) x = x * 10 + c - '0';
return x;
}
int main() {
ios_base::sync_with_stdio(0);
int t; t = read();
while(t--) {
n = read(); m = read();
for(int i = 1; i <= n; ++i) a[i] = read();
memset(b, 0, sizeof b);
memset(c, 0, sizeof c);
long long tmp = 0;
for(int i = m + 1; i <= n; ++i) {
tmp += i - m - 1 - sum(c, a[i]);
add(c, a[i], 1);
}
long long ans = tmp;
for(int i = m + 1; i <= n; ++i) {
tmp -= sum(c, a[i] - 1);
tmp -= sum(b, n) - sum(b, a[i]);
add(c, a[i], -1);
tmp += sum(c, a[i - m] - 1);
tmp += sum(b, n) - sum(b, a[i - m]);
add(b, a[i - m], 1);
ans = min(ans, tmp);
}
printf("%I64d\n", ans);
}
return 0;
}

99
HDOJ/5498_autoAC.cpp Normal file
View File

@ -0,0 +1,99 @@
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<math.h>
using namespace std;
#define ll long long
#define maxn 101
int p;
struct Node{
int mat[maxn][maxn];
void init(){
memset(mat,0,sizeof(mat));
}
void one(int n){
init();
for(int i = 0;i < n; i++)
mat[i][i] = 1;
}
int Det(int n, int mod1){
int ans = 1;
int *p1,*p2,mul;
for(int i = 0;i < n; i++){
for(int j = i+1;j < n; j++){
while(mat[i][i] != 0 && mat[j][i] != 0){
if(abs(mat[i][i]) > abs(mat[j][i]))
p1=mat[i],p2=mat[j],mul=mat[i][i]/mat[j][i];
else
p1=mat[j],p2=mat[i],mul=mat[j][i]/mat[i][i];
for(int l = i;l < n; l++)
p1[l] = (p1[l]-(ll)p2[l]*mul)%mod1;
}
if(mat[i][i] == 0){
ans = -ans;
for(int l = i;l < n;l++)
swap(mat[i][l],mat[j][l]);
}
}
ans = (ll)ans*mat[i][i] % mod1;
}
return (ans+mod1)%mod1;
}
};
int matrix[maxn][maxn];
void multi(Node& a,Node& b,int n){
for(int i = 0;i < n; i++)
for(int j = 0;j < n; j++)
matrix[i][j] = 0;
for(int i = 0;i < n; i++){
for(int j = 0;j < n; j++){
if(a.mat[i][j] == 0) continue;
for(int k = 0;k < n; k++){
if(b.mat[j][k] == 0) continue;
matrix[i][k] = (matrix[i][k]+(ll)a.mat[i][j]*b.mat[j][k])%p;
}
}
}
for(int i = 0;i < n; i++)
for(int j = 0;j < n; j++)
a.mat[i][j] = matrix[i][j];
}
int mp[maxn][maxn];
int main(){
int t,n,m,q,u,v;
Node a,b,c;
scanf("%d",&t);
while(t--){
scanf("%d%d%d%d",&n,&m,&p,&q);
c.init();
for(int i = 0;i < m; i++){
scanf("%d%d",&u,&v);
u--,v--;
c.mat[u][u]++;
c.mat[v][v]++;
c.mat[u][v] = c.mat[v][u] = -1;
}
if(q < n-1){
printf("0\n");
continue;
}
int ans = c.Det(n-1,p);
if(ans == 0){
printf("0\n");
continue;
}
a.init();
b.one(n);
for(int i = 1;i < n; i++)
a.mat[i][i] = i,a.mat[i][i-1] = n-i;
while(q){
if(q&1) multi(b,a,n);
multi(a,a,n);
q/=2;
}
ans = (ll)ans*b.mat[n-1][0]%p;
printf("%d\n",ans);
}
return 0;
}

68
HDOJ/5499_autoAC.cpp Normal file
View File

@ -0,0 +1,68 @@
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
#define maxn 110
class node
{
public:
string sex,name;
double sum;
int r1,r2;
void set(string n,string s,int o,int t)
{
sex=s,name=n;
r1=o,r2=t;
}
}male[maxn],fe[maxn],ans[maxn];
bool cmp(node x,node y)
{
return x.sum>y.sum;
}
int main()
{
int T;
cin>>T;
while(T--)
{
int n,m;
cin>>n>>m;
string name,sex;
int r1,r2,rr1=0,rr2=0;
int f=0,p=0,top=0;
for(int i=0;i<n;i++)
{
cin>>name>>sex>>r1>>r2;
rr1=max(rr1,r1);
rr2=max(rr2,r2);
if(sex=="male") male[p++].set(name,sex,r1,r2);
else fe[f++].set(name,sex,r1,r2);
}
double m1,m2;
m1=300*1.0/rr1,m2=300*1.0/rr2;
for(int i=0;i<p;i++)
male[i].sum=male[i].r1*(m1)*0.3+male[i].r2*(m2)*0.7;
for(int i=0;i<p;i++)
fe[i].sum=fe[i].r1*(m1)*0.3+fe[i].r2*(m2)*0.7;
sort(male,male+p,cmp);
sort(fe,fe+f,cmp);
if(f)
ans[top++]=fe[0];
for(int i=0;i<p;i++)
{
ans[top++]=male[i];
}
for(int i=0;i<f;i++)
{
if(fe[i].name!=ans[0].name)
ans[top++]=fe[i];
}
sort(ans+1,ans+top,cmp);
sort(ans,ans+m,cmp);
cout<<"The member list of Shandong team is as follows:"<<endl;
for(int i=0;i<m;i++) cout<<ans[i].name<<endl;
}
return 0;
}

40
HDOJ/5500_autoAC.cpp Normal file
View File

@ -0,0 +1,40 @@
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<cstring>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<algorithm>
#include<iostream>
#include<ctime>
#include<bitset>
using namespace std;
#define N 300005
#define NMAX 2000000000
typedef long long ll;
int a[22];
bool cmp(const int& x, const int& y){
return x>y;
}
int main(){
int T, n, m;
while ( scanf("%d", &T) != EOF){
while ( T > 0 ){
T--;
scanf("%d", &n);
int index = -1;
for ( int i = 0 ;i < n; i++ ){
scanf("%d", &a[i]);
}
int ans = 0, t = n - 1, cur = n;
while ( t >= 0 ){
if ( a[t] == cur ) cur--;
t--;
}
cout << cur << endl;
}
}
return 0;
}

76
HDOJ/5501_autoAC.cpp Normal file
View File

@ -0,0 +1,76 @@
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<time.h>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<limits.h>
#include<map>
#include<set>
#include<bitset>
#define eps 1e-5
#define INF 0x7fffffff
#define FOR(i,a) for((i)=0;i<(a);(i)++)
#define MEM(a) (memset((a),0,sizeof(a)))
#define sfs(a) scanf("%s",a)
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define pfs(a) printf("%s\n",a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,num) scanf("%d%d%d",&a,&b,&num)
#define for1(i,a,b) for(int i=(a);i<b;i++)
#define for2(i,a,b) for(int i=(a);i<=b;i++)
#define for3(i,a,b)for(int i=(b);i>=a;i--)
#define MEM1(a) memset(a,0,sizeof(a))
#define MEM2(a) memset(a,-1,sizeof(a))
#define ll long long
const double PI=acos(-1.0);
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
using namespace std;
int n,m,L;
#define N 50005
#define M 1000100
#define Mod 1000000007
#define p(x,y) make_pair(x,y)
int dp[5010];
struct Node{
int a,b,c;
bool operator <(const Node &x) const{
return (ll)b*x.c>(ll)x.b*c;
}
}e[1010];
int main(){
int T;
sf(T);
while(T--){
int t;
sfd(n,t);
for(int i=1;i<=n;i++){
scanf("%d%d%d",&e[i].a,&e[i].b,&e[i].c);
}
sort(e+1,e+n+1);
memset(dp,0,sizeof dp);
for(int i=1;i<=n;i++){
for(int j=t;j>=e[i].c;j--){
dp[j] = Max(dp[j],dp[j-e[i].c]+Max(0,e[i].a-j*e[i].b));
}
}
int ans=-INF;
for(int i=0;i<=t;i++)
ans = Max(ans,dp[i]);
pf(ans);
}
return 0;
}

42
HDOJ/5502_autoAC.cpp Normal file
View File

@ -0,0 +1,42 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
double dp[1550];
double power[1550];
double ans[1550];
double pre[1550];
int main(){
int T,n;
double p,q;
scanf("%d",&T);
while(T--){
scanf("%d%lf%lf",&n,&p,&q);
power[0]=1;
double pro0=q/p;
double pro1=(p-q)/p;
for(int i=1;i<=n+1;i++){
power[i]=power[i-1]*pro0;
}
ans[0]=0;
n++;
for(int i=1;i<=n;i++){
dp[0]=pre[0]=1;
for(int j=1;j<=n;j++){
double now=pre[j-1];
if(j-1-i>=0) now-=pre[j-1-i]*power[i];
dp[j]=now*pro1;
pre[j]=pre[j-1]*pro0+dp[j];
}
ans[i]=dp[n];
}
double e=0;
for(int i=n;i>=1;i--){
e+=(ans[i]-ans[i-1])*i;
}
e/=pro1;
printf("%.3lf\n",e);
}
return 0;
}

51
HDOJ/5503_autoAC.cpp Normal file
View File

@ -0,0 +1,51 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL long long
#define N 50007
LL c[N], s[N];
void init() {
for (LL i = 1; i < N; ++i)
c[i] = i * (i - 1) >> 1;
}
inline int read() {
int ret = 0;
char ch = getchar();
while (!('0' <= ch && ch <= '9'))
ch = getchar();
while ('0' <= ch && ch <= '9') {
ret = ret * 10 + ch - '0';
ch = getchar();
}
return ret;
}
int main() {
init();
int T = read();
int n, i;
LL sum;
bool flag;
while (T--) {
n = read();
for (i = 0; i < n; ++i)
s[i] = read();
sort(s, s + n);
sum = 0;
flag = true;
for (i = 1; i <= n; ++i) {
sum = sum + s[i - 1];
if (sum < c[i]) {
flag = false;
break;
}
}
if (sum != c[n])
flag = false;
if (flag)
puts("It seems to have no problem.");
else
puts("The data have been tampered with!");
}
return 0;
}

46
HDOJ/5504_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
#define ll long long
#define CL(a) memset(a,0,sizeof(a))
int main()
{
int T;
ll n,x,sum;
cin>>T;
while(T--)
{
cin>>n;
sum = 0;
int k=0,re=0;
ll maxx = -INF;
for(int i=0; i<n; i++)
{
cin>>x;
if(x == 0) {re++; continue;}
if(x < 0)
{
k++;
maxx = max(maxx, x);
}
if(i == re) sum = x;
else sum *= x;
}
if(k%2&&n!=1&&!(k==1&&re+k==n)) sum /= maxx;
if(re>0&&(sum<0||n==1)) sum = 0;
cout<<sum<<endl;
}
return 0;
}