From 34d0415174a35afc48f69d464b502a97b9eb7825 Mon Sep 17 00:00:00 2001 From: KiritoTRw <3021577574@qq.com> Date: Tue, 6 Sep 2016 13:33:42 +0800 Subject: [PATCH] Powered By HC TECH : AutoACer Engine 4600-4699 --- HDOJ/4600_autoAC.cpp | 90 +++++++++++ HDOJ/4601_autoAC.cpp | 179 +++++++++++++++++++++ HDOJ/4602_autoAC.cpp | 68 ++++++++ HDOJ/4603_autoAC.cpp | 370 +++++++++++++++++++++++++++++++++++++++++++ HDOJ/4604_autoAC.cpp | 59 +++++++ HDOJ/4605_autoAC.cpp | 142 +++++++++++++++++ HDOJ/4606_autoAC.cpp | 191 ++++++++++++++++++++++ HDOJ/4607_autoAC.cpp | 81 ++++++++++ HDOJ/4608_autoAC.cpp | 61 +++++++ HDOJ/4609_autoAC.cpp | 121 ++++++++++++++ HDOJ/4610_autoAC.cpp | 142 +++++++++++++++++ HDOJ/4611_autoAC.cpp | 38 +++++ HDOJ/4612_autoAC.cpp | 171 ++++++++++++++++++++ HDOJ/4614_autoAC.cpp | 205 ++++++++++++++++++++++++ HDOJ/4616_autoAC.cpp | 81 ++++++++++ HDOJ/4617_autoAC.cpp | 120 ++++++++++++++ HDOJ/4619_autoAC.cpp | 82 ++++++++++ HDOJ/4620_autoAC.cpp | 76 +++++++++ HDOJ/4622_autoAC.cpp | 116 ++++++++++++++ HDOJ/4623_autoAC.cpp | 123 ++++++++++++++ HDOJ/4625_autoAC.cpp | 94 +++++++++++ HDOJ/4627_autoAC.cpp | 30 ++++ HDOJ/4628_autoAC.cpp | 48 ++++++ HDOJ/4629_autoAC.cpp | 168 ++++++++++++++++++++ HDOJ/4630_autoAC.cpp | 121 ++++++++++++++ HDOJ/4631_autoAC.cpp | 113 +++++++++++++ HDOJ/4632_autoAC.cpp | 66 ++++++++ HDOJ/4633_autoAC.cpp | 40 +++++ HDOJ/4634_autoAC.cpp | 142 +++++++++++++++++ HDOJ/4635_autoAC.cpp | 93 +++++++++++ HDOJ/4636_autoAC.cpp | 98 ++++++++++++ HDOJ/4637_autoAC.cpp | 317 ++++++++++++++++++++++++++++++++++++ HDOJ/4638_autoAC.cpp | 101 ++++++++++++ HDOJ/4639_autoAC.cpp | 37 +++++ HDOJ/4640_autoAC.cpp | 91 +++++++++++ HDOJ/4641_autoAC.cpp | 131 +++++++++++++++ HDOJ/4642_autoAC.cpp | 22 +++ HDOJ/4643_autoAC.cpp | 190 ++++++++++++++++++++++ HDOJ/4644_autoAC.cpp | 56 +++++++ HDOJ/4646_autoAC.cpp | 59 +++++++ HDOJ/4647_autoAC.cpp | 32 ++++ HDOJ/4648_autoAC.cpp | 32 ++++ HDOJ/4649_autoAC.cpp | 72 +++++++++ HDOJ/4650_autoAC.cpp | 61 +++++++ HDOJ/4651_autoAC.cpp | 49 ++++++ HDOJ/4652_autoAC.cpp | 41 +++++ HDOJ/4654_autoAC.cpp | 102 ++++++++++++ HDOJ/4655_autoAC.cpp | 57 +++++++ HDOJ/4656_autoAC.cpp | 116 ++++++++++++++ HDOJ/4657_autoAC.cpp | 45 ++++++ HDOJ/4658_autoAC.cpp | 58 +++++++ HDOJ/4661_autoAC.cpp | 141 +++++++++++++++++ HDOJ/4662_autoAC.cpp | 46 ++++++ HDOJ/4664_autoAC.cpp | 65 ++++++++ HDOJ/4665_autoAC.cpp | 82 ++++++++++ HDOJ/4666_autoAC.cpp | 66 ++++++++ HDOJ/4667_autoAC.cpp | 116 ++++++++++++++ HDOJ/4668_autoAC.cpp | 147 +++++++++++++++++ HDOJ/4669_autoAC.cpp | 73 +++++++++ HDOJ/4670_autoAC.cpp | 171 ++++++++++++++++++++ HDOJ/4671_autoAC.cpp | 42 +++++ HDOJ/4672_autoAC.cpp | 41 +++++ HDOJ/4674_autoAC.cpp | 153 ++++++++++++++++++ HDOJ/4675_autoAC.cpp | 92 +++++++++++ HDOJ/4676_autoAC.cpp | 102 ++++++++++++ HDOJ/4677_autoAC.cpp | 122 ++++++++++++++ HDOJ/4678_autoAC.cpp | 109 +++++++++++++ HDOJ/4679_autoAC.cpp | 177 +++++++++++++++++++++ HDOJ/4680_autoAC.cpp | 336 +++++++++++++++++++++++++++++++++++++++ HDOJ/4681_autoAC.cpp | 125 +++++++++++++++ HDOJ/4685_autoAC.cpp | 125 +++++++++++++++ HDOJ/4686_autoAC.cpp | 76 +++++++++ HDOJ/4687_autoAC.cpp | 187 ++++++++++++++++++++++ HDOJ/4689_autoAC.cpp | 52 ++++++ HDOJ/4690_autoAC.cpp | 105 ++++++++++++ HDOJ/4691_autoAC.cpp | 140 ++++++++++++++++ HDOJ/4696_autoAC.cpp | 92 +++++++++++ HDOJ/4697_autoAC.cpp | 129 +++++++++++++++ HDOJ/4698_autoAC.cpp | 75 +++++++++ HDOJ/4699_autoAC.cpp | 91 +++++++++++ 80 files changed, 8476 insertions(+) create mode 100644 HDOJ/4600_autoAC.cpp create mode 100644 HDOJ/4601_autoAC.cpp create mode 100644 HDOJ/4602_autoAC.cpp create mode 100644 HDOJ/4603_autoAC.cpp create mode 100644 HDOJ/4604_autoAC.cpp create mode 100644 HDOJ/4605_autoAC.cpp create mode 100644 HDOJ/4606_autoAC.cpp create mode 100644 HDOJ/4607_autoAC.cpp create mode 100644 HDOJ/4608_autoAC.cpp create mode 100644 HDOJ/4609_autoAC.cpp create mode 100644 HDOJ/4610_autoAC.cpp create mode 100644 HDOJ/4611_autoAC.cpp create mode 100644 HDOJ/4612_autoAC.cpp create mode 100644 HDOJ/4614_autoAC.cpp create mode 100644 HDOJ/4616_autoAC.cpp create mode 100644 HDOJ/4617_autoAC.cpp create mode 100644 HDOJ/4619_autoAC.cpp create mode 100644 HDOJ/4620_autoAC.cpp create mode 100644 HDOJ/4622_autoAC.cpp create mode 100644 HDOJ/4623_autoAC.cpp create mode 100644 HDOJ/4625_autoAC.cpp create mode 100644 HDOJ/4627_autoAC.cpp create mode 100644 HDOJ/4628_autoAC.cpp create mode 100644 HDOJ/4629_autoAC.cpp create mode 100644 HDOJ/4630_autoAC.cpp create mode 100644 HDOJ/4631_autoAC.cpp create mode 100644 HDOJ/4632_autoAC.cpp create mode 100644 HDOJ/4633_autoAC.cpp create mode 100644 HDOJ/4634_autoAC.cpp create mode 100644 HDOJ/4635_autoAC.cpp create mode 100644 HDOJ/4636_autoAC.cpp create mode 100644 HDOJ/4637_autoAC.cpp create mode 100644 HDOJ/4638_autoAC.cpp create mode 100644 HDOJ/4639_autoAC.cpp create mode 100644 HDOJ/4640_autoAC.cpp create mode 100644 HDOJ/4641_autoAC.cpp create mode 100644 HDOJ/4642_autoAC.cpp create mode 100644 HDOJ/4643_autoAC.cpp create mode 100644 HDOJ/4644_autoAC.cpp create mode 100644 HDOJ/4646_autoAC.cpp create mode 100644 HDOJ/4647_autoAC.cpp create mode 100644 HDOJ/4648_autoAC.cpp create mode 100644 HDOJ/4649_autoAC.cpp create mode 100644 HDOJ/4650_autoAC.cpp create mode 100644 HDOJ/4651_autoAC.cpp create mode 100644 HDOJ/4652_autoAC.cpp create mode 100644 HDOJ/4654_autoAC.cpp create mode 100644 HDOJ/4655_autoAC.cpp create mode 100644 HDOJ/4656_autoAC.cpp create mode 100644 HDOJ/4657_autoAC.cpp create mode 100644 HDOJ/4658_autoAC.cpp create mode 100644 HDOJ/4661_autoAC.cpp create mode 100644 HDOJ/4662_autoAC.cpp create mode 100644 HDOJ/4664_autoAC.cpp create mode 100644 HDOJ/4665_autoAC.cpp create mode 100644 HDOJ/4666_autoAC.cpp create mode 100644 HDOJ/4667_autoAC.cpp create mode 100644 HDOJ/4668_autoAC.cpp create mode 100644 HDOJ/4669_autoAC.cpp create mode 100644 HDOJ/4670_autoAC.cpp create mode 100644 HDOJ/4671_autoAC.cpp create mode 100644 HDOJ/4672_autoAC.cpp create mode 100644 HDOJ/4674_autoAC.cpp create mode 100644 HDOJ/4675_autoAC.cpp create mode 100644 HDOJ/4676_autoAC.cpp create mode 100644 HDOJ/4677_autoAC.cpp create mode 100644 HDOJ/4678_autoAC.cpp create mode 100644 HDOJ/4679_autoAC.cpp create mode 100644 HDOJ/4680_autoAC.cpp create mode 100644 HDOJ/4681_autoAC.cpp create mode 100644 HDOJ/4685_autoAC.cpp create mode 100644 HDOJ/4686_autoAC.cpp create mode 100644 HDOJ/4687_autoAC.cpp create mode 100644 HDOJ/4689_autoAC.cpp create mode 100644 HDOJ/4690_autoAC.cpp create mode 100644 HDOJ/4691_autoAC.cpp create mode 100644 HDOJ/4696_autoAC.cpp create mode 100644 HDOJ/4697_autoAC.cpp create mode 100644 HDOJ/4698_autoAC.cpp create mode 100644 HDOJ/4699_autoAC.cpp diff --git a/HDOJ/4600_autoAC.cpp b/HDOJ/4600_autoAC.cpp new file mode 100644 index 0000000..9fed935 --- /dev/null +++ b/HDOJ/4600_autoAC.cpp @@ -0,0 +1,90 @@ +#include +#include +#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; +} diff --git a/HDOJ/4601_autoAC.cpp b/HDOJ/4601_autoAC.cpp new file mode 100644 index 0000000..315ff74 --- /dev/null +++ b/HDOJ/4601_autoAC.cpp @@ -0,0 +1,179 @@ +#pragma comment(linker, "/STACK:1024000000,1024000000") +#include +#include +#include +#include +#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 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 ) ; + } + } +} diff --git a/HDOJ/4602_autoAC.cpp b/HDOJ/4602_autoAC.cpp new file mode 100644 index 0000000..701503d --- /dev/null +++ b/HDOJ/4602_autoAC.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +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< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +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; +vectorg[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<> 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; +} diff --git a/HDOJ/4604_autoAC.cpp b/HDOJ/4604_autoAC.cpp new file mode 100644 index 0000000..dc5f372 --- /dev/null +++ b/HDOJ/4604_autoAC.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define MAXM 111111 +#define MAXN 111111 +#define INF 1000000007 +#define eps 1e-8 +using namespace std; +typedef vector::iterator Viter; +int n; +vectorg; +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]; + } + pairbounds = 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; +} diff --git a/HDOJ/4605_autoAC.cpp b/HDOJ/4605_autoAC.cpp new file mode 100644 index 0000000..377b892 --- /dev/null +++ b/HDOJ/4605_autoAC.cpp @@ -0,0 +1,142 @@ +#pragma comment(linker, "/STACK:1024000000,1024000000") +#include +#include +#include +#include +#include +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; +vectornum[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(l0) + { + ss+=sum[s][bs]; + s=s-lowbit(s); + } + return ss; +} +void dfs(int x) +{ + int s; + s=num[x].size(); + for(int i=0;i0) + { + 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 +#include +#include +#include +#include +#include +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; +} diff --git a/HDOJ/4607_autoAC.cpp b/HDOJ/4607_autoAC.cpp new file mode 100644 index 0000000..76851a7 --- /dev/null +++ b/HDOJ/4607_autoAC.cpp @@ -0,0 +1,81 @@ +#include +#include +#include +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; +} diff --git a/HDOJ/4608_autoAC.cpp b/HDOJ/4608_autoAC.cpp new file mode 100644 index 0000000..6034c59 --- /dev/null +++ b/HDOJ/4608_autoAC.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +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; +} diff --git a/HDOJ/4609_autoAC.cpp b/HDOJ/4609_autoAC.cpp new file mode 100644 index 0000000..a4ae71e --- /dev/null +++ b/HDOJ/4609_autoAC.cpp @@ -0,0 +1,121 @@ +#include +#include +#include +#include +#include +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; +} diff --git a/HDOJ/4610_autoAC.cpp b/HDOJ/4610_autoAC.cpp new file mode 100644 index 0000000..72ee2c3 --- /dev/null +++ b/HDOJ/4610_autoAC.cpp @@ -0,0 +1,142 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long ll; +const int N = (int)4e6+10; +const int M = (int)1e6+10; +typedef pair pii; +bool p[N]; +int prime[M]; +void init(){ + memset(p,true,sizeof(p)); + p[0] = p[1] = false; + for(int i=2;i*ifactor; +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> 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<>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< 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< +#include +#include +#include +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(nown) 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< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +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 edges; +vector G[MAXN]; +vector 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;ipre[u]) + { + P[v].push_back(u); + P[u].push_back(v); + } + } + else if(pre[v] GG[MAXN]; +int d[MAXN]; +void dfs(int u,int dep) +{ + d[u]=dep; + for(int i=0;imaxn) + { + 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< +#include +#include +#include +#include +#include +#include +#include +#include +#include +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) + { + 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; +} diff --git a/HDOJ/4616_autoAC.cpp b/HDOJ/4616_autoAC.cpp new file mode 100644 index 0000000..40dc7b7 --- /dev/null +++ b/HDOJ/4616_autoAC.cpp @@ -0,0 +1,81 @@ +#include +#include +#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 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +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; +} diff --git a/HDOJ/4619_autoAC.cpp b/HDOJ/4619_autoAC.cpp new file mode 100644 index 0000000..3feca92 --- /dev/null +++ b/HDOJ/4619_autoAC.cpp @@ -0,0 +1,82 @@ +#include +#include +#include +#include +#include +using namespace std; +const int MAXN=2505; +int linker[MAXN]; +bool used[MAXN]; +vectormap[MAXN]; +int uN; +bool dfs(int u) +{ + for(int i=0;ip1[MAXN]; +pairp2[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; +} diff --git a/HDOJ/4620_autoAC.cpp b/HDOJ/4620_autoAC.cpp new file mode 100644 index 0000000..cf1c9d3 --- /dev/null +++ b/HDOJ/4620_autoAC.cpp @@ -0,0 +1,76 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; +struct node +{ + int t,p[15],c,id; +}cut[35]; +bool cmp(node x,node y) +{ + return x.t=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 +#include +#include +#include +#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 ) ; + } + } +} diff --git a/HDOJ/4623_autoAC.cpp b/HDOJ/4623_autoAC.cpp new file mode 100644 index 0000000..16a0a56 --- /dev/null +++ b/HDOJ/4623_autoAC.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +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=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); + } +} diff --git a/HDOJ/4625_autoAC.cpp b/HDOJ/4625_autoAC.cpp new file mode 100644 index 0000000..32ef270 --- /dev/null +++ b/HDOJ/4625_autoAC.cpp @@ -0,0 +1,94 @@ +#include +#include +#include +#include +#include +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 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; +} diff --git a/HDOJ/4627_autoAC.cpp b/HDOJ/4627_autoAC.cpp new file mode 100644 index 0000000..2899bec --- /dev/null +++ b/HDOJ/4627_autoAC.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +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; +} diff --git a/HDOJ/4628_autoAC.cpp b/HDOJ/4628_autoAC.cpp new file mode 100644 index 0000000..2ce41ba --- /dev/null +++ b/HDOJ/4628_autoAC.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include +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<0;j=i&(j-1)) + if(legal[j]) + dp[i]=min(dp[i],dp[i-j]+1); + } + printf("%d\n",dp[(1< +#include +#include +#include +#include +#include +#include +#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 lx; +vector 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 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; +} diff --git a/HDOJ/4630_autoAC.cpp b/HDOJ/4630_autoAC.cpp new file mode 100644 index 0000000..e9fac84 --- /dev/null +++ b/HDOJ/4630_autoAC.cpp @@ -0,0 +1,121 @@ +#include +#include +#include +#include +#include +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; +vectorvt; +struct node +{ + int l, r, id; + int ans; + friend bool operator<(const node A, const node B) + { + return A.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> 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; +} diff --git a/HDOJ/4631_autoAC.cpp b/HDOJ/4631_autoAC.cpp new file mode 100644 index 0000000..191c04c --- /dev/null +++ b/HDOJ/4631_autoAC.cpp @@ -0,0 +1,113 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +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 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; + pairpr1 = Closest_Pair(left,mid); + pairpr2 = 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]); + pairans; + 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 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; + pairpr = 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; +} diff --git a/HDOJ/4632_autoAC.cpp b/HDOJ/4632_autoAC.cpp new file mode 100644 index 0000000..ffccee1 --- /dev/null +++ b/HDOJ/4632_autoAC.cpp @@ -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>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; +} diff --git a/HDOJ/4633_autoAC.cpp b/HDOJ/4633_autoAC.cpp new file mode 100644 index 0000000..b3e60b3 --- /dev/null +++ b/HDOJ/4633_autoAC.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +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; +} diff --git a/HDOJ/4634_autoAC.cpp b/HDOJ/4634_autoAC.cpp new file mode 100644 index 0000000..3c422b7 --- /dev/null +++ b/HDOJ/4634_autoAC.cpp @@ -0,0 +1,142 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +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; +}; +queueq; +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<=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< +#include +#include +#include +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 +#include +#include +#include +#include +#include +#include +#define MID(x,y) ((x+y)>>1) +#define iabs(x) ((x)>0?(x):-(x)) +using namespace std; +vector v[40]; +inline void v_pb(int a,int b){ + v[a].push_back(b); +} +int num[40]; +queueq; +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; +} diff --git a/HDOJ/4637_autoAC.cpp b/HDOJ/4637_autoAC.cpp new file mode 100644 index 0000000..22d0d87 --- /dev/null +++ b/HDOJ/4637_autoAC.cpp @@ -0,0 +1,317 @@ +#include +#include +#include +#include +#include +#include +#include +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)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;i1&&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 result[3]){ + vector 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;i0) 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;i0&&d1<=0&&d2>0)wn++; + if(k<0&&d2<=0&&d1>0)wn--; + } + if(wn) return 1; + else return 0; +} +double PolygonArea(vector p){ + double area=0; + int n=p.size(); + for(int i=1;i* 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;i0){ + for(int i=0;i= 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); + } +} diff --git a/HDOJ/4638_autoAC.cpp b/HDOJ/4638_autoAC.cpp new file mode 100644 index 0000000..0132df1 --- /dev/null +++ b/HDOJ/4638_autoAC.cpp @@ -0,0 +1,101 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +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 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]=1&&pos[A[i]-1] +#include +#include +#include +#include +#include +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); + } +} diff --git a/HDOJ/4640_autoAC.cpp b/HDOJ/4640_autoAC.cpp new file mode 100644 index 0000000..9e8ccbb --- /dev/null +++ b/HDOJ/4640_autoAC.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +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;idist[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<>1]=min(dp[i>>1],dist[i][j]); + int tot_sta=1<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<=INF) + printf("Case %d: -1\n",kk); + else printf("Case %d: %d\n",kk,ans); + } + return 0; +} diff --git a/HDOJ/4641_autoAC.cpp b/HDOJ/4641_autoAC.cpp new file mode 100644 index 0000000..ae4cdac --- /dev/null +++ b/HDOJ/4641_autoAC.cpp @@ -0,0 +1,131 @@ +#include +#include +#include +#include +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=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] +#include +#include +#include +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 +#include +#include +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 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(ab) 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 +#include +#include +#include +using namespace std; +char str[2000100]; +char t[100100]; +pairs[100100]; +int index[100100]; +int n; +int ne[100100]; +void get_next(char* str,int len){ + ne[0]=0; + for(int i=1;i +#include +#include +#include +#define ll long long +using namespace std; +ll t,n; +map 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; +} diff --git a/HDOJ/4647_autoAC.cpp b/HDOJ/4647_autoAC.cpp new file mode 100644 index 0000000..8e11728 --- /dev/null +++ b/HDOJ/4647_autoAC.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include +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; +} diff --git a/HDOJ/4648_autoAC.cpp b/HDOJ/4648_autoAC.cpp new file mode 100644 index 0000000..25f1c7a --- /dev/null +++ b/HDOJ/4648_autoAC.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include +#include +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); + } +} diff --git a/HDOJ/4649_autoAC.cpp b/HDOJ/4649_autoAC.cpp new file mode 100644 index 0000000..7ecc2a6 --- /dev/null +++ b/HDOJ/4649_autoAC.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +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< +#include +#include +#include +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=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 +#include +#include +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< +#include +#include +#include +#include +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; +} diff --git a/HDOJ/4654_autoAC.cpp b/HDOJ/4654_autoAC.cpp new file mode 100644 index 0000000..1fbb142 --- /dev/null +++ b/HDOJ/4654_autoAC.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +#include +#include +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 vst[maxn]; +vector 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;imaxi) { + tmp = u; + maxi = d[u]; + } + } + st[top++] = tmp; + vis[tmp] = true; + if(i == k-1) + mincut = d[tmp]; + for(int j=0;j t) { + int n = t.size(); + for(int i=0;i= K) return 1; + vector x , y; + for(int i=0;i t; + for(int i=0;i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +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; +} diff --git a/HDOJ/4656_autoAC.cpp b/HDOJ/4656_autoAC.cpp new file mode 100644 index 0000000..f65f045 --- /dev/null +++ b/HDOJ/4656_autoAC.cpp @@ -0,0 +1,116 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +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; +} diff --git a/HDOJ/4657_autoAC.cpp b/HDOJ/4657_autoAC.cpp new file mode 100644 index 0000000..4894574 --- /dev/null +++ b/HDOJ/4657_autoAC.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#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;ii) break; + cur=l; + swap(c[i+1],c[cur]); + } + } + for(i=0;i +#include +#include +#include +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; +} diff --git a/HDOJ/4661_autoAC.cpp b/HDOJ/4661_autoAC.cpp new file mode 100644 index 0000000..4099a54 --- /dev/null +++ b/HDOJ/4661_autoAC.cpp @@ -0,0 +1,141 @@ +#pragma comment(linker,"/STACK:102400000,102400000") +#include +#include +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; +} diff --git a/HDOJ/4662_autoAC.cpp b/HDOJ/4662_autoAC.cpp new file mode 100644 index 0000000..1f2da7d --- /dev/null +++ b/HDOJ/4662_autoAC.cpp @@ -0,0 +1,46 @@ +#include +#include +#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; +} diff --git a/HDOJ/4664_autoAC.cpp b/HDOJ/4664_autoAC.cpp new file mode 100644 index 0000000..c417625 --- /dev/null +++ b/HDOJ/4664_autoAC.cpp @@ -0,0 +1,65 @@ +#include +#include +#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; +} diff --git a/HDOJ/4665_autoAC.cpp b/HDOJ/4665_autoAC.cpp new file mode 100644 index 0000000..3ce23f8 --- /dev/null +++ b/HDOJ/4665_autoAC.cpp @@ -0,0 +1,82 @@ +#include +#include +#include +#include +#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 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; +} diff --git a/HDOJ/4666_autoAC.cpp b/HDOJ/4666_autoAC.cpp new file mode 100644 index 0000000..f02700e --- /dev/null +++ b/HDOJ/4666_autoAC.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include +using namespace std; +#define maxn 60002 +int maps[maxn][6]; +multisetse[1<<5]; +multiset::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 +#include +#include +#include +#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.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;i0&&(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; +} diff --git a/HDOJ/4668_autoAC.cpp b/HDOJ/4668_autoAC.cpp new file mode 100644 index 0000000..6997604 --- /dev/null +++ b/HDOJ/4668_autoAC.cpp @@ -0,0 +1,147 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#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; +} diff --git a/HDOJ/4669_autoAC.cpp b/HDOJ/4669_autoAC.cpp new file mode 100644 index 0000000..cb71410 --- /dev/null +++ b/HDOJ/4669_autoAC.cpp @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#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; +} diff --git a/HDOJ/4670_autoAC.cpp b/HDOJ/4670_autoAC.cpp new file mode 100644 index 0000000..061abc9 --- /dev/null +++ b/HDOJ/4670_autoAC.cpp @@ -0,0 +1,171 @@ +#pragma comment(linker,"/STACK:102400000,102400000") +#include +#include +#include +#include +#include +using namespace std; +typedef long long ll; +#define rep(i,s,t) for(int i=s;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]; +mapmp,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 +#include +#include +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]); + } + } +} diff --git a/HDOJ/4672_autoAC.cpp b/HDOJ/4672_autoAC.cpp new file mode 100644 index 0000000..e816a14 --- /dev/null +++ b/HDOJ/4672_autoAC.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include +#include +#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=0) + sg[j+b]=max(sg[b+j],sg[j]+1); + } + ans=0; + for(i=0;i +#include +#include +#include +#include +#include +#include +#include +#include +#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 >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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +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; +} diff --git a/HDOJ/4676_autoAC.cpp b/HDOJ/4676_autoAC.cpp new file mode 100644 index 0000000..8e9221a --- /dev/null +++ b/HDOJ/4676_autoAC.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long ll; +const int MAXN = 20007; +vector 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; +} diff --git a/HDOJ/4677_autoAC.cpp b/HDOJ/4677_autoAC.cpp new file mode 100644 index 0000000..ffb4aba --- /dev/null +++ b/HDOJ/4677_autoAC.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include +#include +using namespace std; +#define N 30050 +struct Q +{ + int r,idx; + Q(int _r=0,int _idx=0):r(_r),idx(_idx){} +}; +vectorque[N]; +vectoredge[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 +#include +#include +#include +#include +#include +#include +#include +#include +#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; +} diff --git a/HDOJ/4679_autoAC.cpp b/HDOJ/4679_autoAC.cpp new file mode 100644 index 0000000..81dd0b4 --- /dev/null +++ b/HDOJ/4679_autoAC.cpp @@ -0,0 +1,177 @@ +#include +#include +#include +#include +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=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)); + queueq; + queueqp; + 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 +#include +#include +#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->valc[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(headc[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; +} diff --git a/HDOJ/4681_autoAC.cpp b/HDOJ/4681_autoAC.cpp new file mode 100644 index 0000000..cfdbab6 --- /dev/null +++ b/HDOJ/4681_autoAC.cpp @@ -0,0 +1,125 @@ +#include +#include +#include +#include +#include +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; +vectorvec[5]; +void dps() +{ + int i,j; + for(i=0;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=0&&n2.x-1>=0)c1=dp[n1.x-1][n2.x-1]; + if(n1.y+1 +#include +#include +#include +#include +#include +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 >g; +vector >ans; +int dfn[MAXN],low[MAXN]; +int color[MAXN],_count; +stackS; +void Tarjan(int u) +{ + low[u]=dfn[u]=++cnt; + mark[u]=true; + S.push(u); + for(int i=0;i +#include +#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; +} diff --git a/HDOJ/4687_autoAC.cpp b/HDOJ/4687_autoAC.cpp new file mode 100644 index 0000000..681bc52 --- /dev/null +++ b/HDOJ/4687_autoAC.cpp @@ -0,0 +1,187 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +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]; +pairp[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(); + vectorans; + 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; +} diff --git a/HDOJ/4689_autoAC.cpp b/HDOJ/4689_autoAC.cpp new file mode 100644 index 0000000..1c5c5ba --- /dev/null +++ b/HDOJ/4689_autoAC.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include +#include +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;i0) + 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< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +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 > coll; +int main(void) +{ + string sad; + ios::sync_with_stdio(false); + cin>>sad; coll.clear(); + string tmp; pair 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 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; +} diff --git a/HDOJ/4691_autoAC.cpp b/HDOJ/4691_autoAC.cpp new file mode 100644 index 0000000..21097d4 --- /dev/null +++ b/HDOJ/4691_autoAC.cpp @@ -0,0 +1,140 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +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<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; +} diff --git a/HDOJ/4696_autoAC.cpp b/HDOJ/4696_autoAC.cpp new file mode 100644 index 0000000..c605388 --- /dev/null +++ b/HDOJ/4696_autoAC.cpp @@ -0,0 +1,92 @@ +#include +#include +#include +#include +#include +#include +#include +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 >nn; +mapmm; +mapll; +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 +#include +#include +#include +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; +} diff --git a/HDOJ/4698_autoAC.cpp b/HDOJ/4698_autoAC.cpp new file mode 100644 index 0000000..2e1a306 --- /dev/null +++ b/HDOJ/4698_autoAC.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#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 xsset; +set::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; +} diff --git a/HDOJ/4699_autoAC.cpp b/HDOJ/4699_autoAC.cpp new file mode 100644 index 0000000..4087cd2 --- /dev/null +++ b/HDOJ/4699_autoAC.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +using namespace std; +#define max(a,b) a>b?a:b +#define min(a,b) aa,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; +}