Powered By HC TECH : AutoACer Engine

4600-4699
This commit is contained in:
KiritoTRw 2016-09-06 13:33:42 +08:00 committed by GitHub
parent c9791a72c9
commit 34d0415174
80 changed files with 8476 additions and 0 deletions

90
HDOJ/4600_autoAC.cpp Normal file
View File

@ -0,0 +1,90 @@
#include <stdio.h>
#include <string.h>
#define LL __int64
struct corns {
int q, p, n, m;
}a[1111];
int s[11] , ss[11], D, Y;
LL ans , val[1002];
void solve1(int i) {
LL cur = Y;
int day = 0, j;
while(day <= D) {
if(day + a[i].n <= D) {
LL tot = cur/a[i].q;
for(j = 9;j >= 1; j--) if(s[j]) {
if(j*a[i].p > a[i].q) {
if(tot <= s[j]) {
cur += tot * (LL)( j*a[i].p - a[i].q );
tot = 0;
break;
}
else {
cur += (LL)s[j] * ( j*a[i].p - a[i].q );
tot -= s[j];
}
}
else break;
}
}
day += a[i].n;
}
if(cur > ans) ans = cur;
}
void solve2(int i) {
int j, k, l;
memset(val, 0, sizeof(val));
LL cur = Y;
for(j = 1;j <= 9; j++) ss[j] = s[j];
for(j= 0;j <= D; j++) {
cur += val[j];
LL tot = cur/a[i].q;
for(k = 9;k >= 1; k--) if(ss[k]){
int to = j+a[i].n;
if(to > D) break;
LL now = k*a[i].p;
now += (LL)(D-to)/a[i].m * k*a[i].p;
if(now > a[i].q) {
LL get;
if(tot > ss[k]) get = ss[k];
else get = tot;
ss[k] -= (int)get;
tot -= get;
cur -= get*a[i].q;
val[to] += get * k *a[i].p;
for(l = to+a[i].m;l <= D; l += a[i].m) val[l] += get *k * a[i].p;
}
else break;
}
}
if(cur > ans) ans = cur;
}
int main() {
int t, w, h, A, i;
scanf("%d", &t);
while(t--) {
scanf("%d%d%d%d%d", &w, &h, &A, &D, &Y);
memset(s, 0, sizeof(s));
s[9] = (w/3) * (h/3);
s[(w%3)*3] += h/3;
s[(h%3)*3] += w/3;
if((w%3) * (h%3) == 1) {
s[5]++; s[3] -= 2; s[1] += 2;
}
else if((w%3) * (h%3) == 4) {
s[8]++; s[6] -= 2; s[4] += 2;
}
else if((w%3)*(h%3) == 2) {
s[7]++; s[6]--; s[3]--; s[2] += 2;
}
else s[(w%3)*(h%3)] ++;
for(i =0;i < A; i++) scanf("%d%d%d%d", &a[i].q, &a[i].p, &a[i].n, &a[i].m);
ans = Y;
for(i = 0;i < A ; i++) {
if(a[i].m == 0) solve1(i);
else solve2(i);
}
printf("%I64d\n", ans);
}
return 0;
}

179
HDOJ/4601_autoAC.cpp Normal file
View File

@ -0,0 +1,179 @@
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#define ll __int64
using namespace std ;
const int maxn = 211111 ;
int c[26][maxn] , tot , dis[maxn] , pos[maxn] , rk[maxn] ;
int tim[maxn] , dp[25][maxn] , cnt[maxn] , g[maxn] , line[maxn] , lon[maxn] ;
ll val[maxn] , f[25] , pow26[maxn] ;
const int mod = 1000000007 ;
struct Edge
{
int to , next , v ;
} edge[maxn<<1] ;
int head[maxn] , E , n ;
struct Point
{
int id ;
int tim ;
bool operator < ( const Point &t ) const
{
return tim < t.tim ;
}
} ;
vector<Point> vec[maxn] ;
void new_edge ( int a , int b , char *s )
{
edge[E].to = b ;
edge[E].next = head[a] ;
edge[E].v = s[0] - 'a' ;
head[a] = E ++ ;
edge[E].to = a ;
edge[E].next = head[b] ;
edge[E].v = s[0] - 'a' ;
head[b] = E ++ ;
}
int new_node ()
{
int i ;
for ( i = 0 ; i < 26 ; i ++ ) c[i][tot] = 0 ;
val[tot] = 0 ;
return tot ++ ;
}
int step ;
void cal ( int u , int fa , int now )
{
int i ;
Point fuck ;
for ( i = head[u] ; i != -1 ; i = edge[i].next )
{
int v = edge[i].to ;
if ( v == fa ) continue ;
dis[v] = dis[u] + 1 ;
tim[v] = ++ step ;
fuck.id = v , fuck.tim = step ;
vec[dis[v]].push_back ( fuck ) ;
int k = edge[i].v ;
if ( !c[k][now] )
{
c[k][now] = new_node () ;
val[c[k][now]] = ( (ll) val[now] * 26 % mod + k ) % mod ;
}
pos[v] = c[k][now] ;
cal ( v , u , c[k][now] ) ;
lon[u] = max ( lon[u] , lon[v] ) ;
cnt[u] += cnt[v] ;
}
lon[u] = max ( lon[u] , dis[u] ) ;
cnt[u] ++ ;
}
void cal1 ( int now )
{
rk[now] = step ++ ;
g[rk[now]] = now ;
int i ;
for ( i = 0 ; i < 26 ; i ++ )
if ( c[i][now] )
cal1 ( c[i][now] ) ;
}
int dep ;
void init ()
{
for ( int i = 0 ; i <= n ; i ++ )
{
head[i] = -1 , vec[i].clear () ;
lon[i] = dis[i] = rk[i] = cnt[i] = 0 ;
}
tot = E = 0 ;
new_node () ;
}
int T ;
void rmq ()
{
int i , j ;
T = 0 ;
for ( i = 1 ; i <= n ; i ++ )
{
int l = vec[i].size () ;
if ( l == 0 ) break ;
line[i] = T + 1 ;
for ( j = 0 ; j < l ; j ++ )
dp[0][++T] = rk[pos[vec[i][j].id]] ;
}
for ( i = 1 ; f[i] <= T ; i ++ )
for ( j = 1 ; j + f[i] - 1 <= T ; j ++ )
dp[i][j] = max ( dp[i-1][j] , dp[i-1][j+f[i-1]] ) ;
}
int query ( int l , int r )
{
if ( l == r ) return dp[0][l] ;
if ( l > r ) swap ( l , r ) ;
int i , j , k ;
for ( i = 0 ; i <= 22 ; i ++ )
if ( f[i] >= ( r - l + 1 ) )
{
k = i - 1 ;
break ;
}
return max ( dp[k][l] , dp[k][r-f[k]+1] ) ;
}
char s[111] ;
int main ()
{
int cas , i , j , a , b , q ;
f[0] = pow26[0] = 1 ;
for ( i = 1 ; i <= 22 ; i ++ )
f[i] = (ll) f[i-1] * 2 % mod ;
for ( i = 1 ; i < 100005 ; i ++ )
pow26[i] = (ll) pow26[i-1] * 26 % mod ;
scanf ( "%d" , &cas ) ;
while ( cas -- )
{
scanf ( "%d" , &n ) ;
init () ;
for ( i = 1 ; i < n ; i ++ )
{
scanf ( "%d%d%s" , &a , &b , s ) ;
new_edge ( a , b , s ) ;
}
step = 0 ;
cal ( 1 , 0 , 0 ) ;
step = 0 ;
cal1 ( 0 ) ;
rmq () ;
pos[1] = 0 ;
scanf ( "%d" , &q ) ;
while ( q -- )
{
int u , m ;
scanf ( "%d%d" , &u , &m ) ;
if ( m == 0 )
{
puts ( "0" ) ;
continue ;
}
if ( dis[u] + m > lon[u] )
{
puts ( "IMPOSSIBLE" ) ;
continue ;
}
int dep = dis[u] + m ;
Point fuck ;
fuck.tim = tim[u] ;
int l = lower_bound ( vec[dep].begin () , vec[dep].end () , fuck ) - vec[dep].begin () ;
if ( l == vec[dep].size () ) l -- ;
l += line[dep] ;
fuck.tim = tim[u] + cnt[u] - 1 ;
int r = upper_bound ( vec[dep].begin () , vec[dep].end () , fuck ) - vec[dep].begin () ;
r -- ;
r += line[dep] ;
int k = query ( l , r ) ;
k = g[k] ;
u = pos[u] ;
printf ( "%I64d\n" , ( val[k] - val[u] * pow26[m] % mod + mod ) % mod ) ;
}
}
}

68
HDOJ/4602_autoAC.cpp Normal file
View File

@ -0,0 +1,68 @@
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int mod=1000000007;
struct Matrix{
long long arr[2][2];
};
Matrix init,unit;
int n,k;
long long num[2][2]={{4,-4},{1,0}};
void Init(){
for(int i=0;i<2;i++)
for(int j=0;j<2;j++){
init.arr[i][j]=num[i][j];
unit.arr[i][j]=(i==j)?1:0;
}
}
Matrix Mul(Matrix a,Matrix b){
Matrix c;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++){
c.arr[i][j]=0;
for(int k=0;k<2;k++)
c.arr[i][j]=(c.arr[i][j]%mod+a.arr[i][k]*b.arr[k][j]%mod+mod)%mod;
c.arr[i][j]%=mod;
}
return c;
}
Matrix Pow(Matrix a,Matrix b,int k){
while(k){
if(k&1){
b=Mul(a,b);
}
a=Mul(a,a);
k>>=1;
}
return b;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&k);
Init();
if(k>n){
printf("0\n");
continue;
}
int tmp=n-k+1;
if(tmp==1){
printf("1\n");
continue;
}
if(tmp==2){
printf("2\n");
continue;
}
if(tmp==3){
printf("5\n");
continue;
}
Matrix res=Pow(init,unit,tmp-3);
long long ans=(res.arr[0][0]*5+res.arr[0][1]*2)%mod;
cout<<ans<<endl;
}
return 0;
}

370
HDOJ/4603_autoAC.cpp Normal file
View File

@ -0,0 +1,370 @@
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
#include<ctime>
using namespace std;
#ifdef _WIN32
typedef __int64 i64;
#define out64 "%I64d\n"
#define in64 "%I64d"
#else
typedef long long i64;
#define out64 "%lld\n"
#define in64 "%lld"
#endif
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define FOR(i,a,b) for( int i = (a) ; i <= (b) ; i ++)
#define FF(i,a) for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a,b) for( int i = (a) ; i >= (b) ; i --)
#define S64(a) scanf(in64,&a)
#define SS(a) scanf("%d",&a)
#define LL(a) ((a)<<1)
#define RR(a) (((a)<<1)+1)
#define pb push_back
#define pf push_front
#define X first
#define Y second
#define CL(Q) while(!Q.empty())Q.pop()
#define MM(name,what) memset(name,what,sizeof(name))
#define MC(a,b) memcpy(a,b,sizeof(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
const int inf = 0x3f3f3f3f;
const long long inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 10e-9;
const double pi = acos(-1.0);
const int maxn = 100111;
const int maxlevel = 21;
struct Node
{
int now;
int to;
int c;
int tot;
int ss;
const bool operator < (const Node& cmp) const {
return tot > cmp.tot;
}
};
int all;
int n, m;
vector<Node>g[maxn];
int t[maxn];
int dep[maxn];
int df;
int dfn[maxn];
int dfv[maxn * 2];
int st[maxn * 2][maxlevel];
int up[maxn][maxlevel];
int dp[maxn]; //down sum
int xtof[maxn];
int ftox[maxn];
int vis[maxn];
int lg2[maxn*2];
void dfs(int now)
{
vis[now] = true;
int to;
for (int i = 0; i < (int)g[now].size(); i++) {
to = g[now][i].to;
if (!vis[to]) {
t[to] = now;
dfs(to);
}
}
}
int treedp(int now)
{
int to,id;
dp[now] = 0;
for (int i = 0; i < (int)g[now].size(); i++) {
to = g[now][i].to;
if (to != t[now]) {
int temp = treedp(to) + g[now][i].c;
g[now][i].tot = temp;
dp[now] += temp;
}
else {
id = i;
}
}
if (t[now] != -1) {
g[now][id].tot = all - dp[now];
}
return dp[now];
}
void euler_circuit(int now ,int step)
{
dep[now] = step;
dfn[now] = df;
dfv[df++] = now;
int to;
for (int i = 0; i < (int)g[now].size(); i++) {
to = g[now][i].to;
if (to != t[now]) {
euler_circuit(to,step+1);
dfv[df++] = now;
}
}
}
void get_up_node()
{
for (int i = 1; i <= n; i++) {
up[i][0] = t[i];
}
int to;
for (int step = 1; step < maxlevel; step++) {
for (int now = 1; now <= n; now++) {
to = up[now][step - 1];
if (to == -1) {
up[now][step] = -1;
}
else {
up[now][step] = up[to][step - 1];
}
}
}
}
void sparse_table()
{
for (int i = 1; i < df; i++){
st[i][0] = dep[dfv[i]];
}
int to;
for (int step = 1; step <= lg2[n] + 1; step++){
for (int now = 1; now < df; now++) {
to = now + (1 << (step - 1));
if (to < df){
st[now][step] = min(st[now][step - 1], st[to][step - 1]);
}
else{
st[now][step] = st[now][step - 1];
}
}
}
}
void relation()
{
int to;
for (int now = 1; now <= n; now++){
for (int i = 0; i < (int)g[now].size(); i++){
to = g[now][i].to;
if (to == t[now]){
xtof[now] = i;
}
else{
ftox[to] = i;
}
}
}
}
int rmq(int l,int r)
{
return min(st[l][lg2[r - l]], st[r - (1 << lg2[r - l])][lg2[r - l]] );
}
int calculate(int x,bool first,int id1,int id2=-1)
{
if (id2 != -1){
if (id1 > id2){
swap(id1, id2);
}
}
int sum = g[x][0].ss;
sum -= g[x][id1].tot;
if (id2 != -1){
sum -= g[x][id2].tot;
}
int size = (int)g[x].size() - 1;
if (size >= 1){
sum += g[x][1].ss;
}
int ans = g[x][0].ss;
if (id1 % 2 ){
if (id1 + 1 <= size){
ans -= g[x][id1 + 1].ss;
if (id1 + 2 <= size){
ans += g[x][id1 + 2].ss;
}
}
if (id2 != -1){
if (id2 % 2){
ans -= g[x][id2].ss;
if (id2 + 1 <= size){
ans += g[x][id2 + 1].ss;
}
}
else{
if (id2 + 1 <= size){
ans -= g[x][id2 + 1].ss;
if (id2 + 2 <= size){
ans += g[x][id2 + 2].ss;
}
}
}
}
}
else{
ans -= g[x][id1].ss;
if (id1 + 1 <= size){
ans += g[x][id1 + 1].ss;
}
if (id2 != -1){
if (id2 % 2){
ans -= g[x][id2].ss;
if (id2 + 1 <= size){
ans += g[x][id2 + 1].ss;
}
}
else{
if (id2 + 1 <= size){
ans -= g[x][id2 + 1].ss;
if (id2 + 2 <= size){
ans += g[x][id2 + 2].ss;
}
}
}
}
}
if (first) return ans;
else return sum - ans;
}
int go_up(int now, int x)
{
int step = 0;
while (x) {
if (x & 1) {
now = up[now][step];
}
step++;
x >>= 1;
}
return now;
}
int find(int a,int b)
{
int l = dfn[a];
int r = dfn[b];
if (l == r){
return g[a][0].ss;
}
if (l > r){
swap(l, r);
}
int lca = rmq(l, r + 1);
if (dep[a] - lca + dep[b] - lca == 1){
if (dep[a] == lca){
return g[b][xtof[b]].tot + calculate(b, false, xtof[b]);
}
else if (dep[b] == lca){
return g[b][ftox[a]].tot + calculate(b, false, ftox[a]);
}
}
else if (dep[a] > dep[b]+1){
int temp = dep[a] - dep[b];
int mid = lca + temp / 2;
int child = go_up(a, dep[a] - mid - 1);
if (temp % 2){
return g[t[child]][ftox[child]].tot + calculate(t[child], false, ftox[child], xtof[t[child]]);
}
else{
return g[t[child]][ftox[child]].tot + calculate(t[child], true, ftox[child], xtof[t[child]]);
}
}
else if (dep[a] == dep[b] + 1) {
int ca = go_up(a, dep[a] - lca - 1);
int cb = go_up(b, dep[b] - lca - 1);
int meet = t[ca];
return g[meet][ftox[ca]].tot + calculate(meet, false, ftox[ca], ftox[cb]);
}
else if (dep[a] < dep[b]){
int temp = dep[b] - dep[a];
int mid = lca + (temp + 1)/ 2;
int child = go_up(b, dep[b] - mid - 1);
if (temp % 2){
return g[t[child]][xtof[t[child]]].tot + calculate(t[child], false, xtof[t[child]], ftox[child]);
}
else{
return g[t[child]][xtof[t[child]]].tot + calculate(t[child], true, xtof[t[child]], ftox[child]);
}
}
else if(dep[a] == dep[b]) {
int ca = go_up(a, dep[a] - lca - 1);
int cb = go_up(b, dep[b] - lca - 1);
int meet = t[ca];
return g[meet][ftox[ca]].tot + calculate(meet, true, ftox[ca], ftox[cb]);
}
assert(false);
}
void start()
{
for (int i = 1; i <= n; i++) {
vis[i] = false;
}
t[0] = t[1] = -1;
dfs(1);
treedp(1);
for (int now = 1; now <= n; now++) {
sort(g[now].begin(), g[now].end());
for (int i =(int) g[now].size() - 1; i >= 0; i--) {
g[now][i].ss = g[now][i].tot;
if (i + 3 <= (int)g[now].size()) {
g[now][i].ss += g[now][i + 2].ss;
}
}
}
df = 1;
euler_circuit(1, 0);
get_up_node();
sparse_table();
relation();
}
int main()
{
for (int i = 0; i < maxlevel; i++){
if ( (1<<i) < maxn*2)
lg2[1 << i] = i;
}
for (int i = 3; i < maxn*2; i++) {
if (!lg2[i]){
lg2[i] = lg2[i - 1];
}
}
int T;
cin >> T;
while (T--) {
all = 0;
cin >> n >> m;
for (int i = 0; i <= n; i++){
g[i].clear();
}
Node node;
for (int i = 1; i <= n - 1; i++) {
SS(node.now);
SS(node.to);
SS(node.c);
g[node.now].push_back(node);
swap(node.now, node.to);
g[node.now].push_back(node);
all += node.c;
}
start();
int a, b;
for (int i = 1; i <= m; i++){
SS(a); SS(b);
printf("%d\n", find(a, b));
}
}
return 0;
}

59
HDOJ/4604_autoAC.cpp Normal file
View File

@ -0,0 +1,59 @@
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define MAXM 111111
#define MAXN 111111
#define INF 1000000007
#define eps 1e-8
using namespace std;
typedef vector<int>::iterator Viter;
int n;
vector<int>g;
int a[MAXN];
int dp1[MAXN], dp2[MAXN], num1[MAXN], num2[MAXN];
void gao(int dp[], int num[])
{
g.clear();
Viter it;
for(int i = n - 1; i >= 0; i--)
{
int sz = g.size();
if(!sz || a[i] >= g[sz - 1])
{
g.push_back(a[i]);
dp[i] = sz + 1;
}
else
{
it = upper_bound(g.begin(), g.end(), a[i]);
dp[i] = it - g.begin() + 1;
*it = a[i];
}
pair<Viter, Viter>bounds = equal_range(g.begin(), g.end(), a[i]);
num[i] = bounds.second - bounds.first;
}
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i = 0; i < n; i++) scanf("%d", &a[i]);
gao(dp1, num1);
for(int i = 0; i < n; i++) a[i] = -a[i];
gao(dp2, num2);
int ans = 0;
for(int i = 0; i < n; i++)
ans = max(ans, dp1[i] + dp2[i] - min(num1[i], num2[i]));
printf("%d\n", ans);
}
return 0;
}

142
HDOJ/4605_autoAC.cpp Normal file
View File

@ -0,0 +1,142 @@
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string.h>
using namespace std;
#define maxn 100005
struct list
{
int l,r;
int w;
}node[maxn];
int fs[maxn*2];
int fss[maxn*2];
struct qq
{
int x;
int id;
}xx;
vector<qq>num[maxn];
int ans[maxn][2];
int sum[maxn][2];
int w[maxn];
int n,m,q,len;
int lowbit(int x)
{
return (x&(-x));
}
int search(int l,int r,int w)
{
while(l<r)
{
int mid=(l+r)/2;
if(fs[mid]==w)return mid;
if(fs[mid]<w)l=mid+1;
else r=mid;
}
}
void add(int x,int bs,int num)
{
for(;x<len;x+=lowbit(x))
{
sum[x][bs]+=num;
}
}
int allsum(int s,int bs)
{
int ss;
ss=0;
while(s>0)
{
ss+=sum[s][bs];
s=s-lowbit(s);
}
return ss;
}
void dfs(int x)
{
int s;
s=num[x].size();
for(int i=0;i<s;i++)
{
xx=num[x][i];
int z,id;
z=search(1,len,xx.x);
id=xx.id;
if(allsum(z,0)-allsum(z-1,0)+allsum(z,1)-allsum(z-1,1)>0)
{
ans[id][0]=-1;
continue;
}
int ll,lr,rl,rr;
ll=allsum(len-1,0)-allsum(z,0);
rl=allsum(z,0);
lr=allsum(len-1,1)-allsum(z,1);
rr=allsum(z,1);
ans[id][0]=rr;
ans[id][1]=(rl+rr)*3+ll+lr;
}
s=search(1,len,node[x].w);
if(node[x].l!=-1)
{
add(s,0,1);
dfs(node[x].l);
add(s,0,-1);
}
if(node[x].r!=-1)
{
add(s,1,1);
dfs(node[x].r);
add(s,1,-1);
}
}
int main()
{
int T,a,b,c,i;
cin>>T;
while(T--)
{
cin>>n;
int ll=1;
memset(fs,0,sizeof(fs));
memset(ans,0,sizeof(ans));
memset(fss,0,sizeof(fss));
for (i = 1; i <= n; ++i) node[i].l = node[i].r = node[i].w = -1;
for(i=1;i<=n;i++)
{
num[i].clear();
scanf("%d",&w[i]);
node[i].w=w[i];
fss[ll++]=w[i];
}
cin>>m;
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
node[a].l=b;
node[a].r=c;
}
cin>>q;
for(i=1;i<=q;i++)
{
scanf("%d%d",&a,&b);
fss[ll++]=b;
xx.id=i;
xx.x=b;
num[a].push_back(xx);
}
len=1;
sort(fss,fss+ll);
for(i=1;i<ll;i++)
if(fss[i]!=fss[i-1])fs[len++]=fss[i];
dfs(1);
for(i=1;i<=q;i++)
{
if(ans[i][0]==-1)printf("0\n");
else printf("%d %d\n",ans[i][0],ans[i][1]);
}
}
return 0;
}

191
HDOJ/4606_autoAC.cpp Normal file
View File

@ -0,0 +1,191 @@
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <vector>
using namespace std;
const double eps = 1e-6;
int sgn(double x)
{
if(fabs(x) < eps)return 0;
if(x < 0)return -1;
else return 1;
}
struct Point
{
double x,y;
Point(double _x = 0,double _y = 0)
{
x = _x; y = _y;
}
Point operator -(const Point &b)const
{
return Point(x-b.x,y-b.y);
}
double operator ^(const Point &b)const
{
return x*b.y - y*b.x;
}
double operator *(const Point &b)const
{
return x*b.x + y*b.y;
}
void input()
{
scanf("%lf%lf",&x,&y);
}
};
struct Line
{
Point s,e;
Line(){}
Line(Point _s,Point _e)
{
s = _s; e = _e;
}
void input()
{
s.input();
e.input();
}
};
double dist(Point a,Point b)
{
return sqrt((a-b)*(a-b));
}
bool inter(Line l1,Line l2)
{
return
sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) < 0 &&
sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e)) < 0;
}
const int MAXN = 440;
Point p[MAXN];
Line line[MAXN];
const double INF = 100000000.0;
double dis[MAXN][MAXN];
int n,m,P;
int b[MAXN];
int uN,vN;
struct Edge
{
int to,next;
}edge[MAXN*MAXN];
int head[MAXN];
int tot;
void addedge(int u,int v)
{
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void init()
{
tot = 0;
memset(head,-1,sizeof(head));
}
int linker[MAXN];
bool used[MAXN];
bool dfs(int u)
{
for(int i = head[u]; i!= -1;i = edge[i].next)
{
int v = edge[i].to;
if(!used[v])
{
used[v] = true;
if(linker[v]==-1 || dfs(linker[v]))
{
linker[v] = u;
return true;
}
}
}
return false;
}
int hungary()
{
int res = 0;
memset(linker,-1,sizeof(linker));
for(int u = 0;u < uN;u ++)
{
memset(used,false,sizeof(used));
if(dfs(u))res++;
}
return res;
}
bool check(double d)
{
uN = vN = n;
init();
for(int i = 0;i < n;i++)
for(int j = i+1;j < n;j++)
if(dis[b[i]][b[j]] < d + eps)
addedge(b[i],b[j]);
if(n - hungary() <= P)return true;
else return false;
}
double solve()
{
double l = 0, r = 100000.0;
double ans;
while(r-l >= eps)
{
double mid = (l+r)/2;
if(check(mid))
{
ans = mid;
r = mid - eps;
}
else l = mid + eps;
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&P);
for(int i = 0;i < n;i++)
p[i].input();
int t = n;
for(int i = 0;i < m;i++)
{
line[i].input();
p[n+2*i] = line[i].s;
p[n+2*i+1] = line[i].e;
}
for(int i = 0;i < n+2*m;i++)
for(int j = 0;j < n+2*m;j++)
{
if(i == j)
{
dis[i][j] = 0;
continue;
}
bool flag = false;
for(int k = 0;k < m;k++)
if(inter(line[k],Line(p[i],p[j])))
{
flag = true;
break;
}
if(flag)dis[i][j] = 1e20;
else dis[i][j] = dist(p[i],p[j]);
}
for(int k = 0;k < n+2*m;k++)
for(int i = 0;i < n+2*m;i++)
for(int j = 0;j < n+2*m;j++)
dis[i][j] = min(dis[i][j],dis[i][k]+dis[k][j]);
for(int i = 0;i < n;i++)
{
scanf("%d",&b[i]);
b[i]--;
}
printf("%.2lf\n",solve());
}
return 0;
}

81
HDOJ/4607_autoAC.cpp Normal file
View File

@ -0,0 +1,81 @@
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
const int N=200010;
int head[N],to[N],ne[N],w[N];
int dis[N],que[N];
bool vis[N];
int edge,m,n;
void init()
{
memset(head,-1,sizeof(head));
edge=0;
}
void add(int u,int v,int c)
{
to[edge]=v,w[edge]=c,ne[edge]=head[u],head[u]=edge++;
to[edge]=u,w[edge]=c,ne[edge]=head[v],head[v]=edge++;
}
void bfs(int s)
{
memset(vis,0,sizeof(vis));
memset(dis,0,sizeof(dis));
int l,r,v,u;
l=r=0;
vis[s]=1;
dis[s]=0;
que[r++]=s;
while(r>l)
{
u=que[l++];
for(int i=head[u]; ~i; i=ne[i])
{
if(!vis[v=to[i]])
{
vis[v]=1;
dis[v]=dis[u]+w[i];
que[r++]=v;
}
}
}
}
int treediameter(int s)
{
int u,maxl;
bfs(s);
maxl=0,u=s;
for(int i=1; i<=n; i++)
if(dis[i]>maxl)
u=i,maxl=dis[i];
bfs(u);
maxl=0;
for(int i=1; i<=n; i++)
if(dis[i]>maxl)
maxl=dis[i];
return maxl;
}
int main()
{
int u,v,d=1,t,i,j,x;
scanf("%d",&t);
while(t--)
{
init();
scanf("%d%d",&n,&m);
for(i=1;i<=n-1;i++)
{
scanf("%d%d",&u,&v);
add(u,v,1);
}
int ans=treediameter(1);
ans++;
while(m--)
{
scanf("%d",&x);
if(x<=ans) printf("%d\n",x-1);
else printf("%d\n",(x-ans)*2+ans-1);
}
}
return 0;
}

61
HDOJ/4608_autoAC.cpp Normal file
View File

@ -0,0 +1,61 @@
#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
char str[100050];
int num[100050];
int main()
{
int t,i;
while(~scanf("%d",&t))
{
while(t--)
{
scanf("%s",str);
int n;
n = strlen(str);
for(i = 0;i < n;i++)
num[i] = str[i]-'0';
int rest;
rest = 0;
for(i = 0;i < n;i++)
rest += num[i];
rest = 10-rest%10;
num[n-1] += rest;
int bit;
bit = 0;
if(num[n-1] > 9)
{
for(i = n-2;i >= 0;i--)
{
bit++;
num[i]++;
num[i] %= 10;
if(num[i])
break;
}
if(i<0)
{
printf("1");
for(i = n-1;i > 0;i--)
printf("0");
puts("9");
}
else
{
num[n-1] = (num[n-1]+1000000-bit)%10;
for(i = 0;i < n;i++)
printf("%d",num[i]);
puts("");
}
}
else
{
for(i = 0;i < n;i++)
printf("%d",num[i]);
puts("");
}
}
}
return 0;
}

121
HDOJ/4609_autoAC.cpp Normal file
View File

@ -0,0 +1,121 @@
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const double PI = acos(-1.0);
struct complex
{
double r,i;
complex(double _r = 0,double _i = 0)
{
r = _r; i = _i;
}
complex operator +(const complex &b)
{
return complex(r+b.r,i+b.i);
}
complex operator -(const complex &b)
{
return complex(r-b.r,i-b.i);
}
complex operator *(const complex &b)
{
return complex(r*b.r-i*b.i,r*b.i+i*b.r);
}
};
void change(complex y[],int len)
{
int i,j,k;
for(i = 1, j = len/2;i < len-1;i++)
{
if(i < j)swap(y[i],y[j]);
k = len/2;
while( j >= k)
{
j -= k;
k /= 2;
}
if(j < k)j += k;
}
}
void fft(complex y[],int len,int on)
{
change(y,len);
for(int h = 2;h <= len;h <<= 1)
{
complex wn(cos(-on*2*PI/h),sin(-on*2*PI/h));
for(int j = 0;j < len;j += h)
{
complex w(1,0);
for(int k = j;k < j+h/2;k++)
{
complex u = y[k];
complex t = w*y[k+h/2];
y[k] = u+t;
y[k+h/2] = u-t;
w = w*wn;
}
}
}
if(on == -1)
for(int i = 0;i < len;i++)
y[i].r /= len;
}
const int MAXN = 400040;
complex x1[MAXN];
int a[MAXN/4];
long long num[MAXN];
long long sum[MAXN];
int main()
{
int T;
int n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
memset(num,0,sizeof(num));
for(int i = 0;i < n;i++)
{
scanf("%d",&a[i]);
num[a[i]]++;
}
sort(a,a+n);
int len1 = a[n-1]+1;
int len = 1;
while( len < 2*len1 )len <<= 1;
for(int i = 0;i < len1;i++)
x1[i] = complex(num[i],0);
for(int i = len1;i < len;i++)
x1[i] = complex(0,0);
fft(x1,len,1);
for(int i = 0;i < len;i++)
x1[i] = x1[i]*x1[i];
fft(x1,len,-1);
for(int i = 0;i < len;i++)
num[i] = (long long)(x1[i].r+0.5);
len = 2*a[n-1];
for(int i = 0;i < n;i++)
num[a[i]+a[i]]--;
for(int i = 1;i <= len;i++)
{
num[i]/=2;
}
sum[0] = 0;
for(int i = 1;i <= len;i++)
sum[i] = sum[i-1]+num[i];
long long cnt = 0;
for(int i = 0;i < n;i++)
{
cnt += sum[len]-sum[a[i]];
cnt -= (long long)(n-1-i)*i;
cnt -= (n-1);
cnt -= (long long)(n-1-i)*(n-i-2)/2;
}
long long tot = (long long)n*(n-1)*(n-2)/6;
printf("%.7lf\n",(double)cnt/tot);
}
return 0;
}

142
HDOJ/4610_autoAC.cpp Normal file
View File

@ -0,0 +1,142 @@
#include<cstdio>
#include<iostream>
#include<bitset>
#include<cmath>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = (int)4e6+10;
const int M = (int)1e6+10;
typedef pair<int,int> pii;
bool p[N];
int prime[M];
void init(){
memset(p,true,sizeof(p));
p[0] = p[1] = false;
for(int i=2;i*i<N;i++)
for(int j=i;j*i<N;j++)
p[i*j] = 0;
prime[0] = 0;
for(int i=2;i<N;i++)
if(p[i])prime[++prime[0]] = i;
}
vector<pii>factor;
void div(int n){
factor.clear();
for(int i=1;prime[i] * prime[i] <= n;i++){
if(n % prime[i] == 0){
int cnt = 0;
while(n % prime[i] == 0){
n /= prime[i];
++cnt;
}
factor.push_back(make_pair(prime[i],cnt));
}
}
if(n > 1)factor.push_back(make_pair(n,1));
}
ll Pow(ll a,ll b){
ll ans = 1;
while(b){
if(b&1)ans = ans * a;
a = a * a;
b >>= 1;
}
return ans;
}
bool isPrime_amount(int n){
if(factor.size() > 1|| n==1 )return false;
return p[factor[0].second + 1];
}
bool isPrime_sum(int n){
if(factor.size() > 1|| n== 1)return false;
int a = factor[0].first,b = factor[0].second;
int sum = 1LL*(Pow(a,b+1) - 1) / (a - 1);
return p[sum];
}
bool isSquare(ll n){
ll _ = sqrt(n*1.0);
return _ * _ == n;
}
bool isSquare_product(int n){
int all = 1;
for(int i=0;i<factor.size();i++)
all *= factor[i].second + 1;
ll sq = sqrt(n*1.0);
ll cur = 1;
int p = all >> 1;
if(p & 1)cur *= n;
if(all & 1)cur *= sq;
return isSquare(cur);
}
int extra[5];
int V[16],cur[5],hash[16];
int have[16];
int main(){
for(int i=0;i<16;i++){
int all = 0;
for(int j=0;j<4;j++)
if(!(i&(1<<j)))++all;
have[i] = all;
}
init();
int T,n,K;
scanf("%d",&T);
while(T--){
memset(V,0,sizeof(V));
scanf("%d%d",&n,&K);
for(int i=1;i<=n;i++){
int a,b;
scanf("%d%d",&a,&b);
int flag = 15,cnt = 0;
div(a);
if(p[a])flag ^= 1<<0 ,cnt ++;
if(isPrime_amount(a)) flag ^= 1<<1,cnt ++;
if(isPrime_sum(a))flag ^= 1<<2,cnt ++;
if(isSquare_product(a)) flag ^= 1<<3,cnt++;
V[flag] += b;
if(i!=1)printf(" ");
printf("%d",cnt);
}
puts("");
int ans = -(~0u>>2);
for(int i=0;i< 4;i++)scanf("%d",&extra[i]);
for(int i=0;i < 1<<16;i++){
memset(cur,0,sizeof(cur));
int res = K,number = 0,all = 0;
int flag = 15;
for(int j = 0;j < 16;j++){
if(i&(1<<j)){
if(V[j] > 0){
flag &= j;
res --;
all += have[j];
cur[have[j]] += V[j] - 1;
}else{
res = -1;
break;
}
}
}
if(res < 0 )continue;
for(int k=4;k >= 0 && res > 0;k--){
if(cur[k] <= res){
all += k * cur[k];
res -= cur[k];
}else{
all += res * k;
res =0;
}
}
if(res == 0){
for(int j=0;j<4;j++)
if(flag & (1<<j))all += extra[j];
ans = max(ans,all);
}
}
printf("%d\n",ans);
}
return 0;
}

38
HDOJ/4611_autoAC.cpp Normal file
View File

@ -0,0 +1,38 @@
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
long long gcd(long long x,long long y)
{
return y==0?x:gcd(y,x%y);
}
long long Count(long long n,long long a,long long b)
{
long long now=0,ret=0,tmp,x=0,y=0;
while(now<n)
{
tmp=min(a-x,b-y);
if (now+tmp>n) tmp=n-now;
ret+=tmp*abs(x-y);
x=(x+tmp)%a;
y=(y+tmp)%b;
now+=tmp;
}
return ret;
}
int main()
{
int i,j,T;
long long a,b,n,l,ans;
scanf("%d",&T);
while(T--)
{
cin>>n>>a>>b;
l=a*b/gcd(a,b);
if (l>=n) ans=Count(n,a,b);
else ans=Count(l,a,b)*(n/l)+Count(n%l,a,b);
cout<<ans<<endl;
}
return 0;
}

171
HDOJ/4612_autoAC.cpp Normal file
View File

@ -0,0 +1,171 @@
#pragma comment(linker,"/STACK:102400000,102400000")
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
using namespace std;
#define CL(x,v); memset(x,v,sizeof(x));
#define INF 0x3f3f3f3f
#define LL long long
#define REP(i,r,n) for(int i=r;i<=n;i++)
#define RREP(i,n,r) for(int i=n;i>=r;i--)
const int MAXN=2e5+100;
int n,m;
struct Edge{
int u,v;
Edge(){}
Edge(int a,int b){
u=a;
v=b;
}
};
vector<Edge> edges;
vector<int> G[MAXN];
vector<int> P[MAXN];
int dfs_clock,bcc_cnt;
int pre[MAXN];
int T[MAXN];
int dfs1(int u,int fa)
{
int lowu=pre[u]=++dfs_clock;
for(int i=0;i<G[u].size();i++)
{
int mm=G[u][i];
int v=edges[mm].v;
if(fa==(mm^1))continue;
if(!pre[v])
{
int lowv=dfs1(v,mm);
lowu=min(lowu,lowv);
if(lowv>pre[u])
{
P[v].push_back(u);
P[u].push_back(v);
}
}
else if(pre[v]<pre[u])
{
lowu=min(pre[v],lowu);
}
}
return lowu;
}
void dfs2(int u,int fa)
{
T[u]=bcc_cnt;
for(int i=0;i<G[u].size();i++)
{
int v=edges[G[u][i]].v;
bool f=true;
for(int j=0;j<P[u].size();j++)
{
int vv=P[u][j];
if(v==vv)
{
f=false;break;
}
}
if(!f||T[v])continue;
dfs2(v,u);
}
}
void find_bcc(int n)
{
dfs_clock=0,bcc_cnt=0;
memset(pre,0,sizeof(pre));
memset(T,0,sizeof(T));
for(int i=1;i<=n;i++)
if(!pre[i])
dfs1(i,-1);
for(int i=1;i<=n;i++)
if(!T[i])
{
bcc_cnt++;
dfs2(i,-1);
}
}
vector<int> GG[MAXN];
int d[MAXN];
void dfs(int u,int dep)
{
d[u]=dep;
for(int i=0;i<GG[u].size();i++)
{
int v=GG[u][i];
if(!d[v])
{
dfs(v,dep+1);
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
if(n+m==0)break;
REP(i,0,n){
G[i].clear();
P[i].clear();
}
edges.clear();
int a,b;
REP(i,1,m)
{
scanf("%d%d",&a,&b);
Edge e;
e=Edge(a,b);
edges.push_back(e);
e=Edge(b,a);
edges.push_back(e);
int mm=edges.size();
G[a].push_back(mm-2);
G[b].push_back(mm-1);
}
find_bcc(n);
REP(i,0,n)
{
GG[i].clear();
}
for(int i=1;i<=n;i++)
{
for(int j=0;j<P[i].size();j++)
{
int v=P[i][j];
if(T[i]!=T[v])
{
GG[T[i]].push_back(T[v]);
}
}
}
CL(d,0);
dfs(1,1);
int maxn=0;
int flag=0;
for(int i=1;i<=bcc_cnt;i++)
{
if(d[i]>maxn)
{
maxn=d[i];
flag=i;
}
}
CL(d,0);
dfs(flag,1);
maxn=0;
for(int i=1;i<=bcc_cnt;i++)
{
if(d[i]>maxn)
maxn=d[i];
}
cout<<bcc_cnt-maxn<<endl;
}
return 0;
}

205
HDOJ/4614_autoAC.cpp Normal file
View File

@ -0,0 +1,205 @@
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <string>
#include <math.h>
using namespace std;
const int MAXN = 50010;
struct Node
{
int l,r;
int sum;
int lazy;
int first;
int last;
}segTree[MAXN*3];
void push_up(int i)
{
if(segTree[i].l==segTree[i].r)return;
segTree[i].sum = segTree[i<<1].sum+segTree[(i<<1)|1].sum;
if(segTree[i<<1].first != -1)segTree[i].first = segTree[i<<1].first;
else segTree[i].first = segTree[(i<<1)|1].first;
if(segTree[(i<<1)|1].last != -1)segTree[i].last = segTree[(i<<1)|1].last;
else segTree[i].last = segTree[(i<<1)].last;
}
void push_down(int i)
{
if(segTree[i].r == segTree[i].l)return;
if(segTree[i].lazy==1)
{
segTree[i<<1].first = segTree[i<<1].l;
segTree[i<<1].last = segTree[i<<1].r;
segTree[i<<1].sum = segTree[i<<1].r-segTree[i<<1].l+1;
segTree[i<<1].lazy=1;
segTree[(i<<1)|1].first = segTree[(i<<1)|1].l;
segTree[(i<<1)|1].last = segTree[(i<<1)|1].r;
segTree[(i<<1)|1].sum = segTree[(i<<1)|1].r-segTree[(i<<1)|1].l+1;
segTree[(i<<1)|1].lazy=1;
}
if(segTree[i].lazy == -1)
{
segTree[i<<1].first = -1;
segTree[i<<1].last = -1;
segTree[i<<1].sum = 0;
segTree[i<<1].lazy=-1;
segTree[(i<<1)|1].first = -1;
segTree[(i<<1)|1].last = -1;
segTree[(i<<1)|1].sum = 0;
segTree[(i<<1)|1].lazy=-1;
}
segTree[i].lazy = 0;
}
void build(int i,int l,int r)
{
segTree[i].l = l;
segTree[i].r = r;
segTree[i].sum = r-l+1;
segTree[i].lazy = 0;
segTree[i].first = l;
segTree[i].last = r;
if(l==r)return ;
int mid = (l+r)/2;
build(i<<1,l,mid);
build((i<<1)|1,mid+1,r);
}
void update(int i,int l,int r,int type)
{
if(segTree[i].l == l && segTree[i].r==r)
{
if(type == 0)
{
if(segTree[i].sum == 0)return;
segTree[i].sum = 0;
segTree[i].lazy = -1;
segTree[i].first = -1;
segTree[i].last = -1;
return;
}
else if(type == 1)
{
if(segTree[i].sum == segTree[i].r-segTree[i].l+1)return;
segTree[i].sum = segTree[i].r-segTree[i].l+1;
segTree[i].lazy = 1;
segTree[i].first = segTree[i].l;
segTree[i].last = segTree[i].r;
return;
}
}
push_down(i);
int mid = (segTree[i].l + segTree[i].r)/2;
if(r <= mid)update(i<<1,l,r,type);
else if(l > mid)update((i<<1)|1,l,r,type);
else
{
update(i<<1,l,mid,type);
update((i<<1)|1,mid+1,r,type);
}
push_up(i);
}
int sum(int i,int l,int r)
{
if(segTree[i].l == l && segTree[i].r == r)
{
return segTree[i].sum;
}
push_down(i);
int mid = (segTree[i].l + segTree[i].r)/2;
if(r <= mid)return sum(i<<1,l,r);
else if(l > mid)return sum((i<<1)|1,l,r);
else return sum((i<<1)|1,mid+1,r)+sum(i<<1,l,mid);
}
int n,m;
int query1(int i,int l,int r)
{
if(segTree[i].l == l && segTree[i].r == r)
{
return segTree[i].first;
}
push_down(i);
int mid = (segTree[i].l + segTree[i].r)/2;
int ans1,ans2;
if(r <= mid)return query1(i<<1,l,r);
else if(l > mid)return query1((i<<1)|1,l,r);
else
{
ans1 = query1(i<<1,l,mid);
if(ans1 != -1)return ans1;
return query1((i<<1)|1,mid+1,r);
}
}
int query2(int i,int l,int r)
{
if(segTree[i].l == l && segTree[i].r == r)
{
return segTree[i].last;
}
push_down(i);
int mid = (segTree[i].l + segTree[i].r)/2;
int ans1,ans2;
if(r <= mid)return query2(i<<1,l,r);
else if(l > mid)return query2((i<<1)|1,l,r);
else
{
ans1 = query2((i<<1)|1,mid+1,r);
if(ans1 != -1)return ans1;
return query2(i<<1,l,mid);
}
}
int bisearch(int A,int F)
{
if(sum(1,A,n)==0)return -1;
if(sum(1,A,n)<F)return n;
int l= A,r = n;
int ans=A;
while(l<=r)
{
int mid = (l+r)/2;
if(sum(1,A,mid)>=F)
{
ans = mid;
r = mid-1;
}
else l = mid+1;
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
build(1,1,n);
int op,u,v;
while(m--)
{
scanf("%d%d%d",&op,&u,&v);
if(op == 1)
{
u++;
int t = bisearch(u,v);
if(t==-1)
{
printf("Can not put any one.\n");
continue;
}
printf("%d %d\n",query1(1,u,t)-1,query2(1,u,t)-1);
update(1,u,t,0);
}
else
{
u++;v++;
printf("%d\n",v-u+1-sum(1,u,v));
update(1,u,v,1);
}
}
printf("\n");
}
return 0;
}

81
HDOJ/4616_autoAC.cpp Normal file
View File

@ -0,0 +1,81 @@
#include <cstdio>
#include <cstring>
#define N 50005
#define INF 99999999
struct Edge
{
int e, next;
}edge[N*2];
int head[N], eNum;
int c[N], val[N], n, k, ans, dp[N][4][2];
void init()
{
memset(head, -1, sizeof(head));
eNum = 0; ans = 0;
for(int i=0; i<n; i++)
{
for(int j=0; j<4; j++)
{
for(int l=0; l<2; l++)
dp[i][j][l] = -INF;
}
}
}
void AddEdge(int u, int v)
{
edge[eNum].e = v;
edge[eNum].next = head[u];
head[u] = eNum++;
}
int max2(int x, int y)
{
return x > y ? x : y;
}
void dfs(int u, int fa)
{
dp[u][c[u]][c[u]] = val[u];
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v = edge[i].e;
if(v==fa) continue;
dfs(v, u);
for(int j=0; j<=k; j++)
{
for(int l=0; l+j<=k; l++)
{
ans = max2(ans, dp[u][j][1]+dp[v][l][1]);
if(j+l!=k) ans = max2(ans, dp[u][j][0]+dp[v][l][0]);
ans = max2(ans, dp[u][j][0]+dp[v][l][1]);
ans = max2(ans, dp[u][j][1]+dp[v][l][0]);
}
}
for(int j=0; j<k; j++)
{
dp[u][j+c[u]][1] = max2(dp[u][j+c[u]][1],dp[v][j][1]+val[u]);
dp[u][j+c[u]][0] = max2(dp[u][j+c[u]][0],dp[v][j][0]+val[u]);
}
if(c[u]==0) dp[u][k][1] = max2(dp[u][k][1], dp[v][k][1]+val[u]);
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
for(int i=0; i<n; i++)
scanf("%d%d",&val[i],&c[i]);
init();
int u, v;
for(int i=0; i<n-1; i++)
{
scanf("%d%d",&u,&v);
AddEdge(u, v);
AddEdge(v, u);
}
dfs(0,-1);
printf("%d\n",ans);
}
return 0;
}

120
HDOJ/4617_autoAC.cpp Normal file
View File

@ -0,0 +1,120 @@
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <string>
#include <math.h>
using namespace std;
const double eps = 1e-8;
int sgn(double x)
{
if(fabs(x) < eps)return 0;
if(x < 0)return -1;
else return 1;
}
struct Point3D
{
double x,y,z;
Point3D(double _x = 0,double _y = 0,double _z = 0)
{
x = _x;
y = _y;
z = _z;
}
Point3D operator -(const Point3D &b)const
{
return Point3D(x-b.x,y-b.y,z-b.z);
}
Point3D operator ^(const Point3D &b)const
{
return Point3D(y*b.z-z*b.y,z*b.x-x*b.z,x*b.y-y*b.x);
}
double operator *(const Point3D &b)const
{
return x*b.x+y*b.y+z*b.z;
}
void input()
{
scanf("%lf%lf%lf",&x,&y,&z);
}
};
double Norm(Point3D p)
{
return sqrt(p*p);
}
double calc(Point3D a,Point3D k1,Point3D b,Point3D k2)
{
Point3D tmp = k1^k2;
return fabs(tmp*(a-b))/sqrt(tmp*tmp);
}
struct Node
{
Point3D o,p1,p2;
void input()
{
o.input();
p1.input();
p2.input();
}
}node[50];
int main()
{
int T;
int n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i = 0;i < n;i++)
node[i].input();
bool flag = false;
double Min = 1e20;
for(int i = 0;i < n;i++)
for(int j = i+1;j < n;j++)
{
if(flag)break;
double r1 = sqrt((node[i].p1-node[i].o)*(node[i].p1-node[i].o));
double r2 = sqrt((node[j].p1-node[j].o)*(node[j].p1-node[j].o));
Point3D k1 = (node[i].p1-node[i].o)^(node[i].p2-node[i].o);
Point3D k2 = (node[j].p1-node[j].o)^(node[j].p2-node[j].o);
if(sgn(Norm(k1^k2))==0)
{
if(sgn( Norm( k1^(node[i].o-node[j].o)) ) == 0)
{
if(sgn(r1-r2) == 0)
{
flag = true;
break;
}
else continue;
}
else
{
double dd = (k1*(node[i].o-node[j].o))/Norm(k1);
double d = sqrt( Norm(node[i].o-node[j].o)*Norm(node[i].o-node[j].o) - dd*dd );
if(d > fabs(r1-r2) &&d < fabs(r1+r2))
{
flag = true;
break;
}
Min = min(Min,d-r1-r2);
}
continue;
}
double d = calc(node[i].o,k1,node[j].o,k2);
if(d < r1 + r2 -eps)
{
flag = true;
break;
}
Min = min(Min,d-r1-r2);
}
if(flag || sgn(Min)<=0)printf("Lucky\n");
else printf("%.2lf\n",Min);
}
return 0;
}

82
HDOJ/4619_autoAC.cpp Normal file
View File

@ -0,0 +1,82 @@
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
using namespace std;
const int MAXN=2505;
int linker[MAXN];
bool used[MAXN];
vector<int>map[MAXN];
int uN;
bool dfs(int u)
{
for(int i=0;i<map[u].size();i++)
{
if(!used[map[u][i]])
{
used[map[u][i]]=true;
if(linker[map[u][i]]==-1||dfs(linker[map[u][i]]))
{
linker[map[u][i]]=u;
return true;
}
}
}
return false;
}
int hungary()
{
int u;
int res=0;
memset(linker,-1,sizeof(linker));
for(u=0;u<uN;u++)
{
memset(used,false,sizeof(used));
if(dfs(u)) res++;
}
return res;
}
pair<int,int>p1[MAXN];
pair<int,int>p2[MAXN];
int main()
{
int n,m;
int x,y;
while(scanf("%d%d",&n,&m)==2)
{
if(n==0 &&m==0)break;
for(int i = 0;i < n;i++)
{
scanf("%d%d",&x,&y);
p1[i]= make_pair(x,y);
}
for(int i = 0;i < m;i++)
{
scanf("%d%d",&x,&y);
p2[i]= make_pair(x,y);
}
uN = n;
for(int i = 0;i < n;i++)
map[i].clear();
for(int i = 0;i < n;i++)
{
for(int j = 0;j < m;j++)
{
int x1 = p1[i].first;
int y1 = p1[i].second;
int x2 = p2[j].first;
int y2 = p2[j].second;
if( (x1==x2 && y1==y2)
||(x1==x2 && y1==y2+1)
||(x1+1==x2 && y1==y2)
||(x1+1==x2 && y1==y2+1)
)
map[i].push_back(j);
}
}
int ans = n+m-hungary();
printf("%d\n",ans);
}
return 0;
}

76
HDOJ/4620_autoAC.cpp Normal file
View File

@ -0,0 +1,76 @@
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
struct node
{
int t,p[15],c,id;
}cut[35];
bool cmp(node x,node y)
{
return x.t<y.t;
}
int T,longe,ans[35],tmpa[35],tmpl,n,w,m,vis[205],num;
void dfs(int pos,int lastt)
{
if(n-pos+tmpl<=longe) return;
int num,i,j,tmp;
for(i=pos+1;i<=n;i++)
{
for(j=1,num=0;j<=cut[i].c;j++)
{
tmp=cut[i].p[j];
if(vis[tmp]==0)
num++;
vis[tmp]++;
}
if((cut[i].t-lastt<=w||!tmpl)&&num>=3)
{
tmpl++;
tmpa[tmpl]=cut[i].id;
dfs(i,cut[i].t);
tmpl--;
}
for(j=1;j<=cut[i].c;j++)
{
tmp=cut[i].p[j];
vis[tmp]--;
}
}
if(longe<tmpl)
{
longe=tmpl;
for(j=0;j<longe;j++)
ans[j]=tmpa[j+1];
}
}
int main()
{
int i,j;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&w);
for(i=1;i<=n;i++)
{
scanf("%d%d",&cut[i].c,&cut[i].t);
for(j=1;j<=cut[i].c;j++)
scanf("%d",&cut[i].p[j]);
cut[i].id=i;
}
sort(cut+1,cut+n+1,cmp);
memset(vis,0,sizeof vis);
longe=0;tmpl=0;
dfs(0,cut[1].t);
sort(ans,ans+longe);
printf("%d\n",longe);
for(i=0;i<longe-1;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[longe-1]);
}
return 0;
}

116
HDOJ/4622_autoAC.cpp Normal file
View File

@ -0,0 +1,116 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include <stdlib.h>
#define ll __int64
using namespace std ;
const int maxn = 1111111 ;
ll f[maxn] , g[maxn] ;
int wa[maxn] , wb[maxn] , wv[maxn] , ws[maxn] , pos[maxn] ;
struct suf
{
int sa[maxn] , hei[maxn] , rank[maxn] ;
int cmp ( int *r , int i , int j , int l )
{
return r[i] == r[j] && r[i+l] == r[j+l] ;
}
void da ( int *r , int n , int m )
{
int *x = wa , *y = wb , *t ;
int i , j , p ;
for ( i = 0 ; i < m ; i ++ ) ws[i] = 0 ;
for ( i = 0 ; i < n ; i ++ ) ws[x[i]=r[i]] ++ ;
for ( i = 1 ; i < m ; i ++ ) ws[i] += ws[i-1] ;
for ( i = n - 1 ; i >= 0 ; i -- ) sa[--ws[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 ( sa[i] >= j ) y[p++] = sa[i] - j ;
for ( i = 0 ; i < m ; i ++ ) ws[i] = 0 ;
for ( i = 0 ; i < n ; i ++ ) ws[x[i]] ++ ;
for ( i = 1 ; i < m ; i ++ ) ws[i] += ws[i-1] ;
for ( i = n - 1 ; i >= 0 ; i -- ) sa[--ws[x[y[i]]]] = y[i] ;
for ( t = x , x = y , y = t , p = 1 , x[sa[0]] = 0 , i = 1 ; i < n ; i ++ )
x[sa[i]] = cmp ( y , sa[i-1] , sa[i] , j ) ? p - 1 : p ++ ;
}
int k = 0 ;
for ( i = 1 ; i < n ; i ++ ) rank[sa[i]] = i ;
for ( i = 0 ; i < n - 1 ; hei[rank[i++]] = k )
for ( k ? k -- : 0 , j = sa[rank[i]-1] ; r[i+k] == r[j+k] ; k ++ ) ;
}
int solve ( int n )
{
int ans = 0 , i ;
for ( i = 1 ; i <= n ; i ++ )
ans += n - sa[i] - hei[i] ;
return ans ;
}
} arr ;
int s1[maxn] , dp[20][2222] ;
void rmq ( int n )
{
int i , j ;
f[0] = 1 ;
for ( i = 1 ; i <= 15 ; i ++ )
f[i] = f[i-1] * 2 ;
g[0] = -1 ;
for ( i = 1 ; i < 2222 ; i ++ )
g[i] = g[i>>1] + 1 ;
for ( i = 1 ; f[i] <= n ; i ++ )
for ( j = 1 ; j + f[i] - 1 <= n ; j ++ )
dp[i][j] = min ( dp[i-1][j] , dp[i-1][j+f[i-1]] ) ;
}
int query ( int l , int r )
{
if ( l == r ) return dp[0][l] ;
if ( l > r ) swap ( l , r ) ;
int i , j , k ;
k = g[r-l+1] ;
return min ( dp[k][l] , dp[k][r-f[k]+1] ) ;
}
char s[maxn] ;
int num[maxn] ;
int main ()
{
int n , k , i , l , r ;
int cas ;
scanf ( "%d" , &cas ) ;
while ( cas -- )
{
int q ;
scanf ( "%s" , s ) ;
int len = strlen ( s ) ;
for ( i = 0 ; i < len ; i ++ ) s1[i] = s[i] ;
s1[len] = 0 ;
arr.da ( s1 , len + 1 , 555 ) ;
for ( i = 1 ; i <= len ; i ++ )
dp[0][i] = arr.hei[i] ;
rmq ( len ) ;
n = len ;
scanf ( "%d" , &q ) ;
while ( q -- )
{
scanf ( "%d%d" , &l , &r ) ;
int ans = ( r - l + 1 ) * ( r - l + 2) / 2 ;
for ( i = 0 ; i <= n ; i ++ ) pos[i] = -1 ;
for ( i = l ; i <= r ; i ++ )
pos[arr.rank[i-1]] = i - 1 ;
int last = -1 , d = 0 ;
for ( i = 1 ; i <= n ; i ++ )
{
if ( pos[i] != -1 )
{
if ( last != -1 )
{
int t = query ( arr.rank[last] + 1 , arr.rank[pos[i]] ) ;
d = min ( d , t ) ;
d = max ( d , min ( t , r - last ) ) ;
ans -= min ( d , r - pos[i] );
}
last = pos[i] ;
}
}
printf ( "%d\n" , ans ) ;
}
}
}

123
HDOJ/4623_autoAC.cpp Normal file
View File

@ -0,0 +1,123 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int prime[]={2,3,5,7,11,13,17,19,23};
const int np=9;
int state[30];
int g[300][300];
int vi[300];
int num[30];
int base[30];
short dp[19][2000000];
bool ok[29];
int n,m,ns,st;
void ini()
{
scanf("%d%d",&n,&m);
memset(g,0,sizeof(g));
memset(vi,0,sizeof(vi));
memset(num,0,sizeof(num));
ns=0;
state[++ns]=0;
num[ns]=1;
for(int i=2;i<=n;i++)
{
st=0;
if(ok[i])
{
num[1]++;
continue;
}
for(int j=0;j<np;j++)
{
if(i%prime[j]==0)
{
st|=(1<<j);
}
}
if(!vi[st])
{
state[++ns]=st;
num[ns]=1;
vi[st]=ns;
}
else
{
num[vi[st]]++;
}
}
for(int i=1;i<=ns;i++)
{
for(int j=1;j<=ns;j++)
{
if((state[i]&state[j])==0)
g[i][j]=1;
}
}
base[1]=1;
st=0;
for(int i=1;i<=ns;i++)
{
base[i+1]=base[i]*(num[i]+1);
st+=base[i]*num[i];
}
}
int getnum(int i,int x)
{
int res=(x%base[i+1])/(base[i]);
return res;
}
int getstate(int i,int num)
{
return num*base[i];
}
void dfs(int t,int x)
{
if(t==0)
{
dp[x][0]=1;
return ;
}
if(dp[x][t]!=-1)
return;
dp[x][t]=0;
for(int i=1;i<=ns;i++)
{
if(g[x][i]&&getnum(i,t)>=1)
{
dfs(t-base[i],i);
dp[x][t]=((int)dp[x][t]+dp[i][t-base[i]])%m;
}
}
return;
}
int main()
{
int T;
scanf("%d",&T);
memset(ok,0,sizeof(ok));
ok[17]=1;
ok[19]=1;
ok[23]=1;
while(T--)
{
ini();
memset(dp,-1,sizeof(dp));
int ans=0;
for(int i=1;i<=ns;i++)
{
dfs(st-base[i],i);
ans=((int)ans+dp[i][st-base[i]])%m;
}
for(int i=1;i<=ns;i++)
{
while(num[i]>1)
{
ans=((int)ans*num[i])%m;
num[i]--;
}
}
printf("%d\n",ans);
}
}

94
HDOJ/4625_autoAC.cpp Normal file
View File

@ -0,0 +1,94 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
const int maxn = 5e4 + 10;
const int maxk = 5e2 + 10;
const int mod = 10007;
typedef long long LL;
struct edge {
int v, to;
};
vector<edge> E;
int L[maxn];
void graph_init()
{
E.clear();
memset(L, -1, sizeof(L));
}
void graph_add(int u, int v)
{
edge t = {v, L[u]};
L[u] = E.size();
E.push_back(t);
}
int s[maxk][maxk];
int f[maxk];
int c[maxk][maxk];
void init()
{
s[0][0] = f[0] = c[0][0] = 1;
for (int i = 1; i < maxk; i++) {
f[i] = f[i-1] * i % mod;
c[i][0] = 1;
for (int j = 1; j < maxk; j++) {
s[i][j] = (j * s[i-1][j] % mod + s[i-1][j-1]) % mod;
c[i][j] = (c[i-1][j] + c[i-1][j-1]) % mod;
}
}
}
int n, k;
int d[maxn][maxk];
void dp_son(int u = 0, int p = -1)
{
memset(d[u], 0, sizeof(d[u]));
for (int i = L[u]; i != -1; i = E[i].to)
if (i != p) {
int v = E[i].v;
dp_son(v, i^1);
for (int j = 0; j <= k; j++)
d[u][j] = (d[u][j] + d[v][j] + (j? d[v][j-1]: 0) + c[1][j]) % mod;
}
}
int tmp[maxk];
void dp_father(int u = 0, int p = -1)
{
if (p != -1) {
int v = E[p].v;
memcpy(tmp, d[v], sizeof(d[v]));
for (int j = 0; j <= k; j++)
tmp[j] = (tmp[j] - (d[u][j] + (j? d[u][j-1]: 0) + c[1][j]) % mod + mod) % mod;
for (int j = 0; j <= k; j++)
d[u][j] = (d[u][j] + tmp[j] + (j? tmp[j-1]: 0) + c[1][j]) % mod;
}
for (int i = L[u]; i != -1; i = E[i].to)
if (i != p) dp_father(E[i].v, i^1);
}
int main()
{
init();
int T;
scanf("%d", &T);
for ( ; T--; ) {
graph_init();
scanf("%d%d", &n, &k);
for (int i = 0; i < n-1; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--, v--;
graph_add(u, v);
graph_add(v, u);
}
dp_son();
dp_father();
for (int u = 0; u < n; u++) {
int sum = 0;
for (int i = 0; i <= k; i++)
sum = (sum + s[k][i]*f[i]%mod*d[u][i]%mod) % mod;
printf("%d\n", sum);
}
}
return 0;
}

30
HDOJ/4627_autoAC.cpp Normal file
View File

@ -0,0 +1,30 @@
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
__int64 n;
scanf("%I64d",&n);
if(n==2)
{
printf("1\n");
continue;
}
if(n&1)
printf("%I64d\n",(n/2)*(n/2+1));
else
{
n/=2;
if(n&1)
printf("%I64d\n",(n-2)*(n+2));
else
printf("%I64d\n",(n-1)*(n+1));
}
}
return 0;
}

48
HDOJ/4628_autoAC.cpp Normal file
View File

@ -0,0 +1,48 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
bool legal[(1<<16)+1];
int dp[(1<<16)+1];
char s[20];
char str[20];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",s);
int num=0;
int len=strlen(s);
for(int i=1;i<(1<<len);i++)
{
int cnt=0;
for(int j=0;j<len;j++)
if(i&(1<<j))
str[cnt++]=s[j];
int l=0,r=cnt-1;
while(l<r && str[l]==str[r])
{
l++;
r--;
}
if(l==r || l-1==r)
legal[i]=true;
else
legal[i]=false;
}
memset(dp,0x3f,sizeof(dp));
dp[0]=0;
for(int i=1;i<(1<<len);i++)
{
for(int j=i;j>0;j=i&(j-1))
if(legal[j])
dp[i]=min(dp[i],dp[i-j]+1);
}
printf("%d\n",dp[(1<<len)-1]);
}
return 0;
}

168
HDOJ/4629_autoAC.cpp Normal file
View File

@ -0,0 +1,168 @@
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<cstdlib>
#define pbk push_back
using namespace std;
const int N = 25050+10;
const double eps = 1e-10;
inline double sqr(double x){
return x * x;
}
inline int dcmp(double x){
return x < -eps ? -1 : x > eps;
}
struct Point{
double x,y;
int kind;
Point(){}
Point(double x,double y,int kind = 0):x(x),y(y),kind(kind){}
bool operator < (const Point &p)const{
return dcmp(x - p.x) < 0 || ( dcmp(x - p.x) == 0 && dcmp(y - p.y) < 0 );
}
Point operator - (const Point &p)const{
return Point(x - p.x, y - p.y);
}
Point operator + (const Point &p)const{
return Point(x + p.x, y + p.y);
}
Point operator * (const double &k)const{
return Point (x*k , y*k);
}
Point operator / (const double &k)const{
return Point (x/k, y/k);
}
double operator * (const Point &p)const{
return x * p.y - y * p.x;
}
double operator / (const Point &p)const{
return x * p.x + y * p.y;
}
void input(){
scanf("%lf%lf",&x,&y);
}
void ot(){
printf("%lf %lf\n",x,y);
}
};
struct Line{
Point a,b;
int kind;
Line (){}
Line (Point a,Point b,int kind = 0):a(a),b(b),kind(kind){}
double operator * (const Point &p)const{
return ( b - a ) * ( p - a );
}
double operator / (const Point &p)const{
return ( p - a) / ( p - b);
}
bool parallel(const Line &v){
return !dcmp( ( b - a ) * ( v.b - v.a ) );
}
int LineCrossLine(const Line &v){
if ( (*this).parallel(v) ){
return ( dcmp( v * a ) == 0);
}return 2;
}
int SegCrossSeg(const Line &v){
int d1 = dcmp( (*this) * v.a);
int d2 = dcmp( (*this) * v.b);
int d3 = dcmp( v * a);
int d4 = dcmp( v * b);
if ( ( d1 ^ d2 ) == -2 && ( d3 ^ d4 ) == -2 ) return 2;
return ( ( d1 == 0 && dcmp( (*this) / v.a ) <= 0 )
|| ( d2 == 0 && dcmp( (*this) / v.b ) <= 0 )
|| ( d3 == 0 && dcmp( v / a ) <= 0 )
|| ( d4 == 0 && dcmp( v / b ) <= 0 )
);
}
Point CrossPoint(const Line &v){
double s1 = v * a, s2 = v * b;
return ( a * s2 - b * s1) / (s2 - s1);
}
void input(){
a.input(); b.input();
}
void ot(){
a.ot(); b.ot();
}
};
int n,poly_n,xn;
vector<double> lx;
vector<Line> line;
double ans[N];
void init(){
int sz = line.size();
for (int i = 0; i < sz; i++){
for (int j = i+1; j < sz; j++){
if (line[i].SegCrossSeg(line[j]) == 2){
Point p = line[i].CrossPoint(line[j]);
lx.pbk(p.x);
}
}
}
sort(lx.begin(),lx.end());
xn = unique(lx.begin(),lx.end()) - lx.begin();
}
vector<Point> qu[N];
void work(){
for (int i = 0; i <= n; i++) ans[i] = 0;
for (int i = 0; i < xn-1; i++){
int k = 0;
for (int j = 0; j+1 < qu[i].size(); j++){
k += qu[i][j].kind;
ans[ k ] += (lx[i+1] - lx[i]) * (qu[i][j+1].x+qu[i][j+1].y - qu[i][j].x - qu[i][j].y) / 2;
}
}
for (int i = 1; i <= n; i++) printf("%.10lf\n",ans[i]);
}
void solve(){
for (int i = 0; i < xn; i++) qu[i].clear();
for (int i = 0; i < line.size(); i++){
int j = lower_bound(lx.begin(),lx.begin()+xn,line[i].a.x) - lx.begin();
for (; j+1 < xn; j++ ){
double l = lx[j], r = lx[j+1];
if (dcmp(r - line[i].b.x) > 0) break;
Point p1 = line[i].CrossPoint(Line(Point(l,0), Point(l,1)));
Point p2 = line[i].CrossPoint(Line(Point(r,0), Point(r,1)));
qu[j].pbk(Point(p1.y, p2.y,line[i].kind));
}
}
for (int i = 0; i < xn - 1; i++) sort(qu[i].begin(), qu[i].end());
work();
}
int main(){
int T; scanf("%d",&T);
while (T--){
scanf("%d",&n);
lx.clear(); line.clear();;
for (int i = 0; i < n ;i++){
Point t[4];
for (int j = 0; j < 3; j++ ){
t[j].input();
}
t[3] = t[0];
int flag = 1;
if (dcmp( (t[1] - t[0])*(t[2] - t[0]) ) == 0) flag = 0;
for (int i = 0; i < 3 && flag; i++ ){
lx.pbk(t[i].x);
for (int j = i+1; j < 3; j++){
Line tmp; tmp.a = t[i]; tmp.b = t[j];
if (dcmp( tmp.a.x - tmp.b.x ) > 0) swap(tmp.a, tmp.b);
Line tmp2 = Line(t[3-i-j], Point(t[3-i-j].x, t[3-i-j].y - 1));
if (tmp.LineCrossLine(tmp2) != 2) continue;
Point tp = tmp.CrossPoint(tmp2);
if (dcmp(tp.y - t[3-i-j].y) < 0) tmp.kind = 1;
else tmp.kind = -1;
line.pbk(tmp);
}
}
}
init();
solve();
}
return 0;
}

121
HDOJ/4630_autoAC.cpp Normal file
View File

@ -0,0 +1,121 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
#define lz 2*u,l,mid
#define rz 2*u+1,mid+1,r
const int maxn=50005;
int maxx[4*maxn], flag[4*maxn];
int a[maxn];
int pre[maxn];
int n, m, T;
vector<int>vt;
struct node
{
int l, r, id;
int ans;
friend bool operator<(const node A, const node B)
{
return A.r<B.r;
}
}f[maxn];
bool cmp(node A, node B)
{
return A.id<B.id;
}
void push_down(int u, int l, int r)
{
if(flag[u])
{
flag[2*u]=max(flag[2*u],flag[u]);
flag[2*u+1]=max(flag[2*u+1],flag[u]);
maxx[2*u]=max(maxx[2*u],flag[u]);
maxx[2*u+1]=max(maxx[2*u+1],flag[u]);
flag[u]=0;
}
}
void Update(int u, int l, int r, int tl, int tr, int val)
{
maxx[u]=max(maxx[u],val);
if(tl<=l&&r<=tr)
{
flag[u]=max(flag[u],val);
maxx[u]=max(maxx[u],val);
return ;
}
push_down(u,l,r);
int mid=(l+r)>>1;
if(tr<=mid) Update(lz,tl,tr,val);
else if(tl>mid) Update(rz,tl,tr,val);
else
{
Update(lz,tl,mid,val);
Update(rz,mid+1,tr,val);
}
}
int Query(int u, int l, int r, int tl, int tr)
{
if(tl<=l&&r<=tr) return maxx[u];
push_down(u,l,r);
int mid=(l+r)>>1;
if(tr<=mid) return Query(lz,tl,tr);
else if(tl>mid) return Query(rz,tl,tr);
else
{
int t1=Query(lz,tl,mid);
int t2=Query(rz,mid+1,tr);
return max(t1,t2);
}
}
void Solve(int x, int r)
{
vt.clear();
vt.push_back(x);
for(int i=2; i*i<=x; i++)
if(x%i==0)
{
vt.push_back(i);
vt.push_back(x/i);
}
for(int i=0; i<vt.size(); i++)
{
int l=pre[ vt[i] ];
pre[ vt[i] ]=r;
if(l==-1||l==r) continue;
Update(1,1,n,l,l,vt[i]);
}
}
int main()
{
cin >> T;
while(T--)
{
cin >> n;
for(int i=1; i<=n; i++) scanf("%d", a+i), pre[i]=-1;
cin >> m;
for(int i=1; i<=m; i++) f[i].id=i, scanf("%d%d",&f[i].l,&f[i].r);
sort(f+1,f+m+1);
for(int i=1; i<=4*n; i++) maxx[i]=1, flag[i]=0;
int i=1, j=1;
while(j<=m)
{
if(i<=f[j].r&&i<=n)
{
Solve(a[i],i);
i++;
}
else
{
if(f[j].l!=f[j].r)f[j].ans=Query(1,1,n,f[j].l,f[j].r);
else f[j].ans=0;
j++;
}
}
sort(f+1,f+m+1,cmp);
for(int i=1; i<=m; i++)
printf("%d\n",f[i].ans);
}
return 0;
}

113
HDOJ/4631_autoAC.cpp Normal file
View File

@ -0,0 +1,113 @@
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <map>
#include <vector>
#include <queue>
#include <set>
#include <string>
#include <math.h>
using namespace std;
const int MAXN = 500010;
struct Point
{
int x,y;
int id;
int index;
Point(int _x = 0,int _y = 0,int _index = 0)
{
x = _x;
y = _y;
index = _index;
}
};
Point P[MAXN];
long long dist(Point a,Point b)
{
return ((long long)(a.x-b.x)*(a.x-b.x) + (long long)(a.y-b.y)*(a.y-b.y));
}
Point p[MAXN];
Point tmpt[MAXN];
bool cmpxy(Point a,Point b)
{
if(a.x != b.x)return a.x < b.x;
else return a.y < b.y;
}
bool cmpy(Point a,Point b)
{
return a.y < b.y;
}
pair<int,int> Closest_Pair(int left,int right)
{
long long d = (1LL<<50);
if(left == right)return make_pair(left,right);
if(left + 1 == right)
return make_pair(left,right);
int mid = (left+right)/2;
pair<int,int>pr1 = Closest_Pair(left,mid);
pair<int,int>pr2 = Closest_Pair(mid+1,right);
long long d1,d2;
if(pr1.first == pr1.second)
d1 = (1LL<<50);
else d1 = dist(p[pr1.first],p[pr1.second]);
if(pr2.first == pr2.second)
d2 = (1LL<<50);
else d2 = dist(p[pr2.first],p[pr2.second]);
pair<int,int>ans;
if(d1 < d2)ans = pr1;
else ans = pr2;
d = min(d1,d2);
int k = 0;
for(int i = left;i <= right;i++)
{
if((long long)(p[mid].x - p[i].x)*(p[mid].x - p[i].x) <= d)
tmpt[k++] = p[i];
}
sort(tmpt,tmpt+k,cmpy);
for(int i = 0;i <k;i++)
{
for(int j = i+1;j < k && (long long)(tmpt[j].y - tmpt[i].y)*(tmpt[j].y - tmpt[i].y) < d;j++)
{
if(d > dist(tmpt[i],tmpt[j]))
{
d = dist(tmpt[i],tmpt[j]);
ans = make_pair(tmpt[i].id,tmpt[j].id);
}
}
}
return ans;
}
int main()
{
int T;
int n,Ax,Ay,Bx,By,Cx,Cy;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d%d%d%d",&n,&Ax,&Bx,&Cx,&Ay,&By,&Cy);
P[0] = Point(0,0,0);
for(int i = 1;i <= n;i++)
{
long long x= ((long long)P[i-1].x*Ax + Bx)%Cx;
long long y = ((long long)P[i-1].y*Ay + By)%Cy;
P[i] = Point(x,y,i);
}
int end = n;
long long ans = 0;
while(end > 1)
{
for(int i = 0;i < end;i++)
p[i] = P[i+1];
sort(p,p+end,cmpxy);
for(int i = 0;i < end;i++)
p[i].id = i;
pair<int,int>pr = Closest_Pair(0,end-1);
int Max = max(p[pr.first].index,p[pr.second].index);
ans += (long long)(end-Max+1)*dist(p[pr.first],p[pr.second]);
end = Max-1;
}
printf("%I64d\n",ans);
}
return 0;
}

66
HDOJ/4632_autoAC.cpp Normal file
View File

@ -0,0 +1,66 @@
#include"iostream"
#include"cstdio"
#include"cstring"
using namespace std;
const int N=1006;
const int mod=10007;
int n,lowbit[N];
__int64 C[N],ans[N];
char str[N];
void update(int k,int dir)
{
while(0<k && k<=n)
{
C[k]=(C[k]+dir)%mod;
k+=lowbit[k];
}
}
int sum(int k)
{
__int64 p=0;
while(0<k && k<=n)
{
p=(p+C[k])%mod;
k-=lowbit[k];
}
return p;
}
int main()
{
int T,Case;
int i,l;
__int64 a,b,t;
__int64 hehe;
for(i=1;i<=1000;i++) lowbit[i]=i&(-i);
cin>>T;
for(Case=1;Case<=T;Case++)
{
hehe=0;
scanf("%s",str+1);
memset(C,0,sizeof(C));
memset(ans,0,sizeof(ans));
for(n=1;str[n];n++);
n--;
for(i=n;i>=1;i--)
{
ans[i]=1;
update(i,1);
for(l=n;l>i;l--)
{
if(str[l]==str[i])
{
a=sum(i);
b=sum(l-1);
t=(b-a)%mod;
while(t<0) t+=mod;
ans[i]=(ans[i]+1+t)%mod;
update(l,1+t);
}
}
}
hehe=0;
for(i=1;i<=n;i++) hehe=(hehe+ans[i])%mod;
printf("Case %d: %I64d\n",Case,hehe);
}
return 0;
}

40
HDOJ/4633_autoAC.cpp Normal file
View File

@ -0,0 +1,40 @@
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
typedef long long LL;
const LL MOD=10007;
LL quick_mod(LL a,LL b)
{
LL ans=1;
a%=MOD;
while(b)
{
if(b&1)
{
ans=ans*a%MOD;
b--;
}
b>>=1;
a=a*a%MOD;
}
return ans;
}
int main()
{
LL tt=1,t,k,ans;
cin>>t;
while(t--)
{
cin>>k;
ans=quick_mod(k,74);
ans+=3*(2*quick_mod(k,20)+quick_mod(k,38))%MOD;
ans+=6*quick_mod(k,38);
ans+=8*quick_mod(k,26);
ans%=MOD;
ans*=quick_mod(24,MOD-2);
ans%=MOD;
printf("Case %I64d: %I64d\n",tt++,ans);
}
return 0;
}

142
HDOJ/4634_autoAC.cpp Normal file
View File

@ -0,0 +1,142 @@
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <map>
#include <vector>
#include <queue>
#include <set>
#include <string>
#include <math.h>
using namespace std;
char g[220][220];
int sx,sy,ex,ey;
int n,m;
int keynum;
int key_s[220][220];
int mo[][2] = {{0,1},{0,-1},{1,0},{-1,0}};
struct Node
{
int key;
int num;
int x,y;
};
queue<Node>q;
bool used[202][202][1<<7];
bool used2[202][202][1<<7][4];
int bfs()
{
while(!q.empty())q.pop();
Node tmp,now;
tmp.key = 0;
tmp.num = 0;
tmp.x = sx;
tmp.y = sy;
q.push(tmp);
memset(used,false,sizeof(used));
memset(used2,false,sizeof(used2));
used[sx][sy][0] = true;
while(!q.empty())
{
tmp = q.front();
q.pop();
for(int i = 0;i < 4;i++)
{
int mx = mo[i][0];
int my = mo[i][1];
int x = tmp.x;
int y = tmp.y;
int ss = tmp.key;
while(1)
{
if(g[x][y] =='L')
{
mx = 0; my = -1;
}
if(g[x][y] == 'U')
{
mx = -1;my = 0;
}
if(g[x][y] == 'D')
{
mx = 1;my = 0;
}
if(g[x][y] == 'R')
{
mx = 0; my = 1;
}
int dir;
if(mx==-1&&my==0)dir=0;
else if(mx==1&&my==0)dir=1;
else if(mx==0&&my==1)dir=2;
else if(mx==0&&my==-1)dir=3;
if(used2[x][y][ss][dir])break;
used2[x][y][ss][dir] = true;
x += mx;
y += my;
if(x < 0 || y < 0 || x >= n || y >= m)break;
if(g[x][y] =='#')break;
if( x == ex && y== ey && ss ==((1<<keynum)-1) )
return tmp.num+1;
if(g[x][y] =='L')
{
mx = 0; my = -1;
}
if(g[x][y] == 'U')
{
mx = -1;my = 0;
}
if(g[x][y] == 'D')
{
mx = 1;my = 0;
}
if(g[x][y] == 'R')
{
mx = 0; my = 1;
}
if(g[x][y] == 'K')
ss |= key_s[x][y];
if(x+mx >=0 && x+mx < n && y+my>=0 && y+my < m && g[x+mx][y+my]=='#')
{
if(used[x][y][ss])break;
now.x = x;now.y = y;
now.key = ss;
now.num = tmp.num + 1;
q.push(now);
used[x][y][ss] = true;
break;
}
}
}
}
return -1;
}
int main()
{
while(scanf("%d%d",&n,&m)==2)
{
keynum = 0;
for(int i = 0;i < n;i++)
{
scanf("%s",g[i]);
for(int j = 0;j < m;j++)
{
if(g[i][j] == 'S')
{
sx = i;sy = j;
}
if(g[i][j] == 'E')
{
ex = i;ey = j;
}
if(g[i][j] == 'K')
{
key_s[i][j] = (1<<keynum);
keynum++;
}
}
}
printf("%d\n",bfs());
}
return 0;
}

93
HDOJ/4635_autoAC.cpp Normal file
View File

@ -0,0 +1,93 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int VM=200010;
const int EM=200010;
const int INF=0x3f3f3f3f;
struct Edge{
int to,nxt;
}edge[EM<<1];
long long n,m,cnt,head[VM];
long long dep,top,atype;
long long dfn[VM],low[VM],vis[VM],stack[VM],belong[VM],indeg[VM],outdeg[VM],sum[VM];
void addedge(int cu,int cv){
edge[cnt].to=cv; edge[cnt].nxt=head[cu]; head[cu]=cnt++;
}
void Tarjan(int u){
dfn[u]=low[u]=++dep;
stack[top++]=u;
vis[u]=1;
for(int i=head[u];i!=-1;i=edge[i].nxt){
int v=edge[i].to;
if(!dfn[v]){
Tarjan(v);
low[u]=min(low[u],low[v]);
}else if(vis[v]){
low[u]=min(low[u],dfn[v]);
}
}
int j;
if(dfn[u]==low[u]){
atype++;
do{
j=stack[--top];
belong[j]=atype;
sum[atype]++;
vis[j]=0;
}while(u!=j);
}
}
void init(){
cnt=0;
memset(head,-1,sizeof(head));
dep=0, top=0, atype=0;
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(vis,0,sizeof(vis));
memset(belong,0,sizeof(belong));
memset(indeg,0,sizeof(indeg));
memset(outdeg,0,sizeof(outdeg));
memset(sum,0,sizeof(sum));
}
int main(){
int t,cases=0;
scanf("%d",&t);
while(t--){
cin>>n>>m;
if(n==1){
printf("Case %d: -1\n",++cases);
continue;
}
init();
int u,v;
for(int i=0;i<m;i++){
scanf("%d%d",&u,&v);
addedge(u,v);
}
for(int i=1;i<=n;i++)
if(!dfn[i])
Tarjan(i);
if(atype==1){
printf("Case %d: -1\n",++cases);
continue;
}
for(int u=1;u<=n;u++)
for(int i=head[u];i!=-1;i=edge[i].nxt){
int v=edge[i].to;
if(belong[u]!=belong[v]){
outdeg[belong[u]]++;
indeg[belong[v]]++;
}
}
long long ans=0,tmp;
for(long long i=1;i<=atype;i++)
if(indeg[i]==0 || outdeg[i]==0){
tmp=sum[i];
ans=max(ans,tmp*(tmp-1)+(n-tmp)*(n-tmp-1)+tmp*(n-tmp)-m);
}
cout<<"Case "<<(++cases)<<": "<<ans<<endl;
}
return 0;
}

98
HDOJ/4636_autoAC.cpp Normal file
View File

@ -0,0 +1,98 @@
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#define MID(x,y) ((x+y)>>1)
#define iabs(x) ((x)>0?(x):-(x))
using namespace std;
vector <int>v[40];
inline void v_pb(int a,int b){
v[a].push_back(b);
}
int num[40];
queue<int>q;
bool vis[40];
bool bfs(int s,int flag){
while(!q.empty()) q.pop();
q.push(s);
int cnt = -1;
while(!q.empty()){
int k = q.front(); q.pop();
cnt++;
for (int i = 0; i < v[k].size(); i++){
int d = v[k][i];
if (!vis[d] && num[d] == flag){
vis[d] = 1;
q.push(d);
}
}
}
if(flag == 0 && cnt == 0)return 1;
return 0;
}
int main(){
v_pb(1,2);v_pb(1,3);v_pb(1,17);v_pb(1,18);v_pb(1,20);v_pb(1,13);
v_pb(2,1);v_pb(2,3);v_pb(2,4);v_pb(2,7);v_pb(2,12);v_pb(2,13);
v_pb(3,1);v_pb(3,2);v_pb(3,4);v_pb(3,5);v_pb(3,20);
v_pb(4,2);v_pb(4,3);v_pb(4,5);v_pb(4,6);v_pb(4,7);v_pb(4,8);
v_pb(5,3);v_pb(5,4);v_pb(5,6);v_pb(5,20);v_pb(5,21);v_pb(5,24);
v_pb(6,4);v_pb(6,5);v_pb(6,8);v_pb(6,9);v_pb(6,24);
v_pb(7,2);v_pb(7,4);v_pb(7,8);v_pb(7,10);v_pb(7,12);
v_pb(8,4);v_pb(8,6);v_pb(8,7);v_pb(8,9);v_pb(8,10);v_pb(8,11);
v_pb(9,6);v_pb(9,8);v_pb(9,11);v_pb(9,24);v_pb(9,26);v_pb(9,27);
v_pb(10,7);v_pb(10,8);v_pb(10,11);v_pb(10,12);v_pb(10,14);v_pb(10,16);
v_pb(11,8);v_pb(11,9);v_pb(11,10);v_pb(11,16);v_pb(11,27);
v_pb(12,2);v_pb(12,7);v_pb(12,10);v_pb(12,13);v_pb(12,14);v_pb(12,15);
v_pb(13,12);v_pb(13,15);v_pb(13,2);v_pb(13,1);v_pb(13,17);
v_pb(14,10);v_pb(14,12);v_pb(14,15);v_pb(14,16);v_pb(14,30);
v_pb(15,12);v_pb(15,13);v_pb(15,14);v_pb(15,30);v_pb(15,32);v_pb(15,17);
v_pb(16,10);v_pb(16,11);v_pb(16,14);v_pb(16,27);v_pb(16,29);v_pb(16,30);
v_pb(17,1);v_pb(17,18);v_pb(17,19);v_pb(17,13);v_pb(17,15);v_pb(17,32);
v_pb(18,1);v_pb(18,17);v_pb(18,19);v_pb(18,20);v_pb(18,22);
v_pb(19,17);v_pb(19,18);v_pb(19,31);v_pb(19,22);v_pb(19,23);v_pb(19,32);
v_pb(20,1);v_pb(20,3);v_pb(20,5);v_pb(20,18);v_pb(20,21);v_pb(20,22);
v_pb(21,5);v_pb(21,20);v_pb(21,22);v_pb(21,24);v_pb(21,25);
v_pb(22,19);v_pb(22,18);v_pb(22,20);v_pb(22,21);v_pb(22,23);v_pb(22,25);
v_pb(23,19);v_pb(23,22);v_pb(23,25);v_pb(23,28);v_pb(23,31);
v_pb(24,5);v_pb(24,6);v_pb(24,9);v_pb(24,21);v_pb(24,25);v_pb(24,26);
v_pb(25,21);v_pb(25,22);v_pb(25,23);v_pb(25,24);v_pb(25,26);v_pb(25,28);
v_pb(26,9);v_pb(26,24);v_pb(26,25);v_pb(26,27);v_pb(26,28);
v_pb(27,9);v_pb(27,11);v_pb(27,16);v_pb(27,26);v_pb(27,28);v_pb(27,29);
v_pb(28,25);v_pb(28,26);v_pb(28,27);v_pb(28,29);v_pb(28,31);v_pb(28,23);
v_pb(29,16);v_pb(29,27);v_pb(29,28);v_pb(29,30);v_pb(29,31);
v_pb(30,14);v_pb(30,15);v_pb(30,16);v_pb(30,29);v_pb(30,31);v_pb(30,32);
v_pb(31,28);v_pb(31,29);v_pb(31,30);v_pb(31,32);v_pb(31,23);v_pb(31,19);
v_pb(32,30);v_pb(32,31);v_pb(32,15);v_pb(32,19);v_pb(32,17);
int t;
scanf("%d", &t);
for (int cas = 1; cas <= t; cas++){
printf("Case %d: ", cas);
int black = 0,white = 0;
for (int i = 1; i <= 32; ++i){
scanf("%d", &num[i]);
if(num[i]) black ++;
}
if (black == 0) {puts("0");continue;}
memset(vis,0,sizeof(vis));
black = 0;
for (int i = 1; i <= 32; ++i){
if (!vis[i]){
vis[i] = 1;
if (num[i]){
black ++;
bfs(i,1);
}else {
white ++;
if(bfs(i,0)) white --;
}
}
}
if(black <= 2) printf("%d\n", black);
else if (white == 1) puts("2");
else puts("3");
}
return 0;
}

317
HDOJ/4637_autoAC.cpp Normal file
View File

@ -0,0 +1,317 @@
#include <cmath>
#include <cstdio>
#include <cstring>
#include <set>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const double eps=1e-10;
const int INF=1<<29;
const double PI=acos(-1.0);
int doublecmp(double x){
if(fabs(x)<eps)return 0;else return x<0?-1:1;
}
struct Point{
double x,y;
Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vector;
Vector operator+(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);}
Vector operator-(Point a,Point 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);}
bool operator<(const Point&a,const Point&b){return a.x<b.x||(a.x==b.x&&a.y<b.y);}
bool operator==(const Point&a,const Point&b){
return doublecmp(a.x-b.x)==0&&doublecmp(a.y-b.y)==0;
}
bool operator!=(const Point&a,const Point&b){return a==b?false:true;}
struct Segment{
Point a,b;
Segment(){}
Segment(Point _a,Point _b){a=_a,b=_b;}
bool friend operator<(const Segment& p,const Segment& q){return p.a<q.a||(p.a==q.a&&p.b<q.b);}
bool friend operator==(const Segment& p,const Segment& q){return (p.a==q.a&&p.b==q.b)||(p.a==q.b&&p.b==q.a);}
};
struct Circle{
Point c;
double r;
Circle(){}
Circle(Point _c, double _r):c(_c),r(_r) {}
Point point(double a)const{return Point(c.x+cos(a)*r,c.y+sin(a)*r);}
bool friend operator<(const Circle& a,const Circle& b){return a.r<b.r;}
};
struct Line{
Point p;
Vector v;
double ang;
Line() {}
Line(const Point &_p, const Vector &_v):p(_p),v(_v){ang = atan2(v.y, v.x);}
bool operator<(const Line &L)const{return ang < L.ang;}
};
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 DegreeToRadius(double deg){return deg/180*PI;}
double GetRerotateAngle(Vector a,Vector b){
double tempa=Angle(a,Vector(1,0));
if(a.y<0) tempa=2*PI-tempa;
double tempb=Angle(b,Vector(1,0));
if(b.y<0) tempb=2*PI-tempb;
if((tempa-tempb)>0) return tempa-tempb;
else return tempa-tempb+2*PI;
}
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);
}
Point GetLineProjection(Point p,Point a,Point b){
Vector v=b-a;
return a+v*(Dot(v,p-a)/Dot(v,v));
}
Point GetLineIntersection(Point p,Vector v,Point q,Vector w){
Vector u=p-q;
double t=Cross(w,u)/Cross(v,w);
return p+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>0) m--;
return m;
}
double Heron(double a,double b,double c){
double p=(a+b+c)/2;
return sqrt(p*(p-a)*(p-b)*(p-c));
}
bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2){
double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1);
double c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
return doublecmp(c1)*doublecmp(c2)<0&&doublecmp(c3)*doublecmp(c4)<0;
}
double CutConvex(const int n,Point* poly, const Point a,const Point b, vector<Point> result[3]){
vector<Point> points;
Point p;
Point p1=a,p2=b;
int cur,pre;
result[0].clear();
result[1].clear();
result[2].clear();
if(n==0) return 0;
double tempcross;
tempcross=Cross(p2-p1,poly[0]-p1);
if(doublecmp(tempcross)==0) pre=cur=2;
else if(tempcross>0) pre=cur=0;
else pre=cur=1;
for(int i=0;i<n;i++){
tempcross=Cross(p2-p1,poly[(i+1)%n]-p1);
if(doublecmp(tempcross)==0) cur=2;
else if(tempcross>0) cur=0;
else cur=1;
if(cur==pre){
result[cur].push_back(poly[(i+1)%n]);
}
else{
p1=poly[i];
p2=poly[(i+1)%n];
p=GetLineIntersection(p1,p2-p1,a,b-a);
points.push_back(p);
result[pre].push_back(p);
result[cur].push_back(p);
result[cur].push_back(poly[(i+1)%n]);
pre=cur;
}
}
sort(points.begin(),points.end());
if(points.size()<2){
return 0;
}
else{
return Length(points.front()-points.back());
}
}
double DistanceToSegment(Point p,Segment s){
if(s.a==s.b) return Length(p-s.a);
Vector v1=s.b-s.a,v2=p-s.a,v3=p-s.b;
if(doublecmp(Dot(v1,v2))<0) return Length(v2);
else if(doublecmp(Dot(v1,v3))>0) return Length(v3);
else return fabs(Cross(v1,v2))/Length(v1);
}
bool isPointOnSegment(Point p,Segment s){
return doublecmp(DistanceToSegment(p,s))==0;
}
int isPointInPolygon(Point p, Point* poly,int n){
int wn=0;
for(int i=0;i<n;i++){
Point& p2=poly[(i+1)%n];
if(isPointOnSegment(p,Segment(poly[i],p2))) return -1;
int k=doublecmp(Cross(p2-poly[i],p-poly[i]));
int d1=doublecmp(poly[i].y-p.y);
int d2=doublecmp(p2.y-p.y);
if(k>0&&d1<=0&&d2>0)wn++;
if(k<0&&d2<=0&&d1>0)wn--;
}
if(wn) return 1;
else return 0;
}
double PolygonArea(vector<Point> p){
double area=0;
int n=p.size();
for(int i=1;i<n-1;i++)
area+=Cross(p[i]-p[0],p[i+1]-p[0]);
return area/2;
}
int PSLGtoPolygons(Segment arr[],int n,vector<Point>* Polygons){
int count=n-1;
bool vis[9999];memset(vis,0,sizeof vis);
Point star=arr[0].a,pre=arr[0].a,cur=arr[0].b,purpose=arr[0].b;vis[0]=true;
int mark;
while(purpose!=star){
double theta=-INF;
for(int i=0;i<n;i++)if(!vis[i]&&arr[i].a==cur&&arr[i].b!=pre){
if(theta<GetRerotateAngle(pre-cur,arr[i].b-cur)){
theta=GetRerotateAngle(pre-cur,arr[i].b-cur);
purpose=arr[i].b;
mark=i;
}
}
vis[mark]=true;count--;
pre=cur;cur=purpose;
}
int polyidx=0;
while(count>0){
for(int i=0;i<n;i++)if(!vis[i]){
star=arr[i].a,pre=arr[i].a,cur=arr[i].b,purpose=arr[i].b;vis[i]=true;count--;
Polygons[polyidx].clear();
Polygons[polyidx].push_back(cur);
break;
}
while(purpose!=star){
double theta=-INF;
for(int i=0;i<n;i++)if(!vis[i]&&arr[i].a==cur&&arr[i].b!=pre){
if(theta<GetRerotateAngle(pre-cur,arr[i].b-cur)){
theta=GetRerotateAngle(pre-cur,arr[i].b-cur);
purpose=arr[i].b;
mark=i;
}
}
vis[mark]=true;count--;
Polygons[polyidx].push_back(purpose);
pre=cur;cur=purpose;
}
polyidx++;
}
return polyidx;
}
int GetLineCircleIntersection(Line L,Circle C,Point& p1,Point& p2){
double a = L.v.x, b = L.p.x - C.c.x, c = L.v.y, d = L.p.y-C.c.y;
double e = a*a + c*c, f = 2*(a*b+c*d), g = b*b + d*d -C.r*C.r;
double delta = f*f - 4*e*g;
if(doublecmp(delta) < 0) return 0;
if(doublecmp(delta) == 0) {
p1=p1=C.point(-f/(2*e));
return 1;
}
p1=(L.p+L.v*(-f-sqrt(delta))/(2*e));
p2=(L.p+L.v*(-f+sqrt(delta))/(2*e));
return 2;
}
struct Allpoint{
Point p;
int pos;
bool friend operator<(Allpoint a,Allpoint b){
if(a.pos==-1&&a.pos==b.pos) return a.p<b.p;
else if(a.pos==-1) return true;
else if(b.pos==-1) return false;
else return a.p<b.p;
}
}allpoint[8000];
int idx=0;
int lpint(Line l, Point a, Point b, Point c, Allpoint *sol) {
int ret = 0;
if (doublecmp(Cross(l.v, a - b)) != 0) {
sol[ret].p = GetLineIntersection(l.p,l.v, a, b-a);
if (isPointOnSegment(sol[ret].p, Segment(a, b))) ret++;
}
if (doublecmp(Cross(l.v, c - b)) != 0) {
sol[ret].p = GetLineIntersection(l.p,l.v,c, b-c);
if (isPointOnSegment(sol[ret].p, Segment(c, b))) ret++;
}
if (doublecmp(Cross(l.v, a - c)) != 0) {
sol[ret].p = GetLineIntersection(l.p,l.v, a, c-a);
if (isPointOnSegment(sol[ret].p, Segment(a, c))) ret++;
}
if (ret < 2) return 0;
sort(sol, sol + ret);
if (ret == 3) {
if (sol[0].p == sol[1].p) sol[1].p = sol[2].p;
}
if (sol[0].p == sol[1].p) return 0;
return 2;
}
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--){
double v1,v2,v,t,x;
int n;
idx=0;
scanf("%lf%lf%lf%lf%lf",&v1,&v2,&v,&t,&x);
scanf("%d",&n);
double time = v2 * t / (v2 - v1);
Line l = Line(Point(x, 0), Point(x - v1 * time, v * time)-Point(x,0));
for(int i=0;i<n;i++){
Point O;
double r,h;
Allpoint ip[4];
scanf("%lf%lf%lf%lf", &O.x, &O.y, &r, &h);
int ipp = lpint(l, Point(O.x + r, O.y), Point(O.x - r, O.y), Point(O.x, O.y + h), ip);
if (ipp) {
if (ip[1] < ip[0]) swap(ip[0], ip[1]);
for (int i = 0; i < 2; i++) ip[i].pos= i & 1, allpoint[idx++] = ip[i];
}
int ipc = GetLineCircleIntersection(l,Circle(O, r), ip[0].p,ip[1].p);
if (ipc==2) {
if (ip[1] < ip[0]) swap(ip[0], ip[1]);
if (doublecmp(ip[1].p.y - O.y) >= 0) continue;
if (doublecmp(ip[0].p.y - O.y) > 0) ip[0].p = GetLineIntersection(l.p,l.v, O, Point(1.0, 0));
for (int i = 0; i < 2; i++) ip[i].pos = i & 1, allpoint[idx++] = ip[i];
}
}
sort(allpoint, allpoint + idx);
int cnt = 0;
double sum = 0, lp = x - v1 * time, rp = x, ls = lp;
for (int i = 0; i < idx; i++) {
if (allpoint[i].p.x > rp) break;
if (allpoint[i].p.x < lp) {
if (allpoint[i].pos) cnt--;
else cnt++;
} else {
if (allpoint[i].pos) {
if (cnt == 1) sum += allpoint[i].p.x - ls;
cnt--;
} else {
if (cnt == 0) ls = allpoint[i].p.x;
cnt++;
}
}
}
if (cnt > 0) sum += rp - ls;
printf("Case %d: %.4f\n", cas++, sum / v1);
}
}

101
HDOJ/4638_autoAC.cpp Normal file
View File

@ -0,0 +1,101 @@
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
using namespace std;
#define CL(x,v); memset(x,v,sizeof(x));
#define INF 0x3f3f3f3f
#define LL long long
#define REP(i,r,n) for(int i=r;i<=n;i++)
#define RREP(i,n,r) for(int i=n;i>=r;i--)
const int MAXN=1e5+100;
int tree[MAXN];
int n;
int lowbit(int x)
{
return x&(-x);
}
void add(int pos,int val)
{
while(pos <=n)
{
tree[pos] += val;
pos += lowbit(pos);
}
}
int read(int x)
{
int s=0;
while(x>0)
{
s += tree[x];
x -= lowbit(x);
}
return s;
}
int A[MAXN],pos[MAXN];
struct node{
int l,r,index;
bool operator <(const node& rsh)const{
if(r==rsh.r)
return l<rsh.l;
else return r<rsh.r;
}
};
vector<node> Q;
int ans[MAXN];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int m;
scanf("%d%d",&n,&m);
REP(i,1,n){
scanf("%d",&A[i]);
pos[A[i]]=i;
}
Q.clear();
node tmp;
REP(i,1,m)
{
scanf("%d%d",&tmp.l,&tmp.r);
tmp.index=i;
Q.push_back(tmp);
}
sort(Q.begin(),Q.end());
CL(tree,0);
int j=0;
for(int i=1;i<=n;i++)
{
add(i,1);
if(A[i]+1<=n&&pos[A[i]+1]<i)
{
add(pos[A[i]+1],-1);
}
if(A[i]-1>=1&&pos[A[i]-1]<i)
{
add(pos[A[i]-1],-1);
}
while(Q[j].r==i)
{
ans[Q[j].index]=read(Q[j].r)-read(Q[j].l-1);
j++;
}
}
REP(i,1,m)
{
printf("%d\n",ans[i]);
}
}
return 0;
}

37
HDOJ/4639_autoAC.cpp Normal file
View File

@ -0,0 +1,37 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
const int V = 5050 + 50;
const int MaxN = 500 + 5;
const int mod = 10000 + 7;
int T, ans, num[V];
char ch[10100];
int main() {
int i, j, k;
scanf("%d", &T);
num[0] = num[1] = 1;
for(i = 2; i <= V; ++i)
num[i] = (num[i - 1] + num[i - 2]) % mod;
for(i = 1; i <= T; ++i) {
ans = 1;
int sum = 0;
scanf("%s", &ch);
int len = strlen(ch);
for(j = 1; j < len; ++j) {
if(ch[j] == 'e' && ch[j - 1] == 'h') {
sum++;
j++;
}
else {
ans = (ans * num[sum]) % mod;
sum = 0;
}
}
ans = (ans * num[sum]) % mod;
printf("Case %d: %d\n", i, ans);
}
}

91
HDOJ/4640_autoAC.cpp Normal file
View File

@ -0,0 +1,91 @@
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define INF 2000000000
int q[4*(1<<17)][2];
int maxQ=4*(1<<17)-3;
int front,rear,n,m;
int mat[20][20];
int dp[1<<17],f[1<<17],dist[1<<17][18],vis[1<<17][18];
int main ()
{
int T;scanf("%d",&T);
for(int kk=1;kk<=T;++kk)
{
scanf("%d%d",&n,&m);
memset(dist,127,sizeof(dist));
memset(dp,127,sizeof(dp));
memset(vis,0,sizeof(vis));
memset(mat,127,sizeof(mat));
front=rear=0;
int u,v,s;
for(int i=1;i<=m;++i)
{
scanf("%d%d%d",&u,&v,&s);
u--;v--;
if(mat[u][v]>s)
mat[u][v]=mat[v][u]=s;
}
dist[0][0]=0;
q[++rear][0]=0;q[rear][1]=0;
vis[0][0]=1;
while(front!=rear)
{
if(++front==maxQ) front=0;
int u_sta=q[front][0],u=q[front][1];
vis[u_sta][u]=0;
for(int i=0;i<n;++i)
{
int v_sta=u_sta|(1<<i);
if(dist[v_sta][i]>dist[u_sta][u]+mat[u][i])
{
dist[v_sta][i]=dist[u_sta][u]+mat[u][i];
if(!vis[v_sta][i])
{
if(++rear==maxQ) rear=0;
vis[v_sta][i]=1;
q[rear][0]=v_sta;q[rear][1]=i;
}
}
}
}
for(int i=0;i<(1<<n);++i)
for(int j=0;j<n;++j)
dp[i>>1]=min(dp[i>>1],dist[i][j]);
int tot_sta=1<<n-1;
for(int i=0;i<tot_sta;++i)
f[i]=dp[i];
for(int k=1;k<=2;++k)
{
for(int i=tot_sta-1;i;--i)
for(u=i;u;u=(--u)&i)
{
v=i^u;
if(dp[i]>max(dp[u],f[v]))
{
if(i==224 && max(dp[u],f[v])==5)
cout<<"dd";
dp[i]=max(dp[u],f[v]);
}
}
}
int query;scanf("%d",&query);
int sta=0;
while(query--)
{
scanf("%d",&u);
sta|=1<<u-2;
}
int ans=INF;
for(int i=0;i<tot_sta;++i)
if((i&sta)==sta)
{
ans=min(ans,dp[i]);
}
if(ans>=INF)
printf("Case %d: -1\n",kk);
else printf("Case %d: %d\n",kk,ans);
}
return 0;
}

131
HDOJ/4641_autoAC.cpp Normal file
View File

@ -0,0 +1,131 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define Maxn 250009
int root,last;
int tots;
int l;
int sv[Maxn*2];
struct query{int a;long long ans;}qu[Maxn];
struct sam_node{
int fa,son[26];
int len;
void init(int _len){len=_len;fa=-1;memset(son,-1,sizeof(son));}
}t[Maxn*2];
void sam_init(){
tots=0;
root=last=0;
t[tots].init(0);
}
void extend(int w){
int p=last;
int np=++tots;t[tots].init(t[p].len+1);
sv[l]=np;
int q,nq;
while(p!=-1&&t[p].son[w]==-1){t[p].son[w]=np;p=t[p].fa;}
if (p==-1) t[np].fa=root;
else{
q=t[p].son[w];
if (t[p].len+1==t[q].len){t[np].fa=q;}
else{
nq=++tots;t[nq].init(0);
t[nq]=t[q];
t[nq].len=t[p].len+1;
t[q].fa=nq;t[np].fa=nq;
while(p!=-1&&t[p].son[w]==q){t[p].son[w]=nq;p=t[p].fa;}
}
}
last=np;
}
int w[Maxn],r[Maxn*2];
void topsort(){
int i;
for(i=0;i<=l;++i) w[i]=0;
for(i=1;i<=tots;++i)w[t[i].len]++;
for(i=1;i<=l;++i) w[i]+=w[i-1];
for(i=tots;i>=1;--i)r[w[t[i].len]--]=i;
r[0]=0;
}
int dp[Maxn*2];
int sub[Maxn*2];
char s[Maxn];
int set[Maxn*2];
int findset(int x){
int y;
while(x!=set[x]){
y=set[x];
set[x]=set[y];
x=y;
}
return x;
}
int main(){
int n,m,k,f,y,p,i,a;
long long ans;
while(scanf("%d%d%d",&n,&m,&k)!=EOF){
scanf("%s",s);
int tl=strlen(s);
l=0;
sam_init();
for(i=0;i<tl;++i){
++l;
extend(s[i]-'a');
}
char ts[20];
for(i=1;i<=m;++i){
scanf("%d",&a);
if (a==1){
scanf("%s",ts);
s[l++]=ts[0];
qu[i].a=a;
extend(ts[0]-'a');
}
else if (a==2) qu[i].a=a;
}
for(i=0;i<=tots;++i){dp[i]=0;}
topsort();
p=root;
for(i=0;i<l;++i){
p=t[p].son[s[i]-'a'];
dp[p]++;
}
for(i=tots;i>=1;--i){
p=r[i];
if (t[p].fa!=-1) dp[t[p].fa]+=dp[p];
}
ans=0;
for(i=1;i<=tots;++i)
if (dp[i]>=k) ans+=t[i].len-t[t[i].fa].len;
for(i=0;i<=tots;++i){set[i]=i;sub[i]=0;}
for(i=m;i>=1;--i){
if (qu[i].a==2) qu[i].ans=ans;
else{
p=sv[l];
l--;
y=findset(p);
while(y!=0&&dp[y]<k){
f=t[y].fa;
set[y]=f=findset(f);
y=f;
}
y=findset(p);
if (y==root) {continue;}
sub[y]++;
while(y!=0&&(dp[y]-sub[y]<k)){
ans=ans-t[y].len+t[t[y].fa].len;
f=t[y].fa;
f=findset(f);
sub[f]+=sub[y];
set[y]=f;
y=f;
}
}
}
for(i=1;i<=m;++i)if (qu[i].a==2){
printf("%I64d\n",qu[i].ans);
}
}
return 0;
}

22
HDOJ/4642_autoAC.cpp Normal file
View File

@ -0,0 +1,22 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m;
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
int x;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
scanf("%d",&x);
if(x)
puts("Alice");
else
puts("Bob");
}
return 0;
}

190
HDOJ/4643_autoAC.cpp Normal file
View File

@ -0,0 +1,190 @@
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
const double eps=1e-11;
struct point
{
double x,y;
}city[55],base[55];
int N,M,K,a,b;
struct node
{
int i,j;
point p;
double dist;
}jiao[3000];
inline bool mo_ee(double x,double y)
{
double ret=x-y;
if(ret<0) ret=-ret;
if(ret<eps) return 1;
return 0;
}
inline bool mo_gg(double x,double y) { return x > y + eps;} // x > y
inline bool mo_ll(double x,double y) { return x < y - eps;} // x < y
inline bool mo_ge(double x,double y) { return x > y - eps;} // x >= y
inline bool mo_le(double x,double y) { return x < y + eps;} // x <= y
inline double min(double a,double b)
{
if(a<b) return a;
return b;
}
inline double max(double a,double b)
{
if(a>b) return a;
return b;
}
point getxiang(point xiang)
{
point a;
if(mo_ee(xiang.x,0))
{
a.x=1;
a.y=0;
return a;
}else if(mo_ee(xiang.y,0))
{
a.x=0;
a.y=1;
return a;
}else
{
a.x=1;
a.y=-1.0*xiang.x/xiang.y;
return a;
}
}
inline double mo_distance(point p1,point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
point mo_intersection(point u1,point u2,point v1,point v2)
{
point ret=u1;
double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))
/((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
ret.x+=(u2.x-u1.x)*t;
ret.y+=(u2.y-u1.y)*t;
return ret;
}
int segjiao(point &ji,point a,point b,point c,point d)
{
ji=mo_intersection(a,b,c,d);
if(mo_ll(ji.x,min(c.x,d.x))) return 0;
if(mo_ll(ji.y,min(c.y,d.y))) return 0;
if(mo_gg(ji.x,max(c.x,d.x))) return 0;
if(mo_gg(ji.y,max(c.y,d.y))) return 0;
return 1;
}
node jiaojiao(int a,int b,int ii,int jj)
{
node cur;
point xiang1,xiang2;
xiang1.x=city[b].x-city[a].x,xiang1.y=city[b].y-city[a].y;
xiang2.x=base[jj].x-base[ii].x,xiang2.y=base[jj].y-base[ii].y;
if(mo_ee(xiang1.x*xiang2.x,xiang1.y*xiang2.y))
{
cur.i=-1;
return cur;
}
xiang2=getxiang(xiang2);
point zhong;
zhong.x=(base[ii].x+base[jj].x)/2,zhong.y=(base[ii].y+base[jj].y)/2;
point zhongxia;
zhongxia.x=zhong.x+xiang2.x;
zhongxia.y=zhong.y+xiang2.y;
point jiaodian;
int jjao=segjiao(jiaodian,zhong,zhongxia,city[a],city[b]);
if(jjao==0)
{
cur.i=-1;
return cur;
}
cur.p=jiaodian;
cur.i=ii;
cur.j=jj;
cur.dist=mo_distance(jiaodian,city[a]);
return cur;
}
bool cmp(const node &aa,const node &bb)
{
if(mo_ee(aa.dist,bb.dist))
{
point temp;
temp.x=(base[aa.i].x+base[aa.j].x)/2;
temp.y=(base[aa.i].y+base[aa.j].y)/2;
double dist1=mo_distance(aa.p,temp);
temp.x=(base[bb.i].x+base[bb.j].x)/2;
temp.y=(base[bb.i].y+base[bb.j].y)/2;
double dist2=mo_distance(bb.p,temp);
return mo_ll(dist1,dist2);
}
return mo_ll(aa.dist,bb.dist);
}
inline int nextno(int j,int no)
{
if(jiao[j].i==no) return jiao[j].j;
if(jiao[j].j==no) return jiao[j].i;
return no;
}
int main()
{
int i,j,ncase;
while(scanf("%d%d",&N,&M)!=EOF)
{
for(i=1;i<=N;++i)
{
scanf("%lf%lf",&city[i].x,&city[i].y);
}
for(i=1;i<=M;++i)
{
scanf("%lf%lf",&base[i].x,&base[i].y);
}
scanf("%d",&K);
for(ncase=0;ncase<K;++ncase)
{
int yong=0;
scanf("%d%d",&a,&b);
for(i=1;i<=M;++i)
{
for(j=i+1;j<=M;++j)
{
if(i==j) continue;
node cur=jiaojiao(a,b,i,j);
if(cur.i<0)
{
continue;
}else
{
jiao[yong++]=cur;
}
}
}
sort(jiao,jiao+yong,cmp);
double mindist=-1;
int minno;
for(i=1;i<=M;++i)
{
double disttemp=sqrt((city[a].x-base[i].x)*(city[a].x-base[i].x)+(city[a].y-base[i].y)*(city[a].y-base[i].y));
if(mindist<0||mo_ll(disttemp,mindist))
{
mindist=disttemp;
minno=i;
}
}
int dang=minno,ret=0;
for(i=0;i<yong;++i)
{
int xinno=nextno(i,dang);
if(xinno!=dang)
{
dang=xinno;
ret++;
}
}
printf("%d\n",ret);
}
}
return 0;
}

56
HDOJ/4644_autoAC.cpp Normal file
View File

@ -0,0 +1,56 @@
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
char str[2000100];
char t[100100];
pair<int,int>s[100100];
int index[100100];
int n;
int ne[100100];
void get_next(char* str,int len){
ne[0]=0;
for(int i=1;i<len;i++){
int j=ne[i-1];
while(j&&str[i]!=str[j])j=ne[j-1];
if(str[i]==str[j])j++;
ne[i]=j;
}
}
bool match(char *s,int len){
int k=0;
for(int i=0;i<n;i++){
while(k&&str[k]!=s[i])k=ne[k-1];
if(str[k]==s[i])k++;
if(k==len)return true;
}
return false;
}
int main(){
while(~scanf("%s",str)){
n=strlen(str);
for(int i=0;i<n;i++){
s[i].first=str[i];
s[i].second=i;
}
stable_sort(s,s+n);
for(int i=0;i<n;i++)index[i]=s[i].second;
int now=index[0];
n--;
for(int i=0;i<n;i++){
t[i]=s[now].first;
now=index[now];
}
t[n]='\0';
int q;
scanf("%d",&q);
while(q--){
scanf("%s",str);
int len=strlen(str);
get_next(str,len);
if(match(t,len))printf("YES\n");
else printf("NO\n");
}
}
}

59
HDOJ/4646_autoAC.cpp Normal file
View File

@ -0,0 +1,59 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#define ll long long
using namespace std;
ll t,n;
map<ll,ll> g;
ll f(ll x)
{
if (x%2==0) return 2*(x/6+1)-1;
else return 2*((x-3)/6+1);
}
ll get(ll x)
{
if (g[x]!=0) return g[x];
g[x]=f(x);
for (ll i=2; i*i<=x; i++)
{
if (x%i>0) continue;
g[x]-=get(i);
if (i*i!=x)
{
g[x]-=get(x/i);
}
}
return g[x];
}
int main()
{
scanf("%I64d",&t);
while (t--)
{
scanf("%I64d",&n);
if (n%3==0)
{
printf("0\n");
continue;
}
if ((n&1)==0)
{
printf("0\n");
continue;
}
if (n==1)
{
printf("1\n");
continue;
}
if (n==3)
{
printf("0\n");
continue;
}
n=(n+3)>>1;
printf("%I64d\n",get(n));
}
return 0;
}

32
HDOJ/4647_autoAC.cpp Normal file
View File

@ -0,0 +1,32 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=100010;
int n,m;
double a[N];
int cmp(double x,double y){
return x>y;
}
int main(){
while(~scanf("%d%d",&n,&m)){
for(int i=1;i<=n;i++)
scanf("%lf",&a[i]);
int u,v,w;
while(m--){
scanf("%d%d%d",&u,&v,&w);
a[u]+=1.0*w/2;
a[v]+=1.0*w/2;
}
double t1=0,t2=0;
sort(a+1,a+1+n,cmp);
for(int i=1;i<=n;i++)
if(i&1)
t1+=a[i];
else
t2+=a[i];
printf("%.0lf\n",t1-t2);
}
return 0;
}

32
HDOJ/4648_autoAC.cpp Normal file
View File

@ -0,0 +1,32 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int V = 100000 + 50;
const int MaxN = 80 + 5;
const int mod = 10000 + 7;
const __int64 INF = 0x7FFFFFFFFFFFFFFFLL;
const int inf = 0x7fffffff;
int n, m, sum[V], ans;
int main() {
int i, j;
while(~scanf("%d%d", &n, &m)) {
ans = 0;
for(i = 1; i <= n; ++i) {
int temp;
scanf("%d", &temp);
sum[i] = (sum[i - 1] + temp) % m;
}
for(i = n; i >= 1 && i > ans; --i) {
for(j = 1; j + i - 1 <= n; ++j)
if((sum[j + i - 1] - sum[j - 1]) % m == 0) {
ans = max(ans, i);
break;
}
}
printf("%d\n", ans);
}
}

72
HDOJ/4649_autoAC.cpp Normal file
View File

@ -0,0 +1,72 @@
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;
#define ls 2*i
#define rs 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 205
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define rank rank1
const int mod = 10007;
int n,a[N];
double dp[N],p[N],ans;
char op[N][5];
int main()
{
int i,j,k,cas=1;
while(~scanf("%d",&n))
{
for(i = 0; i<=n; i++)
scanf("%d",&a[i]);
for(i = 1; i<=n; i++)
scanf("%s",op[i]);
for(i = 1; i<=n; i++)
scanf("%lf",&p[i]);
ans = 0;
for(i = 0; i<=20; i++)
{
double q;
if(a[0]&(1<<i)) q = 1;
else q = 0;
for(j = 1; j<=n; j++)
{
if(a[j]&(1<<i))
{
if(op[j][0]=='&')
continue;
else if(op[j][0]=='^')
q = q*p[j]+(1-q)*(1-p[j]);
else
q = q*p[j]+(1-p[j]);
}
else
{
if(op[j][0]=='&')
q = q*p[j];
else
continue;
}
}
ans += q*(1<<i);
}
printf("Case %d:\n%lf\n",cas++,ans);
}
return 0;
}

61
HDOJ/4650_autoAC.cpp Normal file
View File

@ -0,0 +1,61 @@
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 101;
const double INF = 10000000000.0;
const int INF_INT =10000000;
int side[N][N];
int dis[N][N];
int vis[N][N];
int tmp[N][N];
double ans[N][N];
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
memset(vis,-1,sizeof(vis));
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){ans[i][j]=INF;side[i][j]=INF_INT;}
for(int i=0;i<m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
side[u][v]=w;
vis[u][v]=1;
}
for(int k=1;k<=n;k++)for(int i=1;i<=n;i++)if(vis[i][k]!=-1){
for(int j=1;j<=n;j++)if(vis[k][j]!=-1){
if(vis[i][j]==-1)vis[i][j]=vis[i][k]+vis[k][j];
else vis[i][j]=min(vis[i][j],vis[i][k]+vis[k][j]);
}
}
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)dis[i][j]=side[i][j];
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(vis[i][j]!=-1&&k>=vis[i][j]){
ans[i][j]=min(ans[i][j],(double)dis[i][j]/k);
}
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){tmp[i][j]=dis[i][j];dis[i][j]=INF_INT;}
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){
for(int h=1;h<=n;h++){
if(vis[i][h]==-1||k<vis[i][h])continue;
if(vis[h][j]==-1||k<vis[h][j])continue;
dis[i][j]=min(dis[i][j],tmp[i][h]+side[h][j]);
}
}
}
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(vis[i][j]!=-1){
for(int k=1;k<=n;k++)if(vis[i][k]!=-1&&vis[k][j]!=-1&&vis[k][k]!=-1){
ans[i][j]=min(ans[i][j],ans[k][k]);
}
}
for(int i=1;i<=n;i++){
int j=1;
if(vis[i][j]==-1)printf("NO");
else printf("%.3lf",ans[i][j]);
for(j=2;j<=n;j++){
if(vis[i][j]==-1)printf(" NO");
else printf(" %.3lf",ans[i][j]);
}
printf("\n");
}
}
}

49
HDOJ/4651_autoAC.cpp Normal file
View File

@ -0,0 +1,49 @@
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
long long tar[100002];
const int MOD=1000000007;
void init()
{
memset(tar,0,sizeof(tar));
tar[0]=1;
for(int i=1;i<=100000;i++)
{
int nbit;
for(int j=1;;j++)
{
int element1,element2;
element1=i-j*(3*j-1)/2;
element2=i-j*(3*j+1)/2;
if(j&1)
nbit=1;
else if(j%2==0)
nbit=-1;
if(element2<0 && element1<0)
break;
if(element1>=0)
{
tar[i]=(tar[i]+nbit*tar[element1])%MOD;
}
if(element2>=0)
{
tar[i]=(tar[i]+nbit*tar[element2])%MOD;
}
}
tar[i]=(tar[i]+MOD)%MOD;
}
}
int main()
{
int testcase;
init();
cin>>testcase;
while(testcase--)
{
int rat;
cin>>rat;
cout<<tar[rat]<<endl;
}
return 0;
}

41
HDOJ/4652_autoAC.cpp Normal file
View File

@ -0,0 +1,41 @@
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
double solve0(int m,int n)
{
double ans=0;
for(int i=0;i<=n-1;i++)
ans+=pow(1.0*m,i);
return ans;
}
double solve1(int m,int n)
{
double ans=0;
double tmp=1;
for(int i=1;i<=n;i++)
{
ans+=tmp;
tmp*=(m+0.0)/(m-i);
}
return ans;
}
int main()
{
int t;
while(scanf("%d",&t)!=EOF)
{
while(t--)
{
int n,m,op;
scanf("%d%d%d",&op,&m,&n);
if(op==0)
printf("%.9lf\n",solve0(m,n));
else
printf("%.9lf\n",solve1(m,n));
}
}
return 0;
}

102
HDOJ/4654_autoAC.cpp Normal file
View File

@ -0,0 +1,102 @@
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define inf (1<<29)
const int maxn = 105;
int g[maxn][maxn],a[maxn][maxn],p[maxn];
bool vis[maxn],combine[maxn],par[maxn];
int d[maxn],node[maxn],st[maxn],k,s,t;
vector <int> vst[maxn];
vector <int> pa , pb;
int prim(int n) {
memset(vis,0,sizeof(vis));
memset(d,0,sizeof(d));
int mincut = 0 , tmp = -1;
s = t = -1;
int top = 0;
for(int i=0;i<k;i++) {
int maxi = -inf;
for(int j=0;j<k;j++) {
int u = node[j];
if(!combine[u]&&!vis[u]&&d[u]>maxi) {
tmp = u;
maxi = d[u];
}
}
st[top++] = tmp;
vis[tmp] = true;
if(i == k-1)
mincut = d[tmp];
for(int j=0;j<k;j++) {
int u = node[j];
if(!combine[u]&&!vis[u])
d[u] += g[tmp][u];
}
}
s = st[top-2];
t = st[top-1];
for(int i=0;i<top;i++) node[i] = st[i];
return mincut;
}
int stoer_wagner(int n) {
for(int i=0;i<n;i++) {
vst[i].clear();
vst[i].push_back(i);
}
int ans = inf;
memset(combine,0,sizeof(combine));
for(int i=0;i<n;i++) node[i] = i;
for(int i=1;i<n;i++) {
k = n - i + 1;
int cur = prim(n);
if(cur < ans) {
ans = cur;
for(int j=0;j<n;j++) par[j] = 0;
for(int j=0;j<vst[t].size();j++) par[ vst[t][j] ] = 1;
}
combine[t] = true;
for(int j=0;j<vst[t].size();j++) vst[s].push_back(vst[t][j]);
for(int j=0;j<n;j++) {
if(j == s) continue;
if(!combine[j]) {
g[s][j] += g[t][j];
g[j][s] += g[j][t];
}
}
}
pa.clear(); pb.clear();
for(int i=0;i<n;i++)
if(par[i]) pa.push_back(i);
else pb.push_back(i);
return ans;
}
int K;
int dfs(vector <int> t) {
int n = t.size();
for(int i=0;i<n;i++) for(int j=0;j<n;j++) g[i][j] = a[t[i]][t[j]];
if(stoer_wagner(n) >= K) return 1;
vector <int> x , y;
for(int i=0;i<pa.size();i++) x.push_back(t[pa[i]]);
for(int i=0;i<pb.size();i++) y.push_back(t[pb[i]]);
return dfs(x) + dfs(y);
}
int main() {
int n , m;
while(~scanf("%d%d%d",&n,&m,&K)) {
memset(a , 0 ,sizeof(a));
while(m--) {
int u , v;
scanf("%d%d",&u,&v);
u --; v --;
a[u][v] += 1;
a[v][u] += 1;
}
vector <int> t;
for(int i=0;i<n;i++) t.push_back(i);
printf("%d\n" , dfs(t));
}
return 0;
}

57
HDOJ/4655_autoAC.cpp Normal file
View File

@ -0,0 +1,57 @@
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <time.h>
using namespace std;
const int MOD = 1e9+7;
long long inv(long long a,long long m)
{
if(a == 1)return 1;
return inv(m%a,m)*(m-m/a)%m;
}
const int MAXN = 1000010;
int a[MAXN];
int main()
{
int T;
int n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
long long S = 1;
for(int i = 0;i < n;i++)
{
scanf("%d",&a[i]);
S *= a[i];
S %=MOD;
}
long long ans = S*n%MOD;
sort(a,a+n);
reverse(a,a+n);
int cnt = n-1;
for(int i = 0;i < n;i++)
{
if(cnt == 0)break;
long long tmp = S*inv(a[i],MOD)%MOD;
ans -= tmp;
ans = (ans%MOD+MOD)%MOD;
cnt--;
if(cnt == 0)break;
ans -= tmp;
ans = (ans%MOD+MOD)%MOD;
cnt--;
if(cnt == 0)break;
}
printf("%I64d\n",ans);
}
return 0;
}

116
HDOJ/4656_autoAC.cpp Normal file
View File

@ -0,0 +1,116 @@
#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>
using namespace std;
const int P = 1000003;
const int N1 = 262144;
const int N2 = N1+1;
const int P1 = 998244353;
const int P2 = 995622913;
const int E1 = 996173970;
const int E2 = 88560779;
const int F1 = 121392023;
const int F2 = 840835547;
const int I1 = 998240545;
const int I2 = 995619115;
const long long M1 = 397550359381069386LL;
const long long M2 = 596324591238590904LL;
const long long MM = 993874950619660289LL;
long long mul(long long x,long long y,long long z){
return (x*y - (long long)(x/(long double)z*y+1e-3)*z+z)%z;
}
int trf(int x1,int x2){
return (mul(M1,x1,MM)+mul(M2,x2,MM))%MM%P;
}
int A[N2],B[N2],C[N2];
int A1[N2],B1[N2],C1[N2];
void fft(int *A,int PM,int PW){
for(int m = N1,h;h = m/2, m >= 2;PW = (long long)PW*PW%PM,m=h)
for(int i = 0,w=1;i < h;i++, w = (long long)w*PW%PM)
for(int j = i;j < N1;j += m){
int k = j+h, x = (A[j]-A[k]+PM)%PM;
(A[j]+=A[k])%=PM;
A[k] = (long long)w*x%PM;
}
for(int i = 0,j = 1;j < N1-1;j++){
for(int k = N1/2; k > (i^=k);k /= 2);
if(j < i)swap(A[i],A[j]);
}
}
void mul(){
memset(C,0,sizeof(C));
memcpy(A1,A,sizeof(A));
memcpy(B1,B,sizeof(B));
fft(A1,P1,E1); fft(B1,P1,E1);
for(int i = 0;i < N1;i++)C1[i] = (long long)A1[i]*B1[i]%P1;
fft(C1,P1,F1);
for(int i = 0;i < N1;i++)C1[i] = (long long)C1[i]*I1%P1;
fft(A,P2,E2); fft(B,P2,E2);
for(int i = 0;i < N1;i++)C[i] = (long long)A[i]*B[i]%P2;
fft(C,P2,F2);
for(int i = 0;i < N1;i++)C[i] = (long long)C[i]*I2%P2;
for(int i = 0;i < N1;i++)C[i] = trf(C1[i],C[i]);
}
int INV[P];
const int MAXN = 100010;
int F[MAXN];
int a[MAXN];
int pd[MAXN];
int pb[MAXN];
int pc2[MAXN];
int p[MAXN];
int main()
{
INV[1] = 1;
for(int i = 2;i < P;i++)
INV[i] = (long long)P/i*(P-INV[P%i])%P;
F[0] = 1;
for(int i = 1;i < MAXN;i++)
F[i] = (long long)F[i-1]*i%P;
int n,b,c,d;
while(scanf("%d%d%d%d",&n,&b,&c,&d) == 4){
for(int i = 0;i < n;i++)scanf("%d",&a[i]);
pd[0] = 1;
for(int i = 1;i < n;i++)
pd[i] = (long long)pd[i-1]*d%P;
memset(A,0,sizeof(A));
memset(B,0,sizeof(B));
for(int i = 0;i < n;i++)
A[i] = (long long)a[n-1-i]*F[n-1-i]%P;
for(int i = 0;i < n;i++)
B[i] = (long long)pd[i]*INV[F[i]]%P;
mul();
for(int i = 0;i < n;i++)p[i] = C[i];
reverse(p,p+n);
memset(A,0,sizeof(A));
pb[0] = 1;
for(int i = 1;i < n;i++)
pb[i] = (long long)pb[i-1]*b%P;
pc2[0] = 1;
int c2 = (long long)c*c%P;
for(int i = 1, s = c;i < n;i++){
pc2[i] = (long long)pc2[i-1]*s%P;
s = (long long)s*c2%P;
}
for(int i = 0;i < n;i++)
A[i] = (long long)pb[i]*INV[F[i]]%P*p[i]%P*pc2[i]%P;
memset(B,0,sizeof(B));
B[0] = 1;
for(int i = 1;i < n;i++)
B[i] = B[N1-i] = INV[pc2[i]];
mul();
for(int i = 0;i < n;i++)C[i] = (long long)C[i]*pc2[i]%P;
for(int i = 0;i < n;i++)
printf("%d\n",C[i]);
}
return 0;
}

45
HDOJ/4657_autoAC.cpp Normal file
View File

@ -0,0 +1,45 @@
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<ctime>
#include<cstring>
#include<vector>
#define ll __int64
#define pi acos(-1.0)
#define MAX 100005
#define mod 1000003
using namespace std;
int a[MAX],b[MAX],c[MAX],index[MAX];
int main(){
int t,n,i,j;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i=0;i<n;i++) scanf("%d",&b[i]);
for(i=0;i<n;i++) a[i]=c[i]=index[i]=i;
for(i=0;i<n;i++){
int cur=i;
while((a[cur]+b[cur])%n!=c[cur]){
int l = index[(c[cur]-b[cur]+n)%n];
swap(a[l],a[cur]);
swap(index[a[l]],index[a[cur]]);
if(l>i) break;
cur=l;
swap(c[i+1],c[cur]);
}
}
for(i=0;i<n;i++){
printf("%d",a[i]);
if(i!=n-1) printf(" ");
else printf("\n");
}
for(i=0;i<n;i++){
printf("%d",c[i]);
if(i!=n-1) printf(" ");
else printf("\n");
}
}
return 0;
}

58
HDOJ/4658_autoAC.cpp Normal file
View File

@ -0,0 +1,58 @@
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
using namespace std;
const int mod=1e9+7;
int f[100010];
void init()
{
f[0]=1;
int i,j,k,flag,t,tt;
for(i=1;i<=100000;i++)
{
f[i]=0;
flag=1;
for(j=1;;j++)
{
t=i-j*(3*j-1)/2;
tt=i-j*(3*j+1)/2;
if(t<0)break;
f[i]=(f[i]+flag*f[t])%mod;
if(tt<0)break;
f[i]=(f[i]+flag*f[tt])%mod;
flag=-flag;
}
f[i]=(f[i]+mod)%mod;
}
}
int find(int n,int k)
{
int i,j,ans,flag=-1,t,tt;
ans=f[n];
for(i=1;;i++)
{
t=k*i*(i*3-1)/2;
tt=k*i*(i*3+1)/2;
if(t>n)break;
ans=(ans+flag*f[n-t])%mod;
if(tt>n)break;
ans=(ans+flag*f[n-tt])%mod;
flag=-flag;
}
return (ans+mod)%mod;
}
int main()
{
init();
int T;
cin>>T;
while(T--)
{
int n,k,ans;
scanf("%d%d",&n,&k);
ans=find(n,k) ;
printf("%d\n",ans);
}
return 0;
}

141
HDOJ/4661_autoAC.cpp Normal file
View File

@ -0,0 +1,141 @@
#pragma comment(linker,"/STACK:102400000,102400000")
#include<cstdio>
#include<cstring>
typedef long long LL;
#define MAXN 1000010
#define MAXM 2000010
#define MOD 1000000007
int n;
LL fac[MAXN];
LL invfac[MAXN];
int first[MAXN], next[MAXM], v[MAXM], e;
bool vis[MAXN];
LL dp[MAXN];
LL f[MAXN];
int size[MAXN];
LL ext_gcd(LL a, LL b, LL &x, LL &y) {
LL t, d;
if (b == 0) {
x = 1;
y = 0;
return a;
}
d = ext_gcd(b, a % b, x, y);
t = x;
x = y;
y = t - a / b * y;
return d;
}
LL invmod(LL a, LL n = MOD) {
LL x, y;
if (ext_gcd(a, n, x, y) != 1)
return -1;
return (x % n + n) % n;
}
void init() {
int i;
fac[0] = 1;
for (i = 1; i < MAXN; i++) {
fac[i] = fac[i - 1] * i;
fac[i] %= MOD;
}
for (i = 0; i < MAXN; i++) {
invfac[i] = invmod(fac[i]);
}
}
inline void addEdge(int x, int y) {
v[e] = y;
next[e] = first[x];
first[x] = e++;
}
void getSize(int x) {
vis[x] = true;
size[x] = 1;
for (int i = first[x]; i != -1; i = next[i]) {
int y = v[i];
if (!vis[y]) {
getSize(y);
size[x] += size[y];
}
}
}
void dfs(int x) {
vis[x] = true;
dp[x] = fac[size[x] - 1];
for (int i = first[x]; i != -1; i = next[i]) {
int y = v[i];
if (!vis[y]) {
dfs(y);
dp[x] *= invfac[size[y]];
dp[x] %= MOD;
dp[x] *= dp[y];
dp[x] %= MOD;
}
}
}
void search(int x, int pre) {
vis[x] = true;
if (pre != -1) {
f[x] = fac[n - 1];
f[x] *= invfac[n - size[x]];
f[x] %= MOD;
LL tmp = f[pre];
tmp *= invfac[n - 1];
tmp %= MOD;
tmp *= fac[n - 1 - size[x]];
tmp %= MOD;
tmp *= fac[size[x]];
tmp %= MOD;
tmp *= invmod(dp[x]);
tmp %= MOD;
f[x] *= tmp;
f[x] %= MOD;
for (int i = first[x]; i != -1; i = next[i]) {
int y = v[i];
if (!vis[y]) {
f[x] *= invfac[size[y]];
f[x] %= MOD;
f[x] *= dp[y];
f[x] %= MOD;
}
}
}
for (int i = first[x]; i != -1; i = next[i]) {
int y = v[i];
if (!vis[y]) {
search(y, x);
}
}
}
int main() {
int T;
int i;
int x, y;
int ans;
init();
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
e = 0;
memset(first, -1, sizeof(first));
for (i = 1; i < n; i++) {
scanf("%d%d", &x, &y);
addEdge(x, y);
addEdge(y, x);
}
memset(vis, false, sizeof(vis));
getSize(1);
memset(vis, false, sizeof(vis));
dfs(1);
memset(vis, false, sizeof(vis));
f[1] = dp[1];
search(1, -1);
ans = 0;
for (i = 1; i <= n; i++) {
ans += (f[i] * f[i]) % MOD;
ans %= MOD;
}
printf("%d\n", ans);
}
return 0;
}

46
HDOJ/4662_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include<cstdio>
#include<cstring>
#define MAXN 1000010
char str[MAXN];
bool isOK(int len) {
if (str[0] != 'M') {
return false;
}
for (int i = 1; i < len; i++) {
if (str[i] == 'M') {
return false;
}
}
return true;
}
int main() {
int T;
int len;
int cnt;
int i;
scanf("%d", &T);
while (T--) {
scanf(" %s", str);
len = strlen(str);
if (isOK(len)) {
cnt = 0;
for (i = 1; i < len; i++) {
if (str[i] == 'U') {
cnt += 3;
} else {
cnt++;
}
}
if (len == 2 && str[1] == 'I') {
puts("Yes");
} else if (cnt % 6 == 2 || cnt % 6 == 4) {
puts("Yes");
} else {
puts("No");
}
} else {
puts("No");
}
}
return 0;
}

65
HDOJ/4664_autoAC.cpp Normal file
View File

@ -0,0 +1,65 @@
#include<cstdio>
#include<cstring>
#define MAXN 1010
int sg[MAXN];
bool vis[MAXN];
int SG(int n) {
if (n == 0) {
sg[n] = 0;
} else if (n == 1) {
sg[n] = 0;
} else if (n == 2) {
sg[n] = 1;
} else if (n == 3) {
sg[n] = 1;
} else if (sg[n] == -1) {
int i;
memset(vis, false, sizeof(vis));
for (i = 0; i <= n - 2; i++) {
vis[SG(i) ^ SG(n - i - 2)] = true;
}
for (i = 0;; i++) {
if (!vis[i]) {
break;
}
}
sg[n] = i;
}
return sg[n];
}
void init() {
int i;
memset(sg, -1, sizeof(sg));
for (i = 0; i < MAXN; i++) {
sg[i] = SG(i);
}
}
int getSG(int n) {
if (n < MAXN) {
return sg[n];
} else {
return sg[n % 34 + 2 * 34];
}
}
int main() {
int T;
int n;
int tmp;
int res;
init();
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
res = 0;
while (n--) {
scanf("%d", &tmp);
res ^= getSG(tmp);
}
if (res) {
puts("Carol");
} else {
puts("Dave");
}
}
return 0;
}

82
HDOJ/4665_autoAC.cpp Normal file
View File

@ -0,0 +1,82 @@
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#define MAXN 2010
using namespace std;
int arr[MAXN];
char str[MAXN];
bool flag;
int idx[MAXN];
int cnt[MAXN];
int st[2][MAXN];
int belong[MAXN];
int n;
vector<int> pos[MAXN];
void dfs(int x, int p1, int p2) {
if (x > n) {
flag = true;
}
if (flag) {
return;
}
if (p1 > 0 && p2 > 0
&& arr[st[0][min(p1, p2)]] != arr[st[1][min(p1, p2)]]) {
return;
}
if (belong[x] == 0) {
st[0][p1 + 1] = x;
dfs(x + 1, p1 + 1, p2);
} else if (belong[x] == 1) {
st[1][p2 + 1] = x;
dfs(x + 1, p1, p2 + 1);
} else {
st[0][p1 + 1] = x;
belong[pos[arr[x]][2]] = 1;
dfs(x + 1, p1 + 1, p2);
if (!flag) {
st[1][p2 + 1] = x;
belong[pos[arr[x]][2]] = 0;
dfs(x + 1, p1, p2 + 1);
belong[pos[arr[x]][2]] = -1;
}
}
}
int main() {
int T;
int i;
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
memset(idx, 0, sizeof(idx));
memset(cnt, 0, sizeof(cnt));
for (i = 1; i <= n; i++) {
scanf("%d", &arr[i]);
cnt[arr[i]]++;
idx[i] = cnt[arr[i]];
pos[arr[i]].clear();
}
memset(belong, -1, sizeof(belong));
for (i = 1; i <= n; i++) {
if (idx[i] == 1) {
belong[i] = 0;
} else if (idx[i] == 2 && cnt[arr[i]] == 2) {
belong[i] = 1;
} else if (idx[i] == 4) {
belong[i] = 1;
}
pos[arr[i]].push_back(i);
}
flag = false;
dfs(1, 0, 0);
for (i = 1; i <= (n >> 1); i++) {
str[st[0][i]] = '0';
str[st[1][i]] = '1';
}
for (i = 1; i <= n; i++) {
putchar(str[i]);
}
putchar('\n');
}
return 0;
}

66
HDOJ/4666_autoAC.cpp Normal file
View File

@ -0,0 +1,66 @@
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
#define maxn 60002
int maps[maxn][6];
multiset<int>se[1<<5];
multiset<int>::iterator i1,i2;
int main()
{
int i,j,leap,x,n,m,a;
while(~scanf("%d%d",&n,&m))
{
for(i=0;i<(1<<5);i++)
se[i].clear();
for(i=1;i<=n;i++)
{
scanf("%d",&leap);
if(leap==0)
{
for(j=0;j<m;j++)
{
scanf("%d",&maps[i][j]);
}
for(a=0;a<(1<<m);a++)
{
int as;
as=0;
for(j=0;j<m;j++)
{
if(a&(1<<j))as+=maps[i][j];
else as-=maps[i][j];
}
se[a].insert(as);
}
}
else
{
scanf("%d",&x);
for(a=0;a<(1<<m);a++)
{
int as;
as=0;
for(j=0;j<m;j++)
{
if(a&(1<<j))as+=maps[x][j];
else as-=maps[x][j];
}
i1=se[a].find(as);
se[a].erase(i1);
}
}
int ans=0;
for(a=0;a<(1<<m);a++)
{
i1=se[a].begin();
i2=se[a].end();
i2--;
ans=max(ans,(*i2)-(*i1));
}
cout<<ans<<endl;
}
}
return 0;
}

116
HDOJ/4667_autoAC.cpp Normal file
View File

@ -0,0 +1,116 @@
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<algorithm>
#define PI 3.1415926
using namespace std;
struct list
{
double x;
double y;
int r;
int pos;
}point[500001],tu[500001],pp;
int tops,num;
int cmp1(struct list a,struct list b)
{
if(a.y!=b.y)return a.y<b.y;
else return a.x<b.x;
}
int cmp2(struct list a,struct list b)
{
if((a.x-point[0].x)*(b.y-point[0].y)==(b.x-point[0].x)*(a.y-point[0].y))
return ((a.x-point[0].x)*(a.x-point[0].x)+(a.y-point[0].y)*(a.y-point[0].y))<((point[0].x-b.x)*(point[0].x-b.x)+(point[0].y-b.y)*(point[0].y-b.y));
return (a.x-point[0].x)*(b.y-point[0].y)>(b.x-point[0].x)*(a.y-point[0].y);
}
int ts;
void yuan(int x,int y,int r)
{
pp.r=r;
for(int i=0;i<500;i++)
{
double du;
du=(double)2.0*PI*i/500.0;
pp.x=1.0*x+1.0*r*cos(du);
pp.y=1.0*y+1.0*r*sin(du);
pp.pos=ts;
point[tops++]=pp;
}
}
int pan(int z,int b,int a)
{
double x1,y1,x2,y2;
if(a<0)return 1;
x1=tu[b].x-tu[a].x;
y1=tu[b].y-tu[a].y;
x2=point[z].x-tu[b].x;
y2=point[z].y-tu[b].y;
if(x1*y2>x2*y1)return 1;
return 0;
}
double len(struct list p1,struct list p2)
{
return (double)sqrt(1.0*((p1.x-p2.x)*(p1.x-p2.x)+1.0*(p1.y-p2.y)*(p1.y-p2.y)));
}
void init(int n,int m)
{
int i,j;
tops=0;
int a,b,c;
ts=0;
for(i=0;i<n;i++)
{
scanf("%d%d%d",&a,&b,&c);
ts++;
yuan(a,b,c);
}
for(i=0;i<m;i++)
for(j=0;j<3;j++)
{
scanf("%lf%lf",&point[tops].x,&point[tops].y);
point[tops].pos=0;
tops++;
}
}
void jiantu()
{
int i;
sort(point,point+tops,cmp1);
sort(point+1,point+tops,cmp2);
tu[0]=point[0];
tu[1]=point[1];
tu[2]=point[2];
num=3;
for(i=3;i<tops;i++)
{
while(!pan(i,num-1,num-2))num--;
tu[num++]=point[i];
}
}
void print()
{
double lens;
int i;
lens=0.0;
for(i=0;i<num;i++)
{
if(tu[i].pos>0&&(tu[i].pos==tu[(i+1)%num].pos))
{
lens+=1.0*tu[i].r*2*PI/500.0;
}
else
lens+=len(tu[i],tu[(i+1)%num]);
}
printf("%.5lf\n",lens);
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
init(n,m);
jiantu();
print();
}
return 0;
}

147
HDOJ/4668_autoAC.cpp Normal file
View File

@ -0,0 +1,147 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
#include <string>
#include <queue>
#include <cmath>
#include <algorithm>
#define lson step << 1
#define rson step << 1 | 1
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
const int N = 5005;
struct Node {
string s;
int cnt;
Node () {}
Node (string _s , int c) :s(_s) , cnt(c) {}
string cat () {
string t = s;
for (int i = 1 ; i < cnt ; i ++)
s = s + t;
cnt = 1;
return s;
}
LL len () {
return (LL)s.size() * cnt;
}
string prefix (int l) {
string str = s;
for (int i = 1 ; i < cnt && str.size() < l ; i ++) {
str += s;
}
return str.substr (0 , l);
}
string suffix (int l) {
string str = s;
for (int i = 1 ; i < cnt && str.size() < l ; i ++) {
str += s;
}
return str.substr (str.size() - l , l);
}
}a[N];
char str[N] , pat[N];
int ne[N] , idx , l , p;
void get_next (char *s , int l) {
ne[0] = -1;
int i = 0 , j = -1;
while (i < l) {
if (j == -1 || s[i] == s[j]) {
i ++; j ++;
ne[i] = j;
}
else j = ne[j];
}
}
void gao (string s , int tot) {
if (s == "") return ;
if (idx == 0 || s.size() * tot >= p || a[idx - 1].len() >= p) {
a[idx ++] = Node (s , tot);
}
else {
a[idx - 1].cat ();
a[idx - 1].s += Node (s , tot).cat();
}
}
int match (string s , char *t , int p) {
int l = s.size() ;
int i = 0 , j = 0 , ans = 0;
while (i < s.size()) {
if (j == - 1 || s[i] == t[j]) {
i ++; j ++;
if (j == p) {
ans ++;
j = ne[j];
}
}
else j = ne[j];
}
return ans;
}
int main () {
while (scanf ("%s %s" , str , pat) != EOF) {
idx = 0;
l = strlen (str);p = strlen (pat);
get_next (pat , p);
string s = "";
int tot = 1;
for (int i = 0 ; i < l ; i ++) {
if (str[i] == '[') {
if (s == "") continue;
gao (s , tot);
s = ""; tot = 1;
}
else if (str[i] == ']') {
tot = 0;
i ++;
while (isdigit(str[i]))
tot = tot * 10 + str[i ++] - '0';
i --;
gao (s , tot);
s = ""; tot = 1;
}
else s += str[i];
}
gao (s , tot);
s = ""; tot = 1;
LL ans = 0;
for (int i = 0 ; i < idx ; i ++) {
if (a[i].len() < p) continue;
if (a[i].cnt == 1) ans += match (a[i].s , pat , p);
else {
int use = min(a[i].cnt , 1 + (p - 1 + (int)a[i].s.size() - 1) / (int)a[i].s.size());
string s = "";
for (int j = 1 ; j < use ; j ++) {
s += a[i].s;
}
s = a[i].s + s.substr (0 , min ((int)s.size() , p - 1));
int tmp = match (s , pat , p);
ans += (LL)tmp * (a[i].cnt - use + 1);
if (p) {
s = "";
for (int j = 1 ; j < use ; j ++)
s += a[i].s;
ans += match (s , pat , p);
}
}
}
for (int i = 0 ; i < idx - 1 ; i ++) {
s = a[i].suffix (min (a[i].len () , p - 1LL));
if (a[i + 1].len () < p - 1) {
s += a[i + 1].cat ();
if (i + 2 < idx) {
s += a[i + 2].prefix (min (a[i + 2].len () , p - 1 - a[i + 2].len ()));
}
}
else {
s += a[i + 1].prefix (min (a[i + 1].len () , p - 1LL));
}
ans += match (s , pat , p);
}
printf ("%I64d\n" , ans);
}
return 0;
}

73
HDOJ/4669_autoAC.cpp Normal file
View File

@ -0,0 +1,73 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
#include <string>
#include <queue>
#include <cmath>
#include <algorithm>
#define lson step << 1
#define rson step << 1 | 1
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
const int N = 50005;
const int M = 205;
int n , k , a[N] ,l[N];
int dp[2][M] , prefix[N] , fac[N << 2] , suffix[N];
int cnt[M];
int cal (int x) {
int cnt = 0;
while (x) x /= 10 , cnt ++;
return cnt;
}
int main () {
while (scanf ("%d %d" , &n , &k) != EOF) {
fac[0] = 1;
for (int i = 1 ; i <= (n << 2) ; i ++)
fac[i] = fac[i - 1] * 10 % k;
for (int i = 1 ; i <= n ; i ++) {
scanf ("%d" , &a[i]);
l[i] = cal (a[i]);
}
for (int i = 0 ; i < 2 ; i ++) {
for (int j = 0 ; j < k ; j ++)
dp[i][j] = 0;
}
dp[1][a[1] % k] = 1;
LL ans = dp[1][0];
for (int i = 2 ; i <= n ; i ++) {
for (int j = 0 ; j < k ; j ++)
dp[i & 1][j] = 0;
dp[i & 1][a[i] % k] ++;
for (int j = 0 ; j < k ; j ++) {
dp[i & 1][(j * fac[l[i]] + a[i]) %k] += dp[(i - 1) & 1][j];
}
ans += dp[i & 1][0];
}
prefix[0] = 0;suffix[n + 1] = 0;
for (int i = 1 ; i <= n ; i ++) {
prefix[i] = (prefix[i - 1] * fac[l[i]] + a[i]) % k;
}
int len = 0;
for (int i = n ; i >= 1 ; i --) {
suffix[i] = (a[i] * fac[len] + suffix[i + 1]) % k;
len += l[i];
}
len = 0;
for (int i = 0 ; i < k ; i ++)
cnt[i] = 0;
for (int i = 1 ; i <= n ; i ++) {
cnt[suffix[i]] ++;
len += l[i];
int p = prefix[i];
for (int j = 0 ; j < k ; j ++) {
if ((j * fac[len] + p) % k) continue;
ans += dp[n & 1][j] - cnt[j];
}
}
printf ("%I64d\n" , ans);
}
return 0;
}

171
HDOJ/4670_autoAC.cpp Normal file
View File

@ -0,0 +1,171 @@
#pragma comment(linker,"/STACK:102400000,102400000")
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
#define rep(i,s,t) for(int i=s;i<t;i++)
#define red(i,s,t) for(int i=s-1;i>=t;i--)
#define ree(i,now) for(int i=head[now];i!=-1;i=edge[i].next)
#define clr(a,v) memset(a,v,sizeof a)
const int N=50005;
const int K=33;
struct Edge{
int v,next;
}edge[N*2];
int head[N],e;
int Num[N][K];
int son[N],MaxSize;
int root,all;
int n,k,a[K],d[K],x,y,cnt;
ll v[N],dis[N],three[K],mark[N];
int Ans;
bool vis[N];
map<ll,int>mp,mpp;
inline void Init(){
three[0]=1;
rep(i,1,K) three[i]=three[i-1]*3;
}
inline int getPos(ll v,int x){
return v%three[x+1]/three[x];
}
inline ll get(ll val,int *x){
ll ans=0;
rep(i,0,k) ans+=((getPos(val,i)+x[i])%3)*three[i];
return ans;
}
inline void getCh(ll b,int i){
bool ok=1;
mark[i]=0;
if(b==0)return;
rep(j,0,k){
Num[i][j]=0;
while(b>1 && b%a[j]==0){
Num[i][j]++;
b/=a[j];
}
Num[i][j]%=3;
mark[i]+=three[j]*Num[i][j];
if(Num[i][j]!=0) ok=0;
}
if(ok) Ans++;
}
inline void addEdge(int u,int v){
edge[e].v=v;
edge[e].next=head[u];
head[u]=e++;
}
inline void In(){
scanf("%d",&k);
rep(i,0,k) scanf("%d",&a[i]);
rep(i,1,n+1){
scanf("%I64d",&v[i]);
getCh(v[i],i);
}
clr(head,-1),e=0;
rep(i,1,n){
scanf("%d%d",&x,&y);
addEdge(x,y);addEdge(y,x);
}
clr(vis,0);
}
inline void dfs_Size(int now,int pre){
all++;
ree(i,now){
int nxt=edge[i].v;
if(!vis[nxt] && nxt!=pre) dfs_Size(nxt,now);
}
}
inline void dfs_Root(int now,int pre){
int Max=0;
son[now]=0;
ree(i,now){
int nxt=edge[i].v;
if(!vis[nxt] && pre!=nxt){
dfs_Root(nxt,now);
son[now]+=son[nxt]+1;
if(son[nxt]+1>Max) Max=son[nxt]+1;
}
}
int res=all-son[now]-1;
if(Max<res) Max=res;
if(Max<MaxSize){
MaxSize=Max;root=now;
}
}
inline ll Val(int *c){
ll ans=0;
rep(j,0,k) ans+=c[j]*three[j];
return ans;
}
inline ll Transform(ll a,ll b){
ll ans=0;
rep(j,0,k) ans+=(getPos(a,j)+getPos(b,j))%3*three[j];
return ans;
}
inline void dfs_Dis(int now,int pre,ll val){
dis[cnt++]=val;
ree(i,now){
int nxt=edge[i].v;
if(!vis[nxt] && nxt!=pre){
dfs_Dis(nxt,now,Transform(val,mark[nxt]));
}
}
}
inline ll Suit(ll v){
ll ans=0;
rep(i,0,k){
int x=getPos(v,i);
int y=(3-x)%3;
ans+=three[i]*y;
}
return ans;
}
inline void Work(int now){
all=0;
dfs_Size(now,-1);
MaxSize=n+10;
dfs_Root(now,-1);
vis[root]=1;
mp.clear();
ree(i,root){
int nxt=edge[i].v;cnt=0;
if(vis[nxt]) continue;
dfs_Dis(nxt,root,mark[nxt]);
rep(j,0,cnt){
int vv=mp[Suit(dis[j])];
if(vv) Ans+=vv;
}
rep(j,0,cnt){
ll p=get(dis[j],Num[root]);
if(p==0) Ans++;
mp[p]++;
}
}
ree(i,root){
int nxt=edge[i].v;
if(!vis[nxt]){
Work(nxt);
}
}
}
inline void app(){
int a[]={2,3,5,7,11,13,17,19,23,29};
rep(j,0,10){
printf("3000 10 2 3 5 7 11 13 17 19 23 29\n");
rep(i,0,3000) printf("%d ",a[rand()%10]);
rep(i,1,3000) printf("%d 1\n",i+1);
}
}
int main(){
Init();
while(~scanf("%d",&n)){
Ans=0;
In();
Work(1);
printf("%d\n",Ans);
}
return 0;
}

42
HDOJ/4671_autoAC.cpp Normal file
View File

@ -0,0 +1,42 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int V = 105;
int n, m, ans[V][V];
bool vis[V][V];
int main() {
int i, j, k;
while(~scanf("%d%d", &n, &m)) {
memset(ans, 0, sizeof(ans));
memset(vis, false, sizeof(vis));
for(i = 0, j = 1; i < m; ++i) {
ans[i][0] = j;
vis[i][j] = true;
j = j == n ? 1 : j + 1;
}
for(i = 0; i < m; ++i) {
if(ans[i][1] != 0)
continue;
int now = m % n + 1;
for(j = i; j < m; j += n) {
if(now == ans[j][0])
now = now == n ? 1 : now + 1;
ans[j][1] = now;
vis[j][now] = true;
now = now == n ? 1 : now + 1;
}
}
for(i = 0; i < m; ++i)
for(j = 2, k = 1; j < n; ++j) {
while(vis[i][k])
k++;
ans[i][j] = k++;
}
for(i = 0; i < m; ++i) {
for(j = 0; j < n - 1; ++j)
printf("%d ", ans[i][j]);
printf("%d\n", ans[i][n - 1]);
}
}
}

41
HDOJ/4672_autoAC.cpp Normal file
View File

@ -0,0 +1,41 @@
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<cstring>
#define I(x) scanf("%d",&x)
using namespace std;
int sg[10001],a[100001];
char x[20],y[20];
int main(){
int n,m,i,j,MIN,b,t,ans;
while(scanf("%d%d%s%s",&n,&m,x,y)!=EOF){
memset(sg,-1,sizeof(sg));
sg[0]=0;MIN=1e9;
bool flag=0;
for(i=0;i<n;i++) I(a[i]);
for(i=0;i<m;i++){
I(b);
MIN=min(MIN,b);
for(j=0;j+b<=10000;j++)
if(sg[j]>=0)
sg[j+b]=max(sg[b+j],sg[j]+1);
}
ans=0;
for(i=0;i<n;i++){
t=a[i];
if(t<=10000){
if(sg[t]<0) flag=1;
else ans^=sg[t];
}
else{
t=(t-10000+MIN)%MIN+10000-MIN;
if(sg[t]<0) flag=1;
else ans^=sg[t]+(a[i]-t)/MIN;
}
}
if(!flag&&ans) puts(x);
else puts(y);
}
return 0;
}

153
HDOJ/4674_autoAC.cpp Normal file
View File

@ -0,0 +1,153 @@
#pragma comment(linker, "/STACK:65536000")
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <algorithm>
#define clr(a,b) memset(a,b,sizeof(a))
#define mpr(a,b) make_pair(a,b)
#define ll long long
#define eps 1e-6
using namespace std;
const int N=100005,M=300005;
int n,m,q,eid,id,now,top;
int head[N],ed[M],nxt[M];
int dfn[N],low[N],gid[N];
int sta[N],block[N];
vector<pair<int,int> >gra[N],qu[N];
int f[N],vis[N],come[N],out[N],fa[N];
int tlca[3*N],pt[3*N][2];
int aa[N],bb[N],cc[N];
int findfa(int s){return s==fa[s]?s:fa[s]=findfa(fa[s]);}
void addedge(int s,int e){
ed[eid]=e;nxt[eid]=head[s];head[s]=eid++;
}
void tarjan(int s,int f,int b){
dfn[s]=low[s]=++now;
sta[top++]=s;block[s]=b;
for(int i=head[s];~i;i=nxt[i]){
int e=ed[i];
if(i==f||i==(f^1))continue;
if(!dfn[e]){
tarjan(e,i,b);
low[s]=min(low[s],low[e]);
}else
low[s]=min(low[s],dfn[e]);
}
if(low[s]==dfn[s]){
id++;
while(top){
int k=sta[--top];
gid[k]=id;
if(k==s)return ;
}
}
}
void lca(int s,int f){
fa[s]=s;
for(int i=0;i<(int)gra[s].size();i++){
int e=gra[s][i].first;
if(e==f)continue;
lca(e,s);
fa[findfa(e)]=s;
}
vis[s]=1;
for(int i=0;i<(int)qu[s].size();i++){
int e=qu[s][i].first,d=qu[s][i].second;
if(vis[e])tlca[d]=findfa(e);
}
}
void dfs(int s,int f){
for(int i=0;i<(int)qu[s].size();i++){
int d=qu[s][i].second;
int k=(tlca[d]==s)?-1:out[tlca[d]];
if(~pt[d][0])pt[d][1]=k;
else pt[d][0]=k;
}
for(int i=0;i<(int)gra[s].size();i++){
int e=gra[s][i].first,g=gra[s][i].second;
if(e==f){
come[s]=g;continue;
}
out[s]=g;
dfs(e,s);
}
}
int main(){
while(~scanf("%d%d",&n,&m)){
eid=0;clr(head,-1);
for(int i=0;i<m;i++){
int a,b;scanf("%d%d",&a,&b);
addedge(a,b);addedge(b,a);
}
id=now=top=0;
for(int i=1;i<=n;i++)dfn[i]=0;
int cnt=0;
for(int i=1;i<=n;i++)
if(!dfn[i])tarjan(i,-1,++cnt);
for(int i=1;i<=id;i++){
gra[i].clear();qu[i].clear();
}
for(int i=1;i<=n;i++)
for(int j=head[i];~j;j=nxt[j]){
int s=gid[i],e=gid[ed[j]];
if(s!=e)gra[s].push_back(mpr(e,i));
}
clr(f,0);
scanf("%d",&q);
for(int i=0;i<q;i++){
int a,b,c;scanf("%d%d%d",&a,&b,&c);
if(block[a]!=block[b]){f[i]=1;continue;}
if(block[b]!=block[c]){f[i]=1;continue;}
if(block[a]!=block[c]){f[i]=1;continue;}
if(a==b&&a!=c){f[i]=1;continue;}
int s=gid[a],e=gid[b],k=gid[c];
qu[s].push_back(mpr(e,i*3));
qu[e].push_back(mpr(s,i*3));
qu[s].push_back(mpr(k,i*3+1));
qu[k].push_back(mpr(s,i*3+1));
qu[e].push_back(mpr(k,i*3+2));
qu[k].push_back(mpr(e,i*3+2));
aa[i]=a;bb[i]=b;cc[i]=c;
}
for(int i=1;i<=id;i++)vis[i]=0;
for(int i=0;i<q*3;i++)
pt[i][0]=pt[i][1]=-1;
for(int i=1;i<=id;i++)
if(!vis[i]){
lca(i,-1);
dfs(i,-1);
}
for(int i=0;i<q;i++){
if(f[i]){
puts("No");continue;
}
if(gid[aa[i]]==gid[bb[i]]){
if(gid[cc[i]]==gid[aa[i]])puts("Yes");
else puts("No");
}
else if(gid[cc[i]]==tlca[i*3]){
if(pt[i*3][1]==-1){
int k=(gid[aa[i]]==gid[cc[i]])?aa[i]:bb[i];
if(k==pt[i*3][0]&&cc[i]!=k) puts("No");
else puts("Yes");
}else{
if(pt[i*3][0]==pt[i*3][1]&&cc[i]!=pt[i*3][0]) puts("No");
else puts("Yes");
}
}else if(tlca[i*3+1]==gid[cc[i]]&&tlca[i*3+2]==tlca[i*3]){
int k=(pt[i*3+1][0]==-1)?aa[i]:pt[i*3+1][0];
if(come[gid[cc[i]]]==k&&k!=cc[i])puts("No");
else puts("Yes");
}else if(tlca[i*3+2]==gid[cc[i]]&&tlca[i*3+1]==tlca[i*3]){
int k=(pt[i*3+2][0]==-1)?bb[i]:pt[i*3+2][0];
if(come[gid[cc[i]]]==k&&k!=cc[i])puts("No");
else puts("Yes");
}else puts("No");
}
}
}

92
HDOJ/4675_autoAC.cpp Normal file
View File

@ -0,0 +1,92 @@
#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>
using namespace std;
const int MOD = 1e9+7;
long long pow_m(long long a,long long n)
{
long long ret = 1;
long long tmp = a%MOD;
while(n)
{
if(n&1)
{
ret *= tmp;
ret %= MOD;
}
tmp *= tmp;
tmp %= MOD;
n >>= 1;
}
return ret;
}
long long C[300010];
long long inv(long long a,long long m)
{
if(a == 1)return 1;
return inv(m%a,m)*(m-m/a)%m;
}
long long ans[300010];
int a[300010];
int num[300010];
int b[300010];
int main()
{
int N,M,K;
while(scanf("%d%d%d",&N,&M,&K) == 3)
{
memset(num,0,sizeof(num));
for(int i = 1;i <= N;i++)
{
scanf("%d",&a[i]);
num[a[i]]++;
}
C[N-K] = 1;
for(int i = N-K+1;i <= N;i++)
{
C[i] = C[i-1]*i%MOD*inv(i-(N-K),MOD)%MOD;
}
for(int i = M;i>= 1;i--)
{
int cnt = 0;
long long ss = 0;
for(int j = 1; j*i <= M;j++)
{
cnt += num[i*j];
if(j > 1)ss = (ss + ans[i*j])%MOD;
}
int t = M/i;
if(t == 1)
{
if(cnt == N-K)ans[i] = 1;
else ans[i] = 0;
continue;
}
if(cnt < N-K)
{
ans[i] = 0;
continue;
}
long long tmp = 1;
tmp =(tmp*C[cnt])%MOD;
tmp = ( tmp * pow_m(t-1,cnt-(N-K)) )%MOD;
tmp = (tmp * pow_m(t,N-cnt));
ans[i] = (tmp - ss + MOD)%MOD;
}
for(int i = 1;i <= M;i++)
{
printf("%I64d",ans[i]);
if(i < M)printf(" ");
else printf("\n");
}
}
return 0;
}

102
HDOJ/4676_autoAC.cpp Normal file
View File

@ -0,0 +1,102 @@
#include<cstdio>
#include<cstring>
#include<climits>
#include<string>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<queue>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<stdlib.h>
using namespace std;
typedef long long ll;
const int MAXN = 20007;
vector<int> divisor[MAXN];
int f[MAXN], a[MAXN], L[MAXN], R[MAXN], cnt[MAXN], qid[MAXN], M;
ll ans[MAXN];
void getdivisor() {
int i, j;
for(i = 1; i < MAXN; i++) for(j = i; j < MAXN; j += i) divisor[j].push_back(i);
}
void getf() {
for(int i = 1; i < MAXN; i++) {
int t = i;
int j, sz = divisor[i].size();
for(j = 0; j < sz; j++) t -= f[divisor[i][j]];
f[i] = t;
}
}
int qcmp(int x, int y) {
int px = L[x] / M;
int py = L[y] / M;
if(px != py) return px < py;
return R[x] < R[y];
}
ll erase(int x) {
int i, y, sz = divisor[x].size();
ll ret = 0;
for(i = 0; i < sz; i++) {
y = divisor[x][i];
ret += 1LL*f[y]*(--cnt[y]);
}
return ret;
}
ll insert(int x) {
int i, y, sz = divisor[x].size();
ll ret = 0;
for(i = 0; i < sz; i++) {
y = divisor[x][i];
ret += 1LL*f[y]*(cnt[y]++);
}
return ret;
}
void solve(int n, int q) {
int i;
for(i = 1; i <= q; i++) qid[i] = i;
M = (int)sqrt(n*1.0);
sort(qid+1, qid+1+q, qcmp);
memset(cnt, 0, sizeof(cnt));
int l = 0, r = 0;
ll now = 0;
for(i = 1; i <= q; i++) {
int id = qid[i];
int nl = L[id], nr = R[id];
while(l < nl) {
now -= erase(a[l]);
l++;
}
while(l > nl) {
l--;
now += insert(a[l]);
}
while(r < nr) {
r++;
now += insert(a[r]);
}
while(r > nr) {
now -= erase(a[r]);
r--;
}
ans[id] = now;
}
}
int main() {
getdivisor();
getf();
int T, id = 0;
scanf("%d", &T);
while(T--) {
int i, n, q;
scanf("%d", &n);
for(i = 1; i <= n; i++) scanf("%d", a+i);
scanf("%d", &q);
for(i = 1; i <= q; i++) scanf("%d%d", L+i, R+i);
solve(n, q);
printf("Case #%d:\n", ++id);
for(i = 1; i <= q; i++) printf("%I64d\n", ans[i]);
}
return 0;
}

122
HDOJ/4677_autoAC.cpp Normal file
View File

@ -0,0 +1,122 @@
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
#define N 30050
struct Q
{
int r,idx;
Q(int _r=0,int _idx=0):r(_r),idx(_idx){}
};
vector<Q>que[N];
vector<int>edge[N];
int fa[N][200],scc[200],rr[200];
int ta[N],first[N],ans[N];
int n,m,tot,every;
int find(int i,int pos)
{
return fa[i][pos]==i?i:fa[i][pos]=find(fa[i][pos],pos);
}
void init()
{
for(int i=0;i<=n;++i)
edge[i].clear(),que[i].clear();
memset(scc,0,sizeof(scc));
every=tot=sqrt(n*1.0);
if(n*n!=tot) tot++;
for(int i=0;;++i)
{
rr[i]=i*every;
if(i*every>=n) break;
}
for(int i=0;i<=tot;++i)
for(int j=0;j<=n;++j)
fa[j][i]=j;
memset(first,-1,sizeof(first));
}
int main ()
{
int T;scanf("%d",&T);
for(int kk=1;kk<=T;++kk)
{
scanf("%d%d",&n,&m);
init();
for(int i=1,u,v;i<=m;++i)
{
scanf("%d%d",&u,&v);
if(u==v) continue;
edge[u].push_back(v);
edge[v].push_back(u);
}
int q;scanf("%d",&q);
for(int i=1,u,v;i<=q;++i)
{
scanf("%d%d",&u,&v);
que[u].push_back(Q(v,i));
}
int run_clock=0;
for(int u=n;u>=1;--u)
{
for(int j=1;j<=tot;++j)
{
if(u>rr[j]) continue;
scc[j]++;
int inc=0;
for(int i=0;i<edge[u].size();++i)
{
int v=edge[u][i];
if(u<v&&v<=rr[j])
{
int u1=find(u,j);
int v1=find(v,j);
if(u1!=v1)
{
inc++;
fa[v1][j]=u1;
}
}
}
scc[j]-=inc;
}
for(int z=0;z<que[u].size();++z)
{
run_clock++;
int right=que[u][z].r;
int pos;int inc=0;
for(pos=0;pos<tot;++pos)
if(rr[pos]<right&&right<=rr[pos+1]) break;
for(int i=max(u+1,rr[pos]+1);i<=right;++i)
{
if(first[i]!=run_clock)
{
first[i]=run_clock;
fa[i][tot+1]=i;
}
for(int j=0;j<edge[i].size();++j)
if(u<=edge[i][j]&&edge[i][j]<i)
{
int v1=find(edge[i][j],pos);
if(first[v1]!=run_clock)
{
first[v1]=run_clock;
fa[v1][tot+1]=v1;
}
if(find(i,tot+1)!=find(v1,tot+1))
{
fa[find(v1,tot+1)][tot+1]=find(i,tot+1);
inc++;
}
}
}
ans[que[u][z].idx]=scc[pos]+right-max(rr[pos],u-1)-inc;
}
}
printf("Case #%d:\n",kk);
for(int i=1;i<=q;++i)
printf("%d\n",ans[i]);
}
return 0;
}

109
HDOJ/4678_autoAC.cpp Normal file
View File

@ -0,0 +1,109 @@
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#define loop(s,i,n) for(i = s;i < n;i++)
#define cl(a,b) memset(a,b,sizeof(a))
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
int map[1005][1005];
int vis[1005][1005];
int n,m,k;
int num,odd,uod;
int to[8][2] = {{0,1},{0,-1},{1,0},{1,-1},{1,1},{-1,-1},{-1,0,},{-1,1}};
int judge(int a,int b)
{
if(a >= 0 && a < n && b>= 0 &&b < m)
return 1;
return 0;
}
void paint(int x,int y)
{
int i,j;
for(i = 0;i < 8;i++)
{
int a,b;
a = x+to[i][0];
b = y+to[i][1];
if(judge(a,b) && !vis[a][b])
map[a][b]++;
}
}
void dfs(int x,int y)
{
if(map[x][y] == -1 || vis[x][y])
return;
vis[x][y] = 1;
int i;
for(i = 0;i < 8;i++)
{
int a,b;
a = x+to[i][0];
b = y+to[i][1];
if(judge(a,b)&&vis[a][b] == 0)
{
if(map[a][b] > 0)
num++,vis[a][b] = 1;
else if(map[a][b] == 0)
dfs(a,b);
}
}
return;
}
int main()
{
int t,i,j,x,y;
scanf("%d",&t);
int icase = 0;
while(t--)
{
scanf("%d %d %d",&n,&m,&k);
loop(0,i,n)
{
loop(0,j,m)
map[i][j] = vis[i][j] = 0;
}
while(k--)
{
scanf("%d %d",&x,&y);
map[x][y] = -1;
vis[x][y] = 1;
paint(x,y);
}
odd = uod = 0;
loop(0,i,n)
{
loop(0,j,m)
{
num = 0;
if(map[i][j] == 0 && !vis[i][j])
{
dfs(i,j);
if(num%2)
odd++;
else
uod++;
}
}
}
loop(0,i,n)
{
loop(0,j,m)
{
if(!vis[i][j] && map[i][j] > 0)
uod++;
}
}
printf("Case #%d: ",++icase);
if(uod%2 || odd%2)
puts("Xiemao");
else
puts("Fanglaoshi");
}
return 0;
}

177
HDOJ/4679_autoAC.cpp Normal file
View File

@ -0,0 +1,177 @@
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
#define maxn 100011
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define INF 99999999
struct list
{
int u,v,w,number;
int next;
} node[maxn*4];
struct list2
{
int point;
int len;
}no[4][maxn],pp;
int head[maxn*10];
int low[maxn*2];
int f[maxn];
int ipf[maxn][2];
int ips[maxn][2];
int h[maxn];
int tops,maxx,ipos,n;
void add(int u,int v,int w,int number)
{
node[tops].u=u;
node[tops].v=v;
node[tops].w=w;
node[tops].number=number;
node[tops].next=head[u];
head[u]=tops++;
}
void init()
{
memset(head,-1,sizeof(head));
memset(f,0,sizeof(f));
memset(h,0,sizeof(h));
memset(ipf,0,sizeof(ipf));
memset(low,0,sizeof(low));
memset(ips,0,sizeof(ips));
tops=0;
maxx=INF;
ipos=maxn+10;
scanf("%d",&n);
for(int i=0;i<=n;i++)
{
no[1][i].len=no[2][i].len=no[3][i].len=0;
no[1][i].point=no[2][i].point=no[3][i].point=0;
}
int a,b,c;
for(int i=1; i<n; i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c,i);
add(b,a,c,i);
}
}
int dfs(int x,int pre)
{
int ts,tt;
ts=0;
f[x]=0;
for(int i=head[x]; i!=-1; i=node[i].next)
{
int y=node[i].v;
if(y==pre)continue;
tt=dfs(y,x)+1;
ts=max(tt,ts);
if(f[y]>=ipf[x][0])
{
ipf[x][1]=ipf[x][0];
ipf[x][0]=f[y];
ips[x][1]=ips[x][0]=y;
}
else if(f[y]>=ipf[x][1])
{
ipf[x][1]=f[y];
}
f[x]=max(f[x],f[y]);
pp.point=y;
pp.len=tt;
for(int j=1; j<=3; j++)
if(no[j][x].len<=tt)
{
for(int k=3; k>=j+1; k--)
{
no[k][x]=no[k-1][x];
}
no[j][x]=pp;
break;
}
}
int ds=no[1][x].len+no[2][x].len;
f[x]=max(f[x],ds);
return ts;
}
int chu(int pre,int x,int y)
{
int s1,s2,s3,s4;
int leap=0;
s1=s2=s3=s4=0;
if(ips[x][0]!=y)s1=ipf[x][0];
else s1=ipf[x][1];
if(no[1][x].point!=y)s2=no[1][x].len,leap++;
if(no[2][x].point!=y)s2+=no[2][x].len,leap++;
if(no[3][x].point!=y&&leap==1)s2+=no[3][x].len,leap++;
s3=h[x];
if(no[1][pre].point!=x)s4=no[1][pre].len;
else s4=no[2][pre].len;
s4=max(s4,low[pre]);
if(no[1][x].point!=y)s4+=no[1][x].len;
else s4+=no[2][x].len;
if(pre!=0)s4++;
return max(max(s1,s2),max(s3,s4));
}
void spfa(int x,int pre)
{
int visit[maxn];
memset(visit,0,sizeof(visit));
queue<int>q;
queue<int>qp;
q.push(x);
qp.push(pre);
visit[x]=1;
while(!q.empty())
{
x=q.front();
q.pop();
pre=qp.front();
qp.pop();
for(int i=head[x]; i!=-1; i=node[i].next)
{
int y=node[i].v;
if(y==pre)continue;
h[y]=chu(pre,x,y);
int ss=max(h[y],f[y])*node[i].w;
if(ss<maxx)
{
maxx=ss;
ipos=node[i].number;
}
else if(ss==maxx)ipos=min(ipos,node[i].number);
q.push(y);
qp.push(x);
}
}
}
void panlow(int x,int pre)
{
for(int i=head[x]; i!=-1; i=node[i].next)
{
int y=node[i].v;
if(y==pre)continue;
if(y!=no[1][x].point)
{
low[y]=max(no[1][x].len+1,low[x]+1);
}
else low[y]=max(no[2][x].len+1,low[x]+1);
panlow(y,x);
}
}
int main()
{
int t,T;
scanf("%d",&T);
for(t=1; t<=T; t++)
{
init();
if(dfs(1,0));
panlow(1,0);
spfa(1,0);
printf("Case #%d: %d\n",t,ipos);
}
return 0;
}

336
HDOJ/4680_autoAC.cpp Normal file
View File

@ -0,0 +1,336 @@
#include<cstdio>
#include<iostream>
#include<cstring>
#define L x->c[0]
#define R x->c[1]
#define KT root->c[1]->c[0]
using namespace std;
const int maxn = 300010;
const int inf = 1000000007;
int n,Type;
int gcd(int a,int b)
{
if(a<0||a>=inf) return b;
if(b<0||b>=inf) return a;
return b == 0?a:gcd(b,a%b);
}
struct Node
{
struct Node *c[2],*fa;
int id,size,g,val,who;
}S[maxn],*null = &S[0];
Node* Q[maxn];
Node* ID[maxn];
int scnt;
struct snode
{
int type;
Node*root;
inline Node *NewNode(int value,Node *p)
{
Node *e = S+(++scnt);
e->val = value;
e->id = scnt;
e->size = 1;
e->fa = p;
e->g = value;
e->c[0] = e->c[1] = null;
ID[scnt] = e;
return e;
}
inline void Update(Node *p)
{
p->size = p->c[0]->size+p->c[1]->size+1;
p->g = gcd(p->c[0]->g,p->c[1]->g);
p->g = gcd(p->val,p->g);
}
inline void Rotate(Node *x,int k)
{
Node *y=x->fa;
y->c[k]=x->c[!k];
y->c[k]->fa=y;
x->fa=y->fa;
if(y->fa->c[0]==y)
y->fa->c[0]=x;
else
y->fa->c[1]=x;
y->fa=x;
x->c[!k]=y;
Update(y);
Update(x);
if(root==y) root=x;
}
inline void Splay(Node *x,Node *y)
{
while(x->fa!=y)
{
if(x->fa->fa==y)
{
if(x->fa->c[0]==x)
Rotate(x,0);
else
Rotate(x,1);
}
else if(x->fa->fa->c[0]==x->fa)
{
if(x->fa->c[0]==x)
Rotate(x->fa,0),Rotate(x,0);
else
Rotate(x,1),Rotate(x,0);
}
else
{
if(x->fa->c[1]==x)
Rotate(x->fa,1),Rotate(x,1);
else
Rotate(x,0),Rotate(x,1);
}
}
Update(x);
}
inline void Select(int k,Node *y)
{
Node *x=root;
while(k!=x->c[0]->size+1)
{
if(k<=x->c[0]->size)
x=x->c[0];
else
{
k-=x->c[0]->size+1;
x=x->c[1];
}
}
Splay(x,y);
}
inline void Init(int v)
{
root = NewNode(v,null);
type = ++Type;
root->who = type;
}
inline void Delete(Node *x)
{
Splay(x,null);
Node *t = root;
if(t->c[1]!=null)
{
root = t->c[1];
Select(1,null);
root->c[0] = t->c[0];
if(root->c[0]!=null)
root->c[0]->fa = root;
}
else
{
root = t->c[0];
}
root->fa = null;
if(root!=null)Update(root);
}
inline void Insert(Node *x)
{
x->c[0] = x->c[1] = null;
insert(root,x);
Splay(x,null);
x->who = type;
}
inline void insert(Node* &x,Node* y)
{
if(x == null)
{
x = y;
return;
}
if(y->val<=x->val)
{
insert(x->c[0],y);
x->c[0]->fa = x;
}
else
{
insert(x->c[1],y);
x->c[1]->fa = x;
}
Update(x);
}
inline void Change(Node* tmp,int v)
{
Delete(tmp);
tmp->val = v;
Insert(tmp);
}
inline Node *FindSucc(Node *x,int v,int opt)
{
if(x == null)return x;
if(x->val == v&&opt)return x;
else if(x->val>v)
{
Node *tmp = FindSucc(x->c[0],v,opt);
return tmp == null?x:tmp;
}
else
{
return FindSucc(x->c[1],v,opt);
}
}
inline Node *FindSucc(int v,int opt)
{
Node *tmp = FindSucc(root,v,opt);
Splay(tmp,null);
return tmp;
}
inline Node *FindPre(Node *x,int v)
{
if(x == null)return x;
if(x->val<v)
{
Node *tmp = FindPre(x->c[1],v);
return tmp == null?x:tmp;
}
else return FindPre(x->c[0],v);
}
inline Node *FindPre(int v)
{
Node* tmp = FindPre(root,v);
Splay(tmp,null);
return tmp;
}
inline void Merge(snode &tree)
{
int head,tail;
head = tail = 0;
tree.Select(1,null);
tree.Select(tree.root->size,tree.root);
Q[++tail] = tree.KT;
while(head<tail)
{
Node* f = Q[++head];
if(f->c[0]!=null)Q[++tail] = f->c[0];
if(f->c[1]!=null)Q[++tail] = f->c[1];
Insert(f);
f->who = type;
}
tree.KT = null;
tree.Update(tree.root->c[1]);
tree.Update(tree.root);
}
inline int SolveTri()
{
if(root->size<=4)return root->size-2;
Select(2,null);
int a = root->val;
Select(3,null);
int b = root->val;
int ans = 2;
while(true)
{
if(a+b>=inf)break;
int c = FindSucc(a+b,1)->val;
if(c>=inf||c == -1)break;
a = b;
b = c;
ans++;
}
return ans;
}
inline int SolveGcd(int l,int r)
{
Node *pre = FindPre(l);
Node *succ = FindSucc(r,0);
Splay(pre,null);
Splay(succ,root);
return KT->g;
}
void vist(Node *x)
{
if(x != null)
{
printf("root %2d: left-son: %2d right-son: %2d size:%2d val=%2d gcd: %2d\n",x->id,x->c[0]->id,x->c[1]->id,x->size,x->val,x->g);
vist(x->c[0]);
vist(x->c[1]);
}
}
}spt[maxn];
void prepare()
{
null->id = 0;
null->c[0] = null->c[1] = null->fa = NULL;
null->size = null->val = 0;
null->g = -1;
Type = 0;
scnt = 0;
}
int main()
{
int t,n,m,a,u,v,op,l,r,x,cas = 1;
scanf("%d",&t);
while(t--)
{
prepare();
printf("Case #%d:\n",cas++);
scanf("%d%d",&n,&m);
for(int i = 1;i<=n;i++)
{
spt[i].Init(-1);
Node *tmp = spt[i].NewNode(inf,null);
spt[i].Insert(tmp);
}
for(int i = 1;i<=n;i++)
{
scanf("%d",&a);
Node *tmp = spt[i].NewNode(a,null);
spt[i].Insert(tmp);
}
int tot = 0;
while(m--)
{
scanf("%d",&op);
if(op == 1)
{
scanf("%d%d",&u,&v);
Node *t1 = ID[u+2*n];
Node *t2 = ID[v+2*n];
int tree1 = t1->who;
int tree2 = t2->who;
if(tree1 == tree2)continue;
int size1 = spt[tree1].root->size;
int size2 = spt[tree2].root->size;
if(size1>size2)spt[tree1].Merge(spt[tree2]);
else spt[tree2].Merge(spt[tree1]);
}
else if(op == 2)
{
scanf("%d%d",&u,&v);
Node *t1 = ID[u+2*n];
Node *t2 = ID[v+2*n];
int tree1 = t1->who;
int tree2 = t2->who;
spt[tree1].Delete(t1);
spt[tree2].Insert(t1);
}
else if(op == 3)
{
scanf("%d%d",&u,&x);
Node *tmp = ID[u+2*n];
int tree = tmp->who;
spt[tree].Change(tmp,x);
}
else if(op == 4)
{
scanf("%d",&u);
Node *tmp = ID[u+2*n];
int tree = tmp->who;
printf("%d\n",spt[tree].SolveTri());
}
else
{
scanf("%d%d%d",&u,&l,&r);
Node *tmp = ID[u+2*n];
int tree = tmp->who;
printf("%d\n",spt[tree].SolveGcd(l,r));
}
}
}
return 0;
}

125
HDOJ/4681_autoAC.cpp Normal file
View File

@ -0,0 +1,125 @@
#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<string.h>
using namespace std;
char sta[1001];
char stb[1001];
char stc[1001];
int lena,lenb,lenc;
int dp[1001][1001];
int dpf[1001][1001];
struct list
{
int x;
int y;
}node,n1,n2;
vector<list>vec[5];
void dps()
{
int i,j;
for(i=0;i<lena;i++)
if(sta[i]==stb[0])
for(i;i<lena;i++)dp[i][0]=1;
for(i=0;i<lenb;i++)
if(sta[0]==stb[i])
for(i;i<lenb;i++)dp[0][i]=1;
for(i=1;i<lena;i++)
{
for(j=1;j<lenb;j++)
{
if(sta[i]==stb[j])dp[i][j]=dp[i-1][j-1]+1;
else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
}
void dpfs()
{
int i,j;
for(i=lena-1;i>=0;i--)
if(sta[i]==stb[lenb-1])
for(i;i>=0;i--)dpf[i][lenb-1]=1;
for(i=lenb-1;i>=0;i--)
if(sta[lena-1]==stb[i])
for(i;i>=0;i--)dpf[lena-1][i]=1;
for(i=lena-2;i>=0;i--)
{
for(j=lenb-2;j>=0;j--)
{
if(sta[i]==stb[j])dpf[i][j]=dpf[i+1][j+1]+1;
else dpf[i][j]=max(dpf[i+1][j],dpf[i][j+1]);
}
}
}
void init()
{
memset(dp,0,sizeof(dp));
memset(dpf,0,sizeof(dpf));
vec[0].clear();
vec[1].clear();
gets(sta);
gets(stb);
gets(stc);
lena=strlen(sta);
lenb=strlen(stb);
lenc=strlen(stc);
}
void sear(char str[],int pos)
{
int lens=strlen(str);
int i,j,k;
for(i=0;i<lens;i++)
{
if(str[i]==stc[0])
{
for(k=0,j=i;j<lens;j++)
{
if(str[j]==stc[k])k++;
if(k==lenc)break;
}
if(j<lens)
{
node.x=i;
node.y=j;
vec[pos].push_back(node);
}
}
}
}
void prin(int cas)
{
int l1=vec[0].size();
int l2=vec[1].size();
int maxx=0;
int i,j;
for(i=0;i<l1;i++)
{
for(j=0;j<l2;j++)
{
n1=vec[0][i];
n2=vec[1][j];
int c1,c2;
c1=c2=0;
if(n1.x-1>=0&&n2.x-1>=0)c1=dp[n1.x-1][n2.x-1];
if(n1.y+1<lena&&n2.y+1<lenb)c2=dpf[n1.y+1][n2.y+1];
maxx=max(maxx,c1+c2+lenc);
}
}
printf("Case #%d: %d\n",cas,maxx);
}
int main()
{
int T,t;
scanf("%d%*c",&T);
for(t=1;t<=T;t++)
{
init();
dps();
dpfs();
sear(sta,0);
sear(stb,1);
prin(t);
}
return 0;
}

125
HDOJ/4685_autoAC.cpp Normal file
View File

@ -0,0 +1,125 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<vector>
using namespace std;
#define MAXN 1111
int n,m,N,cnt;
bool map[MAXN][MAXN];
bool mark[MAXN];
int ly[MAXN];
int lx[MAXN];
int dfs(int u,int m)
{
for(int v=1;v<=m;v++){
if(map[u][v]&&!mark[v]){
mark[v]=true;
if(ly[v]==-1||dfs(ly[v],m)){
ly[v]=u;
lx[u]=v;
return 1;
}
}
}
return 0;
}
int MaxMatch(int n,int m)
{
memset(ly,-1,sizeof(ly));
memset(lx,-1,sizeof(lx));
int ans=0;
for(int i=1;i<=n;i++){
memset(mark,false,sizeof(mark));
ans+=dfs(i,m);
}
return ans;
}
vector<vector<int> >g;
vector<vector<int> >ans;
int dfn[MAXN],low[MAXN];
int color[MAXN],_count;
stack<int>S;
void Tarjan(int u)
{
low[u]=dfn[u]=++cnt;
mark[u]=true;
S.push(u);
for(int i=0;i<g[u].size();i++){
int v=g[u][i];
if(dfn[v]==0){
Tarjan(v);
low[u]=min(low[u],low[v]);
}else if(mark[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){
_count++;
int x;
do{
x=S.top();
S.pop();
mark[x]=false;
color[x]=_count;
}while(x!=u);
}
}
int main()
{
int _case,k,x,t=1;
scanf("%d",&_case);
while(_case--){
scanf("%d%d",&n,&m);
memset(map,false,sizeof(map));
for(int i=1;i<=n;i++){
scanf("%d",&k);
while(k--){
scanf("%d",&x);
map[i][x]=true;
}
}
cnt=MaxMatch(n,m);
N=n+m-cnt;
for(int i=n+1;i<=N;i++)
for(int j=1;j<=N;j++)
map[i][j]=true;
for(int i=m+1;i<=N;i++)
for(int j=1;j<=N;j++)
map[j][i]=true;
MaxMatch(N,N);
g.clear();
g.resize(N+2);
ans.clear();
ans.resize(N+2);
for(int i=1;i<=N;i++){
for(int j=1;j<=N;j++){
if(lx[i]!=j&&map[i][j]){
g[lx[i]].push_back(j);
}
}
}
memset(dfn,0,sizeof(dfn));
memset(mark,false,sizeof(mark));
_count=cnt=0;
for(int i=1;i<=N;i++)
if(dfn[i]==0)Tarjan(i);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(map[i][j]&&color[lx[i]]==color[j]){
ans[i].push_back(j);
}
}
}
printf("Case #%d:\n",t++);
for(int i=1;i<=n;i++){
printf("%d",(int)ans[i].size());
for(int j=0;j<ans[i].size();j++){
printf(" %d",ans[i][j]);
}
puts("");
}
}
return 0;
}

76
HDOJ/4686_autoAC.cpp Normal file
View File

@ -0,0 +1,76 @@
#include<stdio.h>
#include<string.h>
#define LL long long
#define M 1000000007
struct Matrix
{
LL a[6][6];
}origin,res,tmp,A,ans;
int n;
Matrix mul(Matrix x,Matrix y)
{
int i,j,k;
memset(tmp.a,0,sizeof(tmp.a));
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
for(k=1;k<=n;k++)
{
tmp.a[i][j]+=(x.a[i][k]*y.a[k][j])%M;
tmp.a[i][j]%=M;
}
return tmp;
}
void quickpow(LL k)
{
int i;
memset(res.a,0,sizeof(res.a));
for(i=1;i<=n;i++)
res.a[i][i]=1;
while(k)
{
if(k&1)
res=mul(res,A);
A=mul(A,A);
k>>=1;
}
}
int main()
{
LL N,a0,ax,ay,b0,bx,by;
LL f1,a1,b1,s0;
while(scanf("%lld %lld %lld %lld %lld %lld %lld",&N,&a0,&ax,&ay,&b0,&bx,&by)!=EOF)
{
if(N==0)
{
printf("0\n");
continue;
}
a1=(a0*ax+ay)%M;
b1=(b0*bx+by)%M;
f1=(a1*b1)%M;
s0=(a0*b0)%M;
n=5;
memset(origin.a,0,sizeof(origin.a));
origin.a[1][1]=f1;
origin.a[1][2]=a1;
origin.a[1][3]=b1;
origin.a[1][4]=1;
origin.a[1][5]=s0;
memset(A.a,0,sizeof(A.a));
A.a[1][1]=(ax*bx)%M;
A.a[1][5]=1;
A.a[2][1]=(ax*by)%M;
A.a[2][2]=ax%M;
A.a[3][1]=(ay*bx)%M;
A.a[3][3]=bx%M;
A.a[4][1]=(ay*by)%M;
A.a[4][2]=ay%M;
A.a[4][3]=by%M;
A.a[4][4]=1;
A.a[5][5]=1;
quickpow(N-1);
ans=mul(origin,res);
printf("%lld\n",ans.a[1][5]);
}
return 0;
}

187
HDOJ/4687_autoAC.cpp Normal file
View File

@ -0,0 +1,187 @@
#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>
using namespace std;
const int MAXN = 50;
int N;
bool Graph[MAXN][MAXN];
int Match[MAXN];
bool InQueue[MAXN],InPath[MAXN],InBlossom[MAXN];
int Head,Tail;
int Queue[MAXN];
int Start,Finish;
int NewBase;
int Father[MAXN],Base[MAXN];
int Count;
void Push(int u)
{
Queue[Tail] = u;
Tail++;
InQueue[u] = true;
}
int Pop()
{
int res = Queue[Head];
Head++;
return res;
}
int FindCommonAncestor(int u,int v)
{
memset(InPath,false,sizeof(InPath));
while(true)
{
u = Base[u];
InPath[u] = true;
if(u == Start) break;
u = Father[Match[u]];
}
while(true)
{
v = Base[v];
if(InPath[v])break;
v = Father[Match[v]];
}
return v;
}
void ResetTrace(int u)
{
int v;
while(Base[u] != NewBase)
{
v = Match[u];
InBlossom[Base[u]] = InBlossom[Base[v]] = true;
u = Father[v];
if(Base[u] != NewBase) Father[u] = v;
}
}
void BloosomContract(int u,int v)
{
NewBase = FindCommonAncestor(u,v);
memset(InBlossom,false,sizeof(InBlossom));
ResetTrace(u);
ResetTrace(v);
if(Base[u] != NewBase) Father[u] = v;
if(Base[v] != NewBase) Father[v] = u;
for(int tu = 1; tu <= N; tu++)
if(InBlossom[Base[tu]])
{
Base[tu] = NewBase;
if(!InQueue[tu]) Push(tu);
}
}
void FindAugmentingPath()
{
memset(InQueue,false,sizeof(InQueue));
memset(Father,0,sizeof(Father));
for(int i = 1;i <= N;i++)
Base[i] = i;
Head = Tail = 1;
Push(Start);
Finish = 0;
while(Head < Tail)
{
int u = Pop();
for(int v = 1; v <= N; v++)
if(Graph[u][v] && (Base[u] != Base[v]) && (Match[u] != v))
{
if((v == Start) || ((Match[v] > 0) && Father[Match[v]] > 0))
BloosomContract(u,v);
else if(Father[v] == 0)
{
Father[v] = u;
if(Match[v] > 0)
Push(Match[v]);
else
{
Finish = v;
return;
}
}
}
}
}
void AugmentPath()
{
int u,v,w;
u = Finish;
while(u > 0)
{
v = Father[u];
w = Match[v];
Match[v] = u;
Match[u] = v;
u = w;
}
}
void Edmonds()
{
memset(Match,0,sizeof(Match));
for(int u = 1; u <= N; u++)
if(Match[u] == 0)
{
Start = u;
FindAugmentingPath();
if(Finish > 0)AugmentPath();
}
}
int getMatch()
{
Edmonds();
Count = 0;
for(int u = 1; u <= N;u++)
if(Match[u] > 0)
Count++;
return Count/2;
}
bool g[MAXN][MAXN];
pair<int,int>p[150];
int main()
{
int m;
while(scanf("%d%d",&N,&m)==2)
{
memset(g,false,sizeof(g));
memset(Graph,false,sizeof(Graph));
int u,v;
for(int i = 1;i <= m;i++)
{
scanf("%d%d",&u,&v);
p[i] = make_pair(u,v);
g[u][v] = true;
g[v][u] = true;
Graph[u][v] = true;
Graph[v][u] = true;
}
int cnt0 = getMatch();
vector<int>ans;
for(int i = 1;i <= m;i++)
{
u = p[i].first;
v = p[i].second;
memcpy(Graph,g,sizeof(g));
for(int j = 1;j <= N;j++)
Graph[j][u] = Graph[u][j] = Graph[j][v] = Graph[v][j] = false;
int cnt = getMatch();
if(cnt < cnt0-1)
ans.push_back(i);
}
int sz = ans.size();
printf("%d\n",sz);
for(int i = 0;i < sz;i++)
{
printf("%d",ans[i]);
if(i < sz-1)printf(" ");
}
printf("\n");
}
return 0;
}

52
HDOJ/4689_autoAC.cpp Normal file
View File

@ -0,0 +1,52 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
#define ll long long
char a[100];
ll dp[22][22][22];
int main ()
{
while(scanf("%s",a)!=EOF)
{
if(a[0]=='-')
{
printf("0\n");
continue;
}
memset(dp,0,sizeof(dp));
dp[1][0][1]=1;
int cnt=0;
for(int i=1;i<strlen(a);++i)
{
if(a[i]=='-')
{
cnt++;
for(int j=0;j<i;++j)
for(int k=0;k<=i;++k)
{
if(i-j-k>0)
dp[i+1][j+1][k]+=dp[i][j][k]*(i-j-k);
if(k)
dp[i+1][j+1][k-1]+=dp[i][j][k]*k;
}
}
else
{
for(int j=0;j<=i;++j)
for(int k=0;k<=i;++k)
{
dp[i+1][j][k+1]+=dp[i][j][k];
if(i-j-k>0)
dp[i+1][j][k+1]+=dp[i][j][k]*(i-j-k);
dp[i+1][j][k]+=dp[i][j][k]*k;
}
}
}
cout<<dp[strlen(a)][cnt][0]<<endl;
}
return 0;
}

105
HDOJ/4690_autoAC.cpp Normal file
View File

@ -0,0 +1,105 @@
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <queue>
#include <deque>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <vector>
#include <utility>
#include <functional>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <cassert>
#include <ctime>
#include <iterator>
const int INF = 0x3f3f3f3f;
const int dir[8][2] = {{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}};
using namespace std;
string A[16][16] = {
"NUL","SOH","STX","ETX","","HT","","DEL","","","","VT","FF","CR","SO","SI",
"DLE","DC1","DC2","DC3","","","BS","","CAN","EM","","","IFS","IGS","IRS","IUS ITB",
"","","","","","LF","ETB","ESC","","","","","","ENQ","ACK","BEL",
"","","SYN","","","","","EOT","","","","","DC4","NAK","","SUB",
"SP","","","","","","","","","","",".","<","(","+","|",
"&","","","","","","","","","","!","$","*",")",";","",
"-","/","","","","","","","","","",",","%","_",">","?",
"","","","","","","","","","`",":","#","@","'","=","\"",
"","a","b","c","d","e","f","g","h","i","","","","","","",
"","j","k","l","m","n","o","p","q","r","","","","","","",
"","~","s","t","u","v","w","x","y","z","","","","","","",
"^","","","","","","","","","","[","]","","","","",
"{","A","B","C","D","E","F","G","H","I","","","","","","",
"}","J","K","L","M","N","O","P","Q","R","","","","","","",
"\\","","S","T","U","V","W","X","Y","Z","","","","","","",
"0","1","2","3","4","5","6","7","8","9","","","","","",""
};
string B[8][16] = {
"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL", "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI",
"DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB", "CAN", "EM", "SUB", "ESC", "IFS", "IGS", "IRS", "IUS ITB",
"SP", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?",
"@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\\","]","^","_",
"`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", "DEL"
};
map<pair<int,int>, pair<int,int> > coll;
int main(void)
{
string sad;
ios::sync_with_stdio(false);
cin>>sad; coll.clear();
string tmp; pair<int,int> so, to;
for (int i = 0; i < 16; ++i)
{
for (int j = 0; j < 16; ++j)
{
if (A[i][j]!="")
{
so.first=i, so.second=j;
bool mrk = false;
for (int k = 0; k < 8; ++k)
{
for (int h = 0; h < 16; ++h)
{
if (A[i][j]==B[k][h])
{
mrk = true;
to.first=k,to.second=h;
coll[so] = to; break;
}
}
if (mrk) break;
}
}
}
}
for (int i = 0; i < sad.size(); i+=2)
{
int X, Y; tmp.clear();
if (sad[i]>='A'&&sad[i]<='F') X = sad[i]- 'A'+10;
else X = sad[i] - '0';
if (sad[i+1]>='A'&&sad[i+1]<='F') Y = sad[i+1]- 'A'+10;
else Y = sad[i+1] - '0';
pair<int,int> hehe, TM;
hehe.first = X, hehe.second = Y;
TM = coll[hehe];
int j = TM.first, k = TM.second;
char XX, YY;
XX = j+ '0';
if (k >= 10) YY = k - 10 + 'A';
else YY = k + '0';
printf("%c%c", XX, YY);
}
printf("\n");
return 0;
}

140
HDOJ/4691_autoAC.cpp Normal file
View File

@ -0,0 +1,140 @@
#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>
using namespace std;
const int MAXN=100010;
int t1[MAXN],t2[MAXN],c[MAXN];
bool cmp(int *r,int a,int b,int l)
{
return r[a] == r[b] && r[a+l] == r[b+l];
}
void da(int str[],int sa[],int ran[],int height[],int n,int m)
{
n++;
int i, j, p, *x = t1, *y = t2;
for(i = 0;i < m;i++)c[i] = 0;
for(i = 0;i < n;i++)c[x[i] = str[i]]++;
for(i = 1;i < m;i++)c[i] += c[i-1];
for(i = n-1;i >= 0;i--)sa[--c[x[i]]] = i;
for(j = 1;j <= n; j <<= 1)
{
p = 0;
for(i = n-j; i < n; i++)y[p++] = i;
for(i = 0; i < n; i++)if(sa[i] >= j)y[p++] = sa[i] - j;
for(i = 0; i < m; i++)c[i] = 0;
for(i = 0; i < n; i++)c[x[y[i]]]++;
for(i = 1; i < m;i++)c[i] += c[i-1];
for(i = n-1; i >= 0;i--)sa[--c[x[y[i]]]] = y[i];
swap(x,y);
p = 1; x[sa[0]] = 0;
for(i = 1;i < n;i++)
x[sa[i]] = cmp(y,sa[i-1],sa[i],j)?p-1:p++;
if(p >= n)break;
m = p;
}
int k = 0;
n--;
for(i = 0;i <= n;i++)ran[sa[i]] = i;
for(i = 0;i < n;i++)
{
if(k)k--;
j = sa[ran[i]-1];
while(str[i+k] == str[j+k])k++;
height[ran[i]] = k;
}
}
int ran[MAXN],height[MAXN];
int RMQ[MAXN];
int mm[MAXN];
int best[20][MAXN];
void initRMQ(int n)
{
mm[0]=-1;
for(int i=1;i<=n;i++)
mm[i]=((i&(i-1))==0)?mm[i-1]+1:mm[i-1];
for(int i=1;i<=n;i++)best[0][i]=i;
for(int i=1;i<=mm[n];i++)
for(int j=1;j+(1<<i)-1<=n;j++)
{
int a=best[i-1][j];
int b=best[i-1][j+(1<<(i-1))];
if(RMQ[a]<RMQ[b])best[i][j]=a;
else best[i][j]=b;
}
}
int askRMQ(int a,int b)
{
int t;
t=mm[b-a+1];
b-=(1<<t)-1;
a=best[t][a];b=best[t][b];
return RMQ[a]<RMQ[b]?a:b;
}
int lcp(int a,int b)
{
a=ran[a];b=ran[b];
if(a>b)swap(a,b);
return height[askRMQ(a+1,b)];
}
char str[MAXN];
int r[MAXN];
int sa[MAXN];
int A[MAXN],B[MAXN];
int calc(int n)
{
if(n == 0)return 1;
int ret = 0;
while(n)
{
ret++;
n /= 10;
}
return ret;
}
int main()
{
while(scanf("%s",str)==1)
{
int n = strlen(str);
for(int i = 0;i < n;i++)
r[i] = str[i];
r[n] = 0;
da(r,sa,ran,height,n,128);
for(int i = 1;i <= n;i++)
RMQ[i] = height[i];
initRMQ(n);
int k,u,v;
long long ans1 = 0, ans2 = 0;
scanf("%d",&k);
for(int i = 0;i < k;i++)
{
scanf("%d%d",&A[i],&B[i]);
if(i == 0)
{
ans1 += B[i] - A[i] + 1;
ans2 += B[i] - A[i] + 3;
continue;
}
int tmp ;
if(A[i]!= A[i-1])tmp = lcp(A[i],A[i-1]);
else tmp = 10000000;
tmp = min(tmp,B[i]-A[i]);
tmp = min(tmp,B[i-1]-A[i-1]);
ans1 += B[i] - A[i] + 1;
ans2 += B[i] - A[i] - tmp + 1;
ans2 += 1;
ans2 += calc(tmp);
}
printf("%I64d %I64d\n",ans1,ans2);
}
return 0;
}

92
HDOJ/4696_autoAC.cpp Normal file
View File

@ -0,0 +1,92 @@
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
using namespace std;
#define LL __int64
#define maxn 100011
int t[maxn],c[maxn],n,q,m[maxn],vis[maxn],a[maxn],b[maxn];
map<int,map<int,int> >nn;
map<int,int>mm;
map<int,int>ll;
int bb[1001];
int flag[maxn],s[maxn],ss[maxn];
void find(int x)
{
ll.clear();
int tt=1,i,j,k,y,z,d;
s[0]=0;s[1]=c[x];ll[x]=1,ss[1]=x;
for(i=2;;i++)
{
vis[x]=1;
x=t[x];
ss[i]=x;
if(ll[x])
break;
if(vis[x])
{
for(j=i-1;j>=1;j--)
{
a[ss[j]]=s[i-1]-s[j-1]+a[x];
b[ss[j]]=b[x];
}
return;
}
ll[x]=++tt;
s[i]=s[i-1]+c[x];
}
d=s[i-1]-s[ll[x]-1];
for(j=1;j<i;j++)
{
if(j<=ll[x])
a[ss[j]]=s[ll[x]-1]-s[j-1];
else
a[ss[j]]=s[i-1]-s[j-1];
b[ss[j]]=d;
}
}
int main()
{
while(scanf("%d%d",&n,&q)!=EOF)
{
int i,j,k;
for(i=1;i<=n;i++)
scanf("%d",&t[i]);
for(i=1;i<=n;i++)
scanf("%d",&c[i]);
for(i=1;i<=q;i++)
scanf("%d",&m[i]);
memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++)
{
if(!vis[i])
find(i);
}
mm.clear();
nn.clear();
int tt=0;
for(i=1;i<=n;i++)
{
if(mm[b[i]]==0)
{
mm[b[i]]=++tt;
bb[tt]=b[i];
}
nn[tt][a[i]]=1;
}
memset(flag,0,sizeof(flag));
for(j=1;j<=q;j++)
{
if(m[j]<=0)continue;
for(i=1;i<=tt;i++)
if(nn[i][m[j]%bb[i]]){flag[j]=1;break;}
}
for(i=1;i<=q;i++)
if(flag[i])printf("YES\n");
else printf("NO\n");
}
return 0;
}

129
HDOJ/4697_autoAC.cpp Normal file
View File

@ -0,0 +1,129 @@
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-9;
inline int dcmp(double x) {
if (fabs(x) < eps)
return 0;
return x > eps ? 1 : -1;
}
struct point {
double x, y;
int id;
inline void in(int i) {
scanf("%lf%lf", &x, &y);
id = i;
}
point(double x = 0, double y = 0) :
x(x), y(y) {
}
inline point operator -(const point &t) const {
return point(x - t.x, y - t.y);
}
inline point operator +(const point &t) const {
return point(x + t.x, y + t.y);
}
point operator /(const double &t) const {
return point(x / t, y / t);
}
point operator *(const double &t) const {
return point(x * t, y * t);
}
inline double operator *(const point &t) const {
return x*t.y-y*t.x;
}
bool operator <(const point &t) const {
if(y == t.y) return x < t.x;
return y < t.y;
}
}p[55], v[55], tp[55], st[55];
double t[55*55*55], T, cur, ans;
int sz, n, m;
double a, b, c;
inline double cross(const point &o, const point &a, const point &b) {
return (a.x-o.x)*(b.y-o.y)-(a.y-o.y)*(b.x-o.x);
}
int graham(point *p, int n, point *st) {
sort(p, p + n);
int top = 0;
int i;
for (i = 0; i < n; i++) {
while (top >= 2 && cross(st[top - 2], st[top - 1], p[i]) < eps)
top--;
st[top++] = p[i];
}
int t = top + 1;
for (i = n - 2; i >= 0; i--) {
while (top >= t && cross(st[top - 2], st[top - 1], p[i]) < eps)
top--;
st[top++] = p[i];
}
return top;
}
inline void gao(int i, int j, int k) {
point a1 = p[i]-p[j], a2 = p[i]-p[k];
point b1 = v[i]-v[j], b2 = v[i]-v[k];
a += b1*b2; b += a1*b2+b1*a2; c += a1*a2;
}
inline void solve(double a, double b, double c) {
double x;
if(a == 0) {
if(b!= 0) {
x = -c/b;
if(x >= 0 && x <= T) t[sz++] = x;
}
return;
}
double dlt = b*b-4*a*c;
if(dlt < 0) return;
if(dlt == 0) {
x = -b*0.5/a;
if(x >= 0 && x <= T) t[sz++] = x;
return;
}
dlt = sqrt(dlt);
x = 0.5*(-b+dlt)/a;
if(x >= 0 && x <= T) t[sz++] = x;
x = 0.5*(-b-dlt)/a;
if(x >= 0 && x <= T) t[sz++] = x;
}
inline double F(double x) {
return a*x*x*x/3.0+b*x*x/2.0+c*x;
}
int main() {
int i, j, k;
while( ~scanf("%d%lf", &n, &T)) {
for(i = 0; i < n; i++) p[i].in(i), v[i].in(i);
if(n <= 2) {
printf("%.10f\n", 0.0);
continue;
}
t[0] = 0; t[1] = T;
sz = 2;
for(i = 0; i < n; i++)
for(j = i+1; j < n; j++)
for(k = j+1; k < n; k++) {
a = b = c = 0;
gao(i, j, k);
solve(a, b, c);
}
sort(t, t+sz);
ans = 0;
for(i = 0; i < sz-1; i++) {
cur = 0.5*(t[i]+t[i+1]);
a = b = c = 0;
for(j = 0; j < n; j++) {
tp[j] = p[j]+v[j]*cur;
tp[j].id = p[j].id;
}
m = graham(tp, n, st);
for(j = 2; j < m; j++)
gao(st[0].id, st[j-1].id, st[j].id);
ans += F(t[i+1])- F(t[i]);
}
printf("%.10f\n", fabs(ans*0.5/T));
}
return 0;
}

75
HDOJ/4698_autoAC.cpp Normal file
View File

@ -0,0 +1,75 @@
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#define LL long long
#define mod 1000000007
#define M 1005
#define INF 0x7fffffff
using namespace std;
struct Point
{
int x, y;
bool operator < (const Point &temp) const
{
if(x!=temp.x) return x<temp.x;
else return y<temp.y;
}
int readPoint()
{
return scanf("%d%d", &x, &y);
}
} p[M];
int n, m, k;
int val[M];
set<int>sset;
set<int>::iterator it;
int main ()
{
while(~scanf("%d%d%d", &n, &m, &k))
{
for(int i = 1; i <= k; ++i)
{
p[i].readPoint();
val[i] = p[i].x;
}
sort(p+1, p+1+k);
sort(val+1, val+1+k);
int tot = unique(val+1, val+1+k) - val;
val[0] = 0;
val[tot] = n+1;
LL ans = 0;
for(int i = 1; i < tot; ++i)
{
LL tt = 0;
int pre = val[i]-val[i-1];
int r;
for(r = 1; r <= k && p[r].x < val[i]; ++r);
sset.clear();
sset.insert(0);
sset.insert(m+1);
for(int j = i; j < tot; ++j)
{
int top, bottom;
for( ; r <= k && p[r].x == val[j]; ++r)
{
if(sset.count(p[r].y)) continue;
it = sset.lower_bound(p[r].y);
top = *it;
bottom = *(--it);
tt = (tt+(LL)(top-p[r].y)*(p[r].y-bottom)%mod)%mod;
sset.insert(p[r].y);
}
int rear = val[j+1]-val[j];
ans = (ans+tt*rear%mod*pre%mod)%mod;
}
}
printf("%I64d\n", ans);
}
return 0;
}

91
HDOJ/4699_autoAC.cpp Normal file
View File

@ -0,0 +1,91 @@
#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
struct node
{
int sum;
int m;
int v;
}ne,cur;
vector<node>a,b;
int main()
{
int num,j,n;
char k;
while(scanf("%d",&num)!=EOF)
{
a.clear();
b.clear();
while(num--)
{
getchar();
scanf("%c",&k);
if(k=='I')
{
scanf("%d",&n);
ne.m=n;
ne.v=n;
ne.sum=n;
if(a.size()==0)
a.push_back(ne);
else
{
int sz=a.size();
cur=a[sz-1];
ne.sum = cur.sum + ne.v;
ne.m = max(ne.sum,cur.m);
a.push_back(ne);
}
}
else if(k=='L')
{
int sz = a.size() ;
if(sz)
{
cur = a[sz-1] ;
a.pop_back();
b.push_back(cur);
}
}
else if(k=='R')
{
j = b.size();
if(j)
{
ne=b[j-1];
b.pop_back();
ne.sum=ne.v;
ne.m=ne.v;
int sz = a.size();
if(sz==0)
{
ne.sum=ne.v;
ne.m=ne.v;
a.push_back(ne);
}
else
{
cur = a[sz-1];
ne.sum = cur.sum + ne.v;
ne.m = max(ne.sum,cur.m);
a.push_back(ne);
}
}
}
else if(k=='D')
{
if(a.size())
a.pop_back();
}
else if(k=='Q')
{
scanf("%d",&n);
printf("%d\n",a[n-1].m);
}
}
}
return 0;
}