From 808996d897639e270b47c77093247dfdeeccd268 Mon Sep 17 00:00:00 2001 From: KiritoTRw <3021577574@qq.com> Date: Sat, 10 Sep 2016 11:37:56 +0800 Subject: [PATCH] Powered By HC TECH : AutoACer Engine 5100-5199 --- HDOJ/5100_autoAC.cpp | 23 +++++ HDOJ/5101_autoAC.cpp | 38 ++++++++ HDOJ/5102_autoAC.cpp | 76 ++++++++++++++++ HDOJ/5103_autoAC.cpp | 54 +++++++++++ HDOJ/5104_autoAC.cpp | 52 +++++++++++ HDOJ/5105_autoAC.cpp | 56 ++++++++++++ HDOJ/5106_autoAC.cpp | 57 ++++++++++++ HDOJ/5107_autoAC.cpp | 125 +++++++++++++++++++++++++ HDOJ/5108_autoAC.cpp | 62 +++++++++++++ HDOJ/5109_autoAC.cpp | 44 +++++++++ HDOJ/5110_autoAC.cpp | 112 +++++++++++++++++++++++ HDOJ/5111_autoAC.cpp | 212 +++++++++++++++++++++++++++++++++++++++++++ HDOJ/5112_autoAC.cpp | 28 ++++++ HDOJ/5113_autoAC.cpp | 61 +++++++++++++ HDOJ/5114_autoAC.cpp | 77 ++++++++++++++++ HDOJ/5115_autoAC.cpp | 34 +++++++ HDOJ/5116_autoAC.cpp | 93 +++++++++++++++++++ HDOJ/5117_autoAC.cpp | 54 +++++++++++ HDOJ/5119_autoAC.cpp | 26 ++++++ HDOJ/5120_autoAC.cpp | 53 +++++++++++ HDOJ/5122_autoAC.cpp | 24 +++++ HDOJ/5123_autoAC.cpp | 33 +++++++ HDOJ/5124_autoAC.cpp | 112 +++++++++++++++++++++++ HDOJ/5125_autoAC.cpp | 70 ++++++++++++++ HDOJ/5126_autoAC.cpp | 139 ++++++++++++++++++++++++++++ HDOJ/5127_autoAC.cpp | 32 +++++++ HDOJ/5128_autoAC.cpp | 107 ++++++++++++++++++++++ HDOJ/5130_autoAC.cpp | 123 +++++++++++++++++++++++++ HDOJ/5131_autoAC.cpp | 80 ++++++++++++++++ HDOJ/5134_autoAC.cpp | 114 +++++++++++++++++++++++ HDOJ/5135_autoAC.cpp | 39 ++++++++ HDOJ/5136_autoAC.cpp | 81 +++++++++++++++++ HDOJ/5137_autoAC.cpp | 41 +++++++++ HDOJ/5138_autoAC.cpp | 32 +++++++ HDOJ/5139_autoAC.cpp | 40 ++++++++ HDOJ/5140_autoAC.cpp | 148 ++++++++++++++++++++++++++++++ HDOJ/5141_autoAC.cpp | 143 +++++++++++++++++++++++++++++ HDOJ/5142_autoAC.cpp | 17 ++++ HDOJ/5143_autoAC.cpp | 27 ++++++ HDOJ/5144_autoAC.cpp | 40 ++++++++ HDOJ/5145_autoAC.cpp | 91 +++++++++++++++++++ HDOJ/5146_autoAC.cpp | 34 +++++++ HDOJ/5147_autoAC.cpp | 65 +++++++++++++ HDOJ/5148_autoAC.cpp | 64 +++++++++++++ HDOJ/5150_autoAC.cpp | 33 +++++++ HDOJ/5151_autoAC.cpp | 44 +++++++++ HDOJ/5152_autoAC.cpp | 134 +++++++++++++++++++++++++++ HDOJ/5154_autoAC.cpp | 27 ++++++ HDOJ/5155_autoAC.cpp | 45 +++++++++ HDOJ/5156_autoAC.cpp | 109 ++++++++++++++++++++++ HDOJ/5157_autoAC.cpp | 79 ++++++++++++++++ HDOJ/5158_autoAC.cpp | 15 +++ HDOJ/5159_autoAC.cpp | 19 ++++ HDOJ/5160_autoAC.cpp | 92 +++++++++++++++++++ HDOJ/5162_autoAC.cpp | 36 ++++++++ HDOJ/5163_autoAC.cpp | 36 ++++++++ HDOJ/5164_autoAC.cpp | 165 +++++++++++++++++++++++++++++++++ HDOJ/5165_autoAC.cpp | 99 ++++++++++++++++++++ HDOJ/5166_autoAC.cpp | 74 +++++++++++++++ HDOJ/5167_autoAC.cpp | 48 ++++++++++ HDOJ/5168_autoAC.cpp | 86 ++++++++++++++++++ HDOJ/5170_autoAC.cpp | 25 +++++ HDOJ/5171_autoAC.cpp | 77 ++++++++++++++++ HDOJ/5172_autoAC.cpp | 72 +++++++++++++++ HDOJ/5174_autoAC.cpp | 50 ++++++++++ HDOJ/5175_autoAC.cpp | 61 +++++++++++++ HDOJ/5176_autoAC.cpp | 88 ++++++++++++++++++ HDOJ/5178_autoAC.cpp | 46 ++++++++++ HDOJ/5179_autoAC.cpp | 52 +++++++++++ HDOJ/5182_autoAC.cpp | 36 ++++++++ HDOJ/5183_autoAC.cpp | 95 +++++++++++++++++++ HDOJ/5184_autoAC.cpp | 63 +++++++++++++ HDOJ/5185_autoAC.cpp | 28 ++++++ HDOJ/5186_autoAC.cpp | 53 +++++++++++ HDOJ/5187_autoAC.cpp | 39 ++++++++ HDOJ/5188_autoAC.cpp | 72 +++++++++++++++ HDOJ/5190_autoAC.cpp | 26 ++++++ HDOJ/5191_autoAC.cpp | 63 +++++++++++++ HDOJ/5192_autoAC.cpp | 142 +++++++++++++++++++++++++++++ HDOJ/5193_autoAC.cpp | 164 +++++++++++++++++++++++++++++++++ HDOJ/5194_autoAC.cpp | 62 +++++++++++++ HDOJ/5195_autoAC.cpp | 90 ++++++++++++++++++ HDOJ/5196_autoAC.cpp | 85 +++++++++++++++++ HDOJ/5197_autoAC.cpp | 113 +++++++++++++++++++++++ HDOJ/5198_autoAC.cpp | 47 ++++++++++ HDOJ/5199_autoAC.cpp | 48 ++++++++++ 86 files changed, 5871 insertions(+) create mode 100644 HDOJ/5100_autoAC.cpp create mode 100644 HDOJ/5101_autoAC.cpp create mode 100644 HDOJ/5102_autoAC.cpp create mode 100644 HDOJ/5103_autoAC.cpp create mode 100644 HDOJ/5104_autoAC.cpp create mode 100644 HDOJ/5105_autoAC.cpp create mode 100644 HDOJ/5106_autoAC.cpp create mode 100644 HDOJ/5107_autoAC.cpp create mode 100644 HDOJ/5108_autoAC.cpp create mode 100644 HDOJ/5109_autoAC.cpp create mode 100644 HDOJ/5110_autoAC.cpp create mode 100644 HDOJ/5111_autoAC.cpp create mode 100644 HDOJ/5112_autoAC.cpp create mode 100644 HDOJ/5113_autoAC.cpp create mode 100644 HDOJ/5114_autoAC.cpp create mode 100644 HDOJ/5115_autoAC.cpp create mode 100644 HDOJ/5116_autoAC.cpp create mode 100644 HDOJ/5117_autoAC.cpp create mode 100644 HDOJ/5119_autoAC.cpp create mode 100644 HDOJ/5120_autoAC.cpp create mode 100644 HDOJ/5122_autoAC.cpp create mode 100644 HDOJ/5123_autoAC.cpp create mode 100644 HDOJ/5124_autoAC.cpp create mode 100644 HDOJ/5125_autoAC.cpp create mode 100644 HDOJ/5126_autoAC.cpp create mode 100644 HDOJ/5127_autoAC.cpp create mode 100644 HDOJ/5128_autoAC.cpp create mode 100644 HDOJ/5130_autoAC.cpp create mode 100644 HDOJ/5131_autoAC.cpp create mode 100644 HDOJ/5134_autoAC.cpp create mode 100644 HDOJ/5135_autoAC.cpp create mode 100644 HDOJ/5136_autoAC.cpp create mode 100644 HDOJ/5137_autoAC.cpp create mode 100644 HDOJ/5138_autoAC.cpp create mode 100644 HDOJ/5139_autoAC.cpp create mode 100644 HDOJ/5140_autoAC.cpp create mode 100644 HDOJ/5141_autoAC.cpp create mode 100644 HDOJ/5142_autoAC.cpp create mode 100644 HDOJ/5143_autoAC.cpp create mode 100644 HDOJ/5144_autoAC.cpp create mode 100644 HDOJ/5145_autoAC.cpp create mode 100644 HDOJ/5146_autoAC.cpp create mode 100644 HDOJ/5147_autoAC.cpp create mode 100644 HDOJ/5148_autoAC.cpp create mode 100644 HDOJ/5150_autoAC.cpp create mode 100644 HDOJ/5151_autoAC.cpp create mode 100644 HDOJ/5152_autoAC.cpp create mode 100644 HDOJ/5154_autoAC.cpp create mode 100644 HDOJ/5155_autoAC.cpp create mode 100644 HDOJ/5156_autoAC.cpp create mode 100644 HDOJ/5157_autoAC.cpp create mode 100644 HDOJ/5158_autoAC.cpp create mode 100644 HDOJ/5159_autoAC.cpp create mode 100644 HDOJ/5160_autoAC.cpp create mode 100644 HDOJ/5162_autoAC.cpp create mode 100644 HDOJ/5163_autoAC.cpp create mode 100644 HDOJ/5164_autoAC.cpp create mode 100644 HDOJ/5165_autoAC.cpp create mode 100644 HDOJ/5166_autoAC.cpp create mode 100644 HDOJ/5167_autoAC.cpp create mode 100644 HDOJ/5168_autoAC.cpp create mode 100644 HDOJ/5170_autoAC.cpp create mode 100644 HDOJ/5171_autoAC.cpp create mode 100644 HDOJ/5172_autoAC.cpp create mode 100644 HDOJ/5174_autoAC.cpp create mode 100644 HDOJ/5175_autoAC.cpp create mode 100644 HDOJ/5176_autoAC.cpp create mode 100644 HDOJ/5178_autoAC.cpp create mode 100644 HDOJ/5179_autoAC.cpp create mode 100644 HDOJ/5182_autoAC.cpp create mode 100644 HDOJ/5183_autoAC.cpp create mode 100644 HDOJ/5184_autoAC.cpp create mode 100644 HDOJ/5185_autoAC.cpp create mode 100644 HDOJ/5186_autoAC.cpp create mode 100644 HDOJ/5187_autoAC.cpp create mode 100644 HDOJ/5188_autoAC.cpp create mode 100644 HDOJ/5190_autoAC.cpp create mode 100644 HDOJ/5191_autoAC.cpp create mode 100644 HDOJ/5192_autoAC.cpp create mode 100644 HDOJ/5193_autoAC.cpp create mode 100644 HDOJ/5194_autoAC.cpp create mode 100644 HDOJ/5195_autoAC.cpp create mode 100644 HDOJ/5196_autoAC.cpp create mode 100644 HDOJ/5197_autoAC.cpp create mode 100644 HDOJ/5198_autoAC.cpp create mode 100644 HDOJ/5199_autoAC.cpp diff --git a/HDOJ/5100_autoAC.cpp b/HDOJ/5100_autoAC.cpp new file mode 100644 index 0000000..77ed487 --- /dev/null +++ b/HDOJ/5100_autoAC.cpp @@ -0,0 +1,23 @@ +#include +#include +int main() +{ + int T; + scanf("%d",&T); + while(T--) + { + int n,k; + scanf("%d%d",&n,&k); + if(n +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +int n, k, v[1010][110], m[1100], arr[1010*110]; +int main() +{ + int T; + scanf("%d", &T); + while(T--) + { + int cnt=0; + scanf("%d%d", &n, &k); + for(int i=0; i +#include +#include +#include +#include +#include +using namespace std ; +typedef long long LL ; +#define rep( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i ) +#define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i ) +#define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i ) +#define clr( a , x ) memset ( a , x , sizeof a ) +const int MAXN = 100005 ; +const int MAXE = 200005 ; +struct Node { + int v , p , d ; + Node () {} + Node ( int v , int p , int d ) : v ( v ) , p ( p ) , d ( d ) {} +} ; +struct Edge { + int v , n ; + Edge () {} + Edge ( int v , int n ) : v ( v ) , n ( n ) {} +} ; +Node Q[3000005] ; +Edge E[MAXE] ; +int H[MAXN] , cntE ; +int head , tail ; +LL ans ; +int n , k ; +void clear () { + ans = 0 ; + cntE = 0 ; + clr ( H , -1 ) ; +} +void addedge ( int u , int v ) { + E[cntE] = Edge ( v , H[u] ) ; + H[u] = cntE ++ ; +} +void bfs () { + int cnt = 0 ; + head = tail = 0 ; + For ( i , 1 , n ) Q[tail ++] = Node ( i , 0 , 0 ) ; + while ( head != tail ) { + Node x = Q[head ++] ; + int u = x.v , p = x.p ; + for ( int i = H[u] ; ~i ; i = E[i].n ) { + int v = E[i].v ; + if ( v != p ) { + Q[tail ++] = Node ( v , u , x.d + 1 ) ; + ans += x.d + 1 ; + ++ cnt ; + if ( cnt == k ) return ; + } + } + } +} +void solve () { + int u , v ; + clear () ; + scanf ( "%d%d" , &n , &k ) ; + k *= 2 ; + rep ( i , 1 , n ) { + scanf ( "%d%d" , &u , &v ) ; + addedge ( u , v ) ; + addedge ( v , u ) ; + } + bfs () ; + printf ( "%I64d\n" , ans / 2 ) ; +} +int main () { + int T ; + scanf ( "%d" , &T ) ; + while ( T -- ) solve () ; + return 0 ; +} diff --git a/HDOJ/5103_autoAC.cpp b/HDOJ/5103_autoAC.cpp new file mode 100644 index 0000000..2cfb8e3 --- /dev/null +++ b/HDOJ/5103_autoAC.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include +using namespace std; +typedef long long ll; +const int mod = 1000000007; +int dp[15][1<<15]; +int sum[1<<15]; +int sub[1<<15]; +int L[15],R[15],n; +int cal(int S){ + int num=0; + for(int i=0; i=ge ){ + dp[i][S]= sub[S^(1<0; H=S&(H-1)){ + if((H&(1< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +int is_prime(int n ) +{ + for(int i=2 ;i*i<=n;i++ ) + { + if ( n % i ==0) + return 0; + } + return 1; +} +int n,m; +int main () +{ + int prime[10005]; + memset(prime,0,sizeof(prime)); + for(int i=2 ;i<=10002;i++) + { + if ( is_prime(i) ) + prime[i] = 1; + } + while (cin>>n) + { + int num =0; + for(int i=2 ;i<=n;i++) + for(int j=i ;j<=(n-i)/2;j++) + { + if (prime[i] && prime[j]&& prime[n-i-j]) + { + num++; + } + } + cout< +#include +#include +#include +using namespace std; +double a,b,c,d,L,R; +double f(double x) +{ + return fabs(a*x*x*x+b*x*x+c*x+d); +} +const double eps=1e-9; +int dcmp(double x) +{ + if(fabs(x)<=eps)return 0; + else return x<0?-1:1; +} +bool judge(double x) +{ + return x<=R&&x>=L; +} +int main() +{ + while(scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&L,&R)!=EOF) + { + double delta=4*b*b-12*a*c; + double ans; + if(dcmp(a)==0) + { + double mid=-c/2/b; + if(judge(mid)) + ans=max(f(mid),f(L)); + else ans=f(L); + ans=max(ans,f(R)); + } + else if(delta<=0) + { + ans=max(f(L),f(R)); + } + else + { + delta=sqrt(delta); + double x1=-2*b+delta; + x1=x1/6/a; + double x2=-2*b-delta; + x2=x2/6/a; + ans=f(L); + ans=max(ans,f(R)); + if(judge(x1)) + ans=max(ans,f(x1)); + if(judge(x2)) + ans=max(ans,f(x2)); + } + printf("%.2lf\n",ans); + } + return 0; +} diff --git a/HDOJ/5106_autoAC.cpp b/HDOJ/5106_autoAC.cpp new file mode 100644 index 0000000..5310cbb --- /dev/null +++ b/HDOJ/5106_autoAC.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include +#define RD(x) scanf("%d",&x) +#define RD2(x,y) scanf("%d%d",&x,&y) +#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z) +#define clr0(x) memset(x,0,sizeof(x)) +#define clr1(x) memset(x,-1,sizeof(x)) +using namespace std; +typedef long long LL; +const int maxn = 1005; +const double eps = 1e-3; +const int inf = 0x7fffffff; +char p[maxn]; +const LL mod = 1000000007; +LL c[maxn][maxn]; +LL q[maxn]; +void init() +{ + for(int i = 0 ; i < maxn ; ++i){ + c[i][0] = 1; + } + for(int i = 1 ; i < maxn ; i++){ + for(int j = 1 ; j <= i ; ++j){ + c[i][j] = (c[i-1][j-1] + c[i-1][j])%mod; + } + } + q[0] = 1; + for(int i = 1;i < maxn;++i){ + q[i] = (q[i - 1]*2)%mod; + } +} +int main() +{ + init(); + int n,k; + while(~scanf("%d%s",&n,p)){ + int m = strlen(p); + int k = 0; + LL ans = 0,tmp = 0; + for(int st = 0;st < m;++st){ + if(p[st] == '1'){ + ans += ((m-st-2>=0&&n-k-1>=0?c[m - st - 2][n - k - 1]:0) * (q[m - st - 1] - 1))%mod; + ans += (tmp * q[m - st]%mod * (m-st-1>=0&&n-k>=0?c[m - st - 1][n - k]:0))%mod; + ans %= mod; + } + tmp = (tmp*2 + p[st] - '0')%mod; + k += p[st] - '0'; + } + if(ans < 0) ans += mod; + printf("%I64d\n",ans); + } + return 0; +} diff --git a/HDOJ/5107_autoAC.cpp b/HDOJ/5107_autoAC.cpp new file mode 100644 index 0000000..7d5dd36 --- /dev/null +++ b/HDOJ/5107_autoAC.cpp @@ -0,0 +1,125 @@ +#include "stdio.h" +#include "string.h" +#include "algorithm" +#include "map" +using namespace std; +mapmp; +struct Mark +{ + int x,y,h,op,id; +}mark[60010]; +struct Ans +{ + int w; + int h[11]; +}ans; +struct node +{ + int l,r; + Ans x; +}data[300010]; +int y[60010],pri[30010]; +bool cmp_mark(Mark a,Mark b) +{ + if (a.x!=b.x) return a.x b.w || (i <= a.w && a.h[i] < b.h[j]) ) + c.h[k++] = a.h[i++]; + else + c.h[k++] = b.h[j++]; + } + c.w=k-1; + return c; +} +void build(int l,int r,int k) +{ + int mid; + data[k].l=l; + data[k].r=r; + data[k].x.w=0; + if(l==r) return ; + mid=(l+r)/2; + build(l,mid,k*2); + build(mid+1,r,k*2+1); +} +void updata(int n,int op,int k) +{ + int mid; + if (data[k].l==n && data[k].r==n) + { + data[k].x.w++; + data[k].x.h[data[k].x.w]=op; + sort(data[k].x.h+1,data[k].x.h+1+data[k].x.w); + return ; + } + mid=(data[k].l+data[k].r)/2; + if (n<=mid) updata(n,op,k*2); + else if (n>mid) updata(n,op,k*2+1); + data[k].x=Merge(data[k*2].x,data[k*2+1].x); +} +Ans query(int l,int r,int k) +{ + int mid; + if (data[k].l==l && data[k].r==r) + return data[k].x; + mid=(data[k].l+data[k].r)/2; + if (r<=mid) return query(l,r,k*2); + else + if (l>mid) return query(l,r,k*2+1); + else + return Merge(query(l,mid,k*2),query(mid+1,r,k*2+1)); +} +int main() +{ + int n,m,i,cnt,temp; + while (scanf("%d%d",&n,&m)!=EOF) + { + for (i=0;i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define inf 0x3f3f3f3f +#define mem(a,x) memset(a,x,sizeof(a)) +using namespace std; +typedef long long ll; +typedef pair pii; +inline int in() +{ + int res=0; + char c; + while((c=getchar())<'0' || c>'9'); + while(c>='0' && c<='9')res=res*10+c-'0',c=getchar(); + return res; +} +const int N=32000; +bool vis[N]; +int prime[10000]; +int main() +{ + int p=1; + prime[0]=2; + for(int i=3;i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +int a; +LL s, b, t; +char str[10]; +int main() { + while (~scanf("%d%s", &a, str)) { + int len = strlen(str); + if (len == 1 && str[0] == '0') { + puts("0"); + continue; + } + b = -1; + LL base = 1; + for (int i = 1; i <= len; i++) + base *= 10; + sscanf(str, "%I64d", &s); + for (int i = 1; i <= 10000; i *= 10) { + for (int j = (str[0] == '0'); j < a; j++) { + t = ((LL) (j) * base + s) * i; + int mod = (a - t % a) % a; + if (mod < i) { + t += mod; + if (b < 0 || t < b) + b = t; + } + } + } + printf("%I64d\n", b / a); + } + return 0; +} diff --git a/HDOJ/5110_autoAC.cpp b/HDOJ/5110_autoAC.cpp new file mode 100644 index 0000000..0bd2c08 --- /dev/null +++ b/HDOJ/5110_autoAC.cpp @@ -0,0 +1,112 @@ +#include +#include +#include +#include +#include +#include +using namespace std ; +typedef long long LL ; +#define rep( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i ) +#define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i ) +#define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i ) +#define clr( a , x ) memset ( a , x , sizeof a ) +const int MAXN = 1005 ; +const int L = 33 ; +int G[MAXN][MAXN] ; +int down[MAXN][MAXN] ; +int prefix[MAXN][MAXN] ; +int prefix_dn[MAXN][MAXN] ; +int prefix_lt[MAXN][MAXN] ; +int prefix_rt[MAXN][MAXN] ; +int ans[L][MAXN][MAXN] ; +int n , m , q , sqrtD ; +char s[MAXN] ; +void scanf ( int &x , char c = 0 ) { + while ( ( c = getchar () ) < '0' || c > '9' ) ; + x = c - '0' ; + while ( ( c = getchar () ) >= '0' && c <= '9' ) x = x * 10 + c - '0' ; +} +void read () { + For ( i , 1 , n ) { + scanf ( "%s" , s + 1 ) ; + For ( j , 1 , m ) { + G[i][j] = ( s[j] == 'X' ) ; + prefix[i][j] = prefix[i][j - 1] + G[i][j] ; + } + } +} +void solve1 () { + int x , y , D ; + sqrtD = ( int ) sqrt ( ( double ) n ) ; + For ( k , 1 , sqrtD ) { + For ( i , 1 , n ) { + For ( j , 1 , m ) { + down[i][j] = G[i][j] ; + if ( i - k > 0 ) down[i][j] += down[i - k][j] ; + prefix_dn[i][j] = prefix_dn[i][j - 1] + down[i][j] ; + prefix_lt[i][j] = down[i][j] ; + if ( i - k > 0 ) { + if ( j - k <= 0 ) prefix_lt[i][j] += prefix_dn[i - k][j - 1] - prefix_dn[i - k][0] ; + else { + prefix_lt[i][j] += prefix_dn[i - k][j - 1] - prefix_dn[i - k][j - k] ; + prefix_lt[i][j] += prefix_lt[i - k][j - k] ; + } + } + } + rev ( j , m , 1 ) { + prefix_rt[i][j] = down[i][j] ; + if ( i - k > 0 ) { + if ( j + k > m ) prefix_rt[i][j] += prefix_dn[i - k][m] - prefix_dn[i - k][j] ; + else { + prefix_rt[i][j] += prefix_rt[i - k][j + k] ; + prefix_rt[i][j] += prefix_dn[i - k][j + k - 1] - prefix_dn[i - k][j] ; + } + } + } + For ( j , 1 , m ) ans[k][i][j] = prefix_lt[i][j] + prefix_rt[i][j] - down[i][j] ; + } + } + while ( q -- ) { + scanf ( x ) ; + scanf ( y ) ; + scanf ( D ) ; + if ( D <= sqrtD ) { + //printf ( "%d %d\n" , prefix_lt[x][y][D] , prefix_rt[x][y][D] ) ; + printf ( "%d\n" , ans[D][x][y] ) ; + } + else { + int L = y , R = y , sum = 0 ; + while ( x > 0 ) { + sum += prefix[x][R] - prefix[x][L - 1] ; + x -= D ; + L = max ( 1 , L - D ) ; + R = min ( m , R + D ) ; + } + printf ( "%d\n" , sum ) ; + } + } +} +void solve2 () { + int x , y , D ; + while ( q -- ) { + scanf ( x ) ; + scanf ( y ) ; + scanf ( D ) ; + int L = y , R = y , sum = 0 ; + while ( x > 0 ) { + sum += prefix[x][R] - prefix[x][L - 1] ; + x -= D ; + L = max ( 1 , L - D ) ; + R = min ( m , R + D ) ; + } + printf ( "%d\n" , sum ) ; + } +} +int main () { + while ( ~scanf ( "%d%d%d" , &n , &m , &q ) ) { + read () ; + if ( q > 1000 ) solve1 () ; + else solve2 () ; + } + return 0 ; +} diff --git a/HDOJ/5111_autoAC.cpp b/HDOJ/5111_autoAC.cpp new file mode 100644 index 0000000..677caa8 --- /dev/null +++ b/HDOJ/5111_autoAC.cpp @@ -0,0 +1,212 @@ +#include +#include +#include +#include +#include +using namespace std ; +typedef long long LL ; +#pragma comment(linker, "/STACK:16777216") +#define rep( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i ) +#define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i ) +#define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i ) +#define clr( a , x ) memset ( a , x , sizeof a ) +#define mid ( ( l + r ) >> 1 ) +const int MAXN = 100005 ; +const int MAXE = 100005 ; +struct Node { + Node* c[2] ; + int sum ; +} ; +struct Edge { + int v , n ; + Edge () {} + Edge ( int v , int n ) : v ( v ) , n ( n ) {} +} ; +struct Seg { + int L , R ; + Seg () {} + Seg ( int L , int R ) : L ( L ) , R ( R ) {} +} ; +struct HeavyLightDecompose { + Edge E[MAXE] ; + int H[MAXN] , cntE ; + int pre[MAXN] ; + int pos[MAXN] ; + int dep[MAXN] ; + int siz[MAXN] ; + int son[MAXN] ; + int top[MAXN] ; + int val[MAXN] ; + int tree_ ; + void clear () { + tree_idx = 0 ; + dep[1] = 0 ; + pre[1] = 0 ; + siz[0] = 0 ; + cntE = 0 ; + clr ( H , -1 ) ; + } + void addedge ( int u , int v ) { + E[cntE] = Edge ( v , H[u] ) ; + H[u] = cntE ++ ; + } + void dfs ( int u ) { + siz[u] = 1 ; + son[u] = 0 ; + for ( int i = H[u] ; ~i ; i = E[i].n ) { + int v = E[i].v ; + if ( v == pre[u] ) continue ; + pre[v] = u ; + dep[v] = dep[u] + 1 ; + dfs ( v ) ; + siz[u] += siz[v] ; + if ( siz[v] > siz[son[u]] ) son[u] = v ; + } + } + void rebuild ( int u , int top_ ) { + top[u] = top_ ; + pos[u] = ++ tree_ ; + if ( son[u] ) rebuild ( son[u] , top_ ) ; + for ( int i = H[u] ; ~i ; i = E[i].n ) { + int v = E[i].v ; + if ( v != pre[u] && v != son[u] ) { + rebuild ( v , v ) ; + } + } + } +} ; +Node pool[MAXN * 50] ; +Node* root[MAXN] ; +Node* cur ; +HeavyLightDecompose T1 , T2 ; +Seg seg[MAXN] ; +int top ; +int cnt ; +int n1 , n2 , q ; +map < int , int > mp ; +void clear () { + T1.clear () ; + T2.clear () ; + mp.clear () ; + cur = pool ; +} +void build ( Node* &now , int l , int r ) { + now = cur ++ ; + now->sum = 0 ; + if ( l == r ) return ; + int m = mid ; + build ( now->c[0] , l , m ) ; + build ( now->c[1] , m + 1 , r ) ; +} +void insert ( Node* &now , Node* old , int x , int v , int l , int r ) { + now = cur ++ ; + if ( l == r ) { + now->sum = old->sum + v ; + return ; + } + int m = mid ; + if ( x <= m ) { + now->c[1] = old->c[1] ; + insert ( now->c[0] , old->c[0] , x , v , l , m ) ; + } else { + now->c[0] = old->c[0] ; + insert ( now->c[1] , old->c[1] , x , v , m + 1 , r ) ; + } + now->sum = now->c[0]->sum + now->c[1]->sum ; +} +int query ( Node* now , Node* old , int x , int l , int r ) { + if ( x == 0 ) return 0 ; + int ans = 0 ; + while ( l < r ) { + int m = mid ; + if ( x <= m ) { + now = now->c[0] ; + old = old->c[0] ; + r = m ; + } else { + ans += now->c[0]->sum - old->c[0]->sum ; + now = now->c[1] ; + old = old->c[1] ; + l = m + 1 ; + } + } + ans += now->sum - old->sum ; + return ans ; +} +void dfs ( int u ) { + if ( mp.count ( T2.val[u] ) ) insert ( root[T2.pos[u]] , root[T2.pos[u] - 1] , mp[T2.val[u]] , 1 , 1 , cnt ) ; + else root[T2.pos[u]] = root[T2.pos[u] - 1] ; + if ( T2.son[u] ) dfs ( T2.son[u] ) ; + for ( int i = T2.H[u] ; ~i ; i = T2.E[i].n ) { + int v = T2.E[i].v ; + if ( v != T2.pre[u] && v != T2.son[u] ) { + dfs ( v ) ; + } + } +} +void get_seg ( int x , int y ) { + top = 0 ; + while ( T1.pos[T1.top[x]] != T1.pos[T1.top[y]] ) { + if ( T1.dep[T1.top[x]] < T1.dep[T1.top[y]] ) swap ( x , y ) ; + seg[top ++] = Seg ( T1.pos[T1.top[x]] , T1.pos[x] ) ; + x = T1.pre[T1.top[x]] ; + } + if ( T1.dep[x] > T1.dep[y] ) swap ( x , y ) ; + seg[top ++] = Seg ( T1.pos[x] , T1.pos[y] ) ; +} +int get_sum ( int x , int y ) { + int ans = 0 ; + while ( T2.pos[T2.top[x]] != T2.pos[T2.top[y]] ) { + if ( T2.dep[T2.top[x]] < T2.dep[T2.top[y]] ) swap ( x , y ) ; + rep ( i , 0 , top ) { + int L = seg[i].L ; + int R = seg[i].R ; + ans -= query ( root[T2.pos[x]] , root[T2.pos[T2.top[x]] - 1] , L - 1 , 1 , cnt ) ; + ans += query ( root[T2.pos[x]] , root[T2.pos[T2.top[x]] - 1] , R , 1 , cnt ) ; + } + x = T2.pre[T2.top[x]] ; + } + if ( T2.dep[x] > T2.dep[y] ) swap ( x , y ) ; + rep ( i , 0 , top ) { + int L = seg[i].L ; + int R = seg[i].R ; + ans -= query ( root[T2.pos[y]] , root[T2.pos[x] - 1] , L - 1 , 1 , cnt ) ; + ans += query ( root[T2.pos[y]] , root[T2.pos[x] - 1] , R , 1 , cnt ) ; + } + return ans ; +} +void solve () { + int u , v ; + clear () ; + cnt = n1 ; + For ( i , 2 , n1 ) { + scanf ( "%d" , &u ) ; + T1.addedge ( u , i ) ; + } + For ( i , 1 , n1 ) scanf ( "%d" , &T1.val[i] ) ; + scanf ( "%d" , &n2 ) ; + For ( i , 2 , n2 ) { + scanf ( "%d" , &u ) ; + T2.addedge ( u , i ) ; + } + For ( i , 1 , n2 ) scanf ( "%d" , &T2.val[i] ) ; + T1.dfs ( 1 ) ; + T1.rebuild ( 1 , 1 ) ; + For ( i , 1 , n1 ) mp[T1.val[i]] = T1.pos[i] ; + build ( root[0] , 1 , cnt ) ; + T2.dfs ( 1 ) ; + T2.rebuild ( 1 , 1 ) ; + dfs ( 1 ) ; + scanf ( "%d" , &q ) ; + while ( q -- ) { + scanf ( "%d%d" , &u , &v ) ; + get_seg ( u , v ) ; + scanf ( "%d%d" , &u , &v ) ; + int ans = get_sum ( u , v ) ; + printf ( "%d\n" , ans ) ; + } +} +int main () { + while ( ~scanf ( "%d" , &n1 ) ) solve () ; + return 0 ; +} diff --git a/HDOJ/5112_autoAC.cpp b/HDOJ/5112_autoAC.cpp new file mode 100644 index 0000000..c2e71b0 --- /dev/null +++ b/HDOJ/5112_autoAC.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include +using namespace std; +#define maxn 10010 +struct Node { + int t, x; +} E[maxn]; +bool cmp(Node a, Node b) { + return a.t < b.t; +} +int main() { + int T, N, i, j; + double ans; + scanf("%d", &T); + for(int cas = 1; cas <= T; ++cas) { + scanf("%d", &N); + for(i = 0; i < N; ++i) + scanf("%d%d", &E[i].t, &E[i].x); + sort(E, E + N, cmp); + for(ans = 0.0, i = 1; i < N; ++i) { + ans = max(ans, abs(E[i].x - E[i-1].x) * 1.0 / (E[i].t - E[i-1].t)); + } + printf("Case #%d: %.2lf\n", cas, ans); + } + return 0; +} diff --git a/HDOJ/5113_autoAC.cpp b/HDOJ/5113_autoAC.cpp new file mode 100644 index 0000000..2736ef0 --- /dev/null +++ b/HDOJ/5113_autoAC.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef pair PII; +typedef vector vec; +typedef vector mat; +#define AA first +#define BB second +int T,N,M,K; +int m[10][10]; +int c[100]; +bool hasAns; +bool C(int x,int y,int c){ + int row = true, column = true; + if( x-1>=1&&m[x-1][y]==c ) column = false; + if( y-1>=1 && m[x][y-1]==c ) row = false; + return row&&column; +} +void dfs(int x,int y,int cur){ + for(int i=1;i<=K;i++){ + if( c[i]>(cur+1)/2 ) return; + } + if(hasAns) return; + if( x==N+1 ){ + hasAns = true; + puts("YES"); + for(int i=1;i<=N;i++) { + for(int j=1;j<=M;j++ ){ + printf(j==M?"%d\n":"%d ",m[i][j]); + } + } + return; + } + for(int i=1;i<=K;i++)if( c[i] ){ + if( C(x,y,i) ){ + c[i]--; + m[x][y] = i; + if( y==M ) dfs(x+1,1,cur-1); + else dfs(x,y+1,cur-1); + c[i]++; + m[x][y] = 0; + } + } +} +int main(){ + scanf("%d",&T); + for(int cases = 1; cases <= T; cases++){ + scanf("%d%d%d",&N,&M,&K); + for(int i=1;i<=K;i++){ + scanf("%d",&c[i]); + } + hasAns = false; + printf("Case #%d:\n",cases); + dfs(1,1,N*M); + if(!hasAns) puts("NO"); + } + return 0; +} diff --git a/HDOJ/5114_autoAC.cpp b/HDOJ/5114_autoAC.cpp new file mode 100644 index 0000000..ad392ae --- /dev/null +++ b/HDOJ/5114_autoAC.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +using namespace std; +int n,m; +int MIN(int x,int y) +{ + if (x0||x1==0&&fx11<0) fx11=-fx11; + if (y1==m&&fx12>0||y1==0&&fx12<0) fx12=-fx12; + if (x2==n&&fx21>0||x2==0&&fx21<0) fx21=-fx21; + if (y2==m&&fx22>0||y2==0&&fx22<0) fx22=-fx22; + for (i=1;i<=410000;i++) + { + if (abs(x1-x2)==abs(y1-y2)&&((x2-x1)/fx11>0)&&((y2-y1)/fx12>0)&&((x1-x2)/fx21 >0)&&((y1-y2)/fx22>0))//杩㈢告 + { + ansx=(x1+x2)/2.0; + ansy=(y1+y2)/2.0; + break; + } + if (fx11>0) move=n-x1; + else move=x1; + if (fx12>0) move=MIN(move,m-y1); + else move=MIN(move,y1); + if (fx21>0) move=MIN(move,n-x2); + else move=MIN(move,x2); + if (fx22>0) move=MIN(move,m-y2); + else move=MIN(move,y2); +//琛告 + if (x1==x2&&((y2-y1)/fx12>0)&&((y1-y2)/fx22>0)&&fx11==fx21&&(x1+fx11*abs(y2-y1)/2.0)>=0&&(x1+fx11*abs(y2-y1)/2.0)<=n) + { + ansx=x1+fx11*abs(y2-y1)/2.0; + ansy=(y1+y2)/2.0; + break; + } +//告 + if (y1==y2&&((x2-x1)/fx11>0)&&((x1-x2)/fx21>0)&&fx12==fx22&&(y1+fx12*abs(x2-x1)/2.0)>=0&&(y1+fx12*abs(x2-x1)/2.0)<=m) + { + ansx=(x1+x2)/2.0; + ansy=(y1+fx12*abs(x2-x1)/2.0); + break; + } + x1=move*fx11+x1; + y1=move*fx12+y1; + x2=move*fx21+x2; + y2=move*fx22+y2; + if (x1==n&&fx11>0||x1==0&&fx11<0) fx11=-fx11; + if (y1==m&&fx12>0||y1==0&&fx12<0) fx12=-fx12; + if (x2==n&&fx21>0||x2==0&&fx21<0) fx21=-fx21; + if (y2==m&&fx22>0||y2==0&&fx22<0) fx22=-fx22; + } + printf("Case #%d:\n",ii); + if (i==410001) + { + printf("Collision will not happen.\n"); + continue; + } + else + { + printf("%.1lf %.1lf\n",ansx,ansy); + } + } + return 0; +} diff --git a/HDOJ/5115_autoAC.cpp b/HDOJ/5115_autoAC.cpp new file mode 100644 index 0000000..bdec5ea --- /dev/null +++ b/HDOJ/5115_autoAC.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +using namespace std; +int a[210],b[210]; +int f[210][210]; +int main() +{ + int t,ii,n,i,j,k,l,Min,m; + scanf("%d",&t); + for (ii=1;ii<=t;ii++) + { + memset(f,0,sizeof(0)); + scanf("%d",&n); + for (i=1;i<=n;i++) + scanf("%d",&a[i]); + for (i=1;i<=n;i++) + scanf("%d",&b[i]); + for (i=1;i<=n;i++) + { + for (j=i;j<=n;j++) + f[i][j]=99999999; + } + for (l=0;l<=n;l++) + for (i=1;i +#include +#include +using namespace std; +#define prt(k) cout<<#k" = "< +bool mp[M+5][M+5]; +int dp[M+5][M+5]; +int cnt[M+5][M+5]; +ll S; +int gcd(int a, int b) { return b==0?a:gcd(b, a%b); } +int U[M][M], R[M][M]; +ll T[M][M]; +int f(int i, int j) +{ + int ret = 0; + for (int k=1;k<=j;k++) if (gcd(i,k)==1) ret++; + return ret; +} +void init() +{ + memset(cnt, 0, sizeof cnt); + for (int j=1;j<=200;j++) + for (int i=1;i<=200;i++) + { + dp[i][j] = f(i, j); + cnt[i][j] = cnt[i-1][j] + dp[i][j]; + } +} +int n; +int main() +{ + init(); + int re; scanf("%d", &re); int ca = 1; + while (re--) + { + scanf("%d", &n); + memset(mp, false, sizeof mp); + memset(U, 0, sizeof U); + memset(R, 0, sizeof R); + memset(T, 0, sizeof T); + S = 0; + for (int i=0;i=1;i--) + for (int j=200;j>=1;j--) + if (mp[i][j]) { + if (mp[i+1][j]) U[i][j]=U[i+1][j]+1; + if (mp[i][j+1]) R[i][j]=R[i][j+1]+1; + } + for (int i=1;i<=200;i++) + for (int j=1;j<=200;j++) + if (mp[i][j]) { + ll tmp[M]; + memset(tmp, 0, sizeof tmp); + for (int k=1; k<=U[i][j]; k++) + tmp[k] = dp[k][R[i][j]]; + for (int k = U[i][j]; k; k--) + tmp[k-1] += tmp[k]; + S += tmp[0]; + for (int k=0; k<=U[i][j]; k++) + T[i+k][j] += tmp[k]; + } + ll ans = 0; + for (int i=1;i<200;i++) + { + for (int j=1;j<200;j++) + if (U[i][j]&&R[i][j]) { + ll f = T[i][j]; + ll pp, p; + pp = cnt[U[i][j]][R[i][j]]; + f -= pp; + for (int k=1;k<=R[i][j];k++) + { + f += T[i][j+k]; + p = dp[k][U[i][j]]; + ans += f * p * 2; + } + ans += pp * pp; + } + } + ll ret = S * S - ans; + printf("Case #%d: %I64d\n", ca++ , ret); + } + return 0; +} diff --git a/HDOJ/5117_autoAC.cpp b/HDOJ/5117_autoAC.cpp new file mode 100644 index 0000000..57e5798 --- /dev/null +++ b/HDOJ/5117_autoAC.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +using namespace std; +const int mod = 1000000007; +typedef long long LL; +LL dp[2][8]; +int f[51][51]; +int main() +{ + int T,N,M,K,l,now; + LL tmp; + scanf("%d",&T); + for(int cas=1;cas<=T;cas++){ + LL ans=0; + scanf("%d%d",&N,&M); + memset(f,0,sizeof f); + for(int i=0;i +#include +#include +using namespace std; +__int64 f[2][1048600]; +int a[45]; +int main() +{ + int t,ii,n,i,j,Min,m; + __int64 ans; + scanf("%d",&t); + for (ii=1;ii<=t;ii++) + { + scanf("%d%d",&n,&m); + for (i=1;i<=n;i++) + scanf("%d",&a[i]); + memset(f,0,sizeof(f)); + f[0][0]=1;ans=0; + for (i=1;i<=n;i++) + for (j=0;j<1048576;j++) + f[i%2][j]=f[(i-1)%2][j]+f[(i-1)%2][j^a[i]]; + for (i=m;i<1048576;i++) + ans+=f[n%2][i]; + printf("Case #%d: %I64d\n",ii,ans); + } +} diff --git a/HDOJ/5120_autoAC.cpp b/HDOJ/5120_autoAC.cpp new file mode 100644 index 0000000..424d9ec --- /dev/null +++ b/HDOJ/5120_autoAC.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include +#include +#define inf 0x7fffffff +#define exp 1e-10 +#define PI 3.141592654 +using namespace std; +typedef long long ll; +struct Point +{ + double x,y; + Point (double x=0,double y=0):x(x),y(y){} +}; +double dist(Point a,Point b) +{ + double x=(a.x-b.x)*(a.x-b.x); + double y=(a.y-b.y)*(a.y-b.y); + return sqrt(x+y); +} +double Area_of_overlap(Point c1,double r1,Point c2,double r2) +{ + double d=dist(c1,c2); + if (r1+r2 +#include +#include +using namespace std; +int a[1000010]; +int main() +{ + int t,ii,n,i,Min,ans; + scanf("%d",&t); + for (ii=1;ii<=t;ii++) + { + scanf("%d",&n); + for (i=1;i<=n;i++) + scanf("%d",&a[i]); + Min=a[n]; + ans=0; + for (i=n-1;i>=1;i--) + { + if (a[i]>Min) ans++; + else Min=a[i]; + } + printf("Case #%d: %d\n",ii,ans); + } +} diff --git a/HDOJ/5123_autoAC.cpp b/HDOJ/5123_autoAC.cpp new file mode 100644 index 0000000..dbfd3b5 --- /dev/null +++ b/HDOJ/5123_autoAC.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +using namespace std; +int a[110]; +int main() +{ + int t,n; + scanf("%d",&t); + while(t--) + { + scanf("%d",&n); + memset(a,0,sizeof(a)); + int max=0; + for(int i=0;imax) + max=a[x]; + } + int ans; + for(int i=0;i<=n;i++) + if(a[i]==max) + { + ans=i; + break; + } + printf("%d\n",ans); + } + return 0; +} diff --git a/HDOJ/5124_autoAC.cpp b/HDOJ/5124_autoAC.cpp new file mode 100644 index 0000000..ca61943 --- /dev/null +++ b/HDOJ/5124_autoAC.cpp @@ -0,0 +1,112 @@ +#include +#include +#include +#include +#include +using namespace std; +struct line{ + int l,r; +}; +bool operator < (const line &a,const line &b) +{ + if(a.rl){ + maxv[o]=max(maxv[lc],maxv[rc]); + }else maxv[o]=0; + maxv[o]+=d[o]; +} +void update(int o,int l,int r) +{ + int lc=o*2; + int rc=o*2+1; + if(yl<=l&&yr>=r){ + d[o]++; + } else { + int mid=l+((r-l)>>1); + if(yl<=mid)update(lc,l,mid); + if(yr>mid)update(rc,mid+1,r); + } + maintain(o,l,r); +} +int query(int o,int l,int r,int add) +{ + if(yl<=l&&yr>=r){ + return maxv[o]+add; + } else { + int m=l+((r-l)>>1); + int ret=-1; + if(yl<=m)ret=max(ret,query(o*2 , l , m , add + d[o] ) ); + if(yr>m)ret=max(ret , query(o*2+1 , m+1 , r , add+d[o])); + return ret; + } +} +int bisearch(int x,int tot) +{ + int le=0; + int ri=tot-1; + while(le>1; + if(pos[mid]==x)return mid; + if(pos[mid]>x)ri=mid-1; + else { + le=mid+1; + } + } + return le; +} +int main() +{ + int T; + scanf("%d",&T); + while(T--){ + int n; + scanf("%d",&n); + int tot=0; + set s; + s.clear(); + for(int i=0;i +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +#define INF 1000000000 +//typedef __int64 LL; +#define N 1005 +int t, n, m, a[N], b[N], c[N][2*N], nn, num[2*N]; +int lowbit(int x) { + return x & (-x); +} +int get_max(int x, int k) { + int ret = 0; + while(x > 0) { + ret = max(ret, c[k][x]); + x -= lowbit(x); + } + return ret; +} +void add(int x, int ad, int k) { + while(x <= nn) { + c[k][x] = max(ad, c[k][x]); + x += lowbit(x); + } +} +int main() { +#ifndef ONLINE_JUDGE + freopen("in.txt", "r", stdin); +#endif // ONLINE_JUDGE + scanf("%d", &t); + while(t--) { + scanf("%d%d", &n, &m); + nn = 0; + for(int i = 1;i <= n;i ++) { + scanf("%d%d", &a[i], &b[i]); + nn ++; num[nn] = a[i]; + nn ++; num[nn] = b[i]; + } + sort(num+1, num+1+nn); + nn = unique(num+1, num+1+nn) - num - 1; + for(int i = 1; i<= n; i++) { + a[i] = lower_bound(num+1, num+1+nn, a[i]) - num; + b[i] = lower_bound(num+1, num+1+nn, b[i]) - num; + } + memset(c, 0, sizeof(c)); + int ans = 0; + for(int i = 1; i <= n; i++) { + for(int j = 0; j <= m; j++) { + int temp; + temp = get_max(a[i] - 1, j) + 1; + add(a[i], temp, j); + ans = max(ans, temp); + if(j!=m) { + temp = get_max(b[i] - 1, j+1) + 1; + add(b[i], temp, j); + ans = max(ans, temp); + } + } + } + printf("%d\n", ans); + } + return 0; +} diff --git a/HDOJ/5126_autoAC.cpp b/HDOJ/5126_autoAC.cpp new file mode 100644 index 0000000..0326593 --- /dev/null +++ b/HDOJ/5126_autoAC.cpp @@ -0,0 +1,139 @@ +#include +#include +using namespace std; +struct Node{ + int x,y,z,p,t; + Node(){} + Node(int x,int y,int z,int p,int t):x(x),y(y),z(z),p(p),t(t){} +}; +#define maxn 450007 +Node star[maxn]; +Node star2[maxn]; +Node star3[maxn]; +int tree[maxn]; +int lowbit(int i){ + return i&(-i); +} +void add(int i,int x){ + while(i0){ + ans += tree[i]; + i -= lowbit(i); + } + return ans; +} +int res[maxn]; +void CDQ2(int l,int r){ + if(l == r) return ; + int mid = (l+r)/2; + CDQ2(l,mid); + CDQ2(mid+1,r); + int l1 =l, r1 = mid+1; + while(r1 <= r){ + while(l1 <= mid && star2[l1].y <= star2[r1].y){ + if(star2[l1].t == 0) add(star2[l1].z,1); + l1++; + } + if(star2[r1].t != 0){ + res[star2[r1].p] += get(star2[r1].z)*star2[r1].t; + } + r1++; + } + while(l1 > l){ + --l1; + if(star2[l1].t == 0) add(star2[l1].z,-1); + } + l1 = l, r1 = mid+1; + for(int i = l;i <= r; i++){ + if((l1 <= mid && star2[l1].y <= star2[r1].y) || r1 > r ) + star3[i] = star2[l1++]; + else star3[i] = star2[r1++]; + } + for(int i = l; i <= r; i++) + star2[i] = star3[i]; +} +void CDQ1(int l,int r){ + if(l == r) return ; + int mid = (l+r)/2; + CDQ1(l,mid); + CDQ1(mid+1,r); + int l1 = l, r1 = mid+1,n = 0; + while(r1 <= r){ + while(star[l1].t != 0 && l1 <= mid) l1++; + while(star[r1].t == 0 && r1 <= r) r1++; + if(r1 > r) break; + if((star[l1].x <= star[r1].x && l1 <= mid)|| r1 > r) + star2[n++] = star[l1++]; + else star2[n++] = star[r1++]; + } + if(n > 0) CDQ2(0,n-1); + l1 = l, r1 = mid+1; + for(int i = l;i <= r; i++){ + if((star[l1].x <= star[r1].x && l1 <= mid)|| r1 > r) + star3[i] = star[l1++]; + else star3[i] = star[r1++]; + } + for(int i = l;i <= r; i++) + star[i] = star3[i]; +} +int compn(Node a,Node b){ + return a.p < b.p; +} +int compz(Node a,Node b){ + return a.z < b.z; +} +int main(){ + int t,q,a,x1,y1,z1,x2,y2,z2; + scanf("%d",&t); + while(t--){ + int n = 0; + scanf("%d",&q); + while(q--){ + scanf("%d",&a); + if(a == 1){ + scanf("%d%d%d",&x1,&y1,&z1); + star[n++] = Node(x1,y1,z1,n,0); + } + else { + scanf("%d%d%d%d%d%d",&x1,&y1,&z1,&x2,&y2,&z2); + star[n++] = Node(x2,y2,z2,n,1); + star[n++] = Node(x2,y1-1,z2,n,-1); + star[n++] = Node(x1-1,y2,z2,n,-1); + star[n++] = Node(x2,y2,z1-1,n,-1); + star[n++] = Node(x1-1,y1-1,z2,n,1); + star[n++] = Node(x1-1,y2,z1-1,n,1); + star[n++] = Node(x2,y1-1,z1-1,n,1); + star[n++] = Node(x1-1,y1-1,z1-1,n,-1); + } + } + for(int i = 0;i < n; i++) + res[i] = 0; + sort(star,star+n,compz); + a = 1; + star[n].z = -1; + for(int i = 0;i < n; i++){ + if(star[i].z != star[i+1].z) + star[i].z = a++; + else star[i].z = a; + } + sort(star,star+n,compn); + CDQ1(0,n-1); + sort(star,star+n,compn); + for(int i = 0;i < n; i++){ + if(star[i].t != 0){ + int ans = 0; + for(int j = 0;j < 8; j++) + ans += res[i+j]; + printf("%d\n",ans); + i += 7; + } + } + } + return 0; +} diff --git a/HDOJ/5127_autoAC.cpp b/HDOJ/5127_autoAC.cpp new file mode 100644 index 0000000..19303e8 --- /dev/null +++ b/HDOJ/5127_autoAC.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +using namespace std; +#include +typedef pair candy; +list s; +int main() +{ + int n; + while( scanf("%d",&n) && n ){ + s.clear(); + while(n--){ + long long x,y,z; + scanf("%I64d%I64d%I64d",&z,&x,&y); + candy aa ; aa.first=x; aa.second=y; + if(z==1 ) { s.push_front(aa); } + else if(z==-1) { + for(list::iterator i=s.begin();i!=s.end();i++) + if((*i).first==x&&(*i).second==y) { s.erase(i); break; } + } + else if(z==0) { + long long sum=-0x7f7f7f7f; + for(list::iterator i=s.begin();i!=s.end();i++) { + sum = max(sum, (*i).first*x+(*i).second*y); + } + cout< +#include +#include +#include +#include +#include +#include +#define MAXN 10010 +#define LL long long +using namespace std; +bool vis[MAXN][MAXN]; +struct DOT { + int x, y; + DOT() {} + DOT(int _x, int _y) { + x = _x; y = _y; + } +}dot[MAXN]; +struct Matrix { + DOT up, down; + Matrix() { } + Matrix(DOT a, DOT b) { + up = a; + down = b; + } +}maxrix[MAXN]; +int area(Matrix m) { + return (m.down.x-m.up.x)*(m.up.y-m.down.y); +} +vector vec; +bool cmp(DOT a, DOT b) { + if(a.x < b.x) + return true; + if(a.x > b.x) + return false; + return a.y <= b.y; +} +int son_judge(DOT t, Matrix m) { + int x1 = m.up.x; + int y1 = m.up.y; + int x2 = m.down.x; + int y2 = m.down.y; + if(t.x>=x1 && t.x<=x2 && t.y<=y1 && t.y>=y2) { + if(t.x>x1 && t.xy2) + return 1; + return 0; + } + return -1; +} +int judge(int i, int j) { + DOT t1 = DOT(vec[i].up.x, vec[i].down.y); + DOT t2 = DOT(vec[i].down.x, vec[i].up.y); + int ok1 = son_judge(vec[i].up, vec[j]); + int ok2 = son_judge(vec[i].down, vec[j]); + int ok3 = son_judge(t1, vec[j]); + int ok4 = son_judge(t2, vec[j]); + if(ok1==1&&ok2==1&&ok3==1&&ok4==1) + return 1; + if(ok1==-1&&ok2==-1&&ok3==-1&&ok4==-1) + return -1; + return 0; +} +int main() { + int n; + while(scanf("%d", &n) && n) { + if(n < 8) { + puts("imp"); + continue; + } + vec.clear(); + memset(vis, 0, sizeof(vis)); + for(int i=0; i=y1) continue; + if(vis[x2][y1] && vis[x1][y2]) { + vec.push_back(Matrix(dot[i], dot[j])); + } + } + } + int ans = 0; + for(int i=0; i +#include +#include +#include +#include +#include +#define eps 1e-8 +using namespace std; +#define N 100017 +struct Point{ + double x,y; + Point(double x=0, double y=0):x(x),y(y) {} + void input() { scanf("%lf%lf",&x,&y); } +}; +typedef Point Vector; +struct Circle{ + Point c; + double r; + Circle(){} + Circle(Point c,double r):c(c),r(r) {} + Point point(double a) { return Point(c.x + cos(a)*r, c.y + sin(a)*r); } + void input() { scanf("%lf%lf%lf",&c.x,&c.y,&r); } +}; +int dcmp(double x) { + if(x < -eps) return -1; + if(x > eps) return 1; + return 0; +} +template T sqr(T x) { return x * x;} +Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); } +Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); } +Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); } +Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); } +bool operator < (const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } +bool operator >= (const Point& a, const Point& b) { return a.x >= b.x && a.y >= b.y; } +bool operator <= (const Point& a, const Point& b) { return a.x <= b.x && a.y <= b.y; } +bool operator == (const Point& a, const Point& b) { return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0; } +double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; } +double Length(Vector A) { return sqrt(Dot(A, A)); } +double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); } +double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } +Vector VectorUnit(Vector x){ return x / Length(x);} +Vector Normal(Vector x) { return Point(-x.y, x.x) / Length(x);} +double angle(Vector v) { return atan2(v.y, v.x); } +bool OnSegment(Point P, Point A, Point B) { + return dcmp(Cross(A-P,B-P)) == 0 && dcmp(Dot(A-P,B-P)) < 0; +} +double DistanceToSeg(Point P, Point A, Point B) +{ + if(A == B) return Length(P-A); + Vector v1 = B-A, v2 = P-A, v3 = P-B; + if(dcmp(Dot(v1, v2)) < 0) return Length(v2); + if(dcmp(Dot(v1, v3)) > 0) return Length(v3); + return fabs(Cross(v1, v2)) / Length(v1); +} +double DistanceToLine(Point P, Point A, Point B){ + Vector v1 = B-A, v2 = P-A; + return fabs(Cross(v1,v2)) / Length(v1); +} +Point DisP(Point A, Point B){ + return Length(B-A); +} +bool SegmentIntersection(Point A,Point B,Point C,Point D) { + return max(A.x,B.x) >= min(C.x,D.x) && + max(C.x,D.x) >= min(A.x,B.x) && + max(A.y,B.y) >= min(C.y,D.y) && + max(C.y,D.y) >= min(A.y,B.y) && + dcmp(Cross(C-A,B-A)*Cross(D-A,B-A)) <= 0 && + dcmp(Cross(A-C,D-C)*Cross(B-C,D-C)) <= 0; +} +Point Zero = Point(0,0); +double TriAngleCircleInsection(Circle C, Point A, Point B) +{ + Vector OA = A-C.c, OB = B-C.c; + Vector BA = A-B, BC = C.c-B; + Vector AB = B-A, AC = C.c-A; + double DOA = Length(OA), DOB = Length(OB),DAB = Length(AB), r = C.r; + if(dcmp(Cross(OA,OB)) == 0) return 0; + if(dcmp(DOA-C.r) < 0 && dcmp(DOB-C.r) < 0) return Cross(OA,OB)*0.5; + else if(DOB < r && DOA >= r) { + double x = (Dot(BA,BC) + sqrt(r*r*DAB*DAB-Cross(BA,BC)*Cross(BA,BC)))/DAB; + double TS = Cross(OA,OB)*0.5; + return asin(TS*(1-x/DAB)*2/r/DOA)*r*r*0.5+TS*x/DAB; + } + else if(DOB >= r && DOA < r) { + double y = (Dot(AB,AC)+sqrt(r*r*DAB*DAB-Cross(AB,AC)*Cross(AB,AC)))/DAB; + double TS = Cross(OA,OB)*0.5; + return asin(TS*(1-y/DAB)*2/r/DOB)*r*r*0.5+TS*y/DAB; + } + else if(fabs(Cross(OA,OB)) >= r*DAB || Dot(AB,AC) <= 0 || Dot(BA,BC) <= 0) { + if(Dot(OA,OB) < 0) { + if(Cross(OA,OB) < 0) return (-acos(-1.0)-asin(Cross(OA,OB)/DOA/DOB))*r*r*0.5; + else return ( acos(-1.0)-asin(Cross(OA,OB)/DOA/DOB))*r*r*0.5; + } + else return asin(Cross(OA,OB)/DOA/DOB)*r*r*0.5; + } + else { + double x = (Dot(BA,BC)+sqrt(r*r*DAB*DAB-Cross(BA,BC)*Cross(BA,BC)))/DAB; + double y = (Dot(AB,AC)+sqrt(r*r*DAB*DAB-Cross(AB,AC)*Cross(AB,AC)))/DAB; + double TS = Cross(OA,OB)*0.5; + return (asin(TS*(1-x/DAB)*2/r/DOA)+asin(TS*(1-y/DAB)*2/r/DOB))*r*r*0.5 + TS*((x+y)/DAB-1); + } +} +Point p[507],A,B; +int main() +{ + int n,i,j,cs = 1; + double k; + while(scanf("%d%lf",&n,&k)!=EOF) + { + for(i=1;i<=n;i++) p[i].input(); + A.input(), B.input(), p[n+1] = p[1]; + double D = (2.0*k*k*A.x-2.0*B.x)/(1.0-k*k); + double E = (2.0*k*k*A.y-2.0*B.y)/(1.0-k*k); + double F = (B.x*B.x+B.y*B.y-k*k*(A.x*A.x+A.y*A.y))/(1.0-k*k); + Circle C = Circle(Point(-D*0.5,-E*0.5),sqrt(D*D+E*E-4.0*F)*0.5); + double ans = 0.0; + for(i=1;i<=n;i++) + ans += TriAngleCircleInsection(C, p[i], p[i+1]); + printf("Case %d: %.10f\n",cs++,fabs(ans)); + } + return 0; +} diff --git a/HDOJ/5131_autoAC.cpp b/HDOJ/5131_autoAC.cpp new file mode 100644 index 0000000..155d457 --- /dev/null +++ b/HDOJ/5131_autoAC.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +#define ll long long +const int maxn=202; +struct P +{ + string name; + int k; +}p[maxn]; +bool cmp(P a,P b) +{ + if(a.k>b.k) + return true; + else if(a.k==b.k) + { + if(a.namemp; +void find(string str) +{ + int major=1; + int minor=1; + for(int i=1;i<=n;i++) + { + if(p[i].name==str) + { + break; + } + if(p[i].k>mp[str]) + major++; + if(p[i].k==mp[str]) + minor++; + } + cout<>n&&n) + { + mp.clear(); + for(int i=1;i<=n;i++) + cin>>p[i].name>>p[i].k; + sort(p+1,p+1+n,cmp); + for(int i=1;i<=n;i++) + { + cout<>q; + while(q--) + { + cin>>str; + find(str); + } + } + return 0; +} diff --git a/HDOJ/5134_autoAC.cpp b/HDOJ/5134_autoAC.cpp new file mode 100644 index 0000000..7ffb72c --- /dev/null +++ b/HDOJ/5134_autoAC.cpp @@ -0,0 +1,114 @@ +#pragma comment(linker, "/STACK:1024000000,1024000000") +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define FIR first +#define SEC second +#define MP make_pair +#define inf 0x3f3f3f3f +#define LL long long +#define CLR(a, b) memset(a, b, sizeof(a)) +using namespace std; +struct Point +{ + double x, y; + Point(double x = 0, double y = 0) + :x(x), y(y) {} +}; +double EP = 0; +double x_mult(Point sp, Point ep, Point op){ + return (sp.x-op.x)*(ep.y-op.y)-(sp.y-op.y)*(ep.x-op.x); +} +double cross(Point a,Point b,Point c){ + return (a.x-c.x)*(b.x-c.x)+(a.y-c.y)*(b.y-c.y); +} +double dist(Point a,Point b){ + return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); +} +double cal_area(Point a,Point b,Point c,double r){ + double A,B,C,x,y,tS; + A=dist(b,c); + B=dist(a,c); + C=dist(b,a); + if(A=r){ + x=(cross(a,c,b)+sqrt(r*r*C*C-x_mult(a,c,b)*x_mult(a,c,b)))/C; + tS=x_mult(a,b,c)/2; + return asin(tS*(1-x/C)*2/r/B*(1-EP))*r*r/2+tS*x/C; + } + else if(A>=r&&B=r*C||cross(b,c,a)<=0||cross(a,c,b)<=0){ + if(cross(a,b,c)<0) + if(x_mult(a,b,c)<0) + return (-acos(-1.0)-asin(x_mult(a,b,c)/A/B*(1-EP)))*r*r/2; + else return (acos(-1.0)-asin(x_mult(a,b,c)/A/B*(1-EP)))*r*r/2; + else return asin(x_mult(a,b,c)/A/B*(1-EP))*r*r/2; + } + else{ + x=(cross(a,c,b)+sqrt(r*r*C*C-x_mult(a,c,b)*x_mult(a,c,b)))/C; + y=(cross(b,c,a)+sqrt(r*r*C*C-x_mult(b,c,a)*x_mult(b,c,a)))/C; + tS=x_mult(a,b,c)/2; + return (asin(tS*(1-x/C)*2/r/B*(1-EP))+asin(tS*(1-y/C)*2/r/A*(1-EP)))*r*r/2+tS*((y+x)/C-1); + } +} +double solve(Point p[], int n, Point cir, double r){ + double area=0; + for(int i=0;i> v0 >> v1 >> D >> T) + { + printf("Case %d: ", cas ++); + Point cir = Point(-D, 0); + double r = v0 * T; + if(v0 * T <= D) + { + printf("%.7f\n", pi * r * r); + continue; + } + double lft = D / v0, rgt = T; + for(int i = 0; i < 100; i ++) + { + double mid = (lft + rgt) / 2; + double midmid = (mid + rgt) / 2; + double _mid = sqrt((v0 * mid) * (v0 * mid) - D * D) + v1 * (T - mid); + double _midmid = sqrt((v0 * midmid) * (v0 * midmid) - D * D) + v1 * (T - midmid); + if(_mid > _midmid) rgt = midmid; + else lft = mid; + } + double t = (lft + rgt) / 2; + double y = sqrt((v0 * t) * (v0 * t) - D * D) + v1 * (T - t); + double x1 = T / t * D, y1 = sqrt(v0 * T * v0 * T - x1 * x1); + x1 -= D; + Point p[8]; + p[0] = Point(0, y); + p[1] = Point(x1, y1); + p[2] = Point(x1, -y1); + p[3] = Point(0, -y); + p[4] = Point(-x1, -y1); + p[5] = Point(-x1, y1); + p[6] = p[0]; + double ans = solve(p, 6, cir, r); + ans = pi * r * r + x1 * y1 * 4 + x1 * (y - y1) * 2 - fabs(ans); + printf("%.7f\n", ans); + } +} diff --git a/HDOJ/5135_autoAC.cpp b/HDOJ/5135_autoAC.cpp new file mode 100644 index 0000000..1735437 --- /dev/null +++ b/HDOJ/5135_autoAC.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +using namespace std; +int n, a[12]; +double dp[1<<12]; +double cal(int a, int b, int c){ + if(a+b<=c) return 0.0; + double p = (a+b+c)*0.5; + return sqrt(p*(p-a)*(p-b)*(p-c)); +} +vector V; +int main(){ + while(~scanf("%d", &n) && n){ + memset(dp, 0, sizeof(dp)); + for(int i=0; ia[k]) V.push_back(st); + } + } + } + for(int i=0; i<(1< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +#define ll __int64 +#define ull unsigned long long +#define eps 1e-8 +#define NMAX 1000000005 +#define MOD 1000000007 +#define lson l,mid,rt<<1 +#define rson mid+1,r,rt<<1|1 +#define PI acos(-1) +template +inline void scan_d(T &ret) +{ + char c; + int flag = 0; + ret=0; + while(((c=getchar())<'0'||c>'9')&&c!='-'); + if(c == '-') + { + flag = 1; + c = getchar(); + } + while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar(); + if(flag) ret = -ret; +} +ll dp[100005],sum[100005]; +void cal() +{ + ll a = 3,b = MOD-2,ret = 1; + while(b) + { + if(b&1) ret = ret*a%MOD; + a = a*a%MOD; + b >>= 1; + } + cout< +#include +#include +using namespace std; +const int N = 40; +const int inf = 1000000000; +int n, m, f[N][N], dp[N][N]; +int solve(int x){ + for(int i=1; i<=n; i++){ + for(int j=1; j<=n; j++){ + if(i==x || j==x) dp[i][j] = inf; + else dp[i][j] = f[i][j]; + } + } + for(int k=1; k<=n; k++){ + for(int i=1; i<=n; i++){ + for(int j=1; j<=n; j++) dp[i][j] = min(dp[i][j], dp[i][k]+dp[k][j]); + } + } + return dp[1][n]; +} +int main(){ + while(~scanf("%d %d", &n, &m) && (n||m)){ + for(int i=1; i<=n; i++){ + for(int j=1; j<=n; j++) f[i][j] = i==j?0:inf; + } + int a, b, c; + while(m--){ + scanf("%d %d %d", &a, &b, &c); + f[a][b] = min(f[a][b], c); + f[b][a] = f[a][b]; + } + int ans = 0; + for(int i=2; i +#include +#include +using namespace std; +int main() +{ + int n; + while(~scanf("%d",&n)) + { + if(n<3) + { + printf("%d\n",n-1); + } + else if(n<5) + { + printf("%d %d\n",n-2,n-1); + } + else if(n<8) + { + printf("%d %d %d\n",n-4,n-2,n-1); + } + else if(n<16) + { + printf("%d %d %d %d\n",n-7,n-4,n-2,n-1); + } + else + { + printf("%d %d %d %d %d\n",n-15,n-7,n-4,n-2,n-1); + } + } + return 0; +} diff --git a/HDOJ/5139_autoAC.cpp b/HDOJ/5139_autoAC.cpp new file mode 100644 index 0000000..e1b86b7 --- /dev/null +++ b/HDOJ/5139_autoAC.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include +using namespace std; +const int maxn=100002; +const int mod=1000000007; +int ans[maxn]; +vector >vc; +int n; +int cnt=0; +int main() +{ + while(scanf("%d",&n)!=EOF) + { + vc.push_back(make_pair(n,cnt++)); + } + sort(vc.begin(),vc.end()); + int MAX=vc[cnt-1].first; + long long tp=1,result=1; + int t=0; + for(int i=1;i<=MAX;i++) + { + tp=tp*i; + if(tp>=mod) + tp%=mod; + result=result*tp; + if(result>=mod) + result%=mod; + while(t +#include +#include +#include +#include +#include +using namespace std; +typedef unsigned long long ull; +typedef long long ll; +const int inf = 0x3f3f3f3f; +const double eps = 1e-8; +const int maxn = 1e5+10; +int tot,tree[maxn],c[maxn*20],lson[maxn*20],rson[maxn*20]; +ll sum[maxn*20]; +int build (int l, int r) +{ + int root = tot++; + c[root] = sum[root] = 0; + if (l != r) + { + int mid = (l + r) >> 1; + lson[root] = build(l,mid); + rson[root] = build(mid+1,r); + } + return root; +} +int MAX; +int update (int root,int pos,int val,ll sa) +{ + int newroot = tot++; + int tmp = newroot; + int l = 1, r = MAX; + c[newroot] = c[root] + val; + sum[newroot] = sum[root] + sa; + while (l < r) + { + int mid = (l + r) >> 1; + if (pos <= mid) + { + r = mid; + rson[newroot] = rson[root]; + root = lson[root]; + lson[newroot] = tot++; + newroot = lson[newroot]; + } + else + { + l = mid + 1; + lson[newroot] = lson[root]; + root = rson[root]; + rson[newroot] = tot++; + newroot = rson[newroot]; + } + c[newroot] = c[root] + val; + sum[newroot] = sum[root] + sa; + } + return tmp; +} +ll query(int root1,int root2,int l,int r,int ua,int ub) +{ + if (ua > ub) + return 0; + if (ua <= l && ub >= r) + { + return sum[root2] - sum[root1]; + } + int mid = (l + r) >> 1; + ll t1 = 0, t2 = 0; + if (ua <= mid) + t1 = query(lson[root1],lson[root2],l,mid,ua,ub); + if (ub > mid) + t2 = query(rson[root1],rson[root2],mid+1,r,ua,ub); + return t1 + t2; +} +int lev[maxn],idx1,idx2,age[maxn]; +struct worker +{ + int s,l,a; + bool operator < (const worker &rhs)const + { + return a < rhs.a; + } +} per[maxn]; +int main(void) +{ + int n; + while (~scanf ("%d",&n)) + { + tot = 0; + memset(sum,0,sizeof(sum)); + MAX = n; + int max_age = 0,min_age = inf; + for (int i = 1; i <= n; i++) + { + scanf ("%d%d%d",&per[i].s, &per[i].l, &per[i].a); + age[i-1] = per[i].a; + lev[i-1] = per[i].l; + max_age = max(max_age,age[i-1]); + min_age = min(min_age,age[i-1]); + } + sort(per+1, per+1+n); + sort(age, age+n); + sort(lev, lev+n); + idx1 = n; + idx2 = unique(lev,lev+n) - lev; + tree[0] = build(1,n); + for (int i = 1; i <= n; i++) + { + int tmp = lower_bound(lev,lev+idx2,per[i].l) - lev + 1; + tree[i] = update(tree[i-1], tmp, 1, (ll)per[i].s); + } + int Q; + ll k = 0; + scanf ("%d",&Q); + for (int i = 0; i < Q; i++) + { + ll LL,HL,LA,HA; + scanf ("%I64d%I64d%I64d%I64d",&LL,&HL,&LA,&HA); + LL += k, HL -= k; + LA += k, HA -= k; + if (LL > HL) + swap(LL, HL); + if (LA > HA) + swap(LA, HA); + LA = max(LA, (ll)0); + HA = min(HA, (ll)1000000000); + int idx_la = lower_bound(age,age+idx1,LA) - age + 1; + int idx_ha = lower_bound(age,age+idx1,HA) - age + 1; + if (age[idx_ha-1] == HA) + { + while (age[idx_ha-1] == HA && idx_ha <= n) + idx_ha ++; + idx_ha--; + } + else + idx_ha--; + LL = max(LL,(ll)0); + HL = min(HL,(ll)1000000000); + int i1 = lower_bound(lev,lev+idx2,LL) - lev + 1; + int i2 = lower_bound(lev,lev+idx2,HL) - lev + 1; + if (lev[i2-1] > HL) + i2--; + k = query(tree[idx_la-1],tree[idx_ha],1,MAX,i1,i2); + printf("%I64d\n",k); + } + } + return 0; +} diff --git a/HDOJ/5141_autoAC.cpp b/HDOJ/5141_autoAC.cpp new file mode 100644 index 0000000..7844495 --- /dev/null +++ b/HDOJ/5141_autoAC.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +#include +#define ls(p) p<<1 +#define rs(p) p<<1|1 +using namespace std; +typedef long long LL; +const int N=100010; +int a[N],b[N]; +int dp[N]; +int sta[N],en[N]; +int len,st; +struct node{ + int l,r; + int len,sta; +}tree[N<<2]; +void pushup(int p) +{ + if(tree[ls(p)].len>tree[rs(p)].len) + { + tree[p].len=tree[ls(p)].len; + tree[p].sta=tree[ls(p)].sta; + } + else if(tree[rs(p)].len>tree[ls(p)].len) + { + tree[p].len=tree[rs(p)].len; + tree[p].sta=tree[rs(p)].sta; + } + else + tree[p].sta=max(tree[ls(p)].sta,tree[rs(p)].sta); +} +void build(int p,int l,int r) +{ + tree[p].l=l;tree[p].r=r; + tree[p].len=-1; + tree[p].sta=-1; + if(l==r) + return; + int m=(l+r)>>1; + build(ls(p),l,m); + build(rs(p),m+1,r); +} +void update(int p,int pos,int len,int st) +{ + if(tree[p].l==tree[p].r) + { + if(tree[p].len==len&&tree[p].sta>1; + if(pos<=m) update(ls(p),pos,len,st); + else update(rs(p),pos,len,st); + pushup(p); +} +void query(int p,int l,int r) +{ + if(l<=tree[p].l&&tree[p].r<=r) + { + if(tree[p].len>len) + { + len=tree[p].len; + st=tree[p].sta; + } + else if(tree[p].len==len&&st>1; + if(l<=m) query(ls(p),l,r); + if(r>m) query(rs(p),l,r); +} +int main() +{ + int n; + while(scanf("%d",&n)==1) + { + LL ret=0; + int cnt; + for(int i=1;i<=n;i++) + { + scanf("%d",a+i); + b[i]=a[i]; + } + if(n==1) + { + printf("1\n"); + continue; + } + sort(b+1,b+1+n); + cnt=unique(b+1,b+1+n)-(b+1); + dp[1]=sta[1]=1; + build(1,1,cnt); + int mpp; + int lest=-1; + for(int i=1;i<=n;i++) + { + len=st=-1; + mpp=lower_bound(b+1,b+1+cnt,a[i])-b; + if(mpp==1) + { + dp[i]=1; + sta[i]=i; + } + else + query(1,1,mpp-1); + if(st==-1) + { + dp[i]=1; + sta[i]=i; + } + else + { + dp[i]=len+1; + sta[i]=st; + } + update(1,mpp,dp[i],sta[i]); + lest=max(lest,dp[i]); + } + int last=n+1; + for(int i=n;i>=1;i--) + { + if(dp[i]==lest) + { + en[i]=last-1; + last=i; + } + } + for (int i=1;i<=n;++i) + { + if (dp[i]==lest) + ret+=(LL)sta[i]*(LL)(en[i]-i+1); + } + printf("%I64d\n", ret); + } + return 0; +} diff --git a/HDOJ/5142_autoAC.cpp b/HDOJ/5142_autoAC.cpp new file mode 100644 index 0000000..eeba401 --- /dev/null +++ b/HDOJ/5142_autoAC.cpp @@ -0,0 +1,17 @@ +#include +#include +#include +int buf[40]; +void go(int n) { + int i, j = 0, res = 0; + memset(buf, 0, sizeof(buf)); + do buf[j++] = n % 2; while (n /= 2); + for (i = j - 1; ~i; i--) res += buf[i] * (int)pow(2, j - i - 1); + printf("%d\n", res); +} +int main() { + int t, n; + scanf("%d", &t); + while (t--) scanf("%d", &n), go(n); + return 0; +} diff --git a/HDOJ/5143_autoAC.cpp b/HDOJ/5143_autoAC.cpp new file mode 100644 index 0000000..1c97dc2 --- /dev/null +++ b/HDOJ/5143_autoAC.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; +int a1,a2,a3,a4; +bool good(int i,int j,int k) +{ + if(((a1-i-k==0)||(a1-i-k>=3))&&((a2-i-j-k==0)||(a2-i-j-k>=3))&&((a3-i-j-k==0)||(a3-i-j-k>=3))&&((a4-j-k==0)||(a4-j-k>=3)))return 1; + else return 0; +} +int main() +{ + int n; + int i,j,k; + int flag; + cin>>n; + while(n--) + { + cin>>a1>>a2>>a3>>a4; + flag=0; + for(i=0;i<=2;i++) + for(j=0;j<=2;j++) + for(k=0;k<=2;k++) + if(good(i,j,k)){flag=1;break;} + if(flag)cout<<"Yes"< +#include +#include +#include +using namespace std; +const double pi=acos(-1.0); +const double g=9.8; +double v,h; +double ans(double a) +{ + double a1=v*v*sin(a)*sin(a); + double a2=2*g*h; + a1=a1+a2; + a1=sqrt(a1); + a1=a1/g; + a1=a1+v*sin(a)/g; + a1=a1*v*cos(a); + return a1; +} +int main() +{ + int T; + scanf("%d",&T); + while (T--) + { + scanf("%lf%lf",&h,&v); + double l=0; + double r=pi/2; + while (r-l>=0.0000001) + { + double m=(l+r)/2; + double mm=(m+r)/2; + if (ans(m) +#include +#include +#include +#include +using namespace std; +const int maxn = 30300; +const int MOD = 1000000007; +int MID; +int a[maxn], p[maxn]; +int cnt[maxn]; +struct query +{ + int l, r, id; + bool operator < (const query& rhs) const + { + return l / MID < rhs.l / MID || (l / MID == rhs.l / MID && r < rhs.r); + } +}q[maxn]; +long long Inv(long long x, long long mod) +{ + long long r, y; + for(r = 1, y = mod - 2; y; x = x * x % mod, y >>= 1) + (y & 1) && (r = r * x % mod); + return r; +} +int inv[maxn]; +void init() +{ + inv[0] = 1; + for(int i = 1; i < maxn; i ++) + { + inv[i] = Inv(i, MOD); + } +} +int main() +{ + int T, n, m; + init(); + scanf("%d", &T); + while(T --) + { + scanf("%d%d", &n, &m); + for(int i = 1; i <= n; i ++) + scanf("%d", &a[i]); + a[0] = 0; + for(int i = 0; i < m; i ++) + { + scanf("%d%d", &q[i].l, &q[i].r); + q[i].id = i; + } + MID = sqrt(n + 0.0); + sort(q, q + m); + int L = 1, R = 1; + int ans = 1, tot = 1; + memset(cnt, 0, sizeof(cnt)); + cnt[a[1]] ++; + for(int i = 0; i < m; i ++) + { + while(R < q[i].r) + { + R ++; tot ++; cnt[a[R]] ++; + ans = 1ll * ans * inv[cnt[a[R]]] % MOD; + ans = 1ll * ans * tot % MOD; + } + while(R > q[i].r) + { + ans = 1ll * ans * cnt[a[R]] % MOD; + ans = 1ll * ans * inv[tot] % MOD; + R --; tot --; cnt[a[R + 1]] --; + } + while(L < q[i].l) + { + ans = 1ll * ans * cnt[a[L]] % MOD; + ans = 1ll * ans * inv[tot] % MOD; + L ++; tot --; cnt[a[L - 1]] --; + } + while(L > q[i].l) + { + L --; tot ++; cnt[a[L]] ++; + ans = 1ll * ans * inv[cnt[a[L]]] % MOD; + ans = 1ll * ans * tot % MOD; + } + p[q[i].id] = ans; + } + for(int i = 0; i < m; i ++) + { + printf("%d\n", p[i]); + } + } +} diff --git a/HDOJ/5146_autoAC.cpp b/HDOJ/5146_autoAC.cpp new file mode 100644 index 0000000..9d27c5b --- /dev/null +++ b/HDOJ/5146_autoAC.cpp @@ -0,0 +1,34 @@ +#include +#include +#define maxn 1000+5 +using namespace std; +int n; +int mapp[maxn]; +int main() +{ + int t; + cin>>t; + while(t--) + { + memset(mapp,0,sizeof(mapp)); + cin>>n; + int j=0,o=0; + for(int i=1;i<=n;i++) + { + cin>>mapp[i]; + if(i%2) j+=mapp[i]; + else o+=mapp[i]; + } + int flag=0; + for(int i=1;i<=n/2;i++) + { + if(mapp[i]!=mapp[n-i+1]) + { + flag=1;break; + } + } + if(!flag||j!=o) cout<<"No"< +#include +#include +#include +using namespace std; +const int maxn=50010; +int a[maxn]; +int pre[maxn],suf[maxn]; +int n; +int c[maxn]; +int lowbit(int x) +{ + return x&(-x); +} +void update(int i,int x) +{ + while(i<=n) + { + c[i]+=x; + i+=lowbit(i); + } +} +int sum(int i) +{ + int s=0; + while(i>0) + { + s+=c[i]; + i-=lowbit(i); + } + return s; +} +int main() +{ + int t; + scanf("%d",&t); + while(t--) + { + scanf("%d",&n); + for(int i=1;i<=n;i++) + { + scanf("%d",&a[i]); + } + memset(c,0,sizeof(c)); + for(int i=1;i<=n;i++) + { + pre[i]=sum(a[i]); + update(a[i],1); + } + memset(c,0,sizeof(c)); + for(int i=n;i>=1;i--) + { + suf[i]=(n-i)-sum(a[i]); + update(a[i],1); + } + long long ans=0,dp=0; + for(int i=1;i<=n-1;i++) + { + ans+=dp*suf[i]; + dp+=pre[i]; + } + printf("%lld\n",ans); + } + return 0; +} diff --git a/HDOJ/5148_autoAC.cpp b/HDOJ/5148_autoAC.cpp new file mode 100644 index 0000000..294456c --- /dev/null +++ b/HDOJ/5148_autoAC.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +using namespace std; +#define maxn 2300 +#define ll long long +const long long inf = 200000 * 10000000LL; +int n,k; +ll dp[maxn][120]; +ll tmp[120]; +struct edge +{ + int u,v,w,next; +}e[maxn*10]; +int head[maxn],cnt; +void add(int u,int v,int w) +{ + e[cnt].u=u; + e[cnt].v=v; + e[cnt].w=w; + e[cnt].next=head[u]; + head[u]=cnt++; +} +void dfs(int u,int fa) +{ + for(int i=head[u];i!=-1;i=e[i].next) + { + int v=e[i].v; + if(v==fa) continue; + dfs(v,u); + for(int j=0;j<=k;j++) tmp[j]=dp[u][j]; + for(int j=0;j<=k;j++) + for(int t=0;t<=j;t++) + tmp[j]=min(tmp[j],dp[u][j-t]+dp[v][t]+(k-t)*t*e[i].w*2); + for(int j=0;j<=k;j++) dp[u][j]=tmp[j]; + } +} +int main() +{ + int cas; + scanf("%d",&cas); + while(cas--) + { + memset(head,-1,sizeof(head)); + cnt=0; + scanf("%d%d",&n,&k); + for(int i=1;i<=n;i++) + for(int j=0;j<=k;j++) + { + if(j<=1) dp[i][j]=0; + else dp[i][j]=inf; + } + for(int i=1;i +#include +#include +#include +#define MAX 1007 +using namespace std; +int n; +int num; +int sum; +int gcd ( int a , int b ) +{ + return b==0?a:gcd ( b , a%b ); +} +int main ( ) +{ + while ( ~scanf ( "%d" , &n ) ) + { + sum = 0; + for ( int i = 0 ; i < n ; i ++ ) + { + bool flag = true; + scanf ( "%d" , &num ); + for ( int i = 2 ; i < num ; i ++ ) + if ( gcd ( num , i ) > 1 ) + { + flag = false; + break; + } + if ( flag ) sum+=num; + } + printf ( "%d\n" , sum ); + } +} diff --git a/HDOJ/5151_autoAC.cpp b/HDOJ/5151_autoAC.cpp new file mode 100644 index 0000000..6f912fc --- /dev/null +++ b/HDOJ/5151_autoAC.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +#define INF 1000000000 +#define N 111 +typedef __int64 LL; +const LL mod = 1000000007; +LL dp[N][N], C[N][N]; +int n, num[N]; +void init() { + for(int i = 0; i < N; i++) { + for(int j = 0; j <= i; j++) { + if(j == 0 || j == i) C[i][j] = 1; + else C[i][j] = (C[i-1][j-1] + C[i-1][j]) % mod; + } + } +} +LL dfs(int L, int R) { + if(dp[L][R] != -1) return dp[L][R]; + if(L == R) return dp[L][R] = 1; + dp[L][R] = (dfs(L+1, R) + dfs(L, R - 1)) % mod; + for(int i = L+1; i <= R-1; i ++) { + if(num[i-1] != num[i+1]) continue; + dp[L][R] = (dp[L][R] + (dfs(L, i - 1) * dfs(i+1, R) % mod) * C[R- L][i - L]%mod) % mod; + } + return dp[L][R]; +} +int main() { + init(); + while(scanf("%d", &n) != EOF) { + memset(dp, -1, sizeof(dp)); + for(int i = 1;i <= n; i++) scanf("%d", &num[i]); + printf("%I64d\n", dfs(1, n)); + } + return 0; +} diff --git a/HDOJ/5152_autoAC.cpp b/HDOJ/5152_autoAC.cpp new file mode 100644 index 0000000..0031e05 --- /dev/null +++ b/HDOJ/5152_autoAC.cpp @@ -0,0 +1,134 @@ +#include +#include +#include +using namespace std ; +typedef long long LL ; +#define rep( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i ) +#define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i ) +#define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i ) +#define clr( a , x ) memset ( a , x , sizeof a ) +#define cpy( a , x ) memcpy ( a , x , sizeof a ) +#define ls ( o << 1 ) +#define rs ( o << 1 | 1 ) +#define lson ls , l , m +#define rson rs , m + 1 , r +#define rt o , l , r +#define root 1 , 1 , n +#define mid ( ( l + r ) >> 1 ) +const int MAXN = 100005 ; +const int MAXE = 100005 ; +const int mod = 2333333 ; +struct Edge { + LL v ; + int n ; + Edge () {} + Edge ( LL v , int n ) : v ( v ) , n ( n ) {} +} ; +int mo[19] = { 2333333 , 2196720 , 580608 , 165888 , 55296 , 18432 , 6144 , 2048 , 1024 , 512 , 256 , 128 , 64 , 32 , 16 , 8 , 4 , 2 , 1 } ; +Edge E[MAXE] ; +int H[MAXN] , cntE ; +int sum[MAXN << 2] ; +int len[MAXN << 2] ; +LL add[MAXN << 2] ; +int n , m ; +void clear () { + cntE = 0 ; + clr ( H , -1 ) ; +} +void addedge ( int u , LL v ) { + E[cntE] = Edge ( v , H[u] ) ; + H[u] = cntE ++ ; +} +int pow ( int a , int b , int mod ) { + int res = 1 ; + while ( b ) { + if ( b & 1 ) res = ( LL ) res * a % mod ; + a = ( LL ) a * a % mod ; + b >>= 1 ; + } + return res % mod ; +} +void pushdown ( int o ) { + if ( add[o] ) { + add[ls] += add[o] ; + add[rs] += add[o] ; + sum[ls] = ( sum[ls] + add[o] % mod * len[ls] ) % mod ; + sum[rs] = ( sum[rs] + add[o] % mod * len[rs] ) % mod ; + add[o] = 0 ; + } +} +void build ( int o , int l , int r ) { + add[o] = 0 ; + len[o] = r - l + 1 ; + if ( l == r ) { + scanf ( "%d" , &sum[o] ) ; + addedge ( l , sum[o] ) ; + sum[o] %= mod ; + return ; + } + int m = mid ; + build ( lson ) ; + build ( rson ) ; + sum[o] = ( sum[ls] + sum[rs] ) % mod ; +} +void update ( int L , int R , int v , int o , int l , int r ) { + if ( L <= l && r <= R ) { + add[o] += v ; + sum[o] = ( sum[o] + ( LL ) len[o] * v ) % mod ; + return ; + } + int m = mid ; + pushdown ( o ) ; + if ( L <= m ) update ( L , R , v , lson ) ; + if ( m < R ) update ( L , R , v , rson ) ; + sum[o] = ( sum[ls] + sum[rs] ) % mod ; +} +int calc ( int i , int x ) { + if ( x == 19 ) return 0 ; + LL tmp = E[i].n == -1 ? E[i].v : pow ( 2 , calc ( E[i].n , x + 1 ) , mo[x] ) + E[i].v ; + return tmp < mo[x] ? tmp : tmp % mo[x] + mo[x] ; +} +void modify ( int x , int o , int l , int r ) { + if ( l == r ) { + E[H[l]].v += add[o] ; + add[o] = 0 ; + addedge ( l , 0 ) ; + sum[o] = calc ( H[l] , 0 ) % mod ; + return ; + } + int m = mid ; + pushdown ( o ) ; + if ( x <= m ) modify ( x , lson ) ; + else modify ( x , rson ) ; + sum[o] = ( sum[ls] + sum[rs] ) % mod ; +} +int query ( int L , int R , int o , int l , int r ) { + if ( L <= l && r <= R ) return sum[o] ; + int m = mid ; + pushdown ( o ) ; + if ( R <= m ) return query ( L , R , lson ) ; + if ( m < L ) return query ( L , R , rson ) ; + return ( query ( L , R , lson ) + query ( L , R , rson ) ) % mod ; +} +void solve () { + int op , l , r , x ; + clear () ; + build ( root ) ; + while ( m -- ) { + scanf ( "%d" , &op ) ; + if ( op == 1 ) { + scanf ( "%d%d" , &l , &r ) ; + printf ( "%d\n" , query ( l , r , root ) ) ; + } else if ( op == 2 ) { + scanf ( "%d" , &x ) ; + modify ( x , root ) ; + } else { + scanf ( "%d%d%d" , &l , &r , &x ) ; + update ( l , r , x , root ) ; + } + } +} +int main () { + while ( ~scanf ( "%d%d" , &n , &m ) ) solve () ; + return 0 ; +} diff --git a/HDOJ/5154_autoAC.cpp b/HDOJ/5154_autoAC.cpp new file mode 100644 index 0000000..4d55ca7 --- /dev/null +++ b/HDOJ/5154_autoAC.cpp @@ -0,0 +1,27 @@ +#include +#include +#include +#include +using namespace std; +int main() +{ + int n, m, u, v, dp[110][110]; + while(~scanf("%d%d", &n,&m)) + { + memset(dp, 0, sizeof(dp)); + for(int i=1; i<=m; i++) + { + scanf("%d%d", &v,&u); + dp[u][v] = 1; + } + for(int k=1; k<=n; k++) + for(int i=1; i<=n; i++) + for(int j=1; j<=n; j++) + dp[i][j] = max(dp[i][j], dp[i][k]&dp[k][j]); + int flag = 0; + for(int i=1; i<=n; i++) + if(dp[i][i] == 1) flag = 1; + printf(!flag ? "YES\n" : "NO\n"); + } + return 0; +} diff --git a/HDOJ/5155_autoAC.cpp b/HDOJ/5155_autoAC.cpp new file mode 100644 index 0000000..a8ebc45 --- /dev/null +++ b/HDOJ/5155_autoAC.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include +#define Mod 1000000007 +#define lll __int64 +using namespace std; +lll c[55][55]; +lll dp[55][55]; +void calc() +{ + for(int i=0;i<=51;i++) + { + c[i][0] = 1; + for(int j=1;j<=i;j++) + c[i][j] = (c[i-1][j-1]+c[i-1][j])%Mod; + } +} +int main() +{ + int n,m,i,j,k,t; + calc(); + while(scanf("%d%d",&n,&m)!=EOF) + { + memset(dp,0,sizeof(dp)); + dp[0][0] = 1; + for(i=1;i<=n;i++) + { + for(k=1;k<=m;k++) + { + for(j=0;j<=k;j++) + { + for(t=max(1,k-j);t<=k;t++) + { + dp[i][k] = (dp[i][k]+dp[i-1][j]*c[m-j][k-j]%Mod*c[j][t-(k-j)]%Mod)%Mod; + } + } + } + } + printf("%I64d\n",dp[n][m]%Mod); + } + return 0; +} diff --git a/HDOJ/5156_autoAC.cpp b/HDOJ/5156_autoAC.cpp new file mode 100644 index 0000000..6cac056 --- /dev/null +++ b/HDOJ/5156_autoAC.cpp @@ -0,0 +1,109 @@ +#include +#include +#include +using namespace std ; +typedef long long LL ; +#define rep( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i ) +#define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i ) +#define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i ) +#define clr( a , x ) memset ( a , x , sizeof a ) +#define cpy( a , x ) memcpy ( a , x , sizeof a ) +const int MAXN = 50005 ; +const int MAXE = 2100005 ; +struct Edge { + int v , n ; + Edge () {} + Edge ( int v , int n ) : v ( v ) , n ( n ) {} +} ; +Edge E[MAXE] ; +int H[MAXN] , cntE ; +int A[MAXN] , B[MAXN << 1] , Q[MAXN] ; +int vis[MAXN] ; +int num[MAXN] ; +int p[MAXN] ; +int n , m ; +void clear () { + cntE = 1 ; + clr ( A , 0 ) ; + clr ( B , 0 ) ; + clr ( Q , 0 ) ; + clr ( H , 0 ) ; + clr ( vis , 0 ) ; + clr ( num , 0 ) ; +} +int find ( int x ) { + int tmp , ans , o = x ; + while ( p[x] != x ) x = p[x] ; + ans = x , x = o ; + while ( p[x] != x ) { + tmp = p[x] ; + p[x] = ans ; + x = tmp ; + } + return ans ; +} +void addedge ( int u , int v , int H[] ) { + E[cntE] = Edge ( v , H[u] ) ; + H[u] = cntE ++ ; +} +void dfs ( int u , int f = 0 ) { + for ( int i = A[u] ; i ; i = E[i].n ) addedge ( E[i].v , u , B ) ; + for ( int i = H[u] ; i ; i = E[i].n ) if ( E[i].v != f ) dfs ( E[i].v , u ) ; +} +void tarjan ( int u , int f = 0 ) { + p[u] = u ; + vis[u] = 1 ; + for ( int i = Q[u] ; i ; i = E[i].n ) if ( vis[E[i].v] ) -- num[find ( E[i].v )] ; + for ( int i = H[u] ; i ; i = E[i].n ) { + int v = E[i].v ; + if ( vis[v] ) continue ; + tarjan ( v , u ) ; + p[v] = u ; + } + for ( int i = H[u] ; i ; i = E[i].n ) { + int v = E[i].v ; + if ( v == f ) continue ; + num[u] += num[v] ; + } +} +void scanf ( int& x , char c = 0 ) { + while ( ( c = getchar () ) < '0' ) ; + x = c - '0' ; + while ( ( c = getchar () ) >= '0' ) x = x * 10 + c - '0' ; +} +void solve () { + int u , v ; + clear () ; + rep ( i , 1 , n ) { + scanf ( u ) ; + scanf ( v ) ; + addedge ( u , v , H ) ; + addedge ( v , u , H ) ; + } + For ( i , 1 , m ) { + scanf ( u ) ; + scanf ( v ) ; + addedge ( u , v , A ) ; + ++ num[u] ; + } + dfs ( 1 ) ; + For ( i , 1 , 100000 ) if ( B[i] ) { + u = 0 ; + for ( int j = B[i] ; j ; j = E[j].n ) { + v = E[j].v ; + if ( u ) { + if ( u != v ) { + addedge ( u , v , Q ) ; + addedge ( v , u , Q ) ; + } else -- num[v] ; + } + u = v ; + } + } + tarjan ( 1 ) ; + For ( i , 1 , n ) printf ( "%d%c" , num[i] , i < n ? ' ' : '\n' ) ; +} +int main () { + while ( ~scanf ( "%d%d" , &n , &m ) ) solve () ; + return 0 ; +} diff --git a/HDOJ/5157_autoAC.cpp b/HDOJ/5157_autoAC.cpp new file mode 100644 index 0000000..e62bb1b --- /dev/null +++ b/HDOJ/5157_autoAC.cpp @@ -0,0 +1,79 @@ +#include +#include +#include +using namespace std ; +typedef long long LL ; +#define rep( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i ) +#define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i ) +#define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i ) +#define clr( a , x ) memset ( a , x , sizeof a ) +#define cpy( a , x ) memcpy ( a , x , sizeof a ) +#define ls ( o << 1 ) +#define rs ( o << 1 | 1 ) +#define lson ls , l , m +#define rson rs , m + 1 , r +#define rt o , l , r +#define root 1 , 1 , n +#define mid ( ( l + r ) >> 1 ) +const int MAXN = 100005 ; +const int N = 26 ; +struct Palindromic_Tree { + int next[MAXN][N] ; + int fail[MAXN] ; + int len[MAXN] ; + int num[MAXN] ; + int S[MAXN] , n ; + int last ; + int p ; + int newnode ( int l ) { + rep ( i , 0 , N ) next[p][i] = 0 ; + num[p] = 0 ; + len[p] = l ; + return p ++ ; + } + void init () { + p = 0 ; + newnode ( 0 ) ; + newnode ( -1 ) ; + last = 1 ; + n = 0 ; + S[0] = -1 ; + fail[0] = 1 ; + } + int get_fail ( int v ) { + while ( S[n - len[v] - 1] != S[n] ) v = fail[v] ; + return v ; + } + int add ( int c ) { + c -= 'a' ; + S[++ n] = c ; + int cur = get_fail ( last ) ; + if ( !next[cur][c] ) { + int now = newnode ( len[cur] + 2 ) ; + fail[now] = next[get_fail ( fail[cur] )][c] ; + next[cur][c] = now ; + num[now] = num[fail[now]] + 1 ; + } + last = next[cur][c] ; + return num[last] ; + } +} ; +Palindromic_ T ; +char s[MAXN] ; +LL sum[MAXN] ; +int n ; +LL ans ; +void solve () { + ans = 0 ; + T.init () ; + n = strlen ( s ) ; + sum[n] = 0 ; + rev ( i , n - 1 , 0 ) sum[i] = sum[i + 1] + T.add ( s[i] ) ; + T.init () ; + rep ( i , 0 , n ) ans += sum[i + 1] * T.add ( s[i] ) ; + printf ( "%I64d\n" , ans ) ; +} +int main () { + while ( ~scanf ( "%s" , s ) ) solve () ; + return 0 ; +} diff --git a/HDOJ/5158_autoAC.cpp b/HDOJ/5158_autoAC.cpp new file mode 100644 index 0000000..485b70a --- /dev/null +++ b/HDOJ/5158_autoAC.cpp @@ -0,0 +1,15 @@ +#include +int main() +{ + int n,m,i,j; + while(scanf("%d %d",&n,&m)!=EOF) + { + for(i=0,j=-1;i +#include +#include +#include +#include +using namespace std; +int main() +{ + int T,a,b; + scanf("%d",&T); + for (int i=1;i<=T;i++) + { + scanf("%d%d",&a,&b); + double a1=1-pow(1-1.0/a,b); + double x=a; + double ans=x*(x+1)/2*a1; + printf("Case #%d: %.3lf\n",i,ans); + } +} diff --git a/HDOJ/5160_autoAC.cpp b/HDOJ/5160_autoAC.cpp new file mode 100644 index 0000000..85d3ca7 --- /dev/null +++ b/HDOJ/5160_autoAC.cpp @@ -0,0 +1,92 @@ +#include +#include +#include +#include +#include +using namespace std; +#define LL __int64 +const int N=100005; +const int MOD=1000000007; +LL Ext_gcd(LL a,LL b,LL &x,LL &y){ + if(b==0) { x=1, y=0; return a; } + LL ret= Ext_gcd(b,a%b,y,x); + y-= a/b*x; + return ret; +} +LL Inv(LL a,int m){ + LL d,x,y,t= (LL)m; + d= Ext_gcd(a,t,x,y); + if(d==1) return (x%t+t)%t; + return -1; +} +LL Cm(LL n, LL m, LL p){ + LL a=1, b=1; + if(m>n) return 0; + while(m){ + a=(a*n)%p; + b=(b*m)%p; + m--; + n--; + } + return (LL)a*Inv(b,p)%p; +} +struct Dt{ + LL x; + int cnt; + Dt(){} + Dt(LL _x,int _c){ + x=_x; + cnt=_c; + } +}; +LL a[N],fro[N],bac[N]; +int main(){ + int T; + int n,cas=0; + scanf("%d",&T); + while(T--){ + memset(fro,0,sizeof(fro)); + memset(bac,0,sizeof(bac)); + scanf("%d",&n); + for(int i=0;i=0;--i){ + ++len; + if(a[i]==a[i+1]){ + ++cnt; + cnm=cnm*len%MOD*Inv(cnt,MOD)%MOD; + bac[i]=pre*cnm%MOD; + } + else{ + cnt=1; + cnm=len; + pre=bac[i+1]; + bac[i]=pre*cnm%MOD; + } + } + bac[n]=1; + LL ans=0; + for(int i=0;i +#include +#include +using namespace std; +bool cmp(int a,int b) +{ + return a>b; +} +int main() +{ + int t,n,a,b,c; + int q[5],p[5]; + scanf("%d",&t); + while(t--) + { + scanf("%d",&n); + for(int i=0;i +#include +using namespace std; +__int64 tt[100005]; +__int64 sum[100005]; +int main() +{ + int t; + scanf("%d",&t); + int n,m; + int x,y; + while(t--) + { + scanf("%d %d",&n,&m); + tt[1]=tt[0]=0; + for(int i=2;i<=n;i++) + { + scanf("%I64d",&tt[i]); + tt[i]=tt[i-1]+tt[i]; + } + for(int i=1;i<=m;i++) + { + scanf("%d %d",&x,&y); + int now=(i-1)%n+1; + if(now<=x && xx && x +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +#define INF 1e9 +#define inf (-((LL)1<<40)) +#define lson k<<1, L, mid +#define rson k<<1|1, mid+1, R +#define mem0(a) memset(a,0,sizeof(a)) +#define mem1(a) memset(a,-1,sizeof(a)) +#define mem(a, b) memset(a, b, sizeof(a)) +#define FOPENIN(IN) freopen(IN, "r", stdin) +#define FOPENOUT(OUT) freopen(OUT, "w", stdout) +#define rep(i, a, b) for(int i = a; i <= b; i ++) +template T CMP_MIN(T a, T b) { return a < b; } +template T CMP_MAX(T a, T b) { return a > b; } +template T MAX(T a, T b) { return a > b ? a : b; } +template T MIN(T a, T b) { return a < b ? a : b; } +template T GCD(T a, T b) { return b ? GCD(b, a%b) : a; } +template T LCM(T a, T b) { return a / GCD(a,b) * b; } +typedef __int64 LL; +const int MAXN = 110000; +const int MAXM = 1000006; +const double eps = 1e-10; +const LL MOD = 1000000007; +int T, n, m, s, k, x, y; +int val[MAXM], f[MAXM], last[MAXM]; +LL ans; +struct Node { + int up, down; + Node(){} + Node(int _up, int _down) { + int g = GCD(_up, _down); + up = _up / g; + down = _down / g; + } + bool operator == (const Node& A) const { + return up == A.up && down == A.down; + } + bool operator != (const Node& A) const { + return up != A.up || down != A.down; + } + bool operator < (const Node& A) const { + if(up != A.up) return up < A.up; + return down < A.down; + } +}; +map >ch; +vectore[MAXM]; +Node fa[MAXN], chi[MAXN]; +void init() +{ + s = 1; ans = 0; + ch.clear(); e[0].clear(); + mem0(val); mem0(f); mem0(last); +} +void input(int n, Node* a) +{ + scanf("%d", &x); + for(int i = 1; i < n; i ++) { + scanf("%d", &y); + a[i - 1] = Node(x, y); + x = y; + } +} +void addEdge() +{ + int p = 0; + for(int i = 0; i < k - 1; i ++) { + Node u = chi[i]; + if(ch[p][u] == 0) { + e[p].push_back(u); + val[s] = 0; + e[s].clear(); + ch[p][u] = s++; + } + p = ch[p][u]; + } + val[p] ++; +} +void getFail() +{ + queueq; + for(int i = 0; i < e[0].size(); i ++) { + int u = ch[0][e[0][i]]; + f[u] = last[u] = 0; + q.push(u); + } + while(!q.empty()) { + int r = q.front(); q.pop(); + for(int i = 0; i < e[r].size(); i ++) { + Node c = e[r][i]; + int u = ch[r][c]; + if(!u) { + ch[r][c] = ch[f[r]][c]; + continue; + } + q.push(u); + int v = f[r]; + while(v && !ch[v][c]) v = f[v]; + f[u] = ch[v][c]; + last[u] = val[f[u]] ? f[u] : last[f[u]]; + val[u] += val[last[u]]; + } + } +} +void getNext() +{ + f[0] = f[1] = 0; + for(int i = 1; i < k - 1; i ++) { + int j = f[i]; + while(j && chi[i] != chi[j]) j = f[j]; + f[i + 1] = chi[i] == chi[j] ? j + 1 : 0; + } +} +LL KMP() +{ + if(k == 1) return ans; + getNext(); + int j = 0; + for(int i = 0; i < n - 1; i ++) { + while(j && chi[j]!=fa[i]) j = f[j]; + if(chi[j] == fa[i]) j ++; + if(j == k - 1) ans ++; + } + return ans; +} +LL ACT() +{ + getFail(); + int j = 0; + for(int i = 0; i < n - 1; i ++) { + Node c = fa[i]; + while(j && !ch[j][c]) j = f[j]; + j = ch[j][c]; + ans += val[j]; + } + return ans; +} +int main() +{ + while(~scanf("%d", &T)) while(T--) { + scanf("%d %d", &n, &m); + init(); + input(n, fa); + for(int i = 1; i <= m; i ++) { + scanf("%d", &k); + input(k, chi); + if(k > 1) addEdge(); + else ans += n; + } + cout<< (m == 1 ? KMP() : ACT()) << endl; + } + return 0; +} diff --git a/HDOJ/5165_autoAC.cpp b/HDOJ/5165_autoAC.cpp new file mode 100644 index 0000000..f1a6f39 --- /dev/null +++ b/HDOJ/5165_autoAC.cpp @@ -0,0 +1,99 @@ +#include +#include +#include +#include +#include +#include +using namespace std; +#define MP make_pair +#define PB push_back +const int N=10005; +vector mp[N],rv[N]; +map< pair,int > vis; +int f[N],p2i[N][N]; +int dp[N*N][2]; +int n,m; +struct Dt{ + int v,turn; + Dt(){} + Dt(int _v,int _turn){ v=_v; turn=_turn; } +}; +queue
que; +void init(){ + int id=0; + for(int i=1;i<=n;++i) + for(int j=i;j<=n;++j) + p2i[j][i]=p2i[i][j]=id++; + for(int i=0;i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define maxn 10000+5 +#define ull unsigned long long +#define ll long long +#define reP(i,n) for(i=1;i<=n;i++) +#define rep(i,n) for(i=0;ib; +}; +int a[1004]; +int b[2]; +int main() +{ + int t,n,mark; + cin>>t; + while(t--) + { + mark=0; + cle(a); + cin>>n; + for(int i=1;i<=n;i++) + cin>>a[i]; + sort(a+1,a+n+1); + cle(b); + int j=0; + for(int i=n-1;i>=1;i--) + { + if(a[i+1]-a[i]==3) + { + mark=1;cout<2)cout<1)cout< +#include +#include +long long fib[50], ans[50]; +int num, flag, sum, cnt; +int DFS(int n, int p) { + if (n == 1) { + flag = 1; + return 1; + } + for (int i = p; i < cnt; i++) { + if (n % ans[i] == 0 && DFS(n / ans[i], i)) { + flag = 1; + return 1; + } + } + return 0; +} +int main() { + fib[0] = 0; + fib[1] = 1; + for (int i = 2; i < 46; i++) { + fib[i] = fib[i - 1] + fib[i - 2]; + } + int T; + scanf("%d", &T); + while (T--) { + scanf("%d", &num); + flag = cnt = 0; + if (num == 0) { + printf("Yes\n"); + continue; + } + for (int i = 3; fib[i] <= num; i++) { + if (num == fib[i]) { + flag = 1; + break; + } + if (num % fib[i] == 0) { + ans[cnt++] = fib[i]; + } + } + if (!flag) DFS(num, 0); + if (flag) printf("Yes\n"); + else printf("No\n"); + } + return 0; +} diff --git a/HDOJ/5168_autoAC.cpp b/HDOJ/5168_autoAC.cpp new file mode 100644 index 0000000..d3d5619 --- /dev/null +++ b/HDOJ/5168_autoAC.cpp @@ -0,0 +1,86 @@ +#include +#include +#include +#include +#include +#include +#define LL __int64 +using namespace std; +const LL INF=(LL)0x3f3f3f3f*0x3f3f3f3f; +const int N=100010; +const int M=200020; +struct TEdge{ + int u,v,w; + bool operator<(const TEdge& a)const { + if(wpoint[N]; +int n,m,k; +void addedge(int u,int v,int w){ + Cedge[tot].u=u; + Cedge[tot].v=v; + Cedge[tot].w=w; + tot++; +} +void Push_into(int p,Point t){ + point[p].push_back(t); +} +bool cmp(Point a,Point b){ + if(a.lw==b.lw) return a.alw>b.alw; + return a.lwCedge[i].w){ + Push_into(v,Point(Cedge[i].w,Cedge[i].w)); + } + } + continue; + } + if(point[u].empty()) continue; + else { + int p=upper_bound(point[u].begin(),point[u].end(),Point(Cedge[i].w-k,-INF),cmp)-point[u].begin(); + if(p==0) continue; + else if(p>0 && ppoint[u][p].alw+(LL)Cedge[i].w) + Push_into(v,Point(Cedge[i].w,point[u][p].alw+(LL)Cedge[i].w)); + } + } + if(point[n].empty()) + printf("-1\n"); + else printf("%I64d\n",point[n][point[n].size()-1].alw); + } + return 0; +} diff --git a/HDOJ/5170_autoAC.cpp b/HDOJ/5170_autoAC.cpp new file mode 100644 index 0000000..a68eeb5 --- /dev/null +++ b/HDOJ/5170_autoAC.cpp @@ -0,0 +1,25 @@ +#include +#include +#define eps 1e-12 +int main() +{ + double a,b,c,d; + while(scanf("%lf%lf%lf%lf",&a,&b,&c,&d)!=EOF) + { + double s,s1; + s=b*log(a); + s1=d*log(c); + if(a==1 &&c==1) + { + printf("=\n"); + continue; + } + if(fabs(s-s1)s1) + printf(">\n"); + else if(s +#include +#include +using namespace std; +#define LL _int64 +#define mod 10000007 +int a[100010]; +struct matrix +{ + LL m[3][3]; +}; +matrix mul(matrix x, matrix y) +{ + matrix temp; + memset(temp.m, 0, sizeof(temp.m)); + int i, j, k; + for (i = 0; i < 3; i++) + { + for (j = 0; j < 3; j++) + { + if (x.m[i][j] == 0) + { + continue; + } + for (k = 0; k < 3; k++) + { + if (y.m[j][k] == 0) + { + continue; + } + temp.m[i][k] = (temp.m[i][k] + (x.m[i][j] * y.m[j][k]) % mod) % mod; + } + } + } + return temp; +} +matrix quickpow(matrix a, int n) +{ + matrix res; + int i; + memset(res.m, 0, sizeof(res.m)); + for (i = 0; i < 3; i++) + { + res.m[i][i] = 1; + } + while (n) + { + if (n & 1) + { + res = mul(res,a); + } + n >>= 1; + a = mul(a, a); + } + return res; +} +int main() +{ + int n, k; + while ((scanf("%d%d", &n, &k)) != EOF) + { + LL sum = 0; + for (int i = 0; i < n; i++) + { + scanf("%d", &a[i]); + sum += a[i]; + } + sort(a, a + n); + matrix ans; + ans.m[0][0] = 1; ans.m[0][1] = 1; ans.m[0][2] = 1; + ans.m[1][0] = 0; ans.m[1][1] = 1; ans.m[1][2] = 1; + ans.m[2][0] = 0; ans.m[2][1] = 1; ans.m[2][2] = 0; + ans = quickpow(ans, k); + printf("%d\n", (ans.m[0][0] * sum + ans.m[0][1] * a[n - 1] + ans.m[0][2] * a[n - 2]) % mod); + } + return 0; +} diff --git a/HDOJ/5172_autoAC.cpp b/HDOJ/5172_autoAC.cpp new file mode 100644 index 0000000..0972faf --- /dev/null +++ b/HDOJ/5172_autoAC.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +#define LL __int64 +#define pi acos(-1.0) +const int mod=1e9+7; +const int INF=0x3f3f3f3f; +const double eqs=1e-9; +#define root 1, n, 1 +#define lson l, mid, rt<<1 +#define rson mid+1, r, rt<<1|1 +int a[1000002], pre[1000002], pos[1000002]; +int Max[4000000]; +LL sum[1000002]; +void PushUp(int rt) +{ + Max[rt]=max(Max[rt<<1],Max[rt<<1|1]); +} +void build(int l, int r, int rt) +{ + if(l==r){ + Max[rt]=pre[l]; + return ; + } + int mid=l+r>>1; + build(lson); + build(rson); + PushUp(rt); +} +int Query(int ll, int rr, int l, int r, int rt) +{ + if(ll<=l&&rr>=r){ + return Max[rt]; + } + int mid=l+r>>1, ans=-1; + if(ll<=mid) ans=max(ans,Query(ll,rr,lson)); + if(rr>mid) ans=max(ans,Query(ll,rr,rson)); + return ans; +} +int main() +{ + int n, m, i, j, x, l, r, ans; + while(scanf("%d%d",&n,&m)!=EOF){ + memset(pos,-1,sizeof(pos)); + sum[0]=0; + for(i=1;i<=n;i++){ + scanf("%d",&x); + pre[i]=pos[x]; + pos[x]=i; + sum[i]=sum[i-1]+x; + } + build(root); + while(m--){ + scanf("%d%d",&l,&r); + if(sum[r]-sum[l-1]!=(LL)(r-l+1)*(r-l+2)/2){ + puts("NO"); + continue ; + } + ans=Query(l,r,root); + if(ans +#include +#include +using namespace std; +#define ll long long +int const MAX = 2147483647; +ll a[105], b[105]; +int re[105]; +int main() +{ + int n, ca = 1; + while(scanf("%d", &n) != EOF) + { + memset(re ,0 , sizeof(re)); + int ans = 0; + for(int i = 1; i <= n; i++) + scanf("%I64d", &a[i]); + sort(a + 1, a + n + 1); + int cnt = 0, pos = 1; + a[n + 1] = -1; + for(int i = 1; i <= n; i++) + { + if(a[i] == a[i + 1]) + cnt++; + else + { + b[pos] = a[i]; + re[pos++] = cnt; + if(i != n) + cnt = 0; + } + } + if(cnt == n - 1) + { + printf("Case #%d: -1\n", ca++); + continue; + } + b[0] = b[pos-1]; + b[pos] = b[1]; + re[0] = re[pos-1]; + re[pos] = re[1]; + for(int i = 1; i < pos; i++) + if((b[i] + b[i - 1]) % MAX == b[i + 1]) + { + ans ++; + ans += re[i]; + } + printf("Case #%d: %d\n", ca++, ans); + } +} diff --git a/HDOJ/5175_autoAC.cpp b/HDOJ/5175_autoAC.cpp new file mode 100644 index 0000000..f814ae6 --- /dev/null +++ b/HDOJ/5175_autoAC.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +const int maxn=101000; +typedef long long LL; +LL fac[maxn],ans[maxn]; +LL N; +int cnt; +int cas; +void init() +{ + LL tmp=sqrt(N); + cnt=0; + for(LL i=1;i<=tmp;i++) + if(N%i==0) + { + fac[cnt++]=i; + if(i*i!=N){fac[cnt++]=N/i;} + } +} +LL gcd(LL a,LL b) +{ + if(b==0)return a; + return gcd(b,a%b); +} +void solve() +{ + int num=0; + for(int i=0;i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define LL long long +#define mod 100000000 +#define inf 0x3f3f3f3f +#define eps 1e-6 +#define N 200010 +#define FILL(a,b) (memset(a,b,sizeof(a))) +#define lson l,m,rt<<1 +#define rson m+1,r,rt<<1|1 +#define PII pair +#pragma comment(linker,"/STACK:1024000000,1024000000") +using namespace std; +int num[N],fa[N],n; +inline int read() { + int x=0; char ch=getchar(); + while(ch<'0'||ch>'9'){ch=getchar();} + while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} + return x; +} +struct edge +{ + int u,v,w; +}e[N]; +int cmp1(edge a,edge b) +{ + return a.wb.w; +} +void init() +{ + for(int i=1;i<=n;i++)fa[i]=i,num[i]=1; +} +int find(int x) +{ + return fa[x]==x?x:fa[x]=find(fa[x]); +} +int main() +{ + int cas=1; + while(scanf("%d",&n)>0) + { + for(int i=1;i +#include +#include +using namespace std; +int main() +{ + int kase; + scanf ("%d", &kase); + while (kase--) + { + int n, k; + long long ans = 0; + int num[100200], pos[100200]; + scanf ("%d %d", &n, &k); + for (int i = 0; i < n; i++) + { + scanf ("%d", &num[i]); + } + sort (num, num + n); + for (int i = 0; i < n; i++) + { + if (i == 0) + { + for (int j = i + 1; j <= n; j++) + if ( (j < n && num[j] - num[i] > k) || (j == n)) + { + pos[i] = j - 1; + ans += j - i - 1; + break; + } + } + else + { + for (int j = pos[i - 1]; j <= n; j++) + if ( (j < n && num[j] - num[i] > k) || j == n) + { + pos[i] = j - 1; + ans += j - i - 1; + break; + } + } + } + printf ("%I64d\n", ans); + } + return 0; +} diff --git a/HDOJ/5179_autoAC.cpp b/HDOJ/5179_autoAC.cpp new file mode 100644 index 0000000..aeeb2fb --- /dev/null +++ b/HDOJ/5179_autoAC.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +using namespace std; +int dp[25][25][3]; +int bit[25]; +int dfs(int pos,int pre,int flag,int z) +{ + if(pos==0)return 1; + if( !flag && dp[pos][pre][z]!=-1)return dp[pos][pre][z]; + int end=flag?bit[pos]:9; + int i=0,ans=0; + if(z==0)i=1; + for(;i<=end;i++) + { + if(pre==0) + ans+=dfs(pos-1,i,flag&&(i==end),z&&(i==0)); + else + { + if( pre>=i && pre%i==0)ans+=dfs(pos-1,i,flag&&(i==end),z&&(i==0)); + } + } + if(!flag)dp[pos][pre][z]=ans; + return ans; +} +int cal(int x) +{ + int len=0; + memset(bit,0,sizeof(bit)); + memset(dp,-1,sizeof(dp)); + while(x) + { + bit[++len]=x%10; + x/=10; + } + return dfs(len,0,1,1); +} +int main() +{ + int l,r; + int T; + cin>>T; + while(T--) + { + scanf("%d%d",&l,&r); + int s1=cal(r); + int s2=cal(l-1); + printf("%d\n",s1-s2); + } + return 0; +} diff --git a/HDOJ/5182_autoAC.cpp b/HDOJ/5182_autoAC.cpp new file mode 100644 index 0000000..b56155d --- /dev/null +++ b/HDOJ/5182_autoAC.cpp @@ -0,0 +1,36 @@ +#include +#include +#define maxn 100+5 +using namespace std; +struct stu +{ + int id,a,b,c; +}; +stu mapp[maxn]; +bool cmp(stu x,stu y) +{ + if(x.c==y.c) + { + if(x.b==y.b) return x.idy.c; +} +int main() +{ + int n; + while(cin>>n) + { + for(int i=0;i>mapp[i].a>>mapp[i].b; + mapp[i].c=mapp[i].a-mapp[i].b; + mapp[i].id=i; + } + sort(mapp,mapp+n,cmp); + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define LL long long +#define mod 100000000 +#define inf 0x3f3f3f3f +#define eps 1e-6 +#define N 1000010 +#define lson l,m,rt<<1 +#define rson m+1,r,rt<<1|1 +#define PII pair +using namespace std; +inline LL read() +{ + char ch=getchar();LL x=0,f=1; + while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();} + while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();} + return x*f; +} +const int MAXN=1000010; +const int HASH=1000007; +struct HASHMAP +{ + int head[HASH],next[MAXN],size; + LL state[MAXN]; + void init() + { + size=0; + memset(head,-1,sizeof(head)); + } + bool check(LL val) + { + int h=(val%HASH+HASH)%HASH; + for(int i=head[h];~i;i=next[i]) + if(state[i]==val)return true; + return false; + } + int insert(LL val) + { + int h=(val%HASH+HASH)%HASH; + for(int i=head[h];~i;i=next[i]) + { + if(val==state[i]) + return 1; + } + state[size]=val; + next[size]=head[h]; + head[h]=size++; + return 0; + } +}H1,H2; +LL a[N]; +int main() +{ + int T,n,k,cas=1; + T=read(); + while(T--) + { + n=read();k=read(); + for(int i=0;i=0&&!flag;i--) + { + if(i&1)sum-=a[i]; + else sum+=a[i]; + if(i%2==0) + { + if(H1.check(sum-k))flag=1; + } + else + { + if(H2.check(-sum-k))flag=1; + } + H1.insert(sum); + H2.insert(-sum); + } + printf("Case #%d: ",cas++); + if(flag)puts("Yes."); + else puts("No."); + } +} diff --git a/HDOJ/5184_autoAC.cpp b/HDOJ/5184_autoAC.cpp new file mode 100644 index 0000000..6f8e721 --- /dev/null +++ b/HDOJ/5184_autoAC.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include +#define LL __int64 +using namespace std; +const LL N=1000005; +const LL MOD=1000000007; +LL mod[N]; +LL ny[N]; +char str[N]; +int n; +LL C(int a,int b){ + if(b<0 || a<0 || a0){ + c=a/b; + d=a; a=b; b=d%b; + d=p; p=q; q=d-c*q; + } + return p<0?p+m:p; +} +int main(){ + mod[0]=1; + ny[0]=for_ny(mod[0],MOD); + for(int i=1;ip) break; + } + if(i +#include +#include +#define ll long long +#define max(a,b) a>b?a:b +int dp[320][50005]; +int main(){ + int T; + while(scanf("%d",&T)!=EOF){ + for(int q=1;q<=T;q++){ + int n,mod,ans=0; + scanf("%d%d",&n,&mod); + memset(dp,0,sizeof(dp)); + int m=(sqrt(8.0*n+1)-1)/2; + int i,j; + dp[0][0]=1; + for(i=1;i<=m;i++){ + for(j=0;j<=n;j++){ + if(i>j)dp[i][j]=0; + else dp[i][j]=(dp[i-1][j-i]+dp[i][j-i])%mod; + if(j==n)ans=(ans+dp[i][j])%mod; + } + } + printf("Case #%d: %d\n",q,ans); + } + } + return 0; +} diff --git a/HDOJ/5186_autoAC.cpp b/HDOJ/5186_autoAC.cpp new file mode 100644 index 0000000..58f12ba --- /dev/null +++ b/HDOJ/5186_autoAC.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include +using namespace std; +int ans[300]; +char str[300]; +int n,b; +maptoINT; +maptoCHAR; +int i,j; +void init() +{ + for( i = 0;i< 10;++i ) + { + toINT[ '0' + i ] = i; + toCHAR[ i ] = '0' +i; + } + for( i = 0; i< 26; ++i ) + { + toINT[ 'a' + i ] = i+10; + toCHAR[ i + 10 ] = 'a' + i; + } +} +int main() +{ + init(); + while( ~scanf("%d %d",&n,&b) ) + { + memset(ans,0,sizeof(ans)); + int ml = 0; + str[0] = '\0'; + while( n-- ) + { + scanf("%s",str); + int len = strlen(str)-1; + ml = ml > len? ml : len; + for( i = len ,j=0; i>=0 ;--i,j++ ) + ans[ j ] = ( toINT[str[i]] + ans[j] ) %b; + } + for( i = ml; i>=0 ; --i ) + if( ans[i] !=0) + break; + if( i < 0) + puts("0"); + else + { + for(;i>=0;--i) + putchar( toCHAR[ ans[i] ] ); + puts(""); + } + } +} diff --git a/HDOJ/5187_autoAC.cpp b/HDOJ/5187_autoAC.cpp new file mode 100644 index 0000000..34c10cc --- /dev/null +++ b/HDOJ/5187_autoAC.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#define LL long long +using namespace std; +LL n, p; +LL multi(LL a, LL b) { + LL ret = 0; + while(b) { + if(b & 1) ret = (ret + a) % p; + a = (a + a) % p; + b >>= 1; + } + return ret; +} +LL powmod(LL a, LL b) { + LL ret = 1; + while(b) { + if(b & 1) ret = multi(ret, a) % p; + a = multi(a, a) % p; + b >>= 1; + } + return ret; +} +int main() { + while(cin >> n >> p) { + if(p == 1) { + cout << 0 << endl; + } else if(n == 1) { + cout << 1 << endl; + } else { + LL ans = powmod(2, n) - 2; + if(ans < 0) ans += p; + cout << ans << endl; + } + } + return 0; +} diff --git a/HDOJ/5188_autoAC.cpp b/HDOJ/5188_autoAC.cpp new file mode 100644 index 0000000..206cc1f --- /dev/null +++ b/HDOJ/5188_autoAC.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +#define B(x) (1<<(x)) +typedef long long ll; +void cmax(ll& a,ll b){ if(b>a)a=b; } +const int oo=0x3f3f3f3f; +const ll OO=1LL<<61; +const int MOD=2012; +const int maxt=3100000; +const int maxn=40; +mapdp[2]; +struct Node{ + int v,t,l; +}a[maxn]; +bool cmp(Node x,Node y){ + return x.l-x.t::iterator it; + ll sum; + while(scanf("%d %d",&n,&w)!=EOF){ + memset(dp,0,sizeof dp); + sum=0; + for(int i=1;i<=n;i++){ + scanf("%d %d %d",&a[i].t,&a[i].v,&a[i].l); + sum+=a[i].v; + } + if(sumfirst; + ll dp_pre=it->second; + cmax(dp[now][u],dp_pre); + int uu=max(a[i].l,u+a[i].t); + cmax(dp[now][uu],dp_pre+a[i].v); + } + } + ans=-1; + for(it=dp[now].begin();it!=dp[now].end();++it){ + if(it->second>=w){ + ans=it->first; + break; + } + } + if(ans!=-1) + printf("%d\n",ans); + else + puts("zhx is naive!"); + } + return 0; +} diff --git a/HDOJ/5190_autoAC.cpp b/HDOJ/5190_autoAC.cpp new file mode 100644 index 0000000..22f50bc --- /dev/null +++ b/HDOJ/5190_autoAC.cpp @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include +#define INF 0x3fffffff +using namespace std; +struct node { + int a, b; +}dian[105]; +int main() { + int n, m; + while(scanf("%d %d", &n, &m) != EOF) { + for(int i = 0; i < m; i++) { + scanf("%d %d", &dian[i].a, &dian[i].b); + } + int ans = INF; + for(int i = 0; i +#include +#include +#include +#include +#include +#include +#include +using namespace std; +long long n,w,h; +long long sums[300600]; +long long sumout[350560]; +long long mins=50000*50000; +int main() +{ + while(~scanf("%I64d%I64d%I64d",&n,&w,&h)) + { + int aa; + long long sum=0; + long long sout=0; + for(int i=0;i<=n+w+w+1;i++) + { + sums[i]=sumout[i]=0; + } + mins=1234567891234; + for(int i=w;ih) + sout=sout+aa-h; + sumout[i]=sout; + sum=sum+aa; + sums[i]=sum; + } + for(int i=n+w;i<=n+w+w+1;i++) //娉ㄦ涓涓㈢sum涔瑕存 + { + sums[i]=sums[i-1]; + sumout[i]=sumout[i-1]; + } + if(sum=w*h) + { + if(allout +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +#define ll __int64 +#define ull unsigned long long +#define eps 1e-8 +#define NMAX 1000000000 +#define MOD 1000000 +#define lson l,mid,rt<<1 +#define rson mid+1,r,rt<<1|1 +#define PI acos(-1) +template +inline void scan_d(T &ret) +{ + char c; + int flag = 0; + ret=0; + while(((c=getchar())<'0'||c>'9')&&c!='-'); + if(c == '-') + { + flag = 1; + c = getchar(); + } + while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar(); + if(flag) ret = -ret; +} +const int maxn = 50000+10; +ll a[maxn*3], t1[maxn], t2[maxn]; +inline int lowbit(int x) +{ + return x&(-x); +} +void add(int x, ll k, int flag) +{ + while(x <= maxn) + { + if(flag) t1[x] += k; + else t2[x] += k; + x += lowbit(x); + } +} +ll sum(int x, int flag) +{ + ll ret = 0; + while(x) + { + if(flag) ret += t1[x]; + else ret += t2[x]; + x -= lowbit(x); + } + return ret; +} +inline void update(ll *a, ll b, ll h) +{ + if(a[0] < 0 || a[0] > b || (a[0] == b && a[1] < h)) + { + a[0] = b; + a[1] = h; + } +} +int main() +{ + int n,h; + ll w; + while(~scanf("%d%I64d%d",&n,&w,&h)) + { + memset(t1,0,sizeof(t1)); + memset(t2,0,sizeof(t2)); + ll all = 0; + for(int i = 1; i <= w; i++) + a[i] = a[i+w+n] = 0; + for(int i = w+1; i <= w+n; i++) + { + scanf("%I64d",&a[i]); + all += a[i]; + } + for(int i = 1; i < w; i++) + add(1,1,0); + ll suma = 0,ans[2]; + ans[0] = -1; + for(int i = w; i <= n+w+w; i++) + { + int p = (a[i] == 0) ? 1 : a[i]; + add(p,1,0); + if(i > w) + { + if(i <= n+w) add(p,p,1); + p = (a[i-w] == 0) ? 1 : a[i-w]; + add(p,-1,0); + if(i-w > w) add(p,-p,1); + } + suma += a[i]-a[i-w]; + ll th = suma/w; + ll tmp[2]; + tmp[0] = -1; + if(th >= h) + { + if(th*w <= all) + { + ll ge = sum(th,0),he = sum(th,1); + update(tmp, max(ge*th-he,suma-he-(w-ge)*th),th); + } + } + if(th+1 >= h) + { + if((th+1)*w <= all) + { + ll ge = sum(th+1,0),he = sum(th+1,1); + update(tmp, max(ge*(th+1)-he,suma-he-(w-ge)*(th+1)),th+1); + } + } + if(tmp[0] == -1) + { + if(h*w <= all) + { + ll ge = sum(h,0),he = sum(h,1); + update(tmp, max(ge*h-he,suma-he-(w-ge)*h),h); + } + } + update(ans,tmp[0],tmp[1]); + } + if(ans[0] == -1) printf("-1\n"); + else printf("%I64d %I64d\n",ans[1],ans[0]); + } + return 0; +} diff --git a/HDOJ/5193_autoAC.cpp b/HDOJ/5193_autoAC.cpp new file mode 100644 index 0000000..5738e50 --- /dev/null +++ b/HDOJ/5193_autoAC.cpp @@ -0,0 +1,164 @@ +#include +#include +using namespace std; +const int m=150; +inline int lowbit(int x) +{ + return x&-x; +} +void add(int c[],int pos,int x) +{ + while(pos<=20000) + { + c[pos]+=x; + pos+=lowbit(pos); + } +} +int sum(int c[],int l,int r) +{ + int sum1=0,sum2=0; + l--; + while(l>0) + { + sum1+=c[l]; + l-=lowbit(l); + } + while(r>0) + { + sum2+=c[r]; + r-=lowbit(r); + } + return sum2-sum1; +} +struct data +{ + int s,a[2*m+5]; + data *next; + int c[20005]; + data() + { + memset(c,0,sizeof(c)); + next=NULL; + } +}; +data *root; +void insert(int x,int pos) +{ + if(root==NULL) + { + root=new data; + root->s=1; + root->a[1]=x; + add(root->c,x,1); + return ; + } + data *k=root; + while(pos>k->s && k->next!=NULL) + { + pos-=k->s; + k=k->next; + } + memmove(k->a+pos+1,k->a+pos,sizeof(int)*(k->s-pos+1)); + k->s++; + k->a[pos]=x; + add(k->c,x,1); + if(k->s==2*m) + { + data *t=new data; + t->next=k->next; + k->next=t; + memcpy(t->a+1,k->a+m+1,sizeof(int)*m); + for(int i=1;i<=m;i++) + { + add(k->c,t->a[i],-1); + add(t->c,t->a[i],1); + } + t->s=k->s=m; + } +} +void del(int pos) +{ + data *k=root; + while(pos>k->s && k->next!=NULL) + { + pos-=k->s; + k=k->next; + } + add(k->c,k->a[pos],-1); + memmove(k->a+pos,k->a+pos+1,sizeof(int)*(k->s-pos)); + k->s--; +} +int find(int pos) +{ + data *k=root; + while(pos>k->s && k->next!=NULL) + { + pos-=k->s; + k=k->next; + } + return k->a[pos]; +} +int work(int pos) +{ + int res=0; + data *k=root; + int x=find(pos); + while(pos>k->s && k->next!=NULL) + { + pos-=k->s; + res+=sum(k->c,x+1,20000); + k=k->next; + } + for(int i=1;ia[i]>x) res++; + for(int i=pos+1;i<=k->s;i++) if(k->a[i]next!=NULL) + { + k=k->next; + res+=sum(k->c,1,x-1); + } + return res; +} +void destroy(data *k) +{ + if(k->next!=NULL) destroy(k->next); + delete k; +} +int main() +{ + int n,p; + while(~scanf("%d %d",&n,&p)) + { + root=NULL; + int ans=0; + for(int i=1;i<=n;i++) + { + int x; + scanf("%d",&x); + insert(x,i); + ans+=work(i); + } + while(p--) + { + int c; + scanf("%d",&c); + if(c==0) + { + int x,y; + scanf("%d %d",&x,&y); + x++; + insert(y,x); + ans+=work(x); + } + else + { + int x; + scanf("%d",&x); + ans-=work(x); + del(x); + } + printf("%d\n",ans); + } + destroy(root); + } + return 0; +} diff --git a/HDOJ/5194_autoAC.cpp b/HDOJ/5194_autoAC.cpp new file mode 100644 index 0000000..b24aeb2 --- /dev/null +++ b/HDOJ/5194_autoAC.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define L(x) (x<<1) +#define R(x) (x<<1|1) +#define MID(x,y) ((x+y)>>1) +#define eps 1e-8 +#define fre(i,a,b) for(i = a; i = a;i--) +#define mem(t, v) memset ((t) , v, sizeof(t)) +#define ssf(n) scanf("%s", n) +#define sf(n) scanf("%d", &n) +#define sff(a,b) scanf("%d %d", &a, &b) +#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c) +#define pf printf +#define bug pf("Hi\n") +using namespace std; +typedef __int64 ll; +#define INF 0x3f3f3f3f +#define N 10005 +ll C(ll a,ll b) +{ + ll x=1,y=1; + ll i; + b=min(b,a-b); + if(b==0) return 1; + fre(i,1,b+1) + { + x=x*a; + y=y*i; + a--; + } + return x/y; +} +ll fdd(ll x,ll y) +{ + if(y==0) return x; + return fdd(y,x%y); +} +int main() +{ + ll i,j; + ll n,m; + while(~scanf("%I64d%I64d",&n,&m)) + { + j=min(n,m); + ll ans=0; + fre(i,1,j+1) + ans=ans+i*C(n,i)*C(m,i); + ll y=C(n+m,n); + ll x=fdd(ans,y); + pf("%I64d/%I64d\n",ans/x,y/x); + } + return 0; +} diff --git a/HDOJ/5195_autoAC.cpp b/HDOJ/5195_autoAC.cpp new file mode 100644 index 0000000..791ce31 --- /dev/null +++ b/HDOJ/5195_autoAC.cpp @@ -0,0 +1,90 @@ +#include "stdio.h" +#include "string.h" +#include "vector" +using namespace std; +const int inf=0x3f3f3f3f; +vectormark[100010]; +int key,cnt; +int du[100010]; +struct node +{ + int x,l,r; +}data[400010]; +int Min(int a,int b) +{ + if (a +#include +#include +using namespace std; +const int N = 100005; +typedef long long ll; +int n, m; +ll q; +struct Hash { + int v, id; + void read(int id) { + scanf("%d", &v); + this->id = id; + } +} h[N]; +bool cmp(Hash a, Hash b) { + return a.v < b.v; +} +int a[N]; +int bit[N], l[N]; +inline int lowbit(int x) {return (x&(-x));} +void add(int x, int v) { + while (x <= n) { + bit[x] += v; + x += lowbit(x); + } +} +int get(int x) { + int ans = 0; + while (x) { + ans += bit[x]; + x -= lowbit(x); + } + return ans; +} +ll sum[N], sum2[N]; +ll ans[N]; +int X[N], Y[N]; +void gao(ll q, int bo) { + if (q < 0) return; + memset(bit, 0, sizeof(bit)); + int r = 0; + ll tot = 0; + for (int i = 1; i <= n; i++) { + while (r <= n && tot <= q) { + r++; + if (r > n) break; + tot += (r - i) - get(a[r]); + add(a[r], 1); + } + l[i] = r - 1; + add(a[i], -1); + tot -= get(a[i] - 1); + } + sum[n + 1] = 0; + for (int i = n; i >= 1; i--) + sum[i] = sum[i + 1] + (l[i] - i + 1); + int x, y; + for (int i = 0; i < m; i++) { + int x = X[i], y = Y[i]; + int tmp = lower_bound(l + x, l + y + 1, y) - l - 1; + ans[i] += bo * (sum[x] - sum[tmp + 1] + sum2[y - tmp]); + } +} +int main() { + for (int i = 1; i < N; i++) + sum2[i] = sum2[i - 1] + i; + while (~scanf("%d%d%I64d", &n, &m, &q)) { + for (int i = 1; i <= n; i++) + h[i].read(i); + sort(h + 1, h + n + 1, cmp); + int tmp = 1; + a[h[1].id] = 1; + for (int i = 1; i <= n; i++) { + if (h[i].v != h[i - 1].v) tmp++; + a[h[i].id] = tmp; + } + for (int i = 0; i < m; i++) scanf("%d%d", &X[i], &Y[i]); + memset(ans, 0, sizeof(ans)); + gao(q, 1); gao(q - 1, -1); + for (int i = 0; i < m; i++) + printf("%I64d\n", ans[i]); + } + return 0; +} diff --git a/HDOJ/5197_autoAC.cpp b/HDOJ/5197_autoAC.cpp new file mode 100644 index 0000000..8f8d5a0 --- /dev/null +++ b/HDOJ/5197_autoAC.cpp @@ -0,0 +1,113 @@ +#include +#include +#include +#include +#include +using namespace std ; +typedef long long LL ; +#define clr( a , x ) memset ( a , x , sizeof a ) +#define cpy( a , x ) memcpy ( a , x , sizeof a ) +const int mod = 999948289 ; +const int MAXN = 100005 ; +const int g = 13 ; +struct Node { + int idx , n ; + Node () {} + Node ( int idx , int n ) : idx ( idx ) , n ( n ) {} + bool operator < ( const Node& a ) const { + if ( n != a.n ) return n > a.n ; + return idx > a.idx ; + } +} ; +vector < int > G[MAXN] ; +int x1[MAXN << 1] , x2[MAXN << 1] ; +int A[MAXN] ; +int f[MAXN] ; +int invf[MAXN] ; +int ff[MAXN] = { 69882273 , 791556261 , 443386966 , 216976807 , 615450328 , 620200386 , 446861303 , 527234626 , 385874772 , 122958863 , 35106575 , 469506825 , 849117472 , 793958001 , 452020289 , 4676385 , 668325472 , 90765471 , 741173520 , 292960868 , 794676333 , 906146069 , 259388637 , 95564208 , 271378066 , 312670526 , 893812493 , 998708474 , 266936264 , 418472146 , 482986423 , 293590649 , 946169270 , 322072286 , 298345339 , 708799889 , 729159009 , 947001046 , 713317958 , 528716034 , 659041757 , 923462973 , 108308863 , 810503508 , 691257344 , 44577283 , 303671798 , 26378399 , 968330581 , 133269594 , 629464814 , 383701511 , 107248414 , 756713235 , 682608228 , 45243233 , 29730607 , 928129208 , 77919155 , 577456879 , 477803768 , 241372843 , 273974160 , 135004679 , 18759653 , 921263284 , 927413084 , 571173344 , 849450420 , 280001219 , 280182102 , 471765511 , 552617665 , 678536791 , 719472567 , 409339140 , 833786546 , 199547621 , 504095173 , 73474955 , 235191090 , 25104958 , 702175539 , 664805355 , 384892206 , 919753472 , 570055488 , 517633839 , 946666972 , 418236783 , 141526729 , 401116525 , 364688445 , 351756910 , 929978083 , 744127744 , 410219723 , 903386921 , 10397619 , 292672496 , 345067388 , 523552083 , 421189060 , 772817702 , 702067604 , 407656772 , 838558008 , 366888490 , 733265485 , 101935555 , 465758684 , 456247580 , 49863634 , 940044402 , 208594100 , 475713181 , 591757338 , 426885417 , 574731054 , 306371541 , 580478774 , 700520798 , 604854237 , 628550010 , 614229828 , 596800066 , 186387316 , 428172129 , 441880990 , 908572606 , 161165821 , 135821670 , 39451264 , 636759055 , 248637650 , 900422956 , 987575526 , 146206173 , 144378428 , 984859458 , 945906788 , 614390815 , 959944022 , 573872290 , 482372774 , 700686219 , 683213989 , 603202477 , 578490080 , 15742988 , 953766856 , 343303448 , 293406332 , 910923238 , 466673107 , 230287260 , 31971863 , 412089584 , 415333822 , 5141190 , 78605027 , 168358509 , 581312160 , 998023617 , 893397256 , 822435495 , 57995800 , 705434759 , 572795704 , 166516606 , 161037416 , 168383845 , 869558200 , 322925569 , 433667444 , 642355815 , 448674266 , 135859659 , 224084982 , 993174767 , 466259649 , 513816219 , 153089740 , 789274830 , 393717679 , 563790438 , 667097553 , 246990172 , 82605246 , 854203585 , 552829545 , 854722457 , 723501019 , 423365839 , 267064284 , 856423915 , 79584268 , 653242694 , 334763889 , 946460749 , 495779508 , 678581977 , 572159717 , 801113676 , 17694875 , 489346941 , 520159316 , 712451262 , 512646102 , 98625173 , 281075174 , 126417219 , 443999667 , 829419892 , 481590520 , 624462589 , 706625430 , 334382549 , 396548707 , 457126026 , 771344797 , 196886249 , 171055541 , 311675783 , 956293535 , 696313936 , 128241027 , 336034496 , 523418062 , 798763948 , 84111300 , 800829312 , 625395544 , 500614440 , 693667842 , 961074871 , 626146398 , 545195644 , 926642113 , 687804010 , 847697467 , 641757700 , 868085106 , 491400010 , 927203814 , 381828916 , 916240332 , 85588576 , 466472554 , 858022328 , 664854550 , 480579956 , 515869204 , 880403159 , 713886759 , 337584729 , 706212548 , 510425383 , 654180230 , 959126384 , 255081030 , 935661083 , 751998379 , 844579617 , 943872436 , 706307343 , 546052985 , 669561488 , 603243382 , 766885812 , 178767339 , 951085566 , 851956883 , 481179237 , 597706499 , 394183053 , 293629880 , 413856189 , 214329561 , 213811293 , 430994640 , 855683642 , 120274250 , 814481427 , 666912447 , 862912318 , 907474880 , 563341124 , 102962767 , 364221323 , 336078158 , 513727019 , 626546332 , 894589534 , 264876061 , 610304218 , 383451496 , 903153841 , 327485311 , 475386192 , 492451940 , 423665925 , 997135284 , 195941463 , 445032717 , 956018500 , 911453634 , 96056536 , 896059204 , 651582680 , 306146295 , 469553736 , 900398557 , 570560384 , 162249798 , 244848963 , 732627210 , 946697553 , 635002635 , 452231824 , 77896604 , 136484860 , 879392754 , 126087417 , 584820990 , 136525199 , 109255745 , 235719082 , 933547861 , 138567112 , 639794200 , 580152595 , 625589203 , 733876837 , 101259850 , 552418145 , 813660931 , 196519288 , 456976229 , 25421384 , 712595297 , 704541085 , 557447132 , 90748767 , 20095929 , 968079867 , 439274655 , 892061081 , 631420710 , 761708823 , 63522826 , 375326405 , 847336986 , 229962565 , 432855344 , 379698327 , 564009295 , 941159124 , 559880756 , 930461914 , 476237204 , 178783982 , 396056996 , 228899021 , 752980027 , 874998159 , 223708216 , 172123540 , 622548375 , 274778429 , 814215923 , 734898238 , 638935987 , 241179940 , 987124093 , 794533496 , 937652043 , 983690543 , 493138649 , 403470735 , 195081860 , 320077468 , 785942949 , 256840417 , 153936326 , 547495244 , 636860057 , 993903452 , 480618639 , 567272415 , 607857590 , 568841890 , 153037457 , 413267013 , 800156606 , 287349476 , 153526493 , 33811963 , 667053655 , 730239961 , 30990337 , 884437514 , 423134555 , 482877273 , 716706877 , 79961414 , 153113282 , 426720544 , 408302696 , 457792636 , 982630227 , 820109433 , 663817234 , 966021112 , 512724775 , 496648265 , 952105142 , 834779459 , 340883527 , 86826201 , 208293269 , 262036542 , 465900101 , 184164779 , 922046925 , 287059785 , 164659088 , 845210103 , 984453191 , 958388335 , 123500283 , 483136423 , 93579300 , 221906912 , 923056607 , 581355334 , 257166825 , 35007443 , 173231747 , 300723407 , 570655111 , 239210038 , 328605627 , 827131050 , 883190274 , 502720573 , 842111854 , 663437999 , 588819297 , 528736498 , 736629016 , 205830419 , 520959797 , 767718942 , 996330390 , 378723801 , 922056580 , 828269403 , 355848593 , 654685459 , 579356774 , 224993757 , 563992210 , 34354314 , 41127812 , 487011765 , 743948237 , 42607233 , 793179355 , 922099087 , 795236763 , 104970413 , 137850518 , 323153445 , 844832100 , 335643302 , 602541999 , 506172202 , 184092356 , 426730643 , 625482293 , 968516614 , 287803058 , 941992949 , 454653688 , 362923901 , 15056006 , 79846658 , 137511716 , 325105470 , 565980790 , 518728236 , 405581353 , 634222533 , 447811637 , 671699463 , 9153273 , 174178785 , 991733853 , 177850771 , 233908440 , 330759922 , 270473391 , 120005719 , 328321673 , 742704498 , 522741629 , 728167412 , 884961232 , 879549768 , 887503752 , 262603856 , 663798514 , 963330659 , 100713216 , 880492916 , 543224716 , 487585183 , 7373897 , 562827165 , 342089383 , 231928715 , 458070746 , 461590267 , 989826760 , 251912942 , 373406560 , 738456792 , 315104902 , 690802034 , 172564163 , 807387201 , 446915848 , 444169919 , 3254250 , 116134967 , 157352193 , 278465933 , 977246804 , 460721567 , 633930865 , 588191311 , 126693578 , 606158621 , 222734769 , 48475104 , 568157953 , 927325187 , 67358120 , 820035600 , 687758215 , 725002146 , 769772598 , 932649813 , 797388164 , 539428861 , 368607052 , 378603176 , 634775529 , 699800232 , 321483965 , 279494988 , 976903837 , 592259353 , 184043602 , 531453990 , 842072702 , 768435762 , 294653272 , 837780286 , 835240007 , 882398267 , 508522223 , 804586121 , 250194887 , 14077583 , 360232823 , 288596963 , 710322941 , 164774727 , 562173265 , 465999135 , 401602179 , 999448184 , 248529438 , 388455137 , 462263760 , 104181526 , 290036210 , 452689549 , 993107251 , 545214312 , 624857376 , 213784660 , 720311222 , 10567316 , 624219369 , 453475724 , 12170781 , 635988144 , 415953655 , 910063428 , 940043264 , 663497852 , 459812153 , 713738904 , 402284167 , 233273213 , 436363830 , 547141750 , 645992377 , 157237863 , 115901665 , 572862631 , 558648869 , 290487718 , 404995907 , 934894741 , 784395003 , 166416663 , 895361111 , 562589658 , 961345846 , 444583454 , 563961881 , 444521940 , 413060059 , 258946869 , 918512528 , 399217777 , 288820547 , 127922835 , 907380975 , 539847308 , 194943546 , 783183611 , 526612272 , 607466731 , 597518432 , 753564534 , 145244826 , 53356822 , 668767501 , 917660707 , 791363628 , 873865188 , 754625043 , 75627876 , 329167756 , 109205633 , 289852337 , 943918898 , 410362534 , 958996738 , 347876644 , 74386839 , 348129015 , 986156066 , 857166465 , 92339218 , 234541783 , 935939427 , 737280793 , 728409172 , 126599226 , 896812051 , 644061170 , 932429377 , 290089424 , 808444548 , 758828389 , 449044940 , 597079020 , 584375293 , 553677062 , 490868858 , 107202669 , 882290559 , 724836565 , 948688036 , 293126814 , 652642166 , 709962599 , 112254976 , 957391661 , 189735497 , 27787352 , 258595651 , 875794763 , 344943006 , 459699983 , 779124068 , 242241547 , 393859356 , 760957472 , 877232553 , 440705326 , 959517034 , 505374815 , 35108527 , 51941195 , 942223277 , 86905956 , 274848272 , 293506405 , 907723656 , 497947456 , 393087291 , 398037754 , 121598763 , 232144028 , 591318742 , 778260280 , 296460416 , 442712280 , 229081970 , 83187891 , 152878902 , 58134845 , 32118829 , 270379787 , 204466744 , 378473059 , 541927089 , 905669511 , 450272138 , 49693392 , 490848880 , 141415171 , 615897904 , 549583527 , 218364809 , 260578555 , 771474057 , 380010129 , 383697701 , 59476689 , 197974682 , 62311567 , 614700290 , 945607981 , 580141879 , 364627623 , 825940168 , 432176905 , 886830329 , 624113027 , 373134390 , 3624311 , 630786212 , 677523637 , 898691846 , 127067438 , 908076025 , 371825879 , 567986890 , 698070761 , 869858982 , 508673575 , 40774280 , 156538688 , 226364986 , 800667727 , 675844125 , 267792942 , 680392510 , 292572755 , 997152235 , 547699475 , 952197906 , 217521334 , 320499148 , 342712538 , 888666275 , 393626985 , 451815656 , 230294621 , 426050340 , 465212052 , 133318659 , 586180960 , 970944670 , 38793481 , 20061168 , 986395939 , 239739019 , 339616855 , 594376766 , 260447199 , 519596678 , 553065488 , 465912604 , 770580728 , 869874189 , 246904788 , 94481443 , 48601881 , 4007176 , 160855859 , 124996407 , 49901709 , 264920367 , 470191858 , 685617453 , 929490173 , 734566387 , 255657601 , 649495111 , 467955866 , 423046166 , 57818945 , 564095879 , 921160407 , 496919993 , 849252865 , 542906152 , 436890148 , 399145512 , 569335744 , 775449829 , 691704409 , 748585910 , 873162972 , 747573203 , 579610029 , 995841512 , 508638032 , 661327227 , 168348266 , 647999270 , 472814117 , 537204701 , 743050918 , 505654858 , 804496062 , 103128634 , 414307482 , 577643489 , 525012382 , 351140781 , 391130511 , 852703045 , 60224751 , 983250374 , 723239627 , 116748847 , 162765489 , 304238235 , 580733772 , 578302313 , 921055970 , 130296719 , 564247321 , 226244861 , 678040840 , 936226561 , 784203456 , 722625922 , 27404584 , 597150412 , 954289593 , 654814409 , 927991004 , 162445852 , 682728416 , 643936903 , 408009707 , 259101129 , 316815527 , 838855414 , 62969134 , 647843058 , 296505125 , 136222214 , 280114658 , 674739161 , 399497483 , 77767868 , 666409066 , 680920629 , 823404901 , 856973432 , 636921766 , 569004500 , 491717880 , 16246940 , 343337017 , 87523073 , 75791793 , 851609146 , 613364476 , 639003 , 447341119 , 513099283 , 302211516 , 582447665 , 550670777 , 339730219 , 289286185 , 841350829 , 52966333 , 401958184 , 110201859 , 170541391 , 293793132 , 460292990 , 255441796 , 594789035 , 974361464 , 164898662 , 715508390 , 598492098 , 954998960 , 622517573 , 145720445 , 980278399 , 847131191 , 792784293 , 35424278 , 168101124 , 122625582 , 148102086 , 436077724 , 439606682 , 863763585 , 884763606 , 978514101 , 236525338 , 246790692 , 889384599 , 860660877 , 605992029 , 76015181 , 637252729 , 892083627 , 336822184 , 832783274 , 267912179 , 520907876 , 33986639 , 170117849 , 965267478 , 916748474 , 855449768 , 528877922 , 913688701 , 751563586 , 823525161 , 17497538 , 305868835 , 790306522 , 917156691 , 496104717 , 522052365 , 414224474 , 249916805 , 468624266 , 418454382 , 826856669 , 938064651 , 622495105 , 910820222 , 154819386 , 121109106 , 256332596 , 29582281 , 297482426 , 6959983 , 927128898 , 534235829 , 501462578 , 555527765 , 213800973 , 424702537 , 834286273 , 722387908 , 381978926 , 822283522 , 554643366 , 475401659 , 821119927 , 99155414 , 748958472 , 75255490 , 297807089 , 188353653 , 616207495 , 493847595 , 776463788 , 540551643 , 679408122 , 272322394 , 13015400 , 536842566 , 37953628 , 495004185 , 827872604 , 947611149 , 276847369 , 0 } ; +int m , root ; +int power ( int a , int b ) { + LL res = 1 , tmp = a ; + while ( b ) { + if ( b & 1 ) res = res * tmp % mod ; + tmp = tmp * tmp % mod ; + b >>= 1 ; + } + return ( int ) res ; +} +void NTT ( int y[] , int n , int rev ) { + for ( int i = 1 , j , k , t ; i < n ; ++ i ) { + for ( j = 0 , k = n >> 1 , t = i ; k ; k >>= 1 , t >>= 1 ) j = j << 1 | t & 1 ; + if ( i < j ) swap ( y[i] , y[j] ) ; + } + for ( int s = 2 , ds = 1 ; s <= n ; ds = s , s <<= 1 ) { + int wn = power ( g , ( mod - 1 ) / s ) ; + if ( rev < 0 ) wn = power ( wn , mod - 2 ) ; + for ( int k = 0 , w = 1 ; k < ds ; ++ k , w = ( LL ) w * wn % mod ) { + for ( int i = k , t ; i < n ; i += s ) { + y[i + ds] = ( y[i] - ( t = ( LL ) w * y[i + ds] % mod ) + mod ) % mod ; + y[i] = ( y[i] + t ) % mod ; + } + } + } + if ( rev < 0 ) { + int inv = power ( n , mod - 2 ) ; + for ( int i = 0 ; i < n ; ++ i ) y[i] = ( LL ) y[i] * inv % mod ; + } +} +void solve () { + priority_queue < Node > q ; + for ( int i = 1 ; i <= m ; ++ i ) { + scanf ( "%d" , &A[i] ) ; + G[i].clear () ; + } + if ( ( LL ) m * m >= mod ) { + printf ( "0\n" ) ; + return ; + } + for ( int i = 1 ; i <= m ; ++ i ) { + G[i].push_back ( ( 1 - i + mod ) % mod ) ; + G[i].push_back ( 1 ) ; + q.push ( Node ( i , 2 ) ) ; + } + while ( !q.empty () ) { + int x = q.top ().idx ; + q.pop () ; + if ( q.empty () ) { + root = x ; + break ; + } + int y = q.top ().idx ; + q.pop () ; + int n1 = G[x].size () , n2 = G[y].size () , nn = n1 + n2 - 1 ; + int n = 1 ; + while ( n < nn ) n <<= 1 ; + for ( int i = 0 ; i < n ; ++ i ) x1[i] = i < n1 ? G[x][i] : 0 ; + for ( int i = 0 ; i < n ; ++ i ) x2[i] = i < n2 ? G[y][i] : 0 ; + NTT ( x1 , n , 1 ) ; + NTT ( x2 , n , 1 ) ; + for ( int i = 0 ; i < n ; ++ i ) x1[i] = ( LL ) x1[i] * x2[i] % mod ; + NTT ( x1 , n , -1 ) ; + G[x].clear () ; + for ( int i = 0 ; i < nn ; ++ i ) G[x].push_back ( x1[i] ) ; + q.push ( Node ( x , nn ) ) ; + } + int ans = power ( invf[m] , m ) ; + int mm = m * m , x ; + for ( x = 0 ; x + 1000000 <= mm ; x += 1000000 ) ans = ( LL ) ans * ff[x / 1000000] % mod ; + for ( ++ x ; x <= mm ; ++ x ) ans = ( LL ) ans * x % mod ; + for ( int i = 1 ; i <= m ; ++ i ) { + if ( ( m + A[i] ) % 2 == 0 ) ans = ( LL ) ans * G[root][A[i]] % mod ; + else ans = ( LL ) ans * ( mod - G[root][A[i]] ) % mod ; + } + printf ( "%d\n" , ans ) ; +} +int main () { + f[0] = 1 ; + for ( int i = 1 ; i < MAXN ; ++ i ) { + f[i] = ( LL ) i * f[i - 1] % mod ; + invf[i] = power ( f[i] , mod - 2 ) ; + } + while ( ~scanf ( "%d" , &m ) ) solve () ; + return 0 ; +} diff --git a/HDOJ/5198_autoAC.cpp b/HDOJ/5198_autoAC.cpp new file mode 100644 index 0000000..74c0409 --- /dev/null +++ b/HDOJ/5198_autoAC.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include +using namespace std; +int nn,num; +int main(){ + char str[100]; + int i; + while(~scanf("%s",str)){ + nn = 0; + bool flag = true; + int len = strlen(str); + char tmp,a = 0,b = 0,c = 0; + tmp = str[0]; + a = tmp; + for(i =0;i < len;i++){ + if(tmp==str[i]){ + nn++; + } + else { + tmp = str[i]; + b = tmp; + break; + } + } + if(nn*3!=len) flag = false; + for(int k = i;i +#include +#include +#include +#define MAXN 1000010 +using namespace std; +struct node{ + int id; + int h; +}q[MAXN]; +int ans[MAXN]; +priority_queue,greater > pq; +bool cmp(node a,node b){ + if(a.h==b.h) return a.id