Powered By HC TECH : AutoACer Engine

4900-4999
This commit is contained in:
KiritoTRw 2016-09-10 11:34:41 +08:00 committed by GitHub
parent 1b77522684
commit dd3af96771
86 changed files with 8091 additions and 0 deletions

102
HDOJ/4900_autoAC.cpp Normal file
View File

@ -0,0 +1,102 @@
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int MAXN = 310;
const int INF = 0x3f3f3f3f;
int mat[MAXN][MAXN], dif[MAXN][MAXN];
int label[MAXN], has[MAXN], tmp[MAXN];
int n, k, T;
int head[MAXN], ecnt;
int to[MAXN << 1], ne[MAXN << 1];
void init() {
memset(head + 1, -1, n * sizeof(int));
ecnt = 0;
}
void add_edge(int u, int v) {
to[ecnt] = v; ne[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; ne[ecnt] = head[v]; head[v] = ecnt++;
}
int que[MAXN], dis[MAXN];
void bfs(int st) {
memset(dis + 1, 0x3f, n * sizeof(int));
int l = 0, r = 0;
dis[que[r++] = st] = 0;
while(l != r) {
int u = que[l++];
for(int p = head[u]; ~p; p = ne[p]) {
int &v = to[p];
if(dis[u] + 1 < dis[v]) {
dis[v] = dis[u] + 1;
que[r++] = v;
}
}
}
for(int i = 1; i <= n; ++i) mat[st][i] = dis[i];
}
int cnt[MAXN];
void dfs(int u, int f, int& c, int st) {
if(++cnt[has[u]] == 1) c++;
dif[st][u] = c;
for(int p = head[u]; ~p; p = ne[p]) {
int &v = to[p];
if(v == f) continue;
dfs(v, u, c, st);
}
if(--cnt[has[u]] == 0) c--;
}
double per[MAXN];
void initPer(int n = 300) {
per[0] = log(1);
for(int i = 1; i <= n; ++i)
per[i] = per[i - 1] + log(i);
}
double c(int n, int k) {
return per[n] - per[n - k] - per[k];
}
double calc(int a, int b) {
int cnt = 2;
for(int i = 1; i <= n; ++i) {
if(i == a || i == b) continue;
if(mat[i][a] > mat[a][b] || mat[i][b] > mat[a][b]) continue;
if(mat[i][b] == mat[a][b] && i < a) continue;
if(mat[i][a] == mat[a][b] && i < b) continue;
cnt++;
}
if(cnt >= k) return dif[a][b] * exp(c(cnt - 2, k - 2) - c(n, k));
else return 0;
}
double solve() {
if(n < k || k == 0) return 0;
if(k == 1) return 0;
double res = 0;
for(int i = 1; i <= n; ++i) {
for(int j = i + 1; j <= n; ++j) {
res += calc(i, j);
}
}
return res;
}
int main() {
initPer();
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &k);
init();
for(int i = 1, a, b; i < n; ++i) {
scanf("%d%d", &a, &b);
add_edge(a, b);
}
for(int i = 1; i <= n; ++i) bfs(i);
for(int i = 1; i <= n; ++i) scanf("%d", &has[i]);
int cnt = 0;
for(int i = 1; i <= n; ++i) tmp[cnt++] = has[i];
sort(tmp, tmp + cnt);
cnt = unique(tmp, tmp + cnt) - tmp;
for(int i = 1; i <= n; ++i) has[i] = lower_bound(tmp, tmp + cnt, has[i]) - tmp;
for(int i = 1, c = 0; i <= n; ++i) dfs(i, 0, c, i);
printf("%.10f\n", solve());
}
}

62
HDOJ/4901_autoAC.cpp Normal file
View File

@ -0,0 +1,62 @@
#include<cstdio>
#include<cstring>
typedef __int64 LL;
#define mod 1000000007
const int MAXN = 1002;
const int MAXA = 1025;
int dp1[MAXN][MAXA], dp2[MAXN][MAXA], dp3[MAXN][MAXA];
int a[MAXN];
int main()
{
int T, n, i, j, t;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
for(i = 0; i < n; i++)
scanf("%d",&a[i]);
memset(dp1, 0, sizeof(dp1));
memset(dp2, 0, sizeof(dp2));
memset(dp3, 0, sizeof(dp3));
dp1[0][a[0]] = 1;
for(i = 1; i < n - 1; i++) {
dp1[i][a[i]]++;
for(j = 0; j < MAXA; j++) {
if(dp1[i-1][j]) {
dp1[i][j] += dp1[i-1][j];
dp1[i][j] %= mod;
t = j ^ a[i];
dp1[i][t] += dp1[i-1][j];
dp1[i][t] %= mod;
}
}
}
dp2[n-1][a[n-1]] = 1;
dp3[n-1][a[n-1]] = 1;
for(i = n-2; i > 0; i--) {
dp2[i][a[i]]++;
dp3[i][a[i]]++;
for(j = 0; j < MAXA; j++) {
if(dp2[i+1][j]) {
dp2[i][j] += dp2[i+1][j];
dp2[i][j] %= mod;
t = j & a[i];
dp2[i][t] += dp2[i+1][j];
dp2[i][t] %= mod;
dp3[i][t] += dp2[i+1][j];
dp3[i][t] %= mod;
}
}
}
int ans = 0;
for(i = 0; i < n - 1; i++) {
for(j = 0; j < MAXA; j++) {
if(dp1[i][j] && dp3[i+1][j]) {
ans += (LL(dp1[i][j]) * dp3[i+1][j] % mod);
ans %= mod;
}
}
}
printf("%d\n", ans);
}
return 0;
}

139
HDOJ/4902_autoAC.cpp Normal file
View File

@ -0,0 +1,139 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL 100005
#define ll __int64
#define lson 2*i
#define rson 2*i+1
#define ls l,mid,2*i
#define rs mid+1,r,2*i+1
struct node
{
int l,r;
ll num,maxn;
} a[LL<<2];
int n,m;
ll gcd(ll a,ll b)
{
if(!b) return a;
else return gcd(b,a%b);
}
void PushUp(int i)
{
a[i].num=(a[lson].num==a[rson].num)?a[lson].num:-1;
a[i].maxn=max(a[lson].maxn,a[rson].maxn);
}
void PushDown(int i)
{
if(a[i].num>-1)
{
a[lson].num = a[rson].num = a[i].num;
a[lson].maxn = a[rson].maxn = a[i].maxn;
}
}
void init(int l,int r,int i)
{
a[i].l = l;
a[i].r = r;
if(l == r)
scanf("%I64d",&a[i].num);
else
{
int mid = (l+r)>>1;
init(ls);
init(rs);
PushUp(i);
}
}
void change1(int L,int R,ll x,int i)
{
int l = a[i].l,r = a[i].r;
if(L<=l && r<=R)
{
a[i].num = x;
a[i].maxn = x;
}
else
{
int mid = (l+r)>>1;
PushDown(i);
if(mid>=R)
change1(L,R,x,lson);
else if(mid<L)
change1(L,R,x,rson);
else
{
change1(L,R,x,lson);
change1(L,R,x,rson);
}
PushUp(i);
}
}
void change2(int L,int R,ll x,int i)
{
int l = a[i].l,r = a[i].r;
if(a[i].maxn<=x) return ;
if(L<=l && r<=R)
{
if(a[i].num>-1)
{
a[i].num = gcd(a[i].num,x);
a[i].maxn = a[i].num;
}
else
{
PushDown(i);
change2(L,R,x,lson);
change2(L,R,x,rson);
PushUp(i);
}
}
else
{
int mid = (l+r)>>1;
PushDown(i);
if(mid>=R)
change2(L,R,x,lson);
else if(mid<L)
change2(L,R,x,rson);
else
{
change2(L,R,x,lson);
change2(L,R,x,rson);
}
PushUp(i);
}
}
void print(int l,int r,int i)
{
if(a[i].num>-1)
for(int j = l; j<=r; j++)
printf("%I64d ",a[i].num);
else
{
int mid = (l+r)>>1;
print(ls);
print(rs);
}
}
int main()
{
int t,l,r,cas;
__int64 x;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
init(1,n,1);
scanf("%d",&m);
while(m--)
{
scanf("%d%d%d%I64d",&cas,&l,&r,&x);
if(cas == 1) change1(l,r,x,1);
else change2(l,r,x,1);
}
print(1,n,1);
printf("\n");
}
}

41
HDOJ/4906_autoAC.cpp Normal file
View File

@ -0,0 +1,41 @@
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<vector>
using namespace std;
#define maxn 1<<21
#define mod 1000000007
#define ll long long
ll dp[2][maxn];
void add(ll&a,ll b){
a+=b;
if(a >= mod) a-=mod;
}
int main(){
int t,n,l,k,i,j;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&k,&l);
int p=0,q=1,f=min(l,k),s=1<<(k-1),s1,s2,s3=(1<<k)-1;
memset(dp[0],0,sizeof(dp[0]));
dp[0][0] = 1;
for( i = 0;i < n; i++){
if(k>16)memset(dp[q],0,sizeof(dp[q]));
else for(j=0;j<=s;j++)dp[q][j]=0;
for(j = 0;j <= s;j++){
if(dp[p][j] == 0) continue;
dp[q][j] = (dp[q][j]+dp[p][j]*(l-f))%mod;
add(dp[q][j],dp[p][j]);
for(s1=1;s1<=f;s1++){
s2 = (1<<(s1-1))|j|(j<<s1);
if(s2&s)add(dp[q][s],dp[p][j]);
else add(dp[q][s2&s3],dp[p][j]);
}
}
swap(p,q);
}
cout<<dp[p][s]<<endl;
}
return 0;
}

82
HDOJ/4907_autoAC.cpp Normal file
View File

@ -0,0 +1,82 @@
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>
#define LL long long
#define IT __int64
#define zero(x) fabs(x)<eps
#define mm(a,b) memset(a,b,sizeof(a))
const int INF=0x7fffffff;
const double inf=1e8;
const double eps=1e-10;
const double PI=acos(-1.0);
const int Max=200001;
using namespace std;
int s[Max];
int t[Max];
int sign(double x)
{
return (x>eps)-(x<-eps);
}
int main()
{
int n,m,i,j,q,p,res,k;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=1;i<=Max;i++)
{
s[i]=0;
}
for(i=1;i<=n;i++)
{
scanf("%d",&q);
s[q]=1;
}
k=0;
for(i=1;i<=Max;i++)
{
if(s[i]==0)
t[++k]=i;
}
sort(t+1,t+(k+1));
while(m--)
{
scanf("%d",&p);
if(s[p]==0)
printf("%d\n",p);
else
{
int left=1;
int right=k;
while(left<right)
{
int mid=(left+right)>>1;
if(p<t[mid])
{
res=t[mid];
right=mid-1;
if(p<t[right])
res=t[right];
}
else
{
left=mid+1;
if(p<t[left])
res=t[left];
}
}
printf("%d\n",res);
}
}
}
return 0;
}

37
HDOJ/4908_autoAC.cpp Normal file
View File

@ -0,0 +1,37 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define repf(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long ll;
const int N = 4e4+10;
int a[N], rec[N<<1];
int n, m, pos, cnt;
ll ans;
int main() {
while (~scanf("%d%d", &n, &m)) {
memset(rec, 0, sizeof(rec));
repf (i, 0, n - 1) {
scanf("%d", &a[i]);
if (a[i] == m) {
pos = i;
}
}
cnt = 0;
for (int i = pos; i >= 0; i--) {
if (a[i] > m) cnt++;
if (a[i] < m) cnt--;
rec[N + cnt]++;
}
cnt = 0;
ans = 0;
for (int i = pos; i < n; i++) {
if (a[i] > m) cnt++;
if (a[i] < m) cnt--;
ans += rec[N - cnt];
}
cout << ans << endl;
}
return 0;
}

81
HDOJ/4909_autoAC.cpp Normal file
View File

@ -0,0 +1,81 @@
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
typedef int ll;
const int N = 20005;
int t, n, v, x;
char str[N];
map<int, int> has;
int main() {
scanf("%d", &t);
while (t--) {
scanf("%s", str);
v = -1;
n = strlen(str);
for (int i = 0; i < n; i++) {
if (str[i] == '?') {
v = i;
break;
}
}
int ans = 0;
if (v == -1) {
x = 0;
has.clear();
has[0]++;
for (int i = 0; i < n; i++) {
x ^= (1<<(str[i] - 'a'));
ans += has[x];
has[x]++;
}
printf("%d\n", ans);
continue;
}
else {
has.clear();
ans = 0;
x = 0;
has[0]++;
for (int i = 0; i < v; i++) {
x ^= (1<<(str[i] - 'a'));
ans += has[x];
has[x]++;
}
has.clear();
x = 0;
has[0]++;
for (int i = v + 1; i < n; i++) {
x ^= (1<<(str[i] - 'a'));
ans += has[x];
has[x]++;
}
has.clear();
x = 0;
has[0]++;
for (int i = v + 1; i < n; i++) {
x ^= (1<<(str[i] - 'a'));
has[x]++;
}
x = 0;
if (has.count(x))
ans += has[x];
for (int j = 0; j < 26; j++) {
if (has.count(x^(1<<j)))
ans += has[x^(1<<j)];
}
for (int i = v - 1; i >= 0; i--) {
x ^= (1<<(str[i] - 'a'));
if (has.count(x))
ans += has[x];
for (int j = 0; j < 26; j++) {
if (has.count(x^(1<<j))) {
ans += has[x^(1<<j)];
}
}
}
}
printf("%d\n", ans);
}
return 0;
}

164
HDOJ/4910_autoAC.cpp Normal file
View File

@ -0,0 +1,164 @@
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
using namespace std;
typedef __int64 LL;
const LL NUM=20;
const int maxn=1e6+10;
int prime[maxn],c[maxn],tot;
LL t,f[1000];
LL mul_mod(LL a,LL b,LL n)
{
a=a%n;
b=b%n;
LL s=0;
while(b)
{
if(b&1)
s=(s+a)%n;
a=(a<<1)%n;
b=b>>1;
}
return s;
}
LL pow_mod(LL a,LL b,LL n)
{
a=a%n;
LL s=1;
while(b)
{
if(b&1)
s=mul_mod(s,a,n);
a=mul_mod(a,a,n);
b=b>>1;
}
return s;
}
bool check(LL a,LL n,LL r,LL s)
{
LL ans,p,i;
ans=pow_mod(a,r,n);
p=ans;
for(i=1;i<=s;i++)
{
ans=mul_mod(ans,ans,n);
if(ans==1&&p!=1&&p!=n-1)return true;
p=ans;
}
if(ans!=1)return true;
return false;
}
bool Miller_Rabin(LL n)
{
if(n<2)return false;
if(n==2)return true;
if(!(n&1))return false;
LL i,r,s,a;
r=n-1;s=0;
while(!(r&1)){r=r>>1;s++;}
for(i=0;i<NUM;i++)
{
a=rand()%(n-1)+1;
if(check(a,n,r,s))
return false;
}
return true;
}
LL gcd(LL a,LL b)
{
return b==0?a:gcd(b,a%b);
}
LL Pollard_rho(LL n,LL c)
{
LL i=1,j,k=2,x,y,d,p;
x=rand()%n;
y=x;
while(true)
{
i++;
x=(mul_mod(x,x,n)+c)%n;
if(y==x)return n;
if(y>x)p=y-x;
else p=x-y;
d=gcd(p,n);
if(d!=1&&d!=n)return d;
if(i==k)
{
y=x;
k+=k;
}
}
}
void find(LL n)
{
if(Miller_Rabin(n))
{
f[t++]=n;
return;
}
LL p=n;
while(p>=n)p=Pollard_rho(p,rand()%(n-1)+1);
find(p);
find(n/p);
}
void init()
{
memset(c,false,sizeof(c));
int i,j;
tot=0;
for(i=2;i<=1e6;i++)
{
if(!c[i])prime[tot++]=i;
for(j=0;j<tot;j++)
{
if(i*prime[j]>1e6)break;
c[i*prime[j]]=true;
if(i%prime[j]==0)break;
}
}
}
bool judge(LL n)
{
int i,j,k;
for(i=0;i<tot;i++)
{
if(n%prime[i]==0)
{
while(n%prime[i]==0)n=n/prime[i];
if(n==1)return true;
return false;
}
}
return false;
}
int main()
{
srand(time(NULL));
init();
LL n;
while(cin>>n)
{
if(n==-1)break;
if(n==1){cout<<0<<endl;continue;}
LL i,j,k,ans=0;
k=n;
while(k%2==0){k=k/2;ans++;}
if(ans>=1)ans--;
if(k!=1)
{
if(Miller_Rabin(k))ans++;
else
{
i=(LL)sqrt(k+0.5);
if(i*i==k&&Miller_Rabin(i)||judge(k))ans++;
else ans+=2;
}
}
if(ans<2)cout<<n-1<<endl;
else cout<<1<<endl;
}
return 0;
}

48
HDOJ/4911_autoAC.cpp Normal file
View File

@ -0,0 +1,48 @@
#include<stdio.h>
#include<string.h>
#define N 100005
__int64 cnt, k;
int a[N],c[N];
void merge(int a[], int first, int mid, int last, int c[])
{
int i = first, j = mid + 1;
int m = mid, n = last;
int k = 0;
while(i <= m || j <= n)
{
if(j > n || (i <= m && a[i] <= a[j]))
c[k++] = a[i++];
else
{
c[k++] = a[j++];
cnt += (m - i + 1);
}
}
for(i = 0; i < k; i++)
a[first + i] = c[i];
}
void merge_sort(int a[], int first, int last, int c[])
{
if(first < last)
{
int mid = (first + last) / 2;
merge_sort(a, first, mid, c);
merge_sort(a, mid+1, last, c);
merge(a, first, mid, last, c);
}
}
int main()
{
int n;
while(~scanf("%d%I64d",&n,&k))
{
memset(c, 0, sizeof(c));
cnt = 0;
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
merge_sort(a, 0, n-1, c);
if(k >= cnt) cnt = 0;
else cnt -= k;
printf("%I64d\n",cnt);
}
}

115
HDOJ/4912_autoAC.cpp Normal file
View File

@ -0,0 +1,115 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN=111111;
struct Edge{
int from,to,next;
Edge(){}
Edge(int f,int t,int n):from(f),to(t),next(n){}
}edge[MAXN*2];
int head[MAXN],tot,fa[MAXN][20],dep[MAXN],q[MAXN],n,m;
bool vis[MAXN];
struct Qes{
int u,v,lca;
Qes(){}
Qes(int _u,int _v,int _fa):u(_u),v(_v),lca(_fa){}
bool operator<(const Qes&rhs)const{
return dep[lca]>dep[rhs.lca];
}
}qes[MAXN];
void init(){
for(int i=1;i<=n;i++){
head[i]=-1;
vis[i]=0;
}
tot=0;
}
void add(int f,int t){
edge[tot]=Edge(f,t,head[f]);
head[f]=tot++;
}
void bfs(int root){
int tail=0;
q[tail++]=root;
fa[root][0]=root;dep[root]=0;
for(int i=0;i<tail;i++){
int u=q[i];
for(int i=1;i<20;i++) fa[u][i]=fa[fa[u][i-1]][i-1];
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].to;
if(v==fa[u][0])continue;
fa[v][0]=u;dep[v]=dep[u]+1;
q[tail++]=v;
}
}
}
int Lca(int x,int y){
if(dep[x]<dep[y]){int t=x;x=y;y=t;}
for(int i=19;~i;i--) if((dep[x]-dep[y])&(1<<i))x=fa[x][i];
if(x==y)return x;
for(int i=19;~i;i--) if(fa[x][i]!=fa[y][i]){x=fa[x][i],y=fa[y][i];}
return fa[x][0];
}
bool can(int u,int v,int lca){
if(vis[u]||vis[v]||vis[lca]) return false;
for(;u!=lca;u=fa[u][0])
if(vis[u]) return false;
for(;v!=lca;v=fa[v][0])
if(vis[v]) return false;
return true;
}
void cut(int u,int v,int lca){
for(;u!=lca;u=fa[u][0])
vis[u]=1;
for(;v!=lca;v=fa[v][0])
vis[v]=1;
vis[lca]=1;
}
inline bool get(int &a){
char c=0;
a=0;
do{
c=getchar();
}while(c!=EOF && (c<'0'||c>'9'));
if(c==EOF)return false;
while('0'<=c && c<='9'){a=a*10-'0'+c;c=getchar();}
return true;
}
inline void ot(int a){
if(a>9)
ot(a/10);
putchar(a%10+'0');
}
int main()
{
while(get(n)){
get(m);
init();
int u,v;
for(int i=1;i<n;i++){
get(u);
get(v);
add(u,v);
add(v,u);
}
bfs(1);
for(int i=0;i<m;i++){
get(u);
get(v);
qes[i]=Qes(u,v,Lca(u,v));
}
sort(qes,qes+m);
int ans=0;
for(int i=0;i<m;i++){
if(can(qes[i].u,qes[i].v,qes[i].lca)){
cut(qes[i].u,qes[i].v,qes[i].lca);
ans++;
}
}
ot(ans);
putchar('\n');
}
return 0;
}

124
HDOJ/4913_autoAC.cpp Normal file
View File

@ -0,0 +1,124 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<cmath>
#include<vector>
#define inf 0x3f3f3f3f
#define Inf 0x3FFFFFFFFFFFFFFFLL
#define eps 1e-8
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int maxn=100000+10;
const int mod=1000000000+7;
ll pow_mod(ll x,ll n)
{
ll res = 1;
while(n)
{
if(n & 1) res = res*x%mod;
n>>=1;
x= x*x%mod;
}
return res;
}
struct Node
{
int a,b,id;
Node(int a = 0,int b = 0,int id = 0):a(a),b(b),id(id){}
}node[maxn];
bool cmp1(Node x,Node y)
{
if(x.a == y.a) return x.id < y.id;
return x.a < y.a;
}
bool cmp2(Node x,Node y)
{
if(x.b == y.b) return x.a < y.a;
return x.b < y.b;
}
ll sum[maxn<<2],size[maxn<<2];
void PushUp(int rt)
{
sum[rt] = sum[rt<<1] + sum[rt<<1|1]*(size[rt<<1] + 1)%mod;
sum[rt] %= mod;
size[rt] = size[rt<<1] + size[rt<<1|1] + size[rt<<1]*size[rt<<1|1]%mod;
size[rt] %= mod;
}
void build(int l,int r,int rt)
{
sum[rt] = size[rt] = 0;
if(l == r) return ;
int m=(l+r)>>1;
build(l,m,rt<<1);
build(m+1,r,rt<<1|1);
}
void Update(int p,int l,int r,int rt,ll v)
{
if(l == r)
{
sum[rt] = v;
size[rt] = 1;
return ;
}
int m = (l+r)>>1;
if(m >= p) Update(p,l,m,rt<<1,v);
else Update(p,m+1,r,rt<<1|1,v);
PushUp(rt);
}
ll Query(int L,int R,int l,int r,int rt,ll & sz)
{
if(l >= L && r <= R)
{
sz = size[rt];
return sum[rt];
}
int m = (l+r)>>1;
if(m >= R) return Query(L,R,l,m,rt<<1,sz);
else if(m < L) return Query(L,R,m+1,r,rt<<1|1,sz);
else
{
ll res1,res2,sz1,sz2;
res1 = Query(L,R,l,m,rt<<1,sz1);
res2 = Query(L,R,m+1,r,rt<<1|1,sz2);
sz = (sz1 + sz2 + sz1*sz2%mod)%mod;
return (res1 + res2*(sz1+1))%mod;
}
}
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i = 1;i <= n;++i)
scanf("%d%d",&node[i].a,&node[i].b);
sort(node + 1,node + n +1,cmp2);
for(int i = 1;i <= n;++i)
node[i].id = i;
sort(node + 1,node + n +1,cmp1);
build(1,n,1);
ll ans = 0,lsize,rval,tmp,p2,p3;
int pos;
for(int i = 1;i <= n;++i)
{
pos = node[i].id;
p2 = pow_mod(2,node[i].a);
p3 = pow_mod(3,node[i].b);
Query(1,pos,1,n,1,lsize);
ans += p2*p3%mod*(lsize + 1)%mod;
rval = Query(pos,n,1,n,1,tmp);
ans += p2*rval%mod;
ans += p2*lsize%mod*rval%mod;
ans %= mod;
Update(pos,1,n,1,p3);
}
printf("%I64d\n",ans);
}
return 0;
}

125
HDOJ/4914_autoAC.cpp Normal file
View File

@ -0,0 +1,125 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const double pi = acos(-1.0);
const int maxn = 1 << 18;
const int mo=119;
struct Complex {
double x, y;
Complex (double real = 0, double imag = 0) : x(real), y(imag) {}
double &real() {
return x;
}
double &imag() {
return y;
}
void print()
{
cout<<"real="<<x<<" imag=%.7lf\n"<<y<<endl;
}
}Pa[50000],Pb[50000],Pc[50000];
int f[50000];
int n,a,b,p,q,N;
Complex operator+(const Complex &a, const Complex &b) {
return Complex(a.x + b.x, a.y + b.y);
}
Complex operator-(const Complex &a, const Complex &b) {
return Complex(a.x - b.x, a.y - b.y);
}
Complex operator*(const Complex &a, const Complex &b) {
return Complex(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}
inline void sincos(double theta,double &p0,double &p1)
{
p0=sin(theta);
p1=cos(theta);
}
void FFT(Complex P[], int n, int oper)
{
for (int i = 1, j = 0; i < n - 1; i++) {
for (int s = n; j ^= s >>= 1, ~j & s;);
if (i < j) {
swap(P[i], P[j]);
}
}
Complex unit_p0;
for (int d = 0; (1 << d) < n; d++) {
int m = 1 << d, m2 = m * 2;
double p0 = pi / m * oper;
sincos(p0, unit_p0.y, unit_p0.x);
for (int i = 0; i < n; i += m2) {
Complex unit = 1;
for (int j = 0; j < m; j++) {
Complex &P1 = P[i + j + m], &P2 = P[i + j];
Complex t = unit * P1;
P1 = P2 - t;
P2 = P2 + t;
unit = unit * unit_p0;
}
}
}
}
struct poly{
int u[50000];
poly() {
memset(u,0,sizeof(u));
}
poly operator *(poly &B) {
poly c;
for (int i=0;i<q;i++) Pa[i]=Complex(u[i],0);
for (int i=0;i<N-q;i++) Pa[i+q]=Complex(0,0);
for (int i=0;i<q;i++) Pb[i]=Complex(B.u[i],0);
for (int i=0;i<N-q;i++) Pb[i+q]=Complex(0,0);
FFT(Pa,N,1),FFT(Pb,N,1);
for (int i=0;i<N;i++) Pc[i]=Pa[i]*Pb[i];
FFT(Pc,N,-1);
for (int i=0;i<N;i++) c.u[i]=((long long)(Pc[i].x/N+0.5))%mo;
for (int i=N;i>=q;i--)
if (c.u[i])
(c.u[i-p]+=(c.u[i]*a)%mo)%=mo,
(c.u[i-q]+=(c.u[i]*b)%mo)%=mo;
return c;
}
void print()
{
for (int i=0;i<q;i++) cout<<u[i]<<' ';cout<<endl;
}
};
int func(int x)
{
if (x<=0) return 1;
return f[x];
}
void fgm(int e,poly &sum,poly &b)
{
sum.u[0]=1;
b.u[1]=1;
for (;e;e>>=1) {
if (e&1) sum=sum*b;
b=b*b;
}
}
int main()
{
for (;cin>>n>>a>>b>>p>>q;) {
a%=mo,b%=mo;
for (int i=1;i<=2*q;i++)
f[i]=(a*func(i-p)+b*func(i-q))%mo;
if (n<=2*q) {
printf("%d\n",f[n]);
continue;
}
for (N=1;N<=q+q+1;N<<=1) ;
poly A,B;
fgm(n-q,A,B);
int ans=0;
for (int i=q-1;i>=0;i--)
ans=(ans+A.u[i]*f[i+q])%mo;
printf("%d\n",ans);
}
return 0;
}

87
HDOJ/4915_autoAC.cpp Normal file
View File

@ -0,0 +1,87 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define mem(a,b) memset(a,0,sizeof(a))
#define L 1000005
#define up(i,x,y) for(i = x;i<=y;i++)
#define down(i,x,y) for(i = x;i>=y;i--)
#define bre(x,y) if(x){flag=y;break;}
#define con(x,y) if(x){ans(y);continue;}
char s[L];
int a[L][2],b[L][2],len,flag;
void ans(int i)
{
if(i==1) printf("Unique\n");
else if(i==2) printf("Many\n");
else printf("None\n");
}
int main()
{
int i,j;
while(~scanf("%s",s+1))
{
len = strlen(s+1);
con((len&1),0)
mem(a,0);
mem(b,0);
flag = 1;
up(i,1,len)
{
if(s[i]=='(')
a[i][0]=a[i-1][0]+1,a[i][1]=a[i-1][1]+1;
else if(s[i]==')')
{
if(!a[i-1][0])
a[i][0]=1;
else
a[i][0]=a[i-1][0]-1;
a[i][1]=a[i-1][1]-1;
}
else if(s[i]=='?')
{
if(!a[i-1][0]) a[i][0]=1;
else
a[i][0]=a[i-1][0]-1;
a[i][1]=a[i-1][1]+1;
}
bre((a[i][0]>a[i][1]),0)
}
con((!flag),0)
down(i,len,1)
{
if(s[i]==')')
b[i-1][0]=b[i][0]+1,b[i-1][1]=b[i][1]+1;
else if(s[i]=='(')
{
if(!b[i][0])
b[i-1][0]=1;
else
b[i-1][0]=b[i][0]-1;
b[i-1][1]=b[i][1]-1;
}
else if(s[i]=='?')
{
if(!b[i][0]) b[i-1][0]=1;
else
b[i-1][0]=b[i][0]-1;
b[i-1][1]=b[i][1]+1;
}
bre((b[i][0]>b[i][1]),0)
}
con((!flag),0)
int l,r;
flag = 1;
up(i,1,len)
{
l = max(a[i][0],b[i][0]);
r = min(a[i][1],b[i][1]);
bre((l>r),0)
bre((l<r),2)
}
con((flag==0),0);
con((flag==1),1);
con((flag==2),2);
}
return 0;
}

120
HDOJ/4916_autoAC.cpp Normal file
View File

@ -0,0 +1,120 @@
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
int fun(){
char ch;int flag=1,a=0;
while(ch=getchar())if((ch>='0'&&ch<='9')||ch=='-')break;
if(ch=='-')flag=-1;else a=ch-'0';
while(ch=getchar()){
if(ch>='0'&&ch<='9')a=10*a+ch-'0';
else break;
}
return flag*a;
}
const int maxn=1001000;
int head[maxn],tol;
int subtree[maxn];
int belong[maxn];
int child[maxn][4];
int que[maxn];
int path[maxn];
int fa[maxn];
int dep[maxn];
struct Edge{
int next,to;
}edge[2*maxn];
void addedge(int u,int v){
edge[tol].to=v;
edge[tol].next=head[u];
head[u]=tol++;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m)){
memset(head,-1,sizeof(head));tol=0;
for(int i=1;i<n;i++){
int u,v;
u=fun();v=fun();
addedge(u,v);
addedge(v,u);
}
int front=0,rear=0;
dep[1]=0;fa[1]=-1;
que[rear++]=1;
while(front!=rear){
int u=que[front++];
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(v==fa[u])continue;
dep[v]=dep[u]+1;
fa[v]=u;
que[rear++]=v;
}
}
for(int i=1;i<=n;i++)
for(int j=0;j<4;j++)
child[i][j]=INF;
for(int i=rear-1;i>=0;i--){
int u=que[i];
subtree[u]=min(u,child[u][0]);
int p=fa[u];
if(p==-1)continue;
child[p][3]=subtree[u];
sort(child[p],child[p]+4);
}
front=0,rear=0;
path[1]=INF;
belong[1]=-1;
for(int i=head[1];i!=-1;i=edge[i].next){
int u=edge[i].to;
path[u]=INF;
belong[u]=subtree[u];
que[rear++]=u;
}
while(front!=rear){
int u=que[front++];
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(v==fa[u])continue;
path[v]=min(path[u],child[u][subtree[v]==child[u][0]]);
belong[v]=belong[u];
que[rear++]=v;
}
path[u]=min(path[u],child[u][0]);
}
int last=0;
while(m--){
int u,v;
u=fun();v=fun();
u^=last;v^=last;
if(u>v)swap(u,v);
if(u!=1&&belong[u]==belong[v])last=1;
else{
int i=0;
while(child[1][i]==belong[u]||child[1][i]==belong[v])i++;
last=u==1?path[v]:min(path[u],path[v]);
last=min(last,child[1][i]);
}
printf("%d\n",last);
}
}
return 0;
}

103
HDOJ/4917_autoAC.cpp Normal file
View File

@ -0,0 +1,103 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#define ll __int64
using namespace std ;
vector<int> vec[44] ;
const int mod = 1000000007 ;
int dp[1<<21] ;
int fa[44] , sz[44] , vis[44] ;
int to[44] , id[44] , du[44] ;
int c[44][44] ;
void init () {
c[0][0] = 1 ;
for ( int i = 1 ; i < 44 ; i ++ ) {
c[i][0] = 1 ;
for ( int j = 1 ; j <= i ; j ++ )
c[i][j] = ( c[i-1][j] + c[i-1][j-1] ) % mod ;
}
}
int find ( int a ) {
return fa[a] == a ? a : fa[a] = find ( fa[a] ) ;
}
struct Point {
int id , st ;
Point () {}
Point (int a , int b):id(a),st(b) {}
} ;
int gao ( int s , int n , int& re) {
int tot = 0 ;
queue<Point> Q ;
for ( int i = 1 ; i <= n ; i ++ ) {
if ( find (i) == s && du[i] == 0 ) {
to[tot++] = 0 ;
Q.push ( Point(i,1<<tot-1) ) ;
}
}
while ( !Q.empty () ) {
Point u = Q.front () ;
int st = u.st ;
int f = u.id ;
Q.pop () ;
for ( int i = 0 ; i < vec[f].size () ; i ++ ) {
int v = vec[f][i] ;
du[v] -- ;
id[v] |= st ;
if ( du[v] == 0 ) {
to[tot++] = id[v] ;
Q.push ( Point(v,id[v]|(1<<(tot-1))) ) ;
}
}
}
ll ret = c[re][tot] ;
re -= tot ;
dp[0] = 1 ;
for ( int i = 1 ; i < 1<<tot ; i ++ ) {
dp[i] = 0 ;
for ( int j = 0 ; j < tot ; j ++ ) {
if ( ((i&(1<<j))) ) {
int st = i ^ (1<<j) ;
if ( (~st)&to[j] ) continue ;
dp[i] += dp[i^(1<<j)] ;
dp[i] %= mod ;
}
}
}
ret *= dp[(1<<tot)-1] ;
ret %= mod ;
return ret ;
}
int main () {
init () ;
int n , m ;
while ( scanf ( "%d%d" , &n , &m ) != EOF ) {
int re = n ;
memset ( vis , 0 , sizeof ( vis ) ) ;
for ( int i = 1 ; i <= n ; i ++ ) {
vec[i].clear () ;
fa[i] = i ;
du[i] = 0 ;
id[i] = 0 ;
}
for ( int i = 1 ; i <= m ; i ++ ) {
int a , b ;
scanf ( "%d%d" , &a , &b ) ;
vec[a].push_back ( b ) ;
du[b] ++ ;
int x = find ( a ) , y = find ( b ) ;
if ( x != y ) fa[x] = y ;
}
ll ans = 1 ;
for ( int i = 1 ; i <= n ; i ++ ) {
if ( !vis[find(i)] ) {
ans *= gao (find(i),n,re) ;
ans %= mod ;
vis[find(i)] = 1 ;
}
}
printf ( "%I64d\n" , ans ) ;
}
return 0 ;
}

204
HDOJ/4918_autoAC.cpp Normal file
View File

@ -0,0 +1,204 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#define inf 0x3f3f3f3f
#define Inf 0x3FFFFFFFFFFFFFFFLL
#define eps 1e-9
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int maxn = 100000 + 10;
const int M = 40;
int pool[maxn*M];
int pool_size;
struct BIT
{
int *C,n;
void Init(int n)
{
this -> n = n;
C = pool + pool_size;
for(int i = 0;i < n;++i)
C[i] = 0;
pool_size += n;
}
void add(int x,int v)
{
for(;x < n; x += x & -x)
C[x] += v;
}
int sum(int x)
{
if(x >= n) x = n - 1;
int res = 0;
for(;x > 0; x -= x & -x)
res += C[x];
return res;
}
}tree[maxn<<1];
struct Node
{
int root,subtree,dis;
Node(int root = 0,int subtree = 0,int dis = 0):root(root),subtree(subtree),dis(dis){}
};
vector<Node>vt[maxn];
struct Edge
{
int v,next;
Edge(int v = 0,int next = 0):v(v),next(next){}
}edges[maxn<<1];
int head[maxn],w[maxn],id[maxn<<1],cnt,nEdge;
int q[maxn],dp[maxn],fa[maxn],flag[maxn],d[maxn];
void AddEdges(int u,int v)
{
edges[++nEdge] = Edge(v,head[u]);
head[u] = nEdge;
edges[++nEdge] = Edge(u,head[v]);
head[v] = nEdge;
}
void Init(int n)
{
pool_size = cnt = 0;
memset(head,0xff,sizeof(head));
memset(flag,0,sizeof(flag));
nEdge = -1;
for(int i = 1;i <= n;++i)
vt[i].clear();
}
int getroot(int x,int & tot)
{
int tail = 0,u,v;
q[tail++] = x;
fa[x] = -1;
for(int i = 0;i < tail;++i)
{
u = q[i];
for(int k = head[u];k != -1;k = edges[k].next)
{
v = edges[k].v;
if(v != fa[u] && !flag[v])
{
fa[v] = u;
q[tail++] = v;
}
}
}
int minv = inf,root = x,mx;
for(int i = tail - 1;i >= 0;--i)
{
u = q[i];
dp[u] = 1;
mx = 0;
for(int k = head[u];k != -1;k = edges[k].next)
{
v = edges[k].v;
if(v != fa[u] && !flag[v])
{
mx = max(mx,dp[v]);
dp[u] += dp[v];
}
}
mx = max(mx,tail - dp[u]);
if(mx < minv) minv = mx,root = u;
}
tot = tail;
return root;
}
void cal(int x,int pa,int subtree)
{
int tail = 0,u,v;
q[tail++] = x;
d[x] = 1;
fa[x] = pa;
for(int i = 0;i < tail;++i)
{
u = q[i];
vt[u].push_back(Node(id[pa],subtree,d[u]));
tree[id[pa]].add(d[u]+1,w[u]);
tree[subtree].add(d[u],w[u]);
for(int k = head[u];k != -1;k = edges[k].next)
{
v = edges[k].v;
if(v != fa[u] && !flag[v])
{
d[v] = d[u] + 1;
fa[v] = u;
q[tail++] = v;
}
}
}
}
void solve(int x)
{
int num;
int rt = getroot(x,num);
flag[rt] = 1;
id[rt] = ++cnt;
vt[rt].push_back(Node(cnt,-1,0));
tree[cnt].Init(num + 2);
tree[cnt].add(1,w[rt]);
for(int k = head[rt];k != -1;k = edges[k].next)
if(!flag[edges[k].v])
{
++cnt;
tree[cnt].Init(dp[edges[k].v] + 2);
cal(edges[k].v,rt,cnt);
}
for(int k = head[rt];k != -1;k = edges[k].next)
if(!flag[edges[k].v]) solve(edges[k].v);
}
char s[20];
int main()
{
int n,q;
while(~scanf("%d%d",&n,&q))
{
Init(n);
for(int i = 1;i <= n;++i)
scanf("%d",&w[i]);
int u,v;
for(int i = 1;i < n;++i)
{
scanf("%d%d",&u,&v);
AddEdges(u,v);
}
solve(1);
Node node;
while(q--)
{
scanf("%s%d%d",s,&u,&v);
if(s[0] == '!')
{
int d = v - w[u];
for(int i = 0;i < (int)vt[u].size();++i)
{
node = vt[u][i];
tree[node.root].add(node.dis + 1,d);
if(node.subtree != -1)
tree[node.subtree].add(node.dis , d);
}
w[u] += d;
}
else
{
int d = v,ans = 0;
for(int i = 0;i < (int)vt[u].size();++i)
{
node = vt[u][i];
ans += tree[node.root].sum(d - node.dis + 1);
if(node.subtree != -1)
ans -= tree[node.subtree].sum(d - node.dis);
}
printf("%d\n",ans);
}
}
}
return 0;
}

42
HDOJ/4920_autoAC.cpp Normal file
View File

@ -0,0 +1,42 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int a[805][805],b[805][805],c[805][805];
int main()
{
int n,i,j,k;
while(~scanf("%d",&n))
{
for(i = 0; i<n; i++)
for(j = 0; j<n; j++)
{
scanf("%d",&a[i][j]);
a[i][j]%=3;
c[i][j] = 0;
}
for(i = 0; i<n; i++)
for(j = 0; j<n; j++)
{
scanf("%d",&b[i][j]);
b[i][j]%=3;
}
for(i = 0; i<n; i++)
for(j = 0; j<n; j++)
{
if(!a[i][j])
continue;
for(k = 0; k<n; k++)
c[i][k] = c[i][k]+a[i][j]*b[j][k];
}
for(i = 0; i<n; i++)
{
for(j = 0; j<n; j++)
if(j==n-1)
printf("%d\n",c[i][j]%3);
else
printf("%d ",c[i][j]%3);
}
}
return 0;
}

69
HDOJ/4921_autoAC.cpp Normal file
View File

@ -0,0 +1,69 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std ;
int son[11111] ;
int chain[10][1111] , a[11111] ;
int vis[11111] , du[11111] , len[11] ;
double dfs ( int u , int step , int c ) {
chain[c][step] = a[u] ;
if ( son[u] == -1 ) return step + 2 ;
return dfs ( son[u] , step + 1 , c ) ;
}
int main () {
int n , m ;
int T ;
scanf ( "%d" , &T ) ;
while ( T -- ) {
scanf ( "%d%d" , &n , &m ) ;
memset ( chain , 0 , sizeof ( chain ) ) ;
memset ( du , 0 , sizeof ( du ) ) ;
memset ( son , -1 , sizeof ( son ) ) ;
for ( int i = 0 ; i < n ; i ++ )
scanf ( "%d" , &a[i] ) ;
for ( int i = 1 ; i <= m ; i ++ ) {
int a , b ;
scanf ( "%d%d" , &a , &b ) ;
son[a] = b ;
du[b] ++ ;
}
double sum = 1 , ans = 0 ;
int tot = 0 ;
for ( int i = 0 ; i < n ; i ++ ) {
if ( du[i] == 0 ) {
len[tot] = dfs ( i , 0 , tot ) ;
sum *= len[tot] ;
len[tot] -- ;
tot ++ ;
}
}
sum -= 1 ;
for ( int i = 0 ; i < 1000 ; i ++ ) {
for ( int j = 0 ; j < 1 << tot ; j ++ ) {
int flag = 0 ;
double add = 0 , p = 1 , x = 0 , cnt = 0 ;
for ( int k = 0 ; k < tot ; k ++ ) {
if ( chain[k][i] ) cnt ++ ;
if ( j & (1<<k) ) {
if ( chain[k][i] == 0 ) {
flag = 1 ;
break ;
}
add += chain[k][i] ;
x ++ ;
p *= len[k] - i ;
}
else {
p *= min ( len[k] + 1 , i + 1 ) ;
}
}
if ( flag ) continue ;
double fuck = ( add + (x>1?(add*x/cnt):0) ) * p ;
ans += fuck ;
}
}
printf ( "%.3f\n" , ans / sum ) ;
}
return 0 ;
}

89
HDOJ/4923_autoAC.cpp Normal file
View File

@ -0,0 +1,89 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
const int maxn=1e5+10;
const double eps=1e-8;
const int INF=2e9;
struct node{
int id,num0,num1;
double rate;
}e[maxn],f,g;
int t,a[maxn];
stack<node>mm;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int i,j,k,n,p,q;
double num,ans=0;
t=0;
scanf("%d",&n);
int l=0,r=n-1;
for(i=0;i<n;i++)scanf("%d",&a[i]);
a[n]=1;
while(a[l]==0)l++;
while(a[r]==1)r--;
if(l>r){printf("0.000000\n");continue;}
for(i=l;i<=r;)
{
j=k=0;
while(a[i]==1){i++;j++;}
while(a[i]==0){i++;k++;}
e[t].id=t;e[t].num1=j;e[t].num0=k;e[t].rate=1.0*j/(j+k);
t++;
}
while(!mm.empty())mm.pop();
for(i=0;i<t;i++)
{
if(mm.empty())mm.push(e[i]);
else
{
f=mm.top();
if(f.rate<=e[i].rate)mm.push(e[i]);
else
{
g=e[i];
while(true)
{
f=mm.top();
if(f.rate>g.rate)
{
g.num1+=f.num1;
g.num0+=f.num0;
g.rate=1.0*g.num1/(g.num0+g.num1);
mm.pop();
}
else
{
mm.push(g);
break;
}
if(mm.empty())
{
mm.push(g);
break;
}
}
}
}
}
while(!mm.empty())
{
f=mm.top();
mm.pop();
ans+=f.rate*f.rate*f.num0+(1-f.rate)*(1-f.rate)*f.num1;
}
printf("%.6f\n",ans);
}
return 0;
}

144
HDOJ/4924_autoAC.cpp Normal file
View File

@ -0,0 +1,144 @@
#include <stdio.h>
#define INF 99999999
#define max(A,B)(A>B?A:B)
struct S{
int c[4],p[4],mx;
bool flag[4];
}node[20];
int n,hash[100],add[20][20],a,b,c,ansc,ansp,df,mf,st,gk,attach,dpc[13][3][13][13],dpp[13][3][13][13],sel[13];
void dfs(int now,int cnt)
{
if(cnt==11)
{
int i,j,k,l,mx;
attach=0;
for(i=0;i<11;i++) for(j=0;j<11;j++) attach+=add[sel[i]][sel[j]];
mx=0;
for(i=0;i<11;i++) mx+=node[sel[i]].mx;
if(mx+attach<ansc) return;
gk=df=mf=st=0;
for(i=0;i<11;i++)
{
if(node[sel[i]].flag[0]) gk++;
if(node[sel[i]].flag[1]) df++;
if(node[sel[i]].flag[2]) mf++;
if(node[sel[i]].flag[3]) st++;
}
if(gk<1 || df<a || mf<b || st<c) return;
for(i=0;i<=11;i++) for(j=0;j<=1;j++) for(k=0;k<=a;k++) for(l=0;l<=b;l++) dpc[i][j][k][l]=-INF;
dpc[0][0][0][0]=dpp[0][0][0][0]=0;
for(i=0;i<11;i++)
{
for(j=0;j<=1;j++)
{
for(k=0;k<=a;k++)
{
for(l=0;l<=b;l++)
{
if(dpc[i][j][k][l]!=-INF)
{
if(node[sel[i]].flag[0])
{
if(dpc[i+1][j+1][k][l]<dpc[i][j][k][l]+node[sel[i]].c[0])
{
dpc[i+1][j+1][k][l]=dpc[i][j][k][l]+node[sel[i]].c[0];
dpp[i+1][j+1][k][l]=dpp[i][j][k][l]+node[sel[i]].p[0];
}
else if(dpc[i+1][j+1][k][l]==dpc[i][j][k][l]+node[sel[i]].c[0])
{
dpp[i+1][j+1][k][l]=max(dpp[i][j][k][l]+node[sel[i]].p[0],dpp[i+1][j+1][k][l]);
}
}
if(node[sel[i]].flag[1])
{
if(dpc[i+1][j][k+1][l]<dpc[i][j][k][l]+node[sel[i]].c[1])
{
dpc[i+1][j][k+1][l]=dpc[i][j][k][l]+node[sel[i]].c[1];
dpp[i+1][j][k+1][l]=dpp[i][j][k][l]+node[sel[i]].p[1];
}
else if(dpc[i+1][j][k+1][l]==dpc[i][j][k][l]+node[sel[i]].c[1])
{
dpp[i+1][j][k+1][l]=max(dpp[i][j][k][l]+node[sel[i]].p[1],dpp[i+1][j][k+1][l]);
}
}
if(node[sel[i]].flag[2])
{
if(dpc[i+1][j][k][l+1]<dpc[i][j][k][l]+node[sel[i]].c[2])
{
dpc[i+1][j][k][l+1]=dpc[i][j][k][l]+node[sel[i]].c[2];
dpp[i+1][j][k][l+1]=dpp[i][j][k][l]+node[sel[i]].p[2];
}
else if(dpc[i+1][j][k][l+1]==dpc[i][j][k][l]+node[sel[i]].c[2])
{
dpp[i+1][j][k][l+1]=max(dpp[i][j][k][l]+node[sel[i]].p[2],dpp[i+1][j][k][l+1]);
}
}
if(node[sel[i]].flag[3])
{
if(dpc[i+1][j][k][l]<dpc[i][j][k][l]+node[sel[i]].c[3])
{
dpc[i+1][j][k][l]=dpc[i][j][k][l]+node[sel[i]].c[3];
dpp[i+1][j][k][l]=dpp[i][j][k][l]+node[sel[i]].p[3];
}
else if(dpc[i+1][j][k][l]==dpc[i][j][k][l]+node[sel[i]].c[3])
{
dpp[i+1][j][k][l]=max(dpp[i][j][k][l]+node[sel[i]].p[3],dpp[i+1][j][k][l]);
}
}
}
}
}
}
}
dpc[11][1][a][b]+=attach;
if(ansc<dpc[11][1][a][b]) ansc=dpc[11][1][a][b],ansp=dpp[11][1][a][b];
else if(ansc==dpc[11][1][a][b]) ansp=max(dpp[11][1][a][b],ansp);
return;
}
if(now>=n || cnt+n-now<11) return;
sel[cnt]=now;
dfs(now+1,cnt+1);
dfs(now+1,cnt);
}
int main()
{
int T,i,j,t,id,val;
char s[5];
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&t);
hash[t]=i;
for(j=0;j<4;j++) node[i].flag[j]=0;
node[i].mx=-INF;
scanf("%d",&t);
while(t--)
{
scanf("%s",s);
if(s[0]=='G') id=0;
else if(s[0]=='D') id=1;
else if(s[0]=='M') id=2;
else id=3;
scanf("%d%d",&node[i].c[id],&node[i].p[id]);
node[i].mx=max(node[i].mx,node[i].c[id]);
node[i].flag[id]=1;
}
}
scanf("%d",&t);
for(i=0;i<n;i++) for(j=0;j<n;j++) add[i][j]=0;
while(t--)
{
scanf("%d%d%s%d",&a,&b,s,&val);
if(s[0]=='D') add[hash[a]][hash[b]]=-val;
else add[hash[a]][hash[b]]=val;
}
scanf("%d-%d-%d",&a,&b,&c);
ansc=ansp=-INF;
dfs(0,0);
if(ansc>-INF) printf("%d %d\n",ansc,ansp);
else printf("Poor Manager!\n");
}
}

57
HDOJ/4925_autoAC.cpp Normal file
View File

@ -0,0 +1,57 @@
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 110;
int map[maxn][maxn];
int n, m;
int cal(int i, int j) {
int ans = 1;
if (map[i][j+1] == 0)
ans <<= 1;
if (map[i+1][j] == 0)
ans <<= 1;
if (map[i-1][j] == 0)
ans <<= 1;
if (map[i][j-1] == 0)
ans <<= 1;
return ans;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &m);
memset(map, -1, sizeof(map));
int flag = 0;
for (int i = 1; i <= m; i++) {
map[1][i] = flag;
flag = !flag;
}
for (int i = 2; i <= n; i++)
for (int j = 1; j <= m; j++)
map[i][j] = !map[i-1][j];
int ans = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (map[i][j])
ans += cal(i, j);
memset(map, -1, sizeof(map));
flag = 1;
for (int i = 1; i <= m; i++) {
map[1][i] = flag;
flag = !flag;
}
for (int i = 2; i <= n; i++)
for (int j = 1; j <= m; j++)
map[i][j] = !map[i-1][j];
int tmp = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (map[i][j])
tmp += cal(i, j);
printf("%d\n", max(ans, tmp));
}
return 0;
}

244
HDOJ/4927_autoAC.cpp Normal file
View File

@ -0,0 +1,244 @@
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 9999
#define MAXSIZE 10
#define DLEN 4
class BigInt
{
private:
int a[500];
int len;
public:
BigInt() {len = 1; memset(a, 0, sizeof(a));}
BigInt(const int);
BigInt(const char *);
BigInt(const BigInt &);
BigInt &operator = (const BigInt &);
BigInt operator + (const BigInt &) const;
BigInt operator - (const BigInt &) const;
BigInt operator * (const BigInt &) const;
BigInt operator / (const int &) const;
bool operator > (const BigInt &T) const;
bool operator < (const BigInt &T) const;
bool operator == (const BigInt &T) const;
bool operator > (const int &t) const;
bool operator < (const int &t) const;
bool operator == (const int &t) const;
void print();
};
bool BigInt::operator == (const BigInt &T) const {
return !(*this > T) && !(T > *this);
}
bool BigInt::operator == (const int &t) const {
BigInt T = BigInt(t);
return *this == T;
}
bool BigInt::operator < (const BigInt &T) const {
return T > *this;
}
bool BigInt::operator < (const int &t) const {
return BigInt(t) > *this;
}
BigInt::BigInt(const int b) {
int c, d = b;
len = 0;
memset(a, 0, sizeof(a));
while(d > MAXN) {
c = d - (d / (MAXN + 1)) * (MAXN + 1);
d = d / (MAXN + 1);
a[len++] = c;
}
a[len++] = d;
}
BigInt::BigInt(const char *s) {
int t, k, index, l, i;
memset(a, 0, sizeof(a));
l = strlen(s);
len = l / DLEN;
if(l % DLEN)
len++;
index = 0;
for(i = l - 1; i >= 0; i -= DLEN)
{
t = 0;
k = i - DLEN + 1;
if(k < 0)
k = 0;
for(int j = k;j <= i; j++)
t = t * 10 + s[j] - '0';
a[index++] = t;
}
}
BigInt::BigInt(const BigInt &T) : len(T.len)
{
int i;
memset(a, 0, sizeof(a));
for(i = 0; i < len; i++)
a[i] = T.a[i];
}
BigInt & BigInt::operator = (const BigInt &n)
{
int i;
len = n.len;
memset(a, 0, sizeof(a));
for(i = 0; i < len; i++)
a[i] = n.a[i];
return *this;
}
BigInt BigInt::operator + (const BigInt &T) const{
BigInt t(*this);
int i, big;
big = T.len > len ? T.len : len;
for(int i = 0; i < big; i++)
{
t.a[i] += T.a[i];
if(t.a[i] > MAXN)
{
t.a[i+1]++;
t.a[i] -= MAXN + 1;
}
}
if(t.a[big] != 0)
t.len = big + 1;
else
t.len = big;
return t;
}
BigInt BigInt::operator - (const BigInt &T) const {
int i, j, big;
bool flag;
BigInt t1, t2;
if(*this > T)
{
t1 = *this;
t2 = T;
flag = 0;
}
else
{
t1 = T;
t2 = *this;
flag = 1;
}
big = t1.len;
for(i = 0; i < big; i++)
{
if(t1.a[i] < t2.a[i])
{
j = i + 1;
while(t1.a[j] == 0)
j++;
t1.a[j--]--;
while(j > i)
t1.a[j--] += MAXN;
t1.a[i] += MAXN + 1 - t2.a[i];
}
else
t1.a[i] -= t2.a[i];
}
t1.len = big;
while(t1.a[t1.len - 1] == 0 && t1.len > 1)
{
t1.len--;
big--;
}
if(flag)
t1.a[big-1] = 0 - t1.a[big-1];
return t1;
}
BigInt BigInt::operator * (const BigInt &T) const {
BigInt ret;
int i, j, up;
int tmp, temp;
for(i = 0; i < len; i++)
{
up = 0;
for(j = 0; j < T.len; j++)
{
temp = a[i] * T.a[j] + ret.a[i+j] + up;
if(temp > MAXN)
{
tmp = temp - temp / (MAXN + 1) * (MAXN + 1);
up = temp / (MAXN + 1);
ret.a[i + j] = tmp;
}
else
{
up = 0;
ret.a[i + j] = temp;
}
}
if(up != 0)
ret.a[i + j] = up;
}
ret.len = i + j;
while(ret.a[ret.len - 1] == 0 && ret.len > 1)
ret.len--;
return ret;
}
BigInt BigInt::operator / (const int &b) const {
BigInt ret;
int i, down = 0;
for(i = len - 1; i >= 0; i--)
{
ret.a[i] = (a[i] + down * (MAXN + 1)) / b;
down = a[i] + down * (MAXN + 1) - ret.a[i] * b;
}
ret.len = len;
while(ret.a[ret.len - 1] == 0 && ret.len > 1)
ret.len--;
return ret;
}
bool BigInt::operator > (const BigInt &T) const {
int ln;
if(len > T.len)
return true;
else if(len == T.len)
{
ln = len - 1;
while(a[ln] == T.a[ln] && ln >= 0)
ln--;
if(ln >= 0 && a[ln] > T.a[ln])
return true;
else
return false;
}
else
return false;
}
bool BigInt::operator > (const int &t) const {
BigInt b(t);
return *this > b;
}
void BigInt::print()
{
printf("%d",a[len-1]);
for(int i = len - 2; i >= 0; i--)
printf("%04d", a[i]);
printf("\n");
}
int main()
{
int n, T, i, j, a[3005];
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i = 1; i <= n; i++)
scanf("%d",&a[i]);
BigInt positive(0), negative(0), C(1), tmp;
for(i = 0; i <= n - 1; i++)
{
if(i > 0) C = (C * (BigInt)(n - i)) / i;
tmp = C * (BigInt)(a[n-i]);
if(i&1)
negative = negative + tmp;
else
positive = positive + tmp;
}
BigInt ans = positive - negative;
ans.print();
}
return 0;
}

63
HDOJ/4928_autoAC.cpp Normal file
View File

@ -0,0 +1,63 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int SIZEN=100005;
LL a[SIZEN];
LL t_a[SIZEN];
int val_h,val_t;
int check(int cnt){
int flag=1;
for(int i=0;i<cnt-1;i++)
if(a[i]>a[i+1]) flag=0;
if(flag) return 1;
flag=1;
for(int i=0;i<cnt-1;i++)
if(a[i]<a[i+1]) flag=0;
if(flag) return -1;
else return 0;
}
void solve(){
int n,cnt;
scanf("%d",&n);
cnt=n;
val_h=val_t=0;
for(int i=0;i<n;i++)
scanf("%I64d",&a[i]);
for(int i=0;i<=n-1;i++){
if(i==n-1||cnt==1){
printf("nice series\n");
return;
}
int flag=check(cnt);
if(flag==0){
if(i==0) printf("ugly series\n");
else printf("%d\n",i-1);
return;
}
else if(flag==-1){
for(int i=0;i<cnt/2;i++)
swap(a[i],a[cnt-1-i]);
swap(val_h,val_t);
}
int ncnt=0;
if(val_h) {t_a[ncnt++]=0;val_h--;}
for(int i=0;i+1<cnt;i++)
t_a[ncnt++]=a[i+1]-a[i];
if(val_t) {t_a[ncnt++]=0;val_t--;}
int j=1;
cnt=0;
a[cnt++]=t_a[0];
if(a[0]==0) while(t_a[j]==0&&j<ncnt) {val_h++;j++;}
for(;j<ncnt;j++) a[cnt++]=t_a[j];
if(a[cnt-1]==0) while(a[cnt-2]==0&&cnt-2>=0) {cnt--;val_t++;}
}
}
int main()
{
int _;
scanf("%d",&_);
while(_--) solve();
return 0;
}

146
HDOJ/4929_autoAC.cpp Normal file
View File

@ -0,0 +1,146 @@
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std ;
void get_num ( int& n ) {
n = 0 ;
char c ;
while ( c = getchar () ) {
if ( c >= '0' && c <= '9' ) break ;
}
n = c - '0' ;
while ( c = getchar () ) {
if ( c < '0' || c > '9' ) break ;
n = n * 10 + c -'0' ;
}
}
const int maxn = 50005 ;
const int mod = 10007 ;
short dp[2][33][33][maxn] ;
int c[33] , d[33] ;
char s[maxn] , s1[33] ;
vector<int> vec[maxn] ;
int p[20][maxn] , fa[maxn] , deep[maxn] ;
struct LCA {
void dfs ( int u ) {
if ( u == 1 ) fa[u] = 0 ;
p[0][u] = fa[u] ;
deep[u] = deep[fa[u]] + 1 ;
for ( int i = 1 ; i < 20 ; i ++ ) p[i][u] = p[i-1][p[i-1][u]] ;
int sz = vec[u].size () ;
for ( int i = 0 ; i < sz ; i ++ ) {
int v = vec[u][i] ;
if ( v == fa[u] ) continue ;
fa[v] = u ;
dfs ( v ) ;
}
}
int father_k ( int u , int k ) {
for ( int i = 0 ; i < 20 ; i ++ )
if ( k & ( 1 << i ) )
u = p[i][u] ;
return u ;
}
int query ( int a , int b ) {
if ( deep[a] > deep[b] ) swap ( a , b ) ;
b = father_k ( b , deep[b] - deep[a] ) ;
if ( a == b ) return a ;
for ( int i = 19 ; i >= 0 ; i -- ) {
if ( fa[a] == fa[b] ) break ;
if ( p[i][a] != p[i][b] ) {
a = p[i][a] ;
b = p[i][b] ;
}
}
return fa[a] ;
}
} lca ;
int l ;
void dfs ( int u , int x , int c ) {
for ( int i = x ; i <= l ; i ++ ) {
dp[c][x][i][u] += dp[c][x][i][fa[u]] ;
if ( dp[c][x][i][u] >= mod ) dp[c][x][i][u] -= mod ;
if ( s[u] == s1[i] )
dp[c][x][i][u] += dp[c][x][i-1][fa[u]] ;
if ( dp[c][x][i][u] >= mod ) dp[c][x][i][u] -= mod ;
}
int sz = vec[u].size () ;
for ( int i = 0 ; i < sz ; i ++ ) {
int v = vec[u][i] ;
if ( v == fa[u] ) continue ;
dfs ( v , x , c ) ;
}
}
void DP ( int n , int c ) {
for ( int i = 0 ; i <= l + 1 ; i ++ ) {
for ( int j = 0 ; j <= n ; j ++ ) {
for ( int k = 0 ; k <= i ; k ++ )
dp[c][k][i][j] = 0 ;
if (i) dp[c][i][i-1][j] = 1 ;
}
}
for ( int i = 1 ; i <= l ; i ++ )
dfs ( 1 , i , c ) ;
}
int main () {
int T , n , q ;
scanf ( "%d" , &T ) ;
while ( T -- ) {
scanf ( "%d%d" , &n , &q ) ;
for ( int i = 1 ; i <= n ; i ++ ) vec[i].clear () ;
for ( int i = 1 ; i < n ; i ++ ) {
int a , b ;
get_num (a) ;
get_num (b) ;
vec[a].push_back (b) ;
vec[b].push_back (a) ;
}
scanf ( "%s" , s + 1 ) ;
scanf ( "%s" , s1 + 1 ) ;
l = strlen ( s1 + 1 ) ;
lca.dfs ( 1 ) ;
reverse ( s1 + 1 , s1 + l + 1 ) ;
DP ( n , 0 ) ;
reverse ( s1 + 1 , s1 + l + 1 ) ;
DP ( n , 1 ) ;
while ( q -- ) {
int a , b , x , y ;
get_num (a) ;
get_num (b) ;
if ( a == b ) {
if ( l == 1 && s[a] == s1[1] ) puts ( "1" ) ;
else puts ( "0" ) ;
continue ;
}
int w = lca.query ( a , b ) ;
int ans = 0 ;
memset ( c , 0 , sizeof ( c ) ) ;
memset ( d , 0 , sizeof ( d ) ) ;
for ( int i = 0 ; i <= l ; i ++ ) {
c[i] = dp[0][l-i+1][l][a] ;
d[i] = dp[1][l-i+1][l][b] ;
for ( int j = 0 ; j < i ; j ++ ) {
c[i] -= (c[j] * dp[0][l-i+1][l-j][w] % mod) ;
d[i] -= (d[j] * dp[1][l-i+1][l-j][w] % mod) ;
c[i] += mod ;
if ( c[i] >= mod ) c[i] -= mod ;
d[i] += mod ;
if ( d[i] >= mod ) d[i] -= mod ;
}
}
for ( int i = 0 ; i <= l ; i ++ ) {
ans += c[i] * d[l-i] % mod ;
if ( ans >= mod ) ans -= mod ;
}
for ( int i = 0 ; i < l ; i ++ ) {
if ( s[w] == s1[i+1] ) {
ans += c[i] * d[l-i-1] % mod ;
if ( ans >= mod ) ans -= mod ;
}
}
printf ( "%d\n" , ans ) ;
}
}
}

149
HDOJ/4930_autoAC.cpp Normal file
View File

@ -0,0 +1,149 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define sss1 {ans(1);continue;}
#define sss0 {ans(0);continue;}
char s1[20],s2[20];
int p1[16],p2[16],len1,len2;
int a[8],b[8],kk[10] = {0,0,0,14,13,12,11,10,9,8};
int check(char c)
{
if(c == 'Y')
return 0;
if(c == 'X')
return 1;
if(c == '2')
return 2;
if(c == 'A')
return 3;
if(c == 'K')
return 4;
if(c == 'Q')
return 5;
if(c == 'J')
return 6;
if(c == 'T')
return 7;
for(int i = '9';i>='3';i--)
{
if(c == i)
return kk[i-'0'];
}
}
void ans(int i)
{
if(i==1)
printf("Yes\n");
else
printf("No\n");
}
int Solo()
{
int i;
for(i = 0; i<15; i++)
{
if(p1[i])
return 1;
if(p2[i])
return 0;
}
}
int Pair()
{
int i;
for(i = 2; i<15; i++)
{
if(p1[i]>=2)
return 1;
if(p2[i]>=2)
return 0;
}
}
int Trio()
{
int i;
for(i = 2; i<15; i++)
{
if(p1[i]>=3)
return 1;
if(p2[i]>=3)
return 0;
}
}
int Bomb()
{
int i;
for(i = 2; i<15; i++)
{
if(p1[i]>=4)
return 1;
if(p2[i]>=4)
return 0;
}
}
int main()
{
int t,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%s%s",s1,s2);
len1 = strlen(s1);
len2 = strlen(s2);
memset(p1,0,sizeof(p1));
memset(p2,0,sizeof(p2));
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(i = 0; i<len1; i++)
p1[check(s1[i])]++;
for(i = 0; i<len2; i++)
p2[check(s2[i])]++;
for(i = 0; i<15; i++)
{
a[p1[i]] = 1;
b[p2[i]] = 1;
}
int flag = 0;
if(p1[0]&&p1[1] || len1 == 1 || (a[2]&&len1==2) || (a[3]&&len1==3) || (a[4]&&len1==4) || (a[3]&&len1==4) || (a[3]&&a[2]&&len1==5) || (a[4]&&len1==6))
sss1;
if(p2[0] && p2[1])
sss0;
if(a[4] && !b[4])
sss1;
if(b[4] && !a[4])
sss0;
if(a[4] && b[4])
{
if(Bomb())
sss1
else
sss0
}
if(a[3] && !b[3])
sss1;
if(a[3] && b[3])
{
if(Trio())
sss1
else
{
if(len1 >= 4 && len2<4)
sss1
else if(a[2] && (!b[2]||len2<5))
sss1
}
}
if(a[2] && !b[2] && !b[3])
sss1
if(a[2] && (b[2]||b[3]))
{
if(Pair())
sss1
}
if(Solo())
sss1
sss0
}
return 0;
}

21
HDOJ/4931_autoAC.cpp Normal file
View File

@ -0,0 +1,21 @@
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[20];
int main()
{
int T;
scanf("%d",&T);
for(int i=1;i<=T;i++)
{
for(int i=1;i<=6;i++)
scanf("%d",&a[i]);
sort(a+1,a+7);
if(a[6]+a[5]>a[4]+a[3]+a[2])
printf("Grandpa Shawn is the Winner!\n");
else
printf("What a sad story!\n");
}
return 0;
}

76
HDOJ/4932_autoAC.cpp Normal file
View File

@ -0,0 +1,76 @@
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int maxn=55;
const int inf=1<<29;
const double eps=1e-6;
int n;
double a[maxn],dp[maxn][2];
vector<double> set;
bool check(double val)
{
bool last=false;
for(int i=1;i<n-1;i++)
{
if(!last&&a[i]-a[i-1]>=val)
{
last=false;
continue;
}
if(last&&fabs(a[i]-a[i-1]-val)<=eps)
{
last=false;
continue;
}
if(last&&(a[i]-a[i-1])/2>=val)
{
last=false;
continue;
}
if(a[i+1]-a[i]>=val)
{
last=true;
continue;
}
return false;
}
return true;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(dp,0,sizeof(dp));
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%lf",&a[i]);
sort(a,a+n);
n=unique(a,a+n)-a;
if(n<=2)
{
printf("0.000\n");
continue;
}
for(int i=1;i<n;i++)
{
set.push_back(a[i]-a[i-1]);
set.push_back((a[i]-a[i-1])/2);
}
sort(set.begin(),set.end());
double ans=0;
for(int i=set.size()-1;i>=0;i--)
if(check(set[i]))
{
ans=set[i];
break;
}
printf("%.3f\n",ans);
}
return 0;
}

239
HDOJ/4933_autoAC.cpp Normal file
View File

@ -0,0 +1,239 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <assert.h>
#include<iostream>
using namespace std;
#define MAXN 10050
#define LL __int64
int dp[2][10][110];
int num[110];
void dos(char str[],int leap)
{
int n=strlen(str);
for(int i=0;i<n;i++)
{
num[i+1]=str[i]-'0';
}
if(leap==0)
{
if(n==1&&num[1]==0)return;
num[n]--;
int x=n;
while(num[x]<0)
{
num[x]+=10;
num[x-1]--;
x--;
}
if(num[1]==0)
{
n--;
for(int i=1;i<=n;i++)
{
num[i]=num[i+1];
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=9;j++)
{
int x=n-i;
if(i!=1)
{
dp[leap][j][x]++;
}
for(int k=1;k+x+1<n;k++)
{
if(k%2)dp[leap][j][x+k-1]-=9;
else dp[leap][j][x+k-1]+=9;
}
for(int k=1;k<i;k++)
{
if(i%2==0)dp[leap][j][n-k-1]-=(num[k]);
else dp[leap][j][n-k-1]+=(num[k]);
if(k==1)
{
if(i%2==0)dp[leap][j][n-k-1]-=(-1);
else dp[leap][j][n-k-1]+=(-1);
}
}
if(j>num[i])continue;
if(j<num[i])
{
if(i%2==0)dp[leap][j][x]-=1;
else dp[leap][j][x]+=1;
continue;
}
for(int k=i+1;k<=n;k++)
{
if(i%2==0)dp[leap][j][n-k]-=num[k];
else dp[leap][j][n-k]+=num[k];
if(k==n)
{
if(i%2==0)dp[leap][j][n-k]-=1;
else dp[leap][j][n-k]+=1;
}
}
if(i==n)
{
if(i%2==0)dp[leap][j][0]-=1;
else dp[leap][j][0]+=1;
}
}
}
}
void hebing()
{
for(int i=0;i<=100;i++)
{
for(int j=1;j<=9;j++)
{
dp[0][j][i]=dp[1][j][i]-dp[0][j][i];
}
}
}
int ans[220];
int fuhao;
int qian()
{
int len=0;
for(int i=0;i<=100;i++)
{
int x=0;
for(int j=1;j<=9;j++)
{
x+=dp[0][j][i]*j;
}
ans[i]=x;
}
int x=0;
for(int i=0;i<=100;i++)
{
x=ans[i];
if(x!=0)len=i;
else continue;
if(x<0)
{
x=-x;
ans[i]=x%10;
ans[i]=-ans[i];
x=-x;
}
else ans[i]=x%10;
ans[i+1]+=x/10;
}
while(x!=0)
{
len++;
if(x<0)
{
x=-x;
ans[len]=x%10;
ans[len]=-ans[len];
x=-x;
}
else ans[len]=x%10;
x=x/10;
}
while(ans[len]==0)len--;
if(ans[len]<0)fuhao=0;
else fuhao=1;
if(fuhao==0)
{
for(int i=0;i<=len;i++)ans[i]=-ans[i];
}
for(int i=0;i<=len;i++)
{
if(ans[i]<0)
{
while(ans[i]<0)
{
ans[i]+=10;
ans[i+1]--;
}
}
}
if(fuhao==0)
{
for(int i=0;i<=len;i++)ans[i]=-ans[i];
}
return len;
}
int getf(int len)
{
int x=0;
int y=0;
for(int i=0;i<=len;i++)
{
x+=ans[i];
y=y*10-ans[i];
y=y%9;
}
if(fuhao==0)
{
y=-y;
while(y<=0)y+=9;
return y;
}
while(x>=10)
{
int y=0;
while(x)
{
y+=x%10;
x=x/10;
}
x=y;
}
return x;
}
void out(int f,int len)
{
int aa=0;
int y=0;
for(int i=len;i>=0;i--)
{
aa=aa*10+ans[i];
aa=aa%f;
y=y*10-ans[i];
y=y%f;
}
if(fuhao==0)
{
y=-y;
cout<<(y%f+f)%f<<endl;
return;
}
aa=(aa+f)%f;
cout<<aa<<endl;
}
char stl[110];
char str[110];
int main()
{
int T,n;
scanf("%d",&T);
while(T--)
{
memset(dp,0,sizeof(dp));
memset(ans,0,sizeof(ans));
scanf("%s%s",stl,str);
dos(stl,0);
dos(str,1);
hebing();
int len=qian();
LL f=getf(len);
if(f==0)
{
cout<<"Error!"<<endl;
}
else
{
out(f,len);
}
}
return 0;
}

69
HDOJ/4937_autoAC.cpp Normal file
View File

@ -0,0 +1,69 @@
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL __int64
const int maxn=1e5+10;
int main()
{
int T;
scanf("%d",&T);
int tt=0;
while(T--)
{
LL n,t,ans=0;
LL i,j,k;
LL a,b,c,d,x;
scanf("%I64d",&n);
if(n>=3&&n<=6){printf("Case #%d: -1\n",++tt);continue;}
for(i=3;i<=6;i++)
{
for(j=3;j<=6;j++)
{
if((n-i)%j==0&&(n-i)/j>max(i,j))
{
//printf(" %I64d+%I64d*%I64d\n",i,j,(n-i)/j);
ans++;
}
}
}
for(i=3;i<=6;i++)
{
for(j=3;j<=6;j++)
{
for(k=3;k<=6;k++)
{
a=i;b=j;c=k-n;
d=(LL)sqrt(b*b-a*c*4+0.5);
if(d*d!=b*b-a*c*4)continue;
if((d-b)%(a*2))continue;
x=(d-b)/(a*2);
if(x>max(max(i,j),k))
{
ans++;
}
}
}
}
for(i=2;i*i*i<=n;i++)
{
t=n;
while(t)
{
if(t%i<3||t%i>6)break;
t=t/i;
}
if(!t)
{
ans++;
}
}
printf("Case #%d: %I64d\n",++tt,ans);
}
return 0;
}

137
HDOJ/4938_autoAC.cpp Normal file
View File

@ -0,0 +1,137 @@
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<time.h>
#include<string>
#define REP(i,n) for(int i=0;i<n;++i)
#define REP1(i,a,b) for(int i=a;i<=b;++i)
#define REP2(i,a,b) for(int i=a;i>=b;--i)
#define MP make_pair
#define LL long long
#define X first
#define Y second
#define MAXN 100050
#define eps 1e-8
using namespace std;
struct Point {
double x, y;
} p1[MAXN], p2[MAXN];
struct Line {
Point p, q;
};
Point Intersect(Line u, Line v)
{
Point ret = u.p;
double t = ((u.p.x - v.p.x) * (v.p.y - v.q.y)
- (u.p.y - v.p.y) * (v.p.x - v.q.x))
/ ((u.p.x - u.q.x) * (v.p.y - v.q.y)
- (u.p.y - u.q.y) * (v.p.x - v.q.x));
ret.x += (u.q.x - u.p.x) * t;
ret.y += (u.q.y - u.p.y) * t;
return ret;
}
struct node {
double x, w;
int id, type;
} s[MAXN];
bool cmp1(node a, node b) {
return a.x < b.x - eps || (fabs(a.x - b.x) < eps && a.type < b.type);
}
bool cmp2(node a, node b) {
return a.x < b.x - eps || (fabs(a.x - b.x) < eps && a.type > b.type);
}
int d[MAXN];
int ans[MAXN];
int main() {
int tt, ri = 0, n;
scanf("%d", &tt);
while (tt--) {
Line L;
L.p.x = L.p.y = L.q.y = 0;
L.q.x = 100;
double v1, v2;
scanf("%d%lf%lf", &n, &v1, &v2);
double si = v1 / sqrt(v1 * v1 + v2 * v2);
REP(i,n)
{
double a, t, p, w;
scanf("%lf%lf%lf%lf", &a, &t, &p, &w);
Line L1;
if (a == 1) {
L1.p.x = 1.0*p;
L1.p.y = -1.0*t * v1;
L1.q.x = 1.0*p - v2;
L1.q.y = -1.0*t * v1 + v1;
} else {
L1.p.x = -1.0*t * v2;
L1.p.y = 1.0*p;
L1.q.x = -1.0*t * v2 - v2;
L1.q.y = 1.0*p + v1;
}
Point e = Intersect(L, L1);
s[i].x = e.x;
s[i].type = a;
if(a==1)
s[i].w = w ;
else
s[i].w=w*1.0*v2/v1;
s[i].id = i;
}
sort(s, s + n, cmp1);
d[n] = 0;
for (int i = n - 1; i >= 0; --i) {
d[i] = d[i + 1];
if (s[i].type == 2) {
d[i]++;
continue;
}
int l, r;
l = i;
r = n - 1;
while (r - l > 1) {
int mid = (l + r) >> 1;
if (s[mid].x - s[i].x > s[i].w + eps)
r = mid;
else
l = mid;
}
if (s[r].x - s[i].x < s[i].w + eps)
ans[s[i].id] = d[i]-d[r+1];
else
ans[s[i].id] = d[i] - d[r];
}
sort(s, s + n, cmp2);
d[n] = 0;
for (int i = n - 1; i >= 0; --i) {
d[i] = d[i + 1];
if (s[i].type == 1) {
d[i]++;
continue;
}
int l, r;
l = i;
r = n - 1;
while (r - l > 1) {
int mid = (l + r) >> 1;
if (s[mid].x - s[i].x > s[i].w + eps)
r = mid;
else
l = mid;
}
if (s[r].x - s[i].x < s[i].w + eps)
ans[s[i].id] = d[i]-d[r+1];
else
ans[s[i].id] = d[i] - d[r];
}
printf("Case #%d:\n",++ri);
for(int i=0;i<n;++i)
printf("%d\n",ans[i]);
}
return 0;
}

32
HDOJ/4939_autoAC.cpp Normal file
View File

@ -0,0 +1,32 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define mem(a,b) memset(a,b,sizeof(a))
#define w(x) while(x)
#define ll __int64
ll n,x,y,z,t,dp[1505][1505],ss,cas=1,i,j,k,r,ans;
int main()
{
scanf("%I64d",&ss);
w(ss--)
{
scanf("%I64d%I64d%I64d%I64d%I64d",&n,&x,&y,&z,&t);
mem(dp,0);
ans=n*t*x;
up(i,1,n)
up(j,0,i)
{
if(!j)
dp[i][j]=dp[i-1][j]+t*(i-j-1)*y;
else
dp[i][j]=max(dp[i-1][j]+(j*z+t)*max(0LL,(i-1-j))*y,dp[i-1][j-1]+((j-1)*z+t)*(i-j)*y);
ans=max(ans,dp[i][j]+(n-i)*(j*z+t)*(x+(i-j)*y));
}
printf("Case #%I64d: %I64d\n",cas++,ans);
}
return 0;
}

130
HDOJ/4940_autoAC.cpp Normal file
View File

@ -0,0 +1,130 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include <iostream>
#include<climits>
using namespace std;
const int N = 1000;
const int M = 100000;
int n;
int ecnt, head[N], nx[M], to[M], va[M], cur_edge[N];
int source, target, flow, pre[N], lev[N], qu[N], sign;
void addedge(int u, int v, int w) {
to[ecnt] = v;
nx[ecnt] = head[u];
va[ecnt] = w;
head[u] = ecnt++;
}
bool bfs(int s, int t) {
std::fill(lev, lev + n, -1);
sign = t;
lev[t] = 0;
int st = 0, ed = 0;
qu[ed++] = t;
while (st != ed && lev[s] == -1) {
int u = qu[st++];
for (int i = head[u]; i != -1; i = nx[i]) {
if (va[i ^ 1] > 0 && lev[to[i]] == -1) {
lev[to[i]] = lev[u] + 1;
qu[ed++] = to[i];
}
}
}
return lev[s] != -1;
}
void push() {
int delta = INT_MAX, u, p;
for (u = target; u != source; u = to[p ^ 1]) {
p = pre[u];
delta = std::min(delta, va[p]);
}
for (u = target; u != source; u = to[p ^ 1]) {
p = pre[u];
va[p] -= delta;
if (!va[p]) {
sign = to[p ^ 1];
}
va[p ^ 1] += delta;
}
flow += delta;
}
void dfs(int u) {
if (u == target)
push();
else {
for (int i = cur_edge[u]; i != -1; i = nx[i]) {
if (va[i] > 0 && lev[u] == lev[to[i]] + 1) {
pre[to[i]] = i;
dfs(to[i]);
if (lev[sign] > lev[u]) {
return;
}
sign = target;
}
}
lev[u] = -1;
}
}
int nc, pc, tc;
int lx[M], ly[M], lv[M];
void dinic(int s, int t, int node_cnt) {
source = s;
target = t;
n = node_cnt;
flow = 0;
while (bfs(source, target)) {
for (int i = 0; i < n; ++i) {
cur_edge[i] = head[i];
}
dfs(source);
}
}
int in[500],out[500];
void solve() {
int n,m;
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
scanf("%d%d",&n,&m);
fill(head,head+n+10,-1);
ecnt=0;
for(int i=0;i<m;++i)
{
int u,v,x,y;
scanf("%d%d%d%d",&u,&v,&x,&y);
in[v]+=x;
in[u]-=x;
addedge(u,v,y);
addedge(v,u,0);
}
int s,t;
s=0;t=n+1;
int sum=0;
for(int i=1;i<=n;++i)
{
if(in[i]>0)
{
sum+=in[i];
addedge(s,i,in[i]);
addedge(i,s,0);
}
else
{
addedge(i,t,-in[i]);
addedge(t,i,0);
}
}
dinic(s,t,t+2);
if(flow==sum)puts("happy");
else puts("unhappy");
}
int main() {
int ri=0,tt;
scanf("%d",&tt);
while(tt--)
{
printf("Case #%d: ",++ri);
solve();
}
return 0;
}

67
HDOJ/4941_autoAC.cpp Normal file
View File

@ -0,0 +1,67 @@
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=1e5+10;
map<int,map<int,int> >mm;
map<int,int>row;
map<int,int>col;
int main()
{
int T,tt=0;
scanf("%d",&T);
while(T--)
{
row.clear();
col.clear();
mm.clear();
int n,m,i,j,k,rr=0,cc=0,a,b,c,x,y;
scanf("%d%d%d",&n,&m,&k);
for(i=0;i<k;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(!row[a])
{
row[a]=++rr;
x=rr;
}
else x=row[a];
if(!col[b])
{
col[b]=++cc;
y=cc;
}
else y=col[b];
mm[x][y]=c;
}
int q;
printf("Case #%d:\n",++tt);
scanf("%d",&q);
while(q--)
{
scanf("%d%d%d",&a,&b,&c);
if(a==1)
{
int t=row[b];
row[b]=row[c];
row[c]=t;
}
else if(a==2)
{
int t=col[b];
col[b]=col[c];
col[c]=t;
}
else
{
printf("%d\n",mm[row[b]][col[c]]);
}
}
}
return 0;
}

138
HDOJ/4942_autoAC.cpp Normal file
View File

@ -0,0 +1,138 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
#pragma comment (linker,"/STACK:102400000,102400000")
#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 ls ( o << 1 )
#define rs ( o << 1 | 1 )
#define lson ls , l , m
#define rson rs , m + 1 , r
#define mid() ( ( l + r ) >> 1 )
#define root 1 , 1 , n
typedef long long LL ;
const int MAXN = 100005 ;
const int mod = 1e9 + 7 ;
struct Node {
Node *c[2] , *f ;
int size[2] ;
int idx ;
LL v ;
Node () : v ( 0 ) {}
} ;
struct Seg_Tree {
Node node[MAXN] , *cur , *null ;
int product[MAXN << 2] ;
int val[MAXN] ;
int n , m ;
int cnt ;
void dfs ( Node* o ) {
if ( o -> c[0] != null ) dfs ( o -> c[0] ) ;
o -> idx = ++ cnt ;
o -> size[0] = o -> idx ;
if ( o -> c[0] != null ) {
o -> v += o -> c[0] -> v ;
o -> size[0] = o -> c[0] -> size[0] ;
}
o -> size[1] = o -> idx ;
if ( o -> c[1] != null ) dfs ( o -> c[1] ) ;
if ( o -> c[1] != null ) {
o -> v += o -> c[1] -> v ;
o -> size[1] = o -> c[1] -> size[1] ;
}
val[o -> idx] = o -> v % mod ;
}
void update ( int pos , int v , int o , int l , int r ) {
if ( l == r ) {
product[o] = v ;
return ;
}
int m = mid () ;
if ( pos <= m ) update ( pos , v , lson ) ;
else update ( pos , v , rson ) ;
product[o] = ( LL ) product[ls] * product[rs] % mod ;
}
int query ( int L , int R , int o , int l , int r ) {
if ( L <= l && r <= R ) return product[o] ;
int m = mid () ;
if ( R <= m ) return query ( L , R , lson ) ;
if ( m < L ) return query ( L , R , rson ) ;
return ( LL ) query ( L , R , lson ) * query ( L , R , rson ) % mod ;
}
void build ( int o , int l , int r ) {
if ( l == r ) {
product[o] = val[l] ;
return ;
}
int m = mid () ;
build ( lson ) ;
build ( rson ) ;
product[o] = ( LL ) product[ls] * product[rs] % mod ;
}
void init () {
cnt = 0 ;
null = node ;
cur = node + 1 ;
}
void rotate ( Node* o , int d ) {
Node* c = o -> c[d ^ 1] ;
Node* p = o -> f ;
o -> v -= ( c -> v ) - ( c -> c[d] -> v ) ;
c -> v += ( o -> v ) - ( c -> c[d] -> v ) ;
o -> c[d ^ 1] = c -> c[d] ;
c -> c[d] -> f = o ;
c -> f = p ;
if ( p != null ) {
if ( o == p -> c[0] ) p -> c[0] = c ;
else p -> c[1] = c ;
}
c -> c[d] = o ;
o -> f = c ;
}
void solve () {
int d , x ;
int w , l , r ;
init () ;
scanf ( "%d%d" , &n , &m ) ;
cur -> f = null ;
FOR ( i , 1 , n ) {
scanf ( "%d%d%d" , &w , &l , &r ) ;
cur -> v = w ;
cur -> c[0] = node + l ;
cur -> c[1] = node + r ;
if ( l ) cur -> c[0] -> f = node + i ;
if ( r ) cur -> c[1] -> f = node + i ;
++ cur ;
}
dfs ( node + 1 ) ;
build ( root ) ;
while ( m -- ) {
scanf ( "%d%d" , &d , &x ) ;
Node* o = node + x ;
if ( d < 2 ) {
Node* c = o -> c[d] ;
if ( c == null ) continue ;
c -> size[d ^ 1] = o -> size[d ^ 1] ;
if ( c -> c[d ^ 1] != null ) o -> size[d] = c -> c[d ^ 1] -> size[d] ;
else o -> size[d] = o -> idx ;
rotate ( o , d ^ 1 ) ;
update ( o -> idx , o -> v % mod , root ) ;
update ( c -> idx , c -> v % mod , root ) ;
} else {
printf ( "%d\n" , query ( o -> size[0] , o -> size[1] , root ) ) ;
}
}
}
} T ;
int main () {
int t , cas = 0 ;
scanf ( "%d" , &t ) ;
while ( t -- ) {
printf ( "Case #%d:\n" , ++ cas ) ;
T.solve () ;
}
return 0 ;
}

48
HDOJ/4944_autoAC.cpp Normal file
View File

@ -0,0 +1,48 @@
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL __int64
const int maxn=5e5+20;
LL mod;
LL val[maxn],dp[maxn];
void fun(int n)
{
for(LL i=1;i<n;i++)
{
for(LL j=i;j<n;j+=i)
{
val[j]+=(j/i+1)*(j/i)/2;
}
}
}
void init()
{
mod=1;
mod=(mod<<32);
memset(val,0,sizeof(val));
fun(maxn);
dp[1]=1;
for(LL i=2;i<maxn;i++)
{
dp[i]=dp[i-1]+val[i]*i;
dp[i]=dp[i]%mod;
}
}
int main()
{
init();
int n,tt=0,T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
printf("Case #%d: %I64d\n",++tt,dp[n]);
}
return 0;
}

110
HDOJ/4945_autoAC.cpp Normal file
View File

@ -0,0 +1,110 @@
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-10
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)
using namespace std;
const int maxn = 100010;
#define mod 998244353
int num[21];
LL dp[15][2100];
LL use[15][2100];
LL f[maxn];
int n;
int add(int x)
{
if(x == 0) return 20;
int ans = 0;
while(x!=1)
{
if(x&1) return 20;
x >>= 1;
ans++;
}
return ans;
}
LL q_mod(LL a,LL b,LL n)
{
LL ret=1;
LL tmp=a%n;
while(b)
{
if(b&0x1) ret=ret*tmp%n;
tmp=tmp*tmp%n;
b>>=1;
}
return ret;
}
LL Del(int n, int m)
{
if(m > n) return 0LL;
LL ans = f[n];
ans = (ans*q_mod((f[m]*f[n-m])%mod, mod-2, mod))%mod;
return ans;
}
void select()
{
for(int i = 1; i <= 11; i++)
for(int j = 0; j < 2048; j++) use[i][j] = Del(num[i-1], j);
}
void change()
{
select();
for(int i = 0; i < 2048; i++) dp[1][i] = use[1][i];
for(int i = 2; i <= 11; i++)
{
int cnt = (1<<(12-i));
for(int j = 0; j < cnt; j++)
{
for(int k = 0; k <= 2*j+1; k += 2)
{
if(!use[i][j-k/2] || !(dp[i-1][k]+dp[i-1][k+1])) continue;
dp[i][j] += ((dp[i-1][k]+dp[i-1][k+1])*use[i][j-k/2])%mod;
}
dp[i][j] %= mod;
}
}
LL ans = 0LL;
ans = q_mod(2, num[20], mod);
LL ps = (dp[11][0]+dp[11][1])%mod;
ps = q_mod(2, n-num[20], mod)-ps;
ps = (ps+mod)%mod;
ans = (ans*ps)%mod;
printf("%I64d\n",ans);
}
void init()
{
f[0] = 1LL;
for(LL i = 1; i < maxn; i++) f[i] = (f[i-1]*i)%mod;
}
int main()
{
int Case = 1;
init();
while(~scanf("%d",&n) && n)
{
memset(num, 0, sizeof(num));
memset(dp, 0, sizeof(dp));
int x;
for(int i = 0; i < n; i++)
{
scanf("%d",&x);
num[add(x)]++;
}
printf("Case #%d: ",Case++);
change();
}
return 0;
}

96
HDOJ/4946_autoAC.cpp Normal file
View File

@ -0,0 +1,96 @@
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <algorithm>
using namespace std;
#define repf(i,a,b) for(int i=(a);i<=(b);i++)
#define sqr(a) ((a) * (a))
#define dis(a, b) sqrt(sqr(a.x - b.x) + sqr(a.y - b.y))
const int MAXN = 1010;
const double PI = acos(-1.0);
int top;
struct Point {
double x;
double y;
double v;
int id;
Point(double a = 0, double b = 0) : x(a), y(b) {}
friend bool operator < (const Point &l, const Point &r) {
return l.y < r.y || (l.y == r.y && l.x < r.x);
}
} p[MAXN], res[MAXN], all[MAXN];
double mult(Point a, Point b, Point o) {
return (a.x - o.x) * (b.y - o.y) > (b.x - o.x) * (a.y - o.y);
}
void Graham(int n) {
top = 1;
sort(p, p + n);
if (n == 0) {
top = 0;
return;
}
res[0] = p[0];
if (n == 1) {
top = 1;
return;
}
res[1] = p[1];
if (n == 2) {
top = 2;
return;
}
res[2] = p[2];
for (int i = 2; i < n; i++) {
while (top && (mult(p[i], res[top], res[top - 1])))
top--;
res[++top] = p[i];
}
int len = top;
res[++top] = p[n - 2];
for (int i = n - 3; i >= 0; i--) {
while (top != len && (mult(p[i], res[top], res[top - 1])))
top--;
res[++top] = p[i];
}
}
int id[MAXN];
int n;
double mmax;
int main() {
int cas = 1;
while (~scanf("%d", &n) && n) {
mmax = 0;
repf (i, 0, n - 1) {
scanf("%lf%lf%lf", &all[i].x, &all[i].y, &all[i].v);
all[i].id = i;
mmax = max(mmax, all[i].v);
}
memset(id, 0, sizeof(id));
repf (i, 0, n - 1)
repf (j, i + 1, n - 1) {
if (all[i].x == all[j].x && all[i].y == all[j].y && all[i].v == all[j].v) {
all[j].v = 0;
id[i] = id[j] = -1;
}
}
int cnt = 0;
repf (i, 0, n - 1) {
if (all[i].v == mmax)
p[cnt++] = all[i];
}
if (mmax) {
Graham(cnt);
repf (i, 0, top - 1) {
if (id[res[i].id] != -1)
id[res[i].id] = 1;
}
}
printf("Case #%d: ", cas++);
repf (i, 0, n - 1) {
printf("%d", id[i] > 0);
}
puts("");
}
return 0;
}

104
HDOJ/4947_autoAC.cpp Normal file
View File

@ -0,0 +1,104 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <cassert>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
typedef long long LL;
const int N = 200000+20;
bool check[N];
int mu[N],prime[N];
vector<int> fac[N];
LL f[N];
int l,q;
void Mobius()
{
CLR(check, 0);
mu[1] = 1;
int tot = 0;
for(int i = 2; i < N ; i++){
if(!check[i]){
prime[tot ++] = i;
mu[i] = -1;
}
for(int j = 0 ;j < tot; j ++){
if(i * prime[j] >= N)break;
check[i * prime[j]] = true;
if(i % prime[j] == 0){
mu[i * prime[j]] = 0;
break;
}else{
mu[i * prime[j]] = -mu[i];
}
}
}
for(int i = 1 ;i < N ; i++){
for(int j = i ; j < N ; j += i){
fac[j].push_back(i);
}
}
}
inline LL sum(int p){
LL s = 0;
while(p > 0){
s += f[p];
p -= p & (-p);
}
return s;
}
inline void add(int p,int v){
while(p <= l){
f[p] += v;
p += (p) & (-p);
}
}
void update(int n,int d,int v){
if(n % d != 0)return;
n = n/d;
for(int i = 0 ;i < fac[n].size() ; i++){
int q = fac[n][i];
add(q * d, v * mu[q]);
}
}
LL query(int p)
{
LL ans = 0;
for(int i = 1,last = i ; i <= p ; i = last + 1){
last = p/(p/i);
ans += (LL)(p/i) * (sum(last) - sum(i-1)) ;
}
return ans;
}
int main()
{
Mobius();
int cas = 0;
while(~scanf("%d%d",&l,&q)){
if(l == 0 && q == 0)break;
CLR(f, 0);
cas ++;
printf("Case #%d:\n",cas);
while(q--){
int t;
scanf("%d",&t);
if(t == 1){
int n,d,v;
scanf("%d%d%d",&n,&d,&v);
update(n, d, v);
}else{
int x;
scanf("%d",&x);
printf("%I64d\n",query(x));
}
}
}
return 0;
}

64
HDOJ/4948_autoAC.cpp Normal file
View File

@ -0,0 +1,64 @@
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#define inf 0x3f3f3f3f
#pragma comment(linker, "/STACK:16777216")
#define eps 1e-6
#define ll long long
using namespace std;
const int maxn=505;
int n,in[maxn],vis[maxn];
char s[maxn][maxn];
int main()
{
int i,j;
while(~scanf("%d",&n)&&n)
{
memset(in,0,sizeof in);
for(i=1;i<=n;i++)
{
scanf("%s",s[i]+1);
for(j=1;j<=n;j++)
if(s[i][j]=='1')
in[j]++;
}
vector<int> ans;
memset(vis,0,sizeof vis);
int maxin,p,flag=1;
while(flag)
{
flag=0,maxin=-1,p=0;
for(i=1;i<=n;i++)
{
if(!vis[i]&&maxin<in[i])
{
flag=1;
maxin=in[i];
p=i;
}
}
if(flag)
{
vis[p]=1;
ans.push_back(p);
for(i=1;i<=n;i++)
{
if(i==p) continue;
if(s[p][i]=='1') in[i]--;
}
}
}
printf("%d",ans[ans.size()-1]);
for(i=ans.size()-2;i>=0;i--)
printf(" %d",ans[i]);
puts("");
}
return 0;
}

109
HDOJ/4949_autoAC.cpp Normal file
View File

@ -0,0 +1,109 @@
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define HASH 100007
#define STATE 500010
#define maxd 15
int maze[maxd][maxd];
int code[maxd];
int n,m;
struct HASHMAP{
int head[HASH];
int state[STATE],nxt[STATE];
int f[STATE];
int sz;
void clear(){sz=0;memset(head,-1,sizeof(head));}
void push(int st,int ans){
int h=st%HASH;
for(int i=head[h];i!=-1;i=nxt[i]){
if(st==state[i]){
f[i] = f[i]<ans?f[i]:ans;
return ;
}
}
state[sz]=st,nxt[sz]=head[h],f[sz]=ans;
head[h]=sz++;
}
}hm[2];
void decode(int st){
for(int i=m;i>=0;--i) code[i]=st&3,st>>=2;
}
int encode(){
int ret=0;
for(int i=0;i<=m;++i) ret=ret<<2|code[i];
return ret;
}
void shift(){
for(int i=m;i;--i) code[i]=code[i-1];
code[0]=0;
}
int ans;
int zo,oz,oo;
void dpblank(int i,int j,int cur){
int mv = j==m?2:0;
int all = (1<<(2*(m+1)-mv) ) -1;
for(int k=0;k<hm[cur].sz;++k){
int st = hm[cur].state[k];
int left = st&(oo>>(2*(j-1))), up = st&(oo>>(2*j));
int L = left>>(2*(m-j+1)), U = up>>(2*(m-j));
int cnt = ((L>>1)+(U>>1))&1;
if(i==1 || U==2 || maze[i-1][j]==U){
int st2 = st^left^up;
if(cnt) st2 = st2 | (zo>>(2*(j-1))) | (zo>>(2*j));
hm[cur^1].push((st2>>mv)&all, hm[cur].f[k]);
}
if(hm[cur].f[k]+2<ans)
if(i==1 || U==2 || maze[i-1][j]==U){
int st2 = st^left^up;
if(!cnt) st2 = st2 | (zo>>(2*(j-1))) | (zo>>(2*j));
hm[cur^1].push((st2>>mv)&all, hm[cur].f[k]+2);
}
if(hm[cur].f[k]+1<ans)
if(i==1 || U==2 || maze[i-1][j]!=U){
int st2 = st^left^up;
if(j>1 && L!=2) st2 = st2 ^ (zo>>(2*(j-2)));
st2 = st2 | (oz>>(2*(j-1))) | (oz>>(2*j));
hm[cur^1].push((st2>>mv)&all, hm[cur].f[k]+1);
}
}
}
void solve(){
zo = 1<<(2*m);
oz = 2<<(2*m);
oo = 3<<(2*m);
int cur=0;
hm[0].clear();
hm[0].push(0,0);
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
hm[cur^1].clear();
dpblank(i,j,cur);
cur^=1;
}
}
for(int k=0;k<hm[cur].sz;++k){
bool yes=true;
decode(hm[cur].state[k]);
for(int j=1;j<=m;++j){
if(code[j]!=2 && code[j]!=maze[n][j]){
yes=false;
break;
}
}
if(yes) ans = ans<hm[cur].f[k]?ans:hm[cur].f[k];
}
}
int main(){
int ca=0;
while(~scanf("%d%d",&n,&m) && n){
printf("Case #%d: ",++ca);
ans=0;
for(int i=1;i<=n;++i) for(int j=1;j<=m;++j)
scanf("%1d",maze[i]+j), ans+=maze[i][j];
ans*=2;
solve();
printf("%d\n",ans);
}
return 0;
}

13
HDOJ/4950_autoAC.cpp Normal file
View File

@ -0,0 +1,13 @@
#include <stdio.h>
int main()
{
__int64 h,a,b,k,cas=1;
while(~scanf("%I64d%I64d%I64d%I64d",&h,&a,&b,&k),h+a+b+k)
{
if(a>=h||(a-b)*k-b>0||(a-b)*(k-1)+a>=h)
printf("Case #%I64d: YES\n",cas++);
else
printf("Case #%I64d: NO\n",cas++);
}
return 0;
}

27
HDOJ/4952_autoAC.cpp Normal file
View File

@ -0,0 +1,27 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <stack>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL __int64
int main()
{
LL x, k;
int cse = 1;
while(scanf("%I64d %I64d",&x,&k)!=EOF){
if(!x) break;
printf("Case #%d: ",cse++);
LL i = 1;
LL tmp = x / i;
while(tmp >= i+1 && i<k){
tmp = tmp - tmp / (i+1);
i++;
}
printf("%I64d\n",tmp*k);
}
return 0;
}

97
HDOJ/4955_autoAC.cpp Normal file
View File

@ -0,0 +1,97 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<stack>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn=1005;
const int mod=1000000007;
int a[maxn],n;
bool vis[maxn];
ll c[maxn][maxn],b[maxn][maxn];
ll f[maxn][maxn];
void init()
{
for(int i=0; i<maxn; i++)c[i][0]=c[i][i]=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];
if(c[i][j]>=mod)c[i][j]-=mod;
}
for(int i=0; i<maxn; i++)b[i][0]=1;
for(int i=1; i<maxn; i++)
for(int j=1; j<=i; j++)
{
b[i][j]=b[i][j-1]*(i-j+1)%mod;
}
}
void pd(ll&ret,ll val)
{
ret+=val;
if(ret>=mod)ret-=mod;
}
int main()
{
init();
int ncase=0;
while(scanf("%d",&n)&&n)
{
memset(vis,0,sizeof(vis));
bool sign=0;
int cc=0;
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
if(a[i])cc++;
for(int j=1; j<=a[i]; j++)
{
int x;
scanf("%d",&x);
if(vis[x])sign=1;
vis[x]=1;
}
}
if(sign||(n==1&&cc)||(n>1&&cc<2))printf("Case #%d: 0\n",++ncase);
else
{
for(int i=0; i<=n; i++)
for(int j=0; j<=i; j++)f[i][j]=0;
int sum=0,sum2=0;
f[0][0]=1;
for(int i=0; i<n; i++)
{
for(int j=0; j<=i; j++)
{
if(f[i][j]==0LL)continue;
if(a[i+1])
{
for(int k=0; k<=j; k++)
{
if(k>a[i+1])break;
pd(f[i+1][j+1-k],f[i][j]*c[a[i+1]][k]%mod*b[j][k]%mod);
pd(f[i+1][j-k],f[i][j]*c[a[i+1]][k]%mod*c[sum-(sum2-j)][1]%mod*b[j][k]%mod);
}
}
else
{
for(int k=0; k<=j; k++)
{
if(k>a[i+1])break;
pd(f[i+1][j-k],f[i][j]*c[a[i+1]][k]%mod*b[j][k]%mod);
}
}
}
sum+=a[i+1];
if(a[i+1])sum2++;
}
printf("Case #%d: %I64d\n",++ncase,f[n][0]);
}
}
return 0;
}

46
HDOJ/4956_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include <cstdio>
typedef __int64 LL;
bool Judge(LL tt)
{
LL sumo = 0, sume = 0;
LL i = -1;
while(tt)
{
i++;
LL t = tt%10;
if(i%2)
sumo += t;
else
sume += t;
tt /= 10;
}
if(sume - sumo == 3)
return true;
return false;
}
int main()
{
int T;
LL l, r;
LL i, j;
scanf("%d",&T);
while(T--)
{
scanf("%I64d%I64d",&l,&r);
for(i = l; ; i++)
{
if(i%11 == 3)
break;
}
for(j = i; j <= r; j+=11)
{
if(!Judge(j))
break;
}
if(j > r)
printf("-1\n");
else
printf("%I64d\n",j);
}
return 0;
}

53
HDOJ/4958_autoAC.cpp Normal file
View File

@ -0,0 +1,53 @@
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include<stack>
#define inf 0x3f3f3f3f
#define ll long long
#define mod 1000000007
using namespace std;
double ans;
double visit[1005][1005];
double dfs(int ji,int ou)
{
if(ji+ou<=0)return 0;
if(visit[ji][ou])return visit[ji][ou];
if(ji%2==1)
return visit[ji][ou]=dfs(ji-1,ou);
if(ji)
{
if(ou)
return visit[ji][ou]=(dfs(ji-1,ou)*ji+dfs(ji,ou-1)*ou)/(ji+ou)+ji+ou;
else
return visit[ji][ou]=dfs(ji-1,ou)+ji+ou;
}
return visit[ji][ou]=dfs(ji,ou-1)+ji+ou;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(visit,0,sizeof(visit));
ans=0;
int n;
scanf("%d",&n);
int ji=0,ou=0;
for(int i=0; i<n; i++)
{
int a;
scanf("%d",&a);
if(a%2)ji++;
else ou++;
}
ans=dfs(ji,ou);
printf("%d\n",(int)(3*ans+0.5));
}
}

139
HDOJ/4959_autoAC.cpp Normal file
View File

@ -0,0 +1,139 @@
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#define sqr(x) ((x)*(x))
#define LL long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define eps 1e-10
#define mod 1000000007
using namespace std;
int cnt=0;
typedef pair <LL,LL> qf;
qf operator + (qf a,qf b)
{
return make_pair((a.first+b.first)%mod,(a.second+b.second)%mod);
}
qf operator * (qf a,qf b)
{
if (a.first<0) a.first+=mod;
if (b.first<0) b.first+=mod;
if (a.second<0) a.second+=mod;
if (b.second<0) b.second+=mod;
return make_pair((((LL)a.first*(LL)b.first)%mod+((LL)a.second*(LL)b.second)%mod*5ll)%mod,
(((LL)a.first*(LL)b.second)%mod+((LL)a.second*(LL)b.first)%mod)%mod);
}
qf pow(qf a, LL x)
{
qf res(1,0);
qf multi=a;
while (x)
{
if (x&1)
{
res=res*multi;
}
multi=multi*multi;
x/=2;
}
return res;
}
LL pow(LL a,LL b)
{
LL res=1;
LL multi=a;
while (b)
{
if (b&1)
{
res=res*multi%mod;
}
multi=multi*multi%mod;
b/=2;
}
return res;
}
qf acce(qf a,LL b)
{
qf ans=make_pair(1,0);
qf powe=a;
qf sum=a;
qf multi=make_pair(1,0);
while (b)
{
if (b&1)
{
ans=ans+(multi*sum);
multi=multi*powe;
}
sum=sum*(powe+make_pair(1,0));
powe=powe*powe;
b/=2;
}
return ans;
}
LL inv[100005];
qf r1[100005],r2[100005];
void egcd (LL a,LL b,LL &x,LL &y)
{
if (b==0)
{
x=1,y=0;
return ;
}
egcd(b,a%b,x,y);
LL t=x;
x=y;y=t-a/b*y;
}
int main()
{
LL x,y;
for (LL i=1;i<=100000;i++)
{
egcd(i,mod,x,y);
x=(x+mod)%mod;
inv[i]=x;
}
r1[0]=make_pair(1,0);
r2[0]=make_pair(1,0);
for (int i=1;i<=100000;i++)
{
r1[i]=r1[i-1]*make_pair(1,1);
r2[i]=r2[i-1]*make_pair(1,-1);
}
int T;
scanf("%d",&T);
while (T--)
{
cnt=0;
LL n,m;
scanf("%I64d%I64d",&n,&m);
qf ans=make_pair(0,0);
LL Ca=1;
LL v=pow(inv[2],m);
for (LL i=0;i<=m;i++)
{
qf p(Ca,0);
qf tmp=r1[i]*r2[m-i]*make_pair(v,0);
tmp=acce(tmp,n);
tmp=tmp*p;
ans=ans+tmp;
Ca=Ca*(m-i)%mod;
Ca=Ca*inv[i+1]%mod;
}
LL aa=(LL)ans.first;
printf("%I64d\n",aa);
}
return 0;
}

69
HDOJ/4960_autoAC.cpp Normal file
View File

@ -0,0 +1,69 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define mem(a,b) memset(a,b,sizeof(a))
#define w(x) while(x)
#define inf 99999999
#define l 5005
#define s(a) scanf("%d",&a)
int dp[l],a[l],cost[l],lnum,rnum,lsum,rsum,len,i,j,ll[l],rr[l],ans,n;
int main()
{
w((s(n),n))
{
up(i,1,n)
s(a[i]);
up(i,1,n)
s(cost[i]);
len=0;
for(i=1,j=n; i<j; i++,j--)
{
lsum=a[i],rsum=a[j];
lnum=rnum=1;
w(lsum!=rsum)
{
if(i>=j)
break;
if(lsum<rsum)
{
lsum+=a[++i];
lnum++;
}
else
{
rsum+=a[--j];
rnum++;
}
}
if(lsum==rsum)
{
len++;
ll[len]=lnum;
rr[len]=rnum;
}
}
up(i,1,len)
{
int t1=0,t2=0;
dp[i]=inf;
down(j,i,1)
{
t1+=ll[j];
t2+=rr[j];
dp[i]=min(dp[i],dp[j-1]+cost[t1]+cost[t2]);
}
}
ans=cost[n];
up(i,1,len)
{
n-=ll[i]+rr[i];
ans=min(ans,dp[i]+cost[n]);
}
printf("%d\n",ans);
}
return 0;
}

66
HDOJ/4961_autoAC.cpp Normal file
View File

@ -0,0 +1,66 @@
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
using namespace std;
const int maxn=100005;
int a[maxn];
int b[maxn];
int c[maxn];
int num[maxn];
int n;
vector<int> f[maxn];
void init()
{
int i,j;
for(i=1;i<maxn;i++)
for(j=1;j<=sqrt((double)i);j++)
{
if(i%j==0)
{
f[i].push_back(j);
f[i].push_back(i/j);
}
}
}
int main()
{
init();
while(scanf("%d",&n)&&n)
{
int i,j;
memset(num,0,sizeof(num));
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++)
{
int sz=f[a[i]].size();
if(num[a[i]]==0) b[i]=a[i];
else b[i]=a[num[a[i]]];
for(j=0;j<sz;j++)
{
num[f[a[i]][j]]=i;
}
}
memset(num,0,sizeof(num));
for(i=n;i>0;i--)
{
int sz=f[a[i]].size();
if(num[a[i]]==0) c[i]=a[i];
else c[i]=a[num[a[i]]];
for(j=0;j<sz;j++)
{
num[f[a[i]][j]]=i;
}
}
long long ans=0;
for(i=1;i<=n;i++)
{
ans=(long long)ans+((long long)b[i]*(long long)c[i]);
}
printf("%I64d\n",ans);
}
return 0;
}

123
HDOJ/4962_autoAC.cpp Normal file
View File

@ -0,0 +1,123 @@
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
using namespace std;
struct Point{
double x,y;
Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vector;
Vector operator + (Vector A,Vector B){return Vector(A.x+B.x,A.y+B.y);}
Vector operator - (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);
}
const double eps=1e-10;
int dcmp(double x)
{
if(fabs(x)<eps)return 0;else return x<0?-1:1;
}
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;}
Point GetLineIntersection(Point P,Vector v,Point Q,Vector w)
{
Vector u=P-Q;
double t=Cross(w,u)/Cross(v,w);
return P+v*t;
}
const int maxn=1e3+10;
struct node{
double x;
int dir;
}e[maxn];
Point p[maxn],f[maxn];
int t;
int cmp(node a,node b)
{
return a.x<b.x;
}
int cmp2(Point a,Point b)
{
if(a.x==b.x)return a.y<b.y;
return a.x<b.x;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int i,j,k,n,tot=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
p[n]=p[0];
p[n+1]=p[1];
Point x(1,0),out,O(0,0),c;
t=0;
for(i=1;i<=n;i++)
{
if(dcmp(p[i].y)==0)
{
if(dcmp(p[i-1].y)*dcmp(p[i+1].y)<0)
{
e[t].dir=dcmp(p[i-1].y);
e[t++].x=p[i].x;
}
else
{
e[t].dir=0;
e[t++].x=p[i].x;
}
}
}
for(i=0;i<n;i++)
{
if(dcmp(p[i].y)==0&&dcmp(p[i+1].y)==0)
{
f[tot].x=min(p[i].x,p[i+1].x);
f[tot++].y=max(p[i].x,p[i+1].x);
continue;
}
if(dcmp(p[i].y)!=0&&dcmp(p[i+1].y)!=0)
{
if(dcmp(p[i].y*p[i+1].y)<0)
{
out=GetLineIntersection(O,x,p[i],p[i+1]-p[i]);
e[t].x=out.x;
if(dcmp(p[i].y)>0)e[t++].dir=1;
else e[t++].dir=-1;
}
}
}
sort(e,e+t,cmp);
sort(f,f+tot,cmp2);
int ans=0,now=0;
for(i=0,j=0;i<t-1;i++)
{
c.x=(e[i].x+e[i+1].x)/2;
c.y=0;
now+=e[i].dir;
while(j<tot&&dcmp(e[i].x-f[j].y)>0)j++;
if(j<tot&&dcmp(e[i].x-f[j].x)>=0)continue;
ans=max(ans,now);
}
printf("%d\n",ans);
}
return 0;
}

136
HDOJ/4963_autoAC.cpp Normal file
View File

@ -0,0 +1,136 @@
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cctype>
using namespace std;
const int maxn=44;
const int maxm=(1<<20)+10;
const int INF=1e9;
char a[maxn];
int c[maxn],b[maxn],x,y,xx,yy,sx,sy,t,f[maxn],g[maxm],len[maxn];
struct node{
int id,w,flag;
bool operator <(const node &a)const{
if(id==a.id)return w<a.w;
return id<a.id;
}
}e[maxm*2];
int bitcount(int i)
{
int x=0;
while(i)
{
x+=(i&1);
i=i>>1;
}
return x;
}
void init()
{
for(int i=0;i<=22;i++)
{
f[i]=(1<<i)-1;
len[i]=i*10000000;
}
for(int i=0;i<(1<<20);i++)
{
g[i]=bitcount(i);
}
}
void judge()
{
int i,j,k;
if((y>>(yy-xx))^x)return ;
e[t].flag=0;
e[t].id=len[yy-xx]+(y&f[yy-xx]);
e[t++].w=sy-sx;
}
void judge2()
{
int i,j,k,p;
if(x^(y&f[xx]))return ;
e[t].flag=1;
e[t].id=len[yy-xx]+(y>>xx);
e[t++].w=sy-sx;
}
int main()
{
init();
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==0)break;
int i,j,k=0,ans=INF;
scanf("%s",a);
for(i=0;i<2*n;i++)
{
b[i]=a[i]-'a';
k+=b[i];
}
for(i=0;i<n*2;i++)
scanf("%d",&c[i]);
if(k%2){printf("-1\n");continue;}
t=0;
for(i=0;i<(1<<n);i++)
{
if(g[i]>n-g[i])continue;
x=y=xx=yy=sx=sy=0;
for(j=n-1;j>=0;j--)
{
if((1<<j)&i)
{
x=(x<<1)|b[n-1-j];
sx+=c[n-1-j];
xx++;
}
else
{
y=(y<<1)|b[n-1-j];
sy+=c[n-1-j];
yy++;
}
}
if(xx<=yy)judge();
}
for(i=0;i<(1<<n);i++)
{
if(g[i]>n-g[i])continue;
x=y=xx=yy=sx=sy=0;
for(j=n-1;j>=0;j--)
{
if((1<<j)&i)
{
x=(x<<1)|b[2*n-1-j];
sx+=c[2*n-1-j];
xx++;
}
else
{
y=(y<<1)|b[2*n-1-j];
sy+=c[2*n-1-j];
yy++;
}
}
if(xx<=yy)judge2();
}
sort(e,e+t);
int p=-1,q=-1;
for(i=0;i<t;i++)
{
if(e[i].flag==0)p=i;
else q=i;
if(p==-1||q==-1)continue;
if(e[p].id==e[q].id)ans=min(ans,abs(e[p].w-e[q].w));
}
if(ans==INF)printf("-1\n");
else printf("%d\n",ans);
}
return 0;
}

134
HDOJ/4964_autoAC.cpp Normal file
View File

@ -0,0 +1,134 @@
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#define CLR(x,y) memset(x,y,sizeof(x))
#define eps 1e-9
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef complex<ld> point;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
template<class T>
inline bool read(T &n)
{
T x = 0, tmp = 1;
char c = getchar();
while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
if(c == EOF) return false;
if(c == '-') c = getchar(), tmp = -1;
while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
n = x*tmp;
return true;
}
template <class T>
inline void write(T n)
{
if(n < 0)
{
putchar('-');
n = -n;
}
int len = 0,data[20];
while(n)
{
data[len++] = n%10;
n /= 10;
}
if(!len) data[len++] = 0;
while(len--) putchar(data[len]+48);
}
char str[100], ans[10010];
int len;
int getmatch(int l)
{
int p = 1;
for (int r = l + 1; ; r++)
{
if (str[r] == '(') p++;
else if (str[r] == ')') p--;
if (p == 0) return r;
}
}
string getStr(int &w)
{
string ret = "";
while (islower(str[w]) or isupper(str[w]) or isdigit(str[w]))
ret += str[w++];
return ret;
}
void getHtmltag(int &w, string &s1, string &s2)
{
string name = "", id = "", cla = "";
name = getStr(w);
while (str[w] == '#' || str[w] == '.')
{
if (str[w] == '#') id = getStr(++w);
else
{
if (cla != "") cla += ' ';
cla += getStr(++w);
}
}
s1 = "<" + name;
if (id != "")
s1 += " id=\""+ id + "\"";
if (cla != "")
s1 += " class=\""+ cla + "\"";
s1 += ">";
s2 = "</" + name + ">";
}
string solve(int l, int r)
{
if (l > r) return "";
if (str[l] == '(')
{
int k = getmatch(l);
return solve(l + 1, k - 1) + solve(k + 1, r);
}
else
{
string s1, s2, s3, ret;
getHtmltag(l, s1, s2);
int k = 0;
if (str[l] == '*')
{
l++;
while (isdigit(str[l]))
k = k * 10 + str[l++] - '0';
}
else k = 1;
s3 = solve(l + 1, r);
ret = "";
while (k--)
ret += s1 + s3 + s2;
return ret;
}
}
int main()
{
int T;
read(T);
while (T --)
{
scanf("%s", str);
cout<<solve(0, strlen(str) - 1)<<endl;
}
return 0;
}

98
HDOJ/4965_autoAC.cpp Normal file
View File

@ -0,0 +1,98 @@
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cctype>
using namespace std;
const int mod=6;
struct matrix{
int f[6][6];
};
int A[1001][6],B[6][1001],C[1001][6],D[1001][1001];
matrix mul(matrix a,matrix b,int n)
{
matrix c;
memset(c.f,0,sizeof(c.f));
int i,j,k;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
c.f[i][j]+=a.f[i][k]*b.f[k][j];
}
c.f[i][j]%=mod;
}
}
return c;
}
matrix pow_mod(matrix a,int b,int n)
{
matrix s;
memset(s.f,0,sizeof(s.f));
for(int i=0;i<n;i++)s.f[i][i]=1;
while(b)
{
if(b&1)s=mul(s,a,n);
a=mul(a,a,n);
b=b>>1;
}
return s;
}
int main()
{
int n,K;
while(scanf("%d%d",&n,&K)!=EOF)
{
if(n==0&&K==0)break;
int i,j,k;
for(i=0;i<n;i++)
for(j=0;j<K;j++)
scanf("%d",&A[i][j]);
for(i=0;i<K;i++)
for(j=0;j<n;j++)
scanf("%d",&B[i][j]);
matrix e,g;
memset(e.f,0,sizeof(e.f));
for(i=0;i<K;i++)
{
for(j=0;j<K;j++)
{
for(k=0;k<n;k++)
e.f[i][j]+=B[i][k]*A[k][j];
e.f[i][j]%mod;
}
}
e=pow_mod(e,n*n-1,K);
memset(C,0,sizeof(C));
for(i=0;i<n;i++)
{
for(j=0;j<K;j++)
{
for(k=0;k<K;k++)
C[i][j]+=A[i][k]*e.f[k][j];
C[i][j]%=mod;
}
}
int ans=0;
memset(D,0,sizeof(D));
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<K;k++)
D[i][j]+=C[i][k]*B[k][j];
ans+=D[i][j]%mod;
}
}
printf("%d\n",ans);
}
return 0;
}

110
HDOJ/4966_autoAC.cpp Normal file
View File

@ -0,0 +1,110 @@
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cctype>
using namespace std;
const int INF=1<<30;
const int maxn=500+50;
const int maxm=2000+10;
struct edge{
int u,v,w;
edge(int u=0,int v=0,int w=0):u(u),v(v),w(w){}
}e[maxn+maxm];
int sum[maxn],a[maxn],tot,in[maxn],pre[maxn],id[maxn],vis[maxn];
void add(int u,int v,int w)
{
e[tot++]=edge(u,v,w);
}
int Directed_MST(int root,int numv,int nume)
{
int i,j,k,u,v,ans=0;
while(true)
{
for(i=0;i<numv;i++)in[i]=INF;
for(i=0;i<nume;i++)
{
u=e[i].u;
v=e[i].v;
if(e[i].w<in[v]&&u!=v)
{
pre[v]=u;
in[v]=e[i].w;
}
}
for(i=0;i<numv;i++)
{
if(i==root)continue;
if(in[i]==INF)return -1;
}
int t=0;
memset(id,-1,sizeof(id));
memset(vis,-1,sizeof(vis));
in[root]=0;
for(i=0;i<numv;i++)
{
ans+=in[i];
v=i;
while(vis[v]!=i&&id[v]==-1&&v!=root)
{
vis[v]=i;
v=pre[v];
}
if(v!=root&&id[v]==-1)
{
for(u=pre[v];u!=v;u=pre[u])
id[u]=t;
id[v]=t++;
}
}
if(t==0)break;
for(i=0;i<numv;i++)
if(id[i]==-1)id[i]=t++;
for(i=0;i<nume;i++)
{
v=e[i].v;
e[i].u=id[e[i].u];
e[i].v=id[e[i].v];
if(e[i].u!=e[i].v)
e[i].w-=in[v];
}
numv=t;
root=id[root];
}
return ans;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0)break;
int i,j,k;
sum[0]=tot=0;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
a[i]++;
sum[i+1]=sum[i]+a[i];
}
for(i=0;i<n;i++)
{
for(j=sum[i+1]-1;j>sum[i];j--)add(j,j-1,0);
add(sum[n],sum[i],0);
}
int c,d,l1,l2,money;
for(i=0;i<m;i++)
{
scanf("%d%d%d%d%d",&c,&l1,&d,&l2,&money);
add(sum[c-1]+l1,sum[d-1]+l2,money);
}
printf("%d\n",Directed_MST(sum[n],sum[n]+1,tot));
}
return 0;
}

112
HDOJ/4967_autoAC.cpp Normal file
View File

@ -0,0 +1,112 @@
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
struct info{
int s;
int x;
int t;
};
const int MAX = 50010;
int N,x,t;
char str[20];
info a[MAX];
int b[MAX];
int ans;
struct interval{
int left, right;
int sum, rsum;
};
struct SegmentTree{
static const int MAX = 50010;
interval node[MAX<<3];
#define ls(o) (o<<1)
#define rs(o) (o<<1|1)
void build(int l, int r, int o){
node[o].left = l;
node[o].right = r;
node[o].sum = node[o].rsum = 0;
if(l == r) return;
int mid = (l + r)>>1;
build(l,mid,ls(o));
build(mid+1,r,rs(o));
}
void pushup(int o){
node[o].sum = node[rs(o)].sum + node[ls(o)].sum;
node[o].rsum = max(node[rs(o)].rsum,node[ls(o)].rsum + node[rs(o)].sum);
}
void update(int value,int pos,int o){
if(node[o].left == node[o].right &&
node[o].left == pos){
node[o].sum = node[o].rsum = value;
return ;
}
int mid = (node[o].left + node[o].right) >> 1;
if(pos <= mid) update(value,pos,ls(o));
else update(value,pos,rs(o));
pushup(o);
}
void getans(int v,int o){
if(node[o].left == node[o].right){
ans = b[node[o].left];
return;
}
if(v + node[rs(o)].rsum > 0)
getans(v,rs(o));
else
getans(v + node[rs(o)].sum,ls(o));
}
void query(int &v,int L, int R, int o){
if(~ans) return;
if(L <= node[o].left && R >= node[o].right){
if(v + node[o].rsum <= 0) v += node[o].sum;
else getans(v,o);
return;
}
int mid = (node[o].left + node[o].right) >> 1;
if(R > mid) query(v,L,R,rs(o));
if(L <= mid) query(v,L,R,ls(o));
}
};
SegmentTree tree;
int main(void)
{
int cas = 1;
while(scanf("%d", &N),N){
for(int i = 0 ; i < N; ++i){
scanf("%s",str);
if(str[1] == 'u'){
a[i].s = 1;
scanf("%d", &a[i].x);
}
else if(str[1] == 'o')
a[i].s = 2;
else
a[i].s = 3;
scanf("%d", &a[i].t);
b[i] = a[i].t;
}
sort(b,b + N);
for(int i = 0 ; i < N; ++i)
a[i].t = lower_bound(b, b + N, a[i].t) - b + 1;
memset(b,0,sizeof(b));
for(int i = 0 ; i < N; ++i)
if(a[i].s == 1)
b[a[i].t] = a[i].x;
printf("Case #%d:\n",cas++);
tree.build(1,N,1);
for(int i = 0 ; i < N; ++i){
if(a[i].s == 1)
tree.update(1,a[i].t,1);
else if(a[i].s == 2)
tree.update(-1,a[i].t,1);
else{
int v =0;
ans = -1;
tree.query(v,1,a[i].t,1);
printf("%d\n",ans);
}
}
}
return 0;
}

49
HDOJ/4968_autoAC.cpp Normal file
View File

@ -0,0 +1,49 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define up(i,x,y) for(i=x;i<=y;i++)
#define up2(i,x,y) for(i=x;y;i++)
#define mem(a,b) memset(a,b,sizeof(a))
#define w(x) while(x)
#define inf 99999999
double a[105],dp[1005][105];
int i,j,k,n,m,t,sum;
int main()
{
up(i,60,100)
{
if(i<=69) a[i]=2;
else if(i<=74) a[i]=2.5;
else if(i<=79) a[i]=3;
else if(i<=84) a[i]=3.5;
else a[i]=4;
}
scanf("%d",&t);
w(t--)
{
scanf("%d%d",&m,&n);
sum=n*m;
up(i,0,sum)
up(j,0,n)
dp[i][j]=inf;
up(i,60,100)
dp[i][1]=a[i];
up(j,2,n)
up(i,0,sum)
up2(k,60,(k<=100&&(i-k)>=60*(j-1)))
dp[i][j]=min(dp[i][j],dp[i-k][j-1]+dp[k][1]);
printf("%.4f ",dp[sum][n]/n);
up(i,0,sum)
up(j,0,n)
dp[i][j]=0;
up(i,60,100)
dp[i][1]=a[i];
up(j,2,n)
up(i,0,sum)
up2(k,60,(k<=100&&(i-k)>=60*(j-1)))
dp[i][j]=max(dp[i][j],dp[i-k][j-1]+dp[k][1]);
printf("%.4f\n",dp[sum][n]/n);
}
return 0;
}

17
HDOJ/4969_autoAC.cpp Normal file
View File

@ -0,0 +1,17 @@
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int v1,v2,d,r;
int main()
{
int T;
cin>>T;
while(T--){
scanf("%d%d%d%d",&v1,&v2,&r,&d);
double ans = r/(double)v1*v2*asin(v1/(double)(v2));
if(ans>d)printf("Why give up treatment\n");
else printf("Wake up to code\n");
}
return 0;
}

32
HDOJ/4970_autoAC.cpp Normal file
View File

@ -0,0 +1,32 @@
#include<cstdio>
#include<cstring>
const int N = 100005;
typedef __int64 LL;
LL attack[N];
int main()
{
int n, l, r, m, k, pos;
LL d, hp;
while(~scanf("%d",&n) && n) {
memset(attack, 0, sizeof(attack));
scanf("%d",&m);
while(m--) {
scanf("%d%d%I64d", &l, &r, &d);
attack[l] += d;
attack[r+1] -= d;
}
for(int i = 2; i <= n; i++)
attack[i] += attack[i-1];
for(int i = n - 1; i > 0; i--)
attack[i] += attack[i+1];
int ans = 0;
scanf("%d",&k);
while(k--) {
scanf("%I64d%d",&hp, &pos);
if(attack[pos] < hp)
ans++;
}
printf("%d\n", ans);
}
return 0;
}

252
HDOJ/4971_autoAC.cpp Normal file
View File

@ -0,0 +1,252 @@
#include<iostream>
#include<cstdio>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define mt(a,b) memset(a,b,sizeof(a))
using namespace std;
const int M = 123456;
const int inf = 0x7f7f7f7f;
int profit[30],cost[60];
class Tarjan {
public:
struct E {
int u,v,next;
} e[M<<4];
int le,head[M],Index,Bcnt,num[M],belong[M],dfn[M],low[M];
bool instack[M];
stack<int> s;
void tarjan(int u) {
dfn[u]=low[u]=++Index;
instack[u]=true;
s.push(u);
int v;
for(int i=head[u]; ~i; i=e[i].next) {
v=e[i].v;
if(!dfn[v]) {
tarjan(v);
low[u]=min(low[u],low[v]);
} else if(instack[v]) {
low[u]=min(low[u],dfn[v]);
}
}
if(dfn[u]==low[u]) {
Bcnt++;
do {
v=s.top();
s.pop();
instack[v]=false;
belong[v]=Bcnt;
num[Bcnt]++;
} while(u!=v);
}
}
void init() {
le=Index=Bcnt=0;
mt(head,-1);
mt(num,0);
mt(dfn,0);
mt(low,0);
mt(instack,0);
while(!s.empty()) s.pop();
}
void add(int u,int v) {
e[le].u=u;
e[le].v=v;
e[le].next=head[u];
head[u]=le++;
}
void solve(int n) {
for(int i=1; i<=n; i++) {
if(!dfn[i]) {
tarjan(i);
}
}
}
int getbcnt() {
return Bcnt;
}
int getbelong(int id) {
return belong[id];
}
int getnum(int id) {
return num[id];
}
} gx;
class Dinic {
struct E {
int u,v,next,flow;
} e[M<<1];
int le,flow,head[M],temp[M],cur[M],level[M],path[M];
bool used[M];
queue<int> q;
public:
int getflow() {
return flow;
}
bool bfs(int s,int t) {
mt(level,-1);
while(!q.empty()) q.pop();
q.push(s);
level[s]=1;
while(!q.empty()) {
int u=q.front();
q.pop();
for(int i=head[u]; ~i; i=e[i].next) {
int v=e[i].v;
if(level[v]==-1&&e[i].flow) {
level[v]=level[u]+1;
q.push(v);
if(v==t) return true;
}
}
}
return false;
}
void init() {
le=0;
mt(head,-1);
}
void add(int u,int v,int flow) {
e[le].u=u;
e[le].v=v;
e[le].flow=flow;
e[le].next=head[u];
head[u]=le++;
e[le].u=v;
e[le].v=u;
e[le].flow=0;
e[le].next=head[v];
head[v]=le++;
}
void solve(int s,int t) {
int p,now,tempp;
bool flag;
flow=0;
while(bfs(s,t)) {
for(int i=0; i<M; i++) {
temp[i]=head[i];
used[i]=true;
}
p=1;
path[p]=s;
while(p) {
int u=path[p];
if(u==t) {
now=inf;
for(int i=1; i<p; i++) {
now=min(now,e[cur[path[i]]].flow);
}
flow+=now;
for(int i=1; i<p; i++) {
e[cur[path[i]]].flow-=now;
e[cur[path[i]]^1].flow+=now;
if(!e[cur[path[i]]].flow) tempp=i;
}
p=tempp;
} else {
flag=false;
for(int i=temp[u]; ~i; i=e[i].next) {
int v=e[i].v;
if(used[v]&&e[i].flow&&level[u]+1==level[v]) {
cur[u]=i;
temp[u]=e[i].next;
flag=true;
path[++p]=v;
break;
}
}
if(flag) continue;
p--;
used[u]=false;
}
}
}
}
} ts;
struct project_problems {
int num;
int question[55];
} project[25];
int Map[55][55];
struct Node {
int value;
} newgraph[55];
int main() {
int _,cas = 0;
int n,m;
int allprofit;
scanf("%d",&_);
while(_--) {
scanf("%d%d",&n,&m);
allprofit = 0;
for(int i=0; i<n; i++) {
scanf("%d",&profit[i]);
allprofit += profit[i];
}
for(int i=0; i<m; i++) {
scanf("%d",&cost[i]);
}
ts.init();
gx.init();
for(int i=0; i<n; i++) {
int num;
scanf("%d",&num);
project[i].num = num;
for(int j=0; j<num; j++) {
scanf("%d",&project[i].question[j]);
}
}
int tmp;
for(int i=0; i<m; i++) {
for(int j=0; j<m; j++) {
scanf("%d",&tmp);
Map[i][j] = tmp;
if(tmp == 1) {
gx.add(i+1,j+1);
}
}
}
gx.solve(m);
for(int i=0; i<=m; i++) {
newgraph[i].value = 0;
}
for(int i=0; i<m; i++) {
newgraph[gx.getbelong(i+1)].value += cost[i];
}
int s = n+m+1;
int t = n+m+2;
for(int i=0; i<n; i++) {
ts.add(s,i,profit[i]);
}
for(int i=0; i<n; i++) {
for(int j=0; j<project[i].num; j++) {
project[i].question[j] = gx.getbelong(project[i].question[j]+1);
}
sort(project[i].question,project[i].question+project[i].num);
project[i].num = unique(project[i].question,project[i].question+project[i].num) - project[i].question;
for(int j=0; j<project[i].num; j++) {
ts.add(i,project[i].question[j]+n,inf);
}
}
bool flag[55][55];
mt(flag,false);
for(int i=0; i<m; i++) {
for(int j=0; j<m; j++) {
if(gx.getbelong(i+1)!=gx.getbelong(j+1) && Map[i][j]==1 && !flag[gx.getbelong(i+1)][gx.getbelong(i+1)]) {
ts.add(gx.getbelong(i+1)+n,gx.getbelong(j+1)+n,inf);
flag[gx.getbelong(i+1)][gx.getbelong(j+1)] = true;
}
}
}
for(int i=1; i<=gx.getbcnt(); i++) {
ts.add(i+n,t,newgraph[i].value);
}
ts.solve(s,t);
printf("Case #%d: %d\n",++cas,allprofit - ts.getflow());
}
return 0;
}

43
HDOJ/4972_autoAC.cpp Normal file
View File

@ -0,0 +1,43 @@
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
using namespace std;
const int maxn=1e5+100;
int n,a[maxn];
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
bool is=true;
for(int i=2;i<=n;i++)
if(abs(a[i]-a[i-1])>3||(a[i]==a[i-1]&&a[i]!=1))
{
is=false;
break;
}
if(!is)
{
printf("Case #%d: 0\n",cas++);
continue;
}
int cnt=0;
for(int i=2;i<=n;i++)
{
if(a[i]==2&&a[i-1]==1)
cnt++;
if(a[i]==1&&a[i-1]==2)
cnt++;
}
if(a[n])
printf("Case #%d: %d\n",cas++,cnt*2+2);
else
printf("Case #%d: %d\n",cas++,cnt+1);
}
return 0;
}

165
HDOJ/4973_autoAC.cpp Normal file
View File

@ -0,0 +1,165 @@
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<ctime>
#include<string.h>
#include<string>
using namespace std;
#define ll __int64
#define eps 1e-8
template<class T>
inline void scan_d(T &ret) {
char c; ret=0;
while((c=getchar())<'0'||c>'9');
while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
}
struct Tree
{
ll zuo;
ll flag;
bool isleaf;
ll maxnum,num;
};
Tree T[50000*4];
ll ans;
void build(ll l, ll r, ll rt)
{
T[rt].flag = 1;
if(l == r)
{
T[rt].isleaf = 1;
T[rt].zuo = 0;
T[rt].maxnum = T[rt].num = 1;
return;
}
ll m = (l+r)>>1;
T[rt].isleaf = 0;
T[rt].zuo = m-l+1;
T[rt].maxnum = 1;
build(l,m,rt<<1);
build(m+1,r,rt<<1|1);
T[rt].num=T[rt<<1].num+T[rt<<1|1].num;
}
void pushup(ll rt)
{
T[rt].maxnum = max(T[rt<<1].maxnum,T[rt<<1|1].maxnum);
T[rt].num = T[rt<<1].num+T[rt<<1|1].num;
T[rt].zuo = T[rt<<1].num;
}
void pushdown(ll rt)
{
if(T[rt].flag != 1)
{
T[rt<<1].maxnum *= T[rt].flag;
if(!T[rt<<1].isleaf) T[rt<<1].zuo *= T[rt].flag;
T[rt<<1].num *= T[rt].flag;
T[rt<<1|1].maxnum *= T[rt].flag;
if(!T[rt<<1|1].isleaf) T[rt<<1|1].zuo *= T[rt].flag;
T[rt<<1|1].num *= T[rt].flag;
T[rt<<1].flag *= T[rt].flag;
T[rt<<1|1].flag *= T[rt].flag;
T[rt].flag = 1;
}
}
void update(ll L, ll R, ll l, ll r, ll rt)
{
if(T[rt].isleaf == 1)
{
if(L >= l && R <= r)
{
T[rt].num+=R-L+(ll)1;
T[rt].maxnum = T[rt].num;
}
else if(L >= l && L <= r && R >= r)
{
T[rt].num+=r-L+(ll)1;
T[rt].maxnum = T[rt].num;
}
else if(L <= l && R >= r)
{
T[rt].num+=r-l+(ll)1;
T[rt].maxnum = T[rt].num;
}
else if(L <= l && R >= l && R <= r)
{
T[rt].num+=R-l+(ll)1;
T[rt].maxnum = T[rt].num;
}
return;
}
if(L <= l && R >= r)
{
T[rt].flag *= (ll)2;
T[rt].num *= (ll)2;
T[rt].maxnum *= (ll)2;
T[rt].zuo *= (ll)2;
return;
}
pushdown(rt);
ll m = l+T[rt].zuo-(ll)1;
if(L <= m) update(L,R,l,m,rt<<1);
if(R > m) update(L,R,m+1,r,rt<<1|1);
pushup(rt);
}
void query(ll L, ll R, ll l, ll r, ll rt)
{
if(T[rt].isleaf == 1)
{
if(L >= l && R <= r)
ans = max(ans,(ll)(R-L+1));
else if(L >= l && L <= r && R >= r)
ans = max(ans,(ll)(r-L+1));
else if(L <= l && R >= r)
ans = max(ans,(ll)(r-l+1));
else if(L <= l && R >= l && R <= r)
ans = max(ans,(ll)(R-l+1));
return;
}
if(L <= l && R >= r)
{
ans = max(ans,T[rt].maxnum);
return;
}
pushdown(rt);
ll m = l+T[rt].zuo-(ll)1;
if(L <= m) query(L,R,l,m,rt<<1);
if(R > m) query(L,R,m+1,r,rt<<1|1);
}
int main()
{
int t,cas=1;
scanf("%d",&t);
while(t--)
{
printf("Case #%d:\n",cas++);
ll n,m;
scan_d(n); scan_d(m);
build(1,n,1);
while(m--)
{
char temp;
ll l,r;
scanf("%c",&temp); scan_d(l); scan_d(r);
if(temp == 'D')
{
update(l,r,1,n,1);
n+=(ll)(r-l+1);
}
else
{
ans = 0;
query(l,r,1,n,1);
printf("%I64d\n",ans);
}
}
}
return 0;
}

15
HDOJ/4974_autoAC.cpp Normal file
View File

@ -0,0 +1,15 @@
#include<cstdio>
#include<algorithm>
using namespace std;
int t,c=1,m,a,i,s,n;
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n),s=0,m=0;
while(n--)
scanf("%d",&a),s+=a,m=max(m,a);
printf("Case #%d: %d\n",c++,max(m,(int)(s/2.0+0.5)));
}
}

164
HDOJ/4975_autoAC.cpp Normal file
View File

@ -0,0 +1,164 @@
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
const int inf=1<<29;
const int maxn=1100;
const int maxm=6e5;
struct Edge
{
int u;
int v;
int f;
int nxt;
Edge(){}
Edge(int su,int sv,int sf,int snxt):u(su),v(sv),f(sf),nxt(snxt){}
}E[maxm];
int n,m,row[510],col[510],sumr,sumc;
int e,st,des,head[maxn],level[maxn];
bool vis[maxn];
int q[maxn];
void AddEdge(int u,int v,int f)
{
E[e]=Edge(u,v,f,head[u]);
head[u]=e++;
E[e]=Edge(v,u,0,head[v]);
head[v]=e++;
}
void Build()
{
e=st=0,des=n+m+1;
memset(head,-1,sizeof(head));
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
AddEdge(i,n+j,9);
for(int i=1;i<=n;i++)
AddEdge(st,i,row[i]);
for(int i=1;i<=m;i++)
AddEdge(n+i,des,col[i]);
}
bool BFS()
{
memset(level,0,sizeof(level));
level[st]=1;
int pre=0,last=1;
q[pre]=st;
while(pre<last)
{
int u=q[pre++];
for(int i=head[u];i!=-1;i=E[i].nxt)
if(E[i].f&&!level[E[i].v])
{
level[E[i].v]=level[u]+1;
q[last++]=E[i].v;
if(E[i].v==des)
return level[E[i].v];
}
}
return level[des];
}
int DFS(int u,int maxf)
{
if(u==des||!maxf)
return maxf;
int f=0;
for(int i=head[u];i!=-1;i=E[i].nxt)
if(E[i].f&&level[E[i].v]==level[u]+1)
{
int t=DFS(E[i].v,min(E[i].f,maxf-f));
if(t>0)
{
E[i].f-=t;
E[i^1].f+=t;
f+=t;
if(f==maxf)
break;
}
else
level[E[i].v]=0;
}
return f;
}
int maxflow()
{
int ans=0;
while(BFS())
ans+=DFS(st,inf);
return ans;
}
bool dfs(int u,int f)
{
vis[u]=1;
for(int &i=head[u];i!=-1;i=E[i].nxt)
if(E[i].f)
{
if(!vis[E[i].v])
{
if(dfs(E[i].v,u))
return true;
}
else if(E[i].v!=f)
return true;
}
vis[u]=0;
return false;
}
void solve(int sumr,int sumc)
{
if(sumr!=sumc)
{
printf("So naive!\n");
return;
}
Build();
int ans=maxflow();
if(ans!=sumc)
{
printf("So naive!\n");
return;
}
memset(vis,0,sizeof(vis));
bool is=false;
for(int i=1;i<=n;i++)
if(dfs(i,-1))
is=true;
if(!is)
printf("So simple!\n");
else
printf("So young!\n");
}
inline bool read(int &n)
{
int x = 0, tmp = 1;
char c = getchar();
while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
if(c == EOF) return false;
if(c == '-') c = getchar(), tmp = -1;
while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
n = x*tmp;
return true;
}
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
sumr=sumc=0;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
read(row[i]);
sumr+=row[i];
}
for(int i=1;i<=m;i++)
{
read(col[i]);
sumc+=col[i];
}
printf("Case #%d: ",cas++);
solve(sumr,sumc);
}
return 0;
}

56
HDOJ/4976_autoAC.cpp Normal file
View File

@ -0,0 +1,56 @@
#include <iostream>
#include<stdio.h>
#include<vector>
#include<queue>
#include<stack>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define LL long long
#define lcm(a,b) (a*b/gcd(a,b))
#define N 1500001
int a[1100];
int b[1100];
int c[1100];
int dp[1100];
int main()
{
int T;
int cas=0;
cin>>T;
int n;
while(T--)
{
cas++;
scanf("%d",&n);
int m=0;
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
m=max(a[i],m);
}
sort(a+1,a+n+1);
memset(c,0,sizeof(c));
for(int i=1; i<=n; i++)
{
int j=a[i];
while(c[j]&&j>0)j--;
if(j!=0)b[j]=a[i]-j;
c[j]=1;
}
memset(dp,0,sizeof(dp));
int maxx=-1;
for(int i=1; i<=m; i++)
{
for(int j=i;j>=1;j--)dp[j]=dp[j-1];
for(int j=0;j<=i;j++)
{
if(j>b[i]&&c[i])dp[j-b[i]-1]=max(dp[j-b[i]-1],dp[j]+1);
}
}
for(int j=0;j<=m;j++)maxx=max(maxx,dp[j]);
printf("Case #%d: %d\n",cas,maxx);
}
return 0;
}

184
HDOJ/4977_autoAC.cpp Normal file
View File

@ -0,0 +1,184 @@
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#define next (i+1)%n
const double eps = 1e-8;
using namespace std;
const double PI = acos(-1);
const double sqrt3 = sqrt(3);
int dcmp(double x)
{
if (x < -eps) return -1;
else if (x > eps) return 1;
else return 0;
}
double add(double a, double b)
{
if (abs(a + b) < eps * (abs(a) + abs(b))) return 0;
return a + b;
}
struct Point
{
double x,y;
Point() {}
Point(double x,double y) : x(x),y(y){}
Point operator + (Point p) {return Point(add(x, p.x),add(y, p.y)); }
Point operator - (Point p) {return Point(add(x, -p.x),add(y, -p.y)); }
Point operator * (double a) {return Point(x * a, y * a); }
Point operator / (double a) {return Point(x / a, y / a); }
bool operator < (const Point p) const {return dcmp(x - p.x) < 0 || dcmp(x - p.x) == 0 && dcmp(y - p.y)<0; }
bool operator == (const Point p) const {return dcmp(x - p.x) == 0 && dcmp(y - p.y) == 0; }
double dot(Point p) {return add(x * p.x, y * p.y); }
double det(Point p) {return add(x * p.y, -y * p.x); }
double length() {return sqrt(x*x + y*y); }
double Angle() {return atan2(y,x);}
Point Rotate(double rad) {return Point(add(x * cos(rad),- y * sin(rad)), add(x * sin(rad), y * cos(rad))); }
Point Normal() {return Point(-y / length(), x / length()); }
};
typedef Point Vector;
int ConvexHull(Point* p,int n, Point* ch)
{
sort(p,p+n);
int m = 0;
for (int i = 0; i < n ; i ++)
{
while (m > 1 && (ch[m-1] - ch[m-2]).det(p[i] - ch[m-2]) <= 0) m --;
ch[m++] = p[i];
}
int k = m;
for (int i = n-2; i >= 0; i--)
{
while (m > k &&(ch[m-1] - ch[m-2]).det(p[i] - ch[m-2]) <= 0) m--;
ch[m++] = p[i];
}
if (n > 1) m--;
return m;
}
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);
}
}C;
int getLineCircleIntersection(Point p,Vector v,Circle C,Point sol[])
{
double t1,t2;
double a= v.x, b = p.x - C.c.x, c = v.y, d = p.y - C.c.y;
double e = a*a + c*c, f = 2*(a*b + c*d), g = b*b + d*d -C.r*C.r;
double delta = f*f - 4*e*g;
if (dcmp(delta) < 0) return 0;
if (dcmp(delta) == 0)
{
t1 = t2 = -f / (2 * e); sol[0] = sol[1] = p + v*t1;
return 1;
}
t1 = (-f - sqrt(delta)) / (2*e); sol[0] = p + v*t1;
t2 = (-f + sqrt(delta)) / (2*e); sol[1] = p + v*t2;
return 2;
}
double getAngle(double R, double l)
{
return asin(R * sin( PI/6 ) / l) - PI/6;
}
bool OnSegment(Point p, Point a1, Point a2)
{
return dcmp((a1 - p).det(a2 - p)) == 0 && dcmp((a1 - p).dot(a2 - p)) < 0;
}
int cnt;
vector <pair<double,int> > ans;
bool Section(Point a,Point b,double R)
{
double aa = a.Angle(), bb = b.Angle();
double da = getAngle(R, a.length()), db = getAngle(R, b.length());
if (dcmp(aa-bb-PI) > 0) aa-=2*PI;
else if(dcmp(bb-aa-PI) > 0) bb-=2*PI;
double l=max(aa-da,bb-db), r = min(aa+da,bb+db);
if (dcmp(r-l)<0)
return false;
cnt++;
for(int i=0;i<6;i++)
{
l+=PI/3,r+=PI/3;
if(dcmp(l-PI)>=0) l -= 2*PI;
if(dcmp(r-PI)>=0) r -= 2*PI;
ans.push_back(make_pair(l,-1));
ans.push_back(make_pair(r,1) );
if(r < l) cnt--;
}
return true;
}
bool getSection(Point a, Point b, Circle C)
{
double lena = a.length(),lenb = b.length();
if (dcmp(lena-C.r) <= 0 && dcmp(lenb-C.r) <= 0)
return true;
Point sol[2];
int n = getLineCircleIntersection(a,b-a,C,sol);
double R = C.r*sqrt3;
if (dcmp(lena-C.r) > 0 && dcmp(lenb - C.r) > 0)
{
if (n == 0) return Section(a,b,R);
bool judge0 = OnSegment(sol[0],a,b);
bool judge1 = OnSegment(sol[1],a,b);
if (judge0 || judge1)
return Section(a,sol[0],R) && Section(sol[1],b,R);
return Section(a, b, R);
}
if (dcmp(lena - C.r) > 0)
return Section(a,sol[0],R);
return Section(sol[1],b,R);
}
Point P[2000],Hull[2000];
int main()
{
int T;
scanf("%d",&T);
for (int kase = 1; kase <= T; kase ++)
{
int m,r;
Circle C = Circle(Point(0,0),0);
scanf("%d%lf", &m, &C.r);
const double R = C.r*sqrt3;
bool flag = true;
for (int i = 0; i < m; i++)
scanf("%lf%lf", &P[i].x, &P[i].y);
int n = ConvexHull(P, m, Hull);
ans.clear();
cnt = 0;
for (int i = 0; i < n && flag; i++)
{
if (dcmp(Hull[i].length()-R) > 0 || dcmp(Hull[next].length()-R) > 0)
{
flag = false;
break;
}
flag = getSection(Hull[i],Hull[next],C);
}
if (flag)
{
sort(ans.begin(), ans.end());
int now = 0,h = 0;
for (int i = 0; i < ans.size(); i++)
{
now -= ans[i].second;
if (h == cnt)
break;
h = max(h, now);
}
if (h!=cnt)
flag = false;
}
printf("Case #%d: ", kase);
if (flag) printf("Succeeded.\n");
else printf("Failed.\n");
}
}

88
HDOJ/4978_autoAC.cpp Normal file
View File

@ -0,0 +1,88 @@
#include<iostream>
#include<math.h>
#include<cstring>
#include<stdio.h>
#include<algorithm>
using namespace std;
#define offset 100
#define eps 1e-8
#define PI acos(-1.0)
#define MAXN 105
#define zero(x) (((x)>0? (x):-(x))<eps)
#define _sign(x) ((x)>eps? 1:((x)<-eps? 2:0))
struct point
{
double x;
double y;
point(const double &x = 0, const double &y = 0):x(x), y(y) {}
void in()
{
scanf("%lf %lf", &x, &y);
}
void out()const
{
printf("%.2f %.2f\n",x, y);
}
};
double xmult(point p1,point p2,point p0)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
double dis(point a, point b)
{
return sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) ) ;
}
point p1,p2;
int graham_cp(const void* a,const void* b)
{
double ret=xmult(*((point*)a),*((point*)b),p1);
return zero(ret)?(xmult(*((point*)a),*((point*)b),p2)>0?1:-1):(ret>0?1:-1);
}
void _graham(int n,point* p,int& s,point* ch)
{
int i,k=0;
for (p1=p2=p[0],i=1; i<n; p2.x+=p[i].x,p2.y+=p[i].y,i++)
if (p1.y-p[i].y>eps||(zero(p1.y-p[i].y)&&p1.x>p[i].x))
p1=p[k=i];
p2.x/=n,p2.y/=n;
p[k]=p[0],p[0]=p1;
qsort(p+1,n-1,sizeof(point),graham_cp);
for (ch[0]=p[0],ch[1]=p[1],ch[2]=p[2],s=i=3; i<n; ch[s++]=p[i++])
for (; s>2&&xmult(ch[s-2],p[i],ch[s-1])<-eps; s--);
}
int graham(int n,point* p,point* convex,int maxsize=1,int dir=1)
{
point* temp=new point[n];
int s,i;
_graham(n,p,s,temp);
for (convex[0]=temp[0],n=1,i=(dir?1:(s-1)); dir?(i<s):i; i+=(dir?1:-1))
if (maxsize||!zero(xmult(temp[i-1],temp[i],temp[(i+1)%s])))
convex[n++]=temp[i];
delete []temp;
return n;
}
int main()
{
int T, N, D, cas = 1;
scanf("%d",&T);
while(T--)
{
point P[MAXN];
point c[MAXN];
scanf("%d%d",&N,&D);
for(int i = 0; i < N; i ++)
P[i].in();
double ans = 0 ;
if(N>2)
{
int num = graham(N,P,c);
for(int i = 0; i < num-1; i ++)
{
ans+=dis(c[i],c[i+1]);
}
ans+=dis(c[0],c[num-1]);
}
else ans = dis(P[0],P[1])*2;
printf("Case #%d: %.4lf\n",cas++,ans/(PI*D));
}
}

78
HDOJ/4979_autoAC.cpp Normal file
View File

@ -0,0 +1,78 @@
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cctype>
#include <ctime>
using namespace std;
int ans[8][8][8]={
{
{1}
},
{
{2},
{1,1}
},
{
{3},
{2,3},
{1,1,1}
},
{
{4},
{2,6},
{2,3,4},
{1,1,1,1}
},
{
{5},
{3,10},
{2,4,10},
{2,3,4,5},
{1,1,1,1,1}
},
{
{6},
{3,15},
{2,6,20},
{2,3,6,15},
{2,3,4,5,6},
{1,1,1,1,1,1}
},
{
{7},
{4,21},
{3,7,35},
{2,5,12,35},
{2,3,5,9,21},
{2,3,4,5,6,7},
{1,1,1,1,1,1,1}
},
{
{8},
{4,28},
{3,11,56},
{2,6,14,70},
{2,4,8,20,56},
{2,3,4,7,12,28},
{2,3,4,5,6,7,8},
{1,1,1,1,1,1,1,1}
}
};
int main()
{
int n,m,r,T,tt=0;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&r);
printf("Case #%d: %d\n",++tt,ans[n-1][m-1][r-1]);
}
return 0;
}

79
HDOJ/4980_autoAC.cpp Normal file
View File

@ -0,0 +1,79 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <functional>
#define snuke(it,x) for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); ++ it)
const int N = 500 + 5;
const int D = 5;
const int INF = 0x3f3f3f3f;
std::vector<std::pair<int,int> > edges[N];
int n,drop_cost,dp[N][D],tmp[D][D];
inline void toMin(int &a,int b) {
if (a>b) a = b;
}
void dfs(int u,int fa) {
dp[u][0] = 0;
snuke(it,edges[u]) {
int v = it->first;
int w = it->second;
if (v==fa) continue;
dfs(v,u);
for (int j = 0; j < D; ++ j) {
for (int k = 0; k < D; ++ k) {
tmp[j][k] = INF;
}
}
for (int j = 0; j < D; ++ j) {
for (int k = 1; j+k < D; ++ k) {
toMin(tmp[j+k][std::min(j,k)],dp[u][j]+dp[v][k]+k*w);
}
}
for (int j = D-1; j >= 2; -- j) {
for (int k = j>>1; k >= 1; -- k) {
toMin(tmp[j-2][k-1],tmp[j][k]+drop_cost);
}
}
for (int j = D-1; j >= 0; -- j) {
dp[u][j] = INF;
if (j!=D-1) toMin(dp[u][j],dp[u][j+1]+drop_cost);
for (int k = 0; k < D; ++ k) {
toMin(dp[u][j],tmp[j][k]);
}
}
}
toMin(dp[u][1],dp[u][0]);
}
inline void read(int &x) {
char c; bool sign = false;
for (c = getchar(); c<'0' || c>'9'; c = getchar()) if (c=='-') sign = true;
for (x = 0; c>='0' && c<='9'; c = getchar()) x = x*10+c-'0';
sign && (x=-x);
}
int main() {
int cas,ca = 0;
read(cas);
while (cas--) {
read(n); read(drop_cost);
std::fill(edges,edges+n,std::vector<std::pair<int,int> >());
for (int i = 0; i < n-1; ++ i) {
int a,b,c;
read(a),read(b),read(c);
edges[a].push_back(std::make_pair(b,c));
edges[b].push_back(std::make_pair(a,c));
}
memset(dp,INF,sizeof(dp));
dfs(0,-1);
printf("Case #%d: ",++ca);
if (n==1) {
puts("0");
continue;
}
int answer = INF;
for (int i = 0; i < D; ++ i) {
toMin(answer,dp[0][i]+i*drop_cost);
}
printf("%d\n",answer);
}
return 0;
}

24
HDOJ/4981_autoAC.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
int N,array[1050];
while(~scanf("%d",&N))
{
int sum = 0;
for(int i = 0; i < N; i++)
{
scanf("%d",&array[i]);
sum += array[i];
}
sort(array,array+N);
double aver = (double)sum/N;
if(aver >= (double)array[(N-1)/2])
printf("NO\n");
else
printf("YES\n");
}
return 0;
}

50
HDOJ/4982_autoAC.cpp Normal file
View File

@ -0,0 +1,50 @@
#include <iostream>
#include <cstring>
#include <cstdlib>
#include<cstdio>
#include <queue>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
#define INF 2000000000
using namespace std;
typedef long long ll;
const int MAX_E=10000;
const int MAX_N=1005;
const int MAX_K=3000;
const int MAXN=10010;
bool judge(int sum,int y,int k)
{
int mid1=(k-1)*k/2;
int mid2=sum-mid1;
if(mid2<=k-1) return false;
if(y>k-1&&y!=mid2) return true;
if(y<=k-1){
if(k-y<=mid2-(k+1)) return true;
else return false;
}
if(y==mid2){
if(mid2-(k-1)>=3) return true;
else return false;
}
}
int main()
{
int n,k,T,mm;
bool flag;
while(scanf("%d%d",&n,&k)!=EOF){
int c1=(int)sqrt((double)k*(k-1)/2);
int c2=(int)sqrt((double)n);
flag=0;
for(int i=c1;i<=c2;i++){
if((k*(k-1)/2<=i*i)&&(i*i<=n-1)&&judge(i*i,n-i*i,k-1)){
flag=1;
break;
}
}
if(flag==1) printf("YES\n");
else printf("NO\n");
}
return 0;
}

76
HDOJ/4983_autoAC.cpp Normal file
View File

@ -0,0 +1,76 @@
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#define mod 1000000007
#define LL __int64
using namespace std;
int prim[81000],tot;
int u[1000005];
bool vis[1000005];
void init(void){
int i;
for(i=1;i<=1000000;i++) u[i]=i;
for(i=2;i<=1000000;i++){
if(!vis[i]){
for(int j=i;j<=1000000;j+=i){
u[j]=u[j]/i*(i-1);
vis[j]=1;
}
prim[tot++]=i;
}
}
}
int get(int n){
int i,s,q;
q=n;
s=n;
for(i=0;i<tot&&(LL)prim[i]*prim[i]<=q;i++){
if(n%prim[i]==0){
s=s/prim[i]*(prim[i]-1);
while(n%prim[i]==0) n=n/prim[i];
}
if(n==1) break;
}
if(n!=1){
s=s/n*(n-1);
}
return s;
}
int main()
{
int i,k;
int n;
LL s=0,q;
init();
while(scanf("%d%d",&n,&k)!=EOF){
if(n==1){
printf("1\n");
continue;
}
s=0;
if(k>2){ printf("0\n");
continue;
}
if(k==2){
printf("1\n");
continue;
}
for(i=1;(LL)i*i<=n;i++){
if(n%i==0){
if(i<=1000000)
q=u[i];
else q=get(i);
if(n/i<=1000000) q=q*u[n/i];
else q=q*get(n/i);
q=q%mod;
if((LL)i*i!=n)
q=(q*2)%mod;
s+=q;
s%=mod;
}
}
printf("%d\n",s);
}
}

117
HDOJ/4984_autoAC.cpp Normal file
View File

@ -0,0 +1,117 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
#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 travel( e , H , u ) for ( Edge* e = H[u] ; e ; e = e -> next )
#define CLR( a , x ) memset ( a , x , sizeof a )
typedef long long LL ;
const int MAXN = 55 ;
const int MAXE = 205 ;
const double INF = 1e10 ;
const double eps = 1e-8 ;
struct Edge {
int v ;
double c ;
Edge* next ;
} E[MAXE] , *H[MAXN] , *cur ;
struct Line {
int x , y , a , b ;
double v ;
void input () {
scanf ( "%d%d%d%d" , &x , &y , &a , &b ) ;
}
bool operator < ( const Line& p ) const {
return v > p.v ;
}
} L[MAXE] ;
int n , m , t ;
int p[MAXN] ;
double f[MAXN] ;
double a[10005] ;
int cnt ;
double st , ed , ans ;
int sgn ( double x ) {
return ( x > eps ) - ( x < -eps ) ;
}
void clear () {
cur = E ;
CLR ( H , 0 ) ;
FOR ( i , 1 , n ) {
p[i] = i ;
f[i] = INF ;
}
}
void addedge ( int u , int v , double c ) {
cur -> v = v ;
cur -> c = c ;
cur -> next = H[u] ;
H[u] = cur ++ ;
}
int find ( int x ) {
return p[x] == x ? x : ( p[x] = find ( p[x] ) ) ;
}
void kruskal () {
sort ( L , L + m ) ;
int cnt = 1 ;
REP ( i , 0 , m ) {
int x = find ( L[i].x ) ;
int y = find ( L[i].y ) ;
if ( x != y ) {
p[x] = y ;
addedge ( L[i].x , L[i].y , L[i].v ) ;
addedge ( L[i].y , L[i].x , L[i].v ) ;
if ( ++ cnt == n ) break ;
}
}
}
void dfs ( int u , int fa ) {
travel ( e , H , u ) {
int v = e -> v ;
if ( v == fa ) continue ;
f[v] = min ( f[u] , e -> c ) ;
dfs ( v , u ) ;
}
}
int unique ( int n ) {
int cnt = 1 ;
sort ( a + 1 , a + n + 1 ) ;
FOR ( i , 2 , n ) if ( sgn ( a[i] - a[cnt] ) ) a[++ cnt] = a[i] ;
return cnt ;
}
void solve () {
ans = 0 ;
cnt = 0 ;
scanf ( "%d%d%d" , &n , &m , &t ) ;
REP ( i , 0 , m ) {
L[i].input () ;
REP ( j , 0 , i ) {
if ( L[i].b == L[j].b ) continue ;
a[++ cnt] = ( double ) ( L[i].a - L[j].a ) / ( L[j].b - L[i].b ) ;
}
}
a[++ cnt] = t ;
cnt = unique ( cnt ) ;
st = 0 ;
FOR ( i , 1 , cnt ) {
if ( a[i] < 0 ) continue ;
if ( a[i] > t ) break ;
ed = a[i] ;
double mid = ( st + ed ) / 2 ;
REP ( j , 0 , m ) L[j].v = L[j].a + L[j].b * mid ;
clear () ;
kruskal () ;
dfs ( 1 , 0 ) ;
FOR ( j , 2 , n ) ans += f[j] * ( ed - st ) ;
st = ed ;
}
printf ( "%.3f\n" , ans / t ) ;
}
int main () {
int T ;
scanf ( "%d" , &T ) ;
while ( T -- ) solve () ;
return 0 ;
}

29
HDOJ/4985_autoAC.cpp Normal file
View File

@ -0,0 +1,29 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 100005;
int n, a[N], vis[N];
int main() {
while (~scanf("%d", &n)) {
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) {
if (vis[i]) continue;
int t = i;
printf("(%d", t);
vis[t] = 1;
t = a[t];
while (vis[t] == 0) {
vis[t] = 1;
printf(" %d", t);
t = a[t];
}
printf(")");
}
printf("\n");
}
return 0;
}

19
HDOJ/4986_autoAC.cpp Normal file
View File

@ -0,0 +1,19 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std ;
double dp[1000005] ;
int main()
{
dp[1]=1.0 ;
for(int i=2 ;i<=1000000 ;i++)
dp[i]=dp[i-1]+1.0/i ;
int n ;
while(~scanf("%d",&n))
{
if(n>1000000)printf("%.4f\n",0.57721566490153286060651209+log(n+1)) ;
else printf("%.4f\n",dp[n]) ;
}
return 0 ;
}

41
HDOJ/4987_autoAC.cpp Normal file
View File

@ -0,0 +1,41 @@
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include<stack>
using namespace std;
typedef long long ll;
const double eps=0.0000000000001;
double dp[1000005];
int main()
{
int n,m;
while(~scanf("%d%d",&m,&n))
{
int flag=1;
memset(dp,0,sizeof(dp));
dp[0]=1.0;
dp[1]=1.0/m;
for(int i=2;i<=n;i++)
{
dp[i]+=dp[i-1]*(m+1)/m;
if(i-1-m>=0)
dp[i]-=dp[i-1-m]/m;
if(fabs(dp[i]-dp[i-1])<eps)
{
printf("%.5lf\n",dp[i]);
flag=0;
break;
}
}
if(flag)
{
printf("%.5lf\n",dp[n]);
}
}
}

247
HDOJ/4988_autoAC.cpp Normal file
View File

@ -0,0 +1,247 @@
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#define INF (~0U>>1)
#define lson l,m,o<<1
#define rson m+1,r,o<<1|1
#define maxn 100010
typedef __int64 ll;
struct node{
int v;
int sz;
int s;
node *p;
node *ch[2];
node(){
sz=v=0;
s=-1;
p=ch[0]=ch[1]=this;
}
bool cmp(int s)
{
return this->s<s;
}
int cd(node *o)
{
return ch[1]==o?1:0;
}
void addc(node *o,int d)
{
ch[d]=o;
o->p=this;
}
}Tnull,*null=&Tnull;
node *newnode(int v)
{
node *u=(node*)malloc(sizeof(node));
u->sz=1;
u->s=rand();
u->v=v;
u->ch[0]=u->ch[1]=null;
return u;
}
void pushup(node *o)
{
o->sz=o->ch[0]->sz+o->ch[1]->sz+1;
}
void rot(node *o,int d)
{
node *k=o->ch[d];
o->addc(k->ch[d^1],d);
o->p->addc(k,o->p->cd(o));
k->addc(o,d^1);
pushup(o);
pushup(k);
}
void Insert(node *o,node *pre,int v,int dd)
{
if(o==null){
pre->addc(newnode(v),dd);
return;
}
int d;
if(v>=o->v){
d=1;
Insert(o->ch[1],o,v,1);
}
else {
d=0;
Insert(o->ch[0],o,v,0);
}
if(o->cmp(o->ch[d]->s))rot(o,d);
else pushup(o);
}
void Delete(node *o,int v)
{
if(o==null)return;
if(v==o->v){
while(o->ch[0]!=null&&o->ch[1]!=null)
{
int d=o->ch[0]->cmp(o->ch[1]->s);
rot(o,d);
--o->p->sz;
}
node *k;
o->ch[0]==null? k=o->ch[1]:k=o->ch[0];
o->p->addc(k,o->p->cd(o));
free(o);
return;
}
v>o->v ? Delete(o->ch[1],v) : Delete(o->ch[0],v);
pushup(o);
}
int Find(node *o,int v,int vis)
{
if(o==null)return 0;
int ax=0;
if(v>o->v){
if(!vis)ax+=o->ch[0]->sz+1;
ax+=Find(o->ch[1],v,vis);
}
else {
if(vis)ax+=o->ch[1]->sz+1;
ax+=Find(o->ch[0],v,vis);
}
return ax;
}
void Free(node *o)
{
if(o==null)return;
Free(o->ch[0]);
Free(o->ch[1]);
free(o);
}
node *tree[maxn];
int a[maxn];
int b[maxn];
int p[maxn];
int pa[maxn];
int pb[maxn];
int n;
int bit(int x)
{
return x&(-x);
}
void buildST()
{
for(int i=0;i<n;++i){
tree[i]=(node*)malloc(sizeof(node));
tree[i]->ch[0]=tree[i]->ch[1]=null;
}
}
void updateST1(int p,int v)
{
while(p<n)
{
Insert(tree[p]->ch[1],tree[p],v,1);
p+=bit(p+1);
}
}
void updateST2(int p,int v)
{
while(p<n)
{
Delete(tree[p]->ch[1],v);
p+=bit(p+1);
}
}
int queryST(int p,int v1,int v2)
{
int ax=0;
while(p>=0)
{
ax+=Find(tree[p]->ch[1],v1,0) + Find(tree[p]->ch[1],v2,1);
p-=bit(p+1);
}
return ax;
}
void FreeST()
{
for(int i=0;i<n;++i)Free(tree[i]);
}
char s[10];
int sum[maxn];
void addbit(int x)
{
while(x<n)
{
sum[x]++;
x+=bit(x+1);
}
}
int getbit(int x)
{
int ans=0;
while(x>=0)
{
ans+=sum[x];
x-=bit(x+1);
}
return ans;
}
int main()
{
int i,Q;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;++i){
scanf("%d",&a[i]);
pa[a[i]]=i;
}
for(i=0;i<n;++i){
scanf("%d",&b[i]);
pb[b[i]]=i;
}
for(i=0;i<n;++i)p[i]=pa[b[i]];
memset(sum,0,sizeof(sum));
ll ans=0;
for(i=0;i<n;++i)
{
ans+=getbit(n-1)-getbit(p[i]);
addbit(p[i]);
}
buildST();
for(i=0;i<n;i++)updateST1(i,p[i]);
scanf("%d",&Q);
for(i=0;i<Q;++i)
{
scanf("%s",s);
if(s[0]=='Q')printf("%I64d\n",ans);
else {
int vis,x,y,temp;
scanf("%d%d%d",&vis,&x,&y);
if(x==y)continue;
if(vis){
swap(b[x],b[y]);
pb[b[x]]=x;
pb[b[y]]=y;
}
else {
swap(a[x],a[y]);
pa[a[x]]=x;
pa[a[y]]=y;
x=pb[a[x]];
y=pb[a[y]];
}
updateST2(x,p[x]);
updateST2(y,p[y]);
if(x>y)swap(x,y);
if(p[x]>p[y]){
y-x==1 ? temp=0 : temp=queryST(y,p[y],p[x])-queryST(x,p[y],p[x]);
ans+=((temp-y+x+1)<<1)-1;
}
else {
y-x==1 ? temp=0 : temp=queryST(y,p[x],p[y])-queryST(x,p[x],p[y]);
ans+=1+((y-x-1-temp)<<1);
}
updateST1(x,p[y]);
updateST1(y,p[x]);
swap(p[x],p[y]);
}
}
FreeST();
}
return 0;
}

30
HDOJ/4989_autoAC.cpp Normal file
View File

@ -0,0 +1,30 @@
#include<iostream>
#include<map>
#define ll long long
#define maxn 100+5
using namespace std;
ll a[maxn];
map<int,int>mapp;
int main()
{
int n;
while(cin>>n)
{
mapp.clear();
ll sum=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
for(int j=0;j<i;j++)
{
if(mapp.find(a[i]+a[j])==mapp.end())
{
sum+=(a[i]+a[j]);
mapp[a[i]+a[j]]=1;
}
}
}
cout<<sum<<endl;
}
return 0;
}

58
HDOJ/4990_autoAC.cpp Normal file
View File

@ -0,0 +1,58 @@
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <memory.h>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <ctype.h>
#include <sstream>
#define INF 1000000000
#define ll long long
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define MAXN 100010
using namespace std;
ll a,b,c,d;
ll n,m;
ll fun(ll a,ll b,ll c,ll d,ll n){
ll ta=1;
ll tb=0;
ll tc=0;
ll td=1;
while(n){
ll aa,bb,cc,dd;
if(n&1){
aa=a*ta+b*tc;
bb=a*tb+b*td;
cc=c*ta+d*tc;
dd=c*tb+d*td;
ta=aa; tb=bb; tc=cc; td=dd;
ta%=m; tb%=m; tc%=m; td%=m;
}
aa=a*a+b*c;
bb=a*b+b*d;
cc=c*a+d*c;
dd=c*b+d*d;
a=aa; b=bb; c=cc; d=dd;
a%=m; b%=m; c%=m; d%=m;
n>>=1;
}
return tb;
}
int main(){
while(~scanf("%I64d%I64d",&n,&m)){
ll nn=(n+1)/2;
ll ans=fun(4,1,0,1,nn);
if(n%2==0)ans*=2;
ans%=m;
printf("%I64d\n",ans);
}
return 0;
}

52
HDOJ/4991_autoAC.cpp Normal file
View File

@ -0,0 +1,52 @@
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;
typedef int ll;
const int MAX_N = 10005;
const ll MOD = 123456789;
ll C[105][MAX_N];
ll A[MAX_N],B[MAX_N];
int len;
int lowbit(int x){
return x&(-x);
}
void add(int x,ll value, int floor){
while(x<=len){
C[floor][x]=(C[floor][x]+value)%MOD;
x+=lowbit(x);
}
}
ll sum(int x, int floor){
ll ans=0;
while(x>0){
ans=(ans+C[floor][x])%MOD;
x-=lowbit(x);
}
return ans;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==2){
for(int i=0; i<n; ++i){
scanf("%d",&A[i]);
B[i] = A[i];
}
sort( B , B + n );
len = unique(B,B+n)-B;
memset(C,0,sizeof(C));
for(int i =0; i<n; ++i){
int loc = lower_bound(B,B+len,A[i])-B+1;
add(loc,1,1);
for(int j = 2; j<=m ; ++j ){
ll num = sum(loc-1,j-1);
add(loc,num,j);
}
}
ll ans = sum(len,m);
printf("%d\n",ans);
}
return 0;
}

169
HDOJ/4992_autoAC.cpp Normal file
View File

@ -0,0 +1,169 @@
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef __int64 LL;
const int MAXN = 1005;
int prim[MAXN], nprm;
bool vis[MAXN];
int n, m;
void init_prim()
{
for (int i = 2; i< MAXN; ++i)
{
if (!vis[i]) prim[nprm++] = i;
for (int j = 0; j< nprm && prim[j]&i < MAXN; ++i)
{
vis[prim[j]*i] = 1;
if (i % prim[j] == 0) break;
}
}
}
int Euler(int x)
{
int res = x;
for (int i = 0, k; i< nprm ; ++i)
{
k = prim[i];
if (k * k > x) break;
if (x % k == 0)
{
res = res/k*(k-1);
while (x%k==0) x/=k;
}
}
if (x!=1) res = res/x*(x-1);
return res;
}
int nfen, fen[100][2];
void m_divide(int x)
{
nfen = 0;
for (int i = 0, k; i< nprm ; ++i)
{
k = prim[i];
if (k * k > x) break;
if (x % k == 0)
{
fen[nfen][0] = k;
fen[nfen][1] = 0;
while (x%k==0) x/=k, ++fen[nfen][1];
++nfen;
}
}
if (x!=1) fen[nfen][0]=x, fen[nfen++][1]=1;
}
LL mpow(LL a, int b, LL mod)
{
LL res = 1LL;
while (b)
{
if (b&1) res = res*a%mod;
a = a*a%mod;
b >>= 1;
}
return res;
}
int caonima = 0;
LL ri;
void dfs(int idx, LL all)
{
if (caonima) return;
if (idx == nfen)
{
if (all == 1LL || all == m) return;
if (mpow(ri, all, n) == 1LL)
caonima = 1;
return;
}
for (int i = 0; i<=fen[idx][1]; ++i)
{
dfs(idx+1, all);
all *= fen[idx][0];
}
}
int check(LL r)
{
LL res = r;
if (mpow(r, m, n) != 1LL) return 0;
caonima = 0;
ri = res;
dfs(0, 1);
if (caonima) return 0;
return 1;
}
int gcd(int a, int b)
{
return b==0?a:gcd(b,a%b);
}
int opt[1000000], cnt;
void solve()
{
m = Euler(n);
m_divide(m);
int ff = 0;
for (int i = 2; i< n; ++i)
{
if (check(i))
{
ff = i;
break;
}
}
if (!ff)
{
printf("-1\n");
return;
}
cnt = 0;
opt[cnt++] = ff;
LL res = ff;
res = res*ff%n;
for (int i = 2; i< m; ++i, res = res*ff%n)
{
if (gcd(i, m) == 1)
{
opt[cnt++] = res;
}
}
sort(opt, opt+cnt);
printf("%d", opt[0]);
for (int i = 1; i< cnt; ++i)
{
printf(" %d", opt[i]);
}
puts("");
}
int main()
{
init_prim();
while (scanf("%d", &n) != EOF)
{
if (n==2) puts("1");
else if (n==4) puts("3");
else
{
int p = n, cc = 0;
if (n%2==0) n>>=1;
for (int i = 0, k; i<nprm ; ++i)
{
k = prim[i];
if (k*k > n) break;
if (n % k == 0)
{
if (++cc > 1) break;
while (n % k==0) n /= k;
}
}
if (n!=1) ++cc;
if (cc!=1) puts("-1");
else
{
n = p;
solve();
}
}
}
return 0;
}

19
HDOJ/4993_autoAC.cpp Normal file
View File

@ -0,0 +1,19 @@
#include<stdio.h>
int main ()
{
int t ,a ,b ,c ,Ans ,x ,y;
scanf("%d" ,&t);
while(t--)
{
scanf("%d %d %d" ,&a ,&b ,&c);
x = 1 ,Ans = 0;
while(1)
{
if(x * a >= c) break;
if((c - x * a) % b == 0) Ans ++;
x ++;
}
printf("%d\n" ,Ans);
}
return 0;
}

37
HDOJ/4994_autoAC.cpp Normal file
View File

@ -0,0 +1,37 @@
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <fstream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <vector>
#include <cmath>
#include <iomanip>
typedef long long LL;
typedef unsigned long long LLU;
const double PI=acos(-1.0);
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n, a[1111], cnt=0;
cin>>n;
for(int i=0; i<n; i++)
cin>>a[i];
for(int i=0; i<n-1 && a[i]==1; i++)
cnt++;
if(cnt&1)
cout<<"No"<<endl;
else cout<<"Yes"<<endl;
}
return 0;
}

66
HDOJ/4995_autoAC.cpp Normal file
View File

@ -0,0 +1,66 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int N = 1e5 + 5;
typedef long long ll;
map<int, int> vis;
int T, n, m, k;
struct Node {
ll x;
double v;
int id;
}node[N];
int cmp (const Node &a, const Node &b) {
return a.x < b.x;
}
double solve () {
double ans = 0;
vis.clear();
sort (node, node + n, cmp);
for (int i = 0; i < n; i++)
vis[node[i].id] = i;
int pos, tmp;
int num;
for (int i = 0; i < m; i++) {
scanf ("%d", &num);
pos = vis[num - 1];
int p1 = pos - 1;
int p2 = pos + 1;
double sum = 0;
tmp = 0;
while (tmp < k) {
if (p1 == -1)
sum += node[p2++].v;
else if (p2 == n)
sum += node[p1--].v;
else {
if (node[pos].x - node[p1].x < node[p2].x - node[pos].x)
sum += node[p1--].v;
else if (node[pos].x - node[p1].x > node[p2].x - node[pos].x)
sum += node[p2++].v;
else if (node[p1].id < node[p2].id)
sum += node[p1--].v;
else
sum += node[p2++].v;
}
tmp++;
}
ans += sum / k;
node[pos].v = sum / k;
}
return ans;
}
int main () {
scanf ("%d", &T);
while (T--) {
scanf ("%d%d%d", &n, &m, &k);
for (int i = 0; i < n; i++) {
scanf ("%I64d%lf", &node[i].x, &node[i].v);
node[i].id = i;
}
printf ("%.6lf\n", solve());
}
return 0;
}

69
HDOJ/4996_autoAC.cpp Normal file
View File

@ -0,0 +1,69 @@
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
#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))
typedef long long LL;
const int maxn = 1<<20;
int n,k;
LL f[20][20],dp[maxn],tmp[maxn];
int cal(int x)
{
int ans = 0;
for(int i = 0;i < 20;++i)
if((1<<i)&x)
ans++;
return ans;
}
void init () {
dp[1] = f[1][1] = 1;
for(int i = 1;i < 18;++i){
for(int j = 0;j < (1<<i);++j)
tmp[j] = dp[j];
for(int j = 0;j < (1<<(i+1));++j)
dp[j] = 0;
for(int j = 0;j < (1<<i);++j)if(tmp[j]){
for(int k = 0;k <= i;++k){
int tot = 0,c[20],st = 0;
for(int l = 0;l < i;++l){
if((1<<l) & j){
c[tot] = l;
if(c[tot] >= k)
c[tot]++;
tot++;
}
}
c[tot++] = k;
for(int l = 0;l < tot;++l){
if(c[l] > k){
c[l] = k;
break;
}
}
for(int l = 0;l < tot;++l){
st |= (1<<c[l]);
}
dp[st] += tmp[j];
}
}
for(int j = 0;j < (1<<(i+1));++j)
f[i+1][cal(j)] += dp[j];
}
}
int main() {
init();
int _;RD(_);while(_--){
RD2(n,k);
printf("%I64d\n",f[n][k]);
}
return 0;
}

93
HDOJ/4997_autoAC.cpp Normal file
View File

@ -0,0 +1,93 @@
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int MAX_N = 10;
const int MAX_S = 1 << MAX_N;
const int Mod = (int)1e9 + 7;
int n, m;
bool edge[MAX_N][MAX_N];
int cntEdge[MAX_S][MAX_S];
long long connected[MAX_S];
long long biconnected[MAX_S];
long long expand[MAX_S][MAX_S];
int main() {
#ifdef LOCAL_JUDGE
freopen("in.txt", "r", stdin);
#endif
int T;
scanf("%d", &T);
for (int cas = 1; cas <= T; cas++) {
scanf("%d %d", &n, &m);
memset(edge, true, sizeof(edge));
for (int i = 1; i <= m; i++) {
int a, b;
scanf("%d %d", &a, &b);
a--;
b--;
edge[a][b] = false;
edge[b][a] = false;
}
for (int i = 0; i < n; i++) {
edge[i][i] = false;
}
int cntState = 1 << n;
for (int i = 0; i < cntState; i++) {
for (int j = 0; j < cntState; j++) {
if (i == j || (i & j) == 0) {
int &ref = cntEdge[i][j];
ref = 0;
for (int x = 0; x < n; x++) {
if ((i >> x) & 1) {
int low = (i == j) ? x + 1 : 0;
for (int y = low; y < n; y++) {
if ((j >> y) & 1) {
ref += edge[x][y];
}
}
}
}
}
}
}
connected[0] = 1;
biconnected[0] = 1;
for (int mask = 1; mask < cntState; mask++) {
int lowbit = mask & -mask;
{
long long &ref = connected[mask];
ref = 1ll << cntEdge[mask][mask];
for (int subset = mask ^ lowbit; subset; subset = (subset - 1) & (mask ^ lowbit)) {
ref -= connected[mask ^ subset] * (1ll << cntEdge[subset][subset]);
}
}
{
for (int target = mask ^ lowbit; target; target = (target - 1) & (mask ^ lowbit)) {
int source = mask ^ target;
int _lowbit = target & -target;
long long &ref = expand[mask][source];
ref = 0;
for (int subset = target ^ _lowbit; ; subset = (subset - 1) & (target ^ _lowbit)) {
int set = target ^ subset;
ref += connected[set] * cntEdge[set][source] * expand[mask ^ set][source];
if (subset == 0) {
break;
}
}
}
expand[mask][mask] = 1;
}
{
long long &ref = biconnected[mask];
ref = connected[mask];
for (int subset = mask ^ lowbit; subset; subset = (subset - 1) & (mask ^ lowbit)) {
ref -= biconnected[mask ^ subset] * expand[mask][mask ^ subset];
}
}
}
int ans = biconnected[cntState - 1] % Mod;
printf("%d\n", ans);
}
return 0;
}

89
HDOJ/4998_autoAC.cpp Normal file
View File

@ -0,0 +1,89 @@
#include<cstdio>
#include<cmath>
using namespace std;
#define PI acos(-1.0)
struct Point {
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {}
};
int n;
Point p[15];
double rad[15];
typedef Point Vector;
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
Vector operator - (Point A, Point B) { return Vector(A.x - B.x, A.y - B.y); }
Vector operator * (Vector A, double p) { return Vector(A.x * p, A.y * p); }
Vector operator / (Vector A, double p) { return Vector(A.x / p, A.y / p); }
bool operator < (const Point& a, const Point& b) {
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
const double eps = 1e-10;
int dcmp(double x) {
if(fabs(x) < eps) return 0;
else return x < 0 ? -1 : 1;
}
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 Rotate(Vector A, double rad) {
return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
}
Vector Normal(Vector A) {
double L = Length(A);
return Vector(-A.y / L, A.x / L);
}
Point GetLineIntersection(Point P, Vector v, Point Q, Vector w) {
Vector u = P - Q;
double t = Cross(w, u) / Cross(v, w);
return P + v * t;
}
Vector Rotate_Point(Vector A) {
for(int i = 0; i < n; i++) {
A = p[i] + Rotate(A - p[i], rad[i]);
}
return A;
}
Vector Get_Mid_Point(Point A, Point B) {
return Vector((A.x + B.x) / 2, (A.y + B.y) / 2);
}
void Get_Ans() {
Point f1[2], f2[2], mid[2], vec[2];
f1[0].x = -1;
f1[0].y = -1;
f1[1].x = -10;
f1[1].y = -50;
for(int i = 0; i < 2; i++) {
f2[i] = Rotate_Point(f1[i]);
mid[i] = Get_Mid_Point(f1[i], f2[i]);
vec[i] = Normal(f1[i] - f2[i]);
}
Point ans = GetLineIntersection(mid[0], vec[0], mid[1], vec[1]);
double ansp = Angle(f1[0] - ans, f2[0] - ans);
if(Cross(f1[0] - ans, f2[0] - ans) < 0)
ansp = 2 * PI - ansp;
if(dcmp(ans.x) == 0) ans.x = 0;
if(dcmp(ans.y) == 0) ans.y = 0;
printf("%.10lf %.10lf %.10lf\n", ans.x, ans.y, ansp);
}
int main()
{
int T;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%lf%lf%lf", &p[i].x, &p[i].y, &rad[i]);
if(dcmp(rad[i] - 2 * PI) == 0 || dcmp(rad[i]) == 0) {
rad[i] = 0;
n--;
i--;
}
}
Get_Ans();
}
return 0;
}

284
HDOJ/4999_autoAC.cpp Normal file
View File

@ -0,0 +1,284 @@
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
using namespace std;
#ifdef _WIN32
#define i64 __int64
#define out64 "%I64d\n"
#define in64 "%I64d"
#else
#define i64 long long
#define out64 "%lld\n"
#define in64 "%lld"
#endif
/************ for topcoder by zz1215 *******************/
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define FOR(i,a,b) for( int i = (a) ; i <= (b) ; i ++)
#define FF(i,a) for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a,b) for( int i = (a) ; i >= (b) ; i --)
#define S64(a) scanf(in64,&a)
#define SS(a) scanf("%d",&a)
#define LL(a) ((a)<<1)
#define RR(a) (((a)<<1)+1)
#define pb push_back
#define pf push_front
#define X first
#define Y second
#define CL(Q) while(!Q.empty())Q.pop()
#define MM(name,what) memset(name,what,sizeof(name))
#define MC(a,b) memcpy(a,b,sizeof(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define read freopen("out.txt","r",stdin)
#define write freopen("out2.txt","w",stdout)
const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 10e-9;
const double pi = acos(-1.0);
const int maxn = 1 << 20;
const i64 mod = 4294967296;
struct zz{
string s;
i64 a;
}zx;
int n;
int c[44][32];
string s[44];
i64 a[44];
i64 dp[maxn];
i64 dpx[maxn];
int cas[44];
int ret[22];
vector<zz>v;
int ns;
int size;
i64 start(){
for (int i = 0; i < (1 << ns); i++){
dp[i] = -1;
}
dp[0] = 0;
size = v.size();
for (int step = 0; step < 32; step++){
for (int state = 0; state < (1 << ns); state++){
dpx[state] = -1;
}
for (int state = 0; state < (1 << ns); state++) if(dp[state] != -1){
i64 tos = 0;
i64 value = 0;
for (int i = 0; i < size; i++){
if (v[i].s == "+="){
if (state & (1 << cas[i])){
if (c[i][step] == 4){
tos |= 1 << cas[i];
}
else if( c[i][step] == 2){
if (value == 1){
tos |= 1 << cas[i];
value = 0;
}
else{
value = 1;
}
}
}
else{
if (c[i][step] == 4){
if (value == 1){
tos |= 1 << cas[i];
value = 0;
}
else {
value = 1;
}
}
}
}
else{
if (c[i][step] == 0){
value = 0;
}
else if (c[i][step] == 1){
value = 1;
}
else if (c[i][step] == 3){
value = 1 - value;
}
}
}
i64 temp = value << step;
temp += dp[state];
if (temp > dpx[tos]){
dpx[tos] = temp;
}
tos = 0;
value = 1;
for (int i = 0; i < size; i++){
if (v[i].s == "+="){
if (state & (1 << cas[i])){
if (c[i][step] == 4){
tos |= 1 << cas[i];
}
else if (c[i][step] == 2){
if (value == 1){
tos |= 1 << cas[i];
value = 0;
}
else{
value = 1;
}
}
}
else{
if (c[i][step] == 4){
if (value == 1){
tos |= 1 << cas[i];
value = 0;
}
else {
value = 1;
}
}
}
}
else{
if (c[i][step] == 0){
value = 0;
}
else if (c[i][step] == 1){
value = 1;
}
else if (c[i][step] == 3){
value = 1 - value;
}
}
}
temp = value << step;
temp += dp[state];
if (temp > dpx[tos]){
dpx[tos] = temp;
}
}
for (int state = 0; state < (1 << ns); state++){
dp[state] = dpx[state];
}
}
i64 ans = -1;
for (int i = 0; i < (1 << ns); i++){
ans = max(ans, dp[i]);
}
return ans;
}
i64 toint(string temp){
i64 re = 0;
for (int i = 0; i < temp.size(); i++){
if (temp[i] <= '9' && temp[i] >= '0'){
re *= (1 << 4);
re += temp[i] - '0';
}
else if (temp[i] >= 'a' && temp[i] <= 'f'){
re *= (1 << 4);
re += temp[i] - 'a' + 10;
}
}
return re;
}
int main(){
int T;
cin >> T;
while (T--){
cin >> n;
string ts;
for (int i = 1; i <= n; i++){
cin >> s[i] >> ts;
a[i] = toint(ts);
if (s[i] == "-="){
s[i] = "+=";
a[i] = -a[i];
a[i] = (a[i] + mod) % mod;
}
}
v.clear();
int temp;
for (int i = 1; i <= n; i++){
zx.s = s[i];
zx.a = a[i];
if (zx.s == "+="){
temp = i + 1;
while (true){
if (temp<=n && s[temp] == "+="){
zx.a += a[temp];
zx.a %= mod;
temp++;
}
else{
i = temp - 1;
break;
}
}
v.push_back(zx);
}
else {
v.push_back(zx);
}
}
ns = 0;
for (int x = 0; x <v.size(); x++){
if (v[x].s == "+="){
cas[x] = ns;
ret[ns++] = x;
for (int i = 0; i < 32; i++){
if (v[x].a &((i64)1 << i)){
c[x][i] = 4;
}
else{
c[x][i] = 2;
}
}
}
else if (v[x].s == "^="){
for (int i = 0; i < 32; i++){
if (v[x].a & ((i64)1 << i)){
c[x][i] = 3;
}
else{
c[x][i] = 2;
}
}
}
else if (v[x].s == "&="){
for (int i = 0; i < 32; i++){
if (v[x].a & ((i64)1 << i)){
c[x][i] = 2;
}
else {
c[x][i] = 0;
}
}
}
else if (v[x].s == "|="){
for (int i = 0; i < 32; i++){
if (v[x].a & ((i64)1 << i)){
c[x][i] = 1;
}
else{
c[x][i] = 2;
}
}
}
}
cout << start() << endl;
}
return 0;
}