Merge pull request #44 from KiritoTRw/master

Powered By HC TECH : AutoACer Engine [END By Now]
This commit is contained in:
Kirigaya Kazuto 2016-09-13 04:57:12 +08:00 committed by GitHub
commit 6b7d3ecc66
258 changed files with 22555 additions and 0 deletions

75
HDOJ/5200_autoAC.cpp Normal file
View File

@ -0,0 +1,75 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define M 500005
struct point{
int i, v;
}p[M];
struct node
{
int v;
int cur;
}s[M];
int has[M], res[M];
bool cmp (point a, point b)
{
return a.v > b.v;
}
int S()
{
int ret=0,ok=0;
char c;
while((c=getchar()))
{
if(c>='0'&&c<='9')
ret=(ret<<3)+ret+ret+c-'0',ok=1;
else if(ok)
return ret;
}
return ret;
}
bool cmp2(node a,node b)
{
return a.v<b.v;
}
int main()
{
int n, d, i, j, ans;
while (~scanf("%d%d",&n,&d))
{
for (i = 0; i < n; i++)
{
p[i].v=S();
p[i].i = i+1;
}
sort(p, p+n, cmp);
for (j = 0; j < d; j++)
{
s[j].v=S();
s[j].cur=j;
}
sort(s,s+d,cmp2);
memset(has, 0, sizeof(has));
ans = 0;
for (i = 0, j = d - 1; j >= 0; j--)
{
for ( ; i < n; i++)
{
if (p[i].v <= s[j].v)
break;
int id = p[i].i;
has[id] = 1;
if (!has[id-1] && !has[id+1]) ++ans;
else if (has[id-1] && has[id+1]) --ans;
}
res[s[j].cur] = ans;
}
for (i = 0; i < d; i++)
{
printf ("%d\n", res[i]);
}
}
return 0;
}

55
HDOJ/5201_autoAC.cpp Normal file
View File

@ -0,0 +1,55 @@
#include<stdio.h>
typedef long long LL;
#define mod 1000000007
LL pow(LL a, LL b) {
LL s = 1;
while(b) {
if(b&1) s = (s * a) % mod;
a = (a*a) % mod;
b >>= 1;
}
return s;
}
LL f[200005],inv[200005];
void init()
{
f[0]=f[1]=inv[0]=inv[1]=1;
for(LL i=2;i<=200000;++i){
f[i]=f[i-1]*i%mod;
inv[i]=pow(f[i],mod-2);
}
}
LL C(int n,int m)
{
if(m>n||m<0) return 0;
return f[n]*inv[m]%mod*inv[n-m]%mod;
}
LL n,m;
LL cal(int x)
{
LL ans=0;
for(LL i=0,s=x;i<m&&s<=n;++i,s+=x)
{
LL t=C(m-1,i)*C(n-s+m-2,n-s)%mod;
if(i&1) ans=((ans-t)%mod+mod)%mod;
else ans=(ans+t)%mod;
}
return ans;
}
int main() {
int t;
scanf("%d",&t);
init();
while(t--)
{
scanf("%lld%lld", &n,&m);
if(m==1) puts("1");
else
{
LL ans=0;
for(LL i=1;i<=n;++i) ans=(ans+cal(i))%mod;
printf("%lld\n",ans);
}
}
return 0;
}

55
HDOJ/5202_autoAC.cpp Normal file
View File

@ -0,0 +1,55 @@
#include <stdio.h>
#include <string.h>
const int N = 1005;
char str[N];
int n, pos[N], num, len, cnt;
bool judge() {
for (int i = 0, j = len - 1; i < len; i++, j--)
if (str[i] != str[j])
return false;
return true;
}
int main() {
while (scanf("%d", &n) == 1) {
scanf("%s", str);
num = 0;
len = strlen(str);
for (int i = 0; i < len; i++) {
if (str[i] == '?') {
str[i] = 'a';
pos[num++] = i;
}
}
if (num == 0 && !judge()) {
printf("%s\n", str);
continue;
}
if (num == 0 && judge() || len == 1) {
printf("QwQ\n");
continue;
}
if (len & 1) {
if (!judge()) {
printf("%s\n", str);
continue;
}
bool flag = judge();
if (flag && pos[num - 1] != len / 2) {
str[pos[num - 1]] = 'b';
printf("%s\n", str);
}
else if (flag && pos[num - 1] == len / 2 && num == 1)
printf("QwQ\n");
else if (flag && pos[num - 1] == len / 2 && num > 1) {
str[pos[num - 2]] = 'b';
printf("%s\n", str);
}
}
else {
if (judge())
str[pos[num - 1]] = 'b';
printf("%s\n", str);
}
}
return 0;
}

49
HDOJ/5203_autoAC.cpp Normal file
View File

@ -0,0 +1,49 @@
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int l,r,x,n,m;
int ok(int a,int b,int c)
{
if(a<b) swap(a,b);
if(a<c) swap(a,c);
if(b+c>a) return true;
else return false;
}
long long calc2(int x,int y)
{
long long tot=0;
for(int i=1;i<x;i++)
if(ok(i,x-i,y)) tot++;
for(int i=1;i<y;i++)
if(ok(i,y-i,x)) tot++;
return tot;
}
long long calc1(int n)
{
long long tot=0;
for(int i=1;i<=n-2;i++)
if(2*i<n)
{
tot+=(n-1)/2-(n/2-i);
}
return tot;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
l=n,r=1;
for(int i=1;i<=m;i++)
{
scanf("%d",&x);
l=min(l,x);
r=max(r,x);
}
if(r==n) printf("%lld\n",calc1(l-1));
else if(l==1) printf("%lld\n",calc1(n-r));
else printf("%lld\n",calc2(l-1,n-r));
}
return 0;
}

122
HDOJ/5204_autoAC.cpp Normal file
View File

@ -0,0 +1,122 @@
#pragma comment(linker, "/STACK:10240000,10240000")
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <ctime>
#include <cctype>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <stdexcept>
#include <utility>
using namespace std;
#define mem0(a) memset(a, 0, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define define_m int m = (l + r) >> 1
#define rep0(a, b) for (int a = 0; a < (b); a++)
#define rep1(a, b) for (int a = 1; a <= (b); a++)
#define all(a) (a).begin(), (a).end()
#define lowbit(x) ((x) & (-(x)))
#define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
#define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
#define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
#define pchr(a) putchar(a)
#define pstr(a) printf("%s", a)
#define sint(a) ReadInt(a)
#define sint2(a, b) ReadInt(a);ReadInt(b)
#define sint3(a, b, c) ReadInt(a);ReadInt(b);ReadInt(c)
#define pint(a) WriteInt(a)
typedef double db;
typedef long long LL;
typedef pair<int, int> pii;
typedef multiset<int> msi;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii;
const int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dy[8] = {1, 0, -1, 0, -1, 1, 1, -1};
const int maxn = 1e3 + 7;
const int maxm = 1e5 + 7;
const int maxv = 1e7 + 7;
const int max_val = 1e6 + 7;
const int MD = 1e9 +7;
const int INF = 1e9 + 7;
const double PI = acos(-1.0);
const double eps = 1e-10;
template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}
template<class T>void ReadInt(T &x){char c=getchar();while(!isdigit(c))c=getchar();x=0;while(isdigit(c)){x=x*10+c-'0';c=getchar();}}
template<class T>void WriteInt(T i) {int p=0;static int b[20];if(i == 0) b[p++] = 0;else while(i){b[p++]=i%10;i/=10;}for(int j=p-1;j>=0;j--)pchr('0'+b[j]);}
struct abc {
pii a[100007];
int l, r;
void Init() { l = r = 0; }
void push_back(int x) {
a[r++] = make_pair(x, 0);
for(int i = l; i < r - 1; i++) a[i].second++;
if (r - l >= 62) {
int pos;
for (int i = l; i < r; i++) {
if (a[i].second == 61) {
pos = i;
break;
}
}
for (int i = pos; i > l; i--) a[i] = a[i - 1];
l++;
}
int p = r - 1;
while (p > l && a[p].first < a[p - 1].first) {
swap(a[p], a[p - 1]);
p--;
}
}
pii &operator [] (int i) {
return a[l + i];
}
int size() {
return r - l;
}
};
abc g;
LL calc(LL x, LL pos) {
if (x < pos) return 0;
return (x - pos) / pos / 2 + 1;
}
int main() {
int n;
while (cin >> n) {
g.Init();
rep0(i, n) {
int id, w;
sint(id);
if (id == 1) {
sint(w);
g.push_back(w);
}
else {
LL L, R, k;
sint3(L, R, k);
int sz = g.size();
rep0(i, sz) {
LL pos = 1LL << g[i].second, c = calc(R, pos) - calc(L - 1, pos);
if (k <= c) {
pint(g[i].first);
pchr('\n');
break;
}
k -= c;
}
}
}
}
return 0;
}

56
HDOJ/5206_autoAC.cpp Normal file
View File

@ -0,0 +1,56 @@
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#define inf 0x7fffffff
using namespace std;
typedef long long LL;
const LL maxn=10000+10;
LL n,m;
struct Point
{
LL x,y,z;
Point (LL x=0,LL y=0,LL z=0):x(x),y(y),z(z){}
}a,b,c,d;
typedef Point Vector;
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)); }
LL dis(Point A,Point B)
{
LL xx=(A.x-B.x)*(A.x-B.x);
LL yy=(A.y-B.y)*(A.y-B.y);
LL zz=(A.z-B.z)*(A.z-B.z);
return xx+yy+zz;
}
int main()
{
int t,ncase=1;
scanf("%d",&t);
while (t--)
{
scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d",
&a.x,&a.y,&a.z,&b.x,&b.y,&b.z,&c.x,&c.y,&c.z,&d.x,&d.y,&d.z);
int flag=0;
LL len,len2,len3;
len=dis(a,b) ;len2=dis(a,c) ;len3=dis(a,d) ;
if ((len==len2&&len3==len+len2)||(len==len3&&len2==len+len3)||(len2==len3&&len==len2+len3))
flag++;
len=dis(b,a) ;len2=dis(b,c) ;len3=dis(b,d) ;
if ((len==len2&&len3==len+len2)||(len==len3&&len2==len+len3)||(len2==len3&&len==len2+len3))
flag++;
len=dis(c,a) ;len2=dis(c,b) ;len3=dis(c,d) ;
if ((len==len2&&len3==len+len2)||(len==len3&&len2==len+len3)||(len2==len3&&len==len2+len3))
flag++;
len=dis(d,a) ;len2=dis(d,b) ;len3=dis(d,c) ;
if ((len==len2&&len3==len+len2)||(len==len3&&len2==len+len3)||(len2==len3&&len==len2+len3))
flag++;
if (flag==4) printf("Case #%d: Yes\n",ncase++);
else printf("Case #%d: No\n",ncase++);
}
return 0;
}

34
HDOJ/5207_autoAC.cpp Normal file
View File

@ -0,0 +1,34 @@
#include<iostream>
#include<cstring>
#include<algorithm>
#define maxn 100000+5
using namespace std;
int mapp[maxn],head[maxn];
int n;
int main()
{
cin.sync_with_stdio(false);
int t,casee=1;
cin>>t;
while(t--)
{
cin>>n;
memset(mapp,0,sizeof(mapp));
memset(head,0,sizeof(head));
int maxx=-1;
for(int i=0;i<n;i++){int x;cin>>x;mapp[x]++;maxx=max(maxx,x);}
for(int i=2;i<=maxx;i++)
{
for(int j=i;j<=maxx;j+=i)
{
if(mapp[j]) head[i]+=mapp[j];
}
}
cout<<"Case #"<<casee++<<": ";
for(int i=maxx;i>=1;i--)
{
if(head[i]>=2) {cout<<i<<endl;break;}
}
}
return 0;
}

61
HDOJ/5208_autoAC.cpp Normal file
View File

@ -0,0 +1,61 @@
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
int _,ans;
int state(int i,int l,int r)
{
if((1 << i) <= l)return 1;
else if((1 << i) > r)return 0;
else return -1;
}
void deal(int & l,int & r,int v,int i)
{
l = max(l - v,0);
r = min(max(r - v,0),(1 << i) - 1);
}
void dfs(int l1,int r1,int l2,int r2,int i,int cur)
{
if(l2 <= l1 && r1 <= r2 || i == -1)return;
if(cur + ((LL)1 << (i + 1)) - 1 < ans)return;
int st1 = state(i,l1,r1),st2 = state(i,l2,r2);
if(st1 >= 0 && st2 >= 0)
{
cur |= ((st1 ^ st2) << i);
ans = max(cur,ans);
deal(l1,r1,st1 << i,i) , deal(l2,r2,st2 << i,i);
dfs(l1,r1,l2,r2,i - 1,cur);
}
else if(st1 == -1 && st2 == -1)
{
dfs(l1,min(r1,(1 << i) - 1),l2,min(r2,(1 << i) - 1),i - 1,cur);
deal(l1,r1,1 << i,i) , deal(l2,r2,1 << i,i);
dfs(l1,r1,l2,r2,i - 1,cur);
}
else if(st1 >= 0 && st2 == -1)
{
deal(l1,r1,st1 << i,i) , deal(l2,r2,st1 << i,i);
dfs(l1,r1,l2,r2,i - 1,cur);
}
else// if(st1 == -1 && st2 >= 0)
{
cur |= (1 << i);
ans = max(cur,ans);
deal(l1,r1,!st2 << i,i) , deal(l2,r2,st2 << i,i);
dfs(l1,r1,l2,r2,i - 1,cur);
}
}
int main()
{
scanf("%d",&_);
for(int kcas = 1;kcas <= _;kcas++)
{
int l1,l2,r1,r2;
scanf("%d%d%d%d",&l1,&r1,&l2,&r2);
ans = 0;
dfs(l1,r1,l2,r2,30,0);
printf("Case #%d: %d\n",kcas,ans);
}
}

61
HDOJ/5209_autoAC.cpp Normal file
View File

@ -0,0 +1,61 @@
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
const int mod=1000000007;
const int maxn=110005;
typedef long long LL;
LL jie[maxn],jie2[maxn];
int mm(int n,int m){
LL s=1,k=n;
while(m>0){
if(m&1){
s=s*k;
s%=mod;
}
k=k*k;
k%=mod;
m>>=1;
}
return s;
}
int zuhe(int n,int m){
LL s;
s=jie[n];
s=s*jie2[m];
s%=mod;
s=s*jie2[n-m];
s%=mod;
return s;
}
void init(void){
int i;
jie[0]=1;
jie2[0]=1;
for(i=1;i<=110000;i++){
jie[i]=jie[i-1]*i;
jie[i]%=mod;
jie2[i]=mm(jie[i],mod-2);
}
}
LL dp[1005];
int main()
{
int i,j,n,m,t,N=0;
cin>>t;
init();
while(t--){
cin>>n>>m;
memset(dp,0,sizeof(dp));
dp[0]=1;
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
dp[i]+=zuhe(i-1,j-1)*((dp[i-j]*zuhe(j+m-1,m-1))%mod);
dp[i]%=mod;
}
}
printf("Case #%d: %d\n",++N,dp[n]);
}
}

27
HDOJ/5210_autoAC.cpp Normal file
View File

@ -0,0 +1,27 @@
#include<iostream>
#include<cstring>
#define maxn 100+5
using namespace std;
int mapp[maxn];
int main()
{
int n;
while(cin>>n)
{
int sum=0;
memset(mapp,0,sizeof(mapp));
for(int i=0;i<n;i++)
{
int x;
cin>>x;
if(!mapp[x]) sum++;
mapp[x]++;
}
int k;
cin>>k;
k-=(n-sum);
if(k<=0) cout<<sum<<endl;
else cout<<sum-k<<endl;
}
return 0;
}

27
HDOJ/5211_autoAC.cpp Normal file
View File

@ -0,0 +1,27 @@
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define MAXN 10005
int num[MAXN],n,f[MAXN];
int main()
{
while(scanf("%d",&n) != EOF)
{
int ans = 0;
memset(f,0,sizeof(f));
for(int i = 0;i < n;i++)
scanf("%d",&num[i]);
for(int i = n - 1;i >= 0;i--)
{
ans += f[num[i]];
int q = sqrt(num[i] + 1) + 1;
for(int j = 1;j <= q && j <= num[i];j++)
if(num[i] % j == 0)
f[j] = i + 1,
f[num[i] / j] = i + 1;
}
printf("%d\n",ans);
}
}

39
HDOJ/5212_autoAC.cpp Normal file
View File

@ -0,0 +1,39 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef long long LL;
int dp[11111];
int num[11111];
int sum[11111];
int n;
int main()
{
while(scanf("%d", &n) != EOF)
{
int maxnum = 0, ans = 0;
memset(sum, 0, sizeof(sum));
for(int i = 0; i < n; i ++)
{
scanf("%d", &num[i]);
sum[num[i]] ++;
maxnum = maxnum < num[i] ? num[i] : maxnum;
}
memset(dp, 0, sizeof(dp));
for(int i = 2; i <= maxnum; i ++)
{
for(int j = i; j <= maxnum; j += i)
{
dp[i] += sum[j];
}
dp[i] *= dp[i];
}
for(int i = maxnum; i >= 2; i --)
{
for(int j = i + i; j <= maxnum; j += i)
dp[i] -= dp[j];
ans = (ans + (LL)i * (i - 1) * dp[i]) % 10007;
}
printf("%d\n", ans);
}
return 0;
}

83
HDOJ/5213_autoAC.cpp Normal file
View File

@ -0,0 +1,83 @@
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<map>
#include<vector>
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
#define N 30005
int a[N];
int n,k,m;
int len,tot;
int l1,l2,r1,r2;
int ans[N],c[N*2];
struct node{
int l,r,id,p;
void set(int i,int ll,int rr,int pp){
id=i;l=ll;r=rr;p=pp;
}
}f[N*4];
bool cmp(node a,node b){
if(a.l/len!=b.l/len) return a.l/len<b.l/len;
return a.r<b.r;
}
void solve(){
int nl=1;
int nr=0;
int res=0;
for(int i=0;i<tot;i++)
{
while(nl>f[i].l){
nl--;
if(k>a[nl]) res+=c[k-a[nl]];
c[a[nl]]++;
}
while(nr<f[i].r){
nr++;
if(k>a[nr]) res+=c[k-a[nr]];
c[a[nr]]++;
}
while(nl<f[i].l){
if(k>a[nl]) res-=c[k-a[nl]];
c[a[nl]]--;
nl++;
}
while(nr>f[i].r){
if(k>a[nr]) res-=c[k-a[nr]];
c[a[nr]]--;
nr--;
}
ans[f[i].id]+=res*f[i].p;
}
}
int main()
{
while(~scanf("%d",&n)){
scanf("%d",&k);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
memset(c,0,sizeof(c));
scanf("%d",&m);
tot=0;
for(int i=1;i<=m;i++)
{
ans[i]=0;
scanf("%d%d%d%d",&l1,&r1,&l2,&r2);
f[tot++].set(i,l1,r2,1);
f[tot++].set(i,l1,l2-1,-1);
f[tot++].set(i,r1+1,r2,-1);
if(r1+1<=l2-1) f[tot++].set(i,r1+1,l2-1,1);
}
len=sqrt(n);
sort(f,f+tot,cmp);
solve();
for(int i=1;i<=m;i++)
printf("%d\n",ans[i]);
}
return 0;
}

45
HDOJ/5214_autoAC.cpp Normal file
View File

@ -0,0 +1,45 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
const int INF = (1<<30)-1;
const int mod=4294967296;
const int maxn=10000005;
unsigned int l[maxn],r[maxn];
int main(){
int T;
scanf("%d",&T);
while(T--){
memset(l,0,sizeof(l));
memset(r,0,sizeof(r));
unsigned int n,a,b,c,d;
cin>>n>>l[1]>>r[1]>>a>>b>>c>>d;
for(int i=2;i<=n;i++) l[i]=l[i-1]*a+b;
for(int i=2;i<=n;i++) r[i]=r[i-1]*c+d;
for(int i=1;i<=n;i++)
if(l[i]>r[i]) swap(l[i],r[i]);
int minn=r[1],maxx=l[1];
for(int i=1;i<=n;i++){
if(l[i]>maxx) maxx=l[i];
if(r[i]<minn) minn=r[i];
}
int flag=0;
for(int i=1;i<=n;i++){
if(l[i]>minn&&r[i]<maxx){
flag=1;
break;
}
}
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}

86
HDOJ/5215_autoAC.cpp Normal file
View File

@ -0,0 +1,86 @@
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=200000+100;
struct Edge
{
int v, nxt;
Edge(int v=0, int nxt=0):v(v), nxt(nxt) {}
} edge[600010];
bool even, odd;
int n, m, head[maxn], color[maxn], tot, cnt, pre[maxn], bel[maxn];
void init()
{
tot=even=odd=0;
cnt=0;
memset(color, 0, sizeof color);
memset(head, -1, sizeof head);
memset(bel, -1, sizeof bel);
}
void AddEdge(int u, int v)
{
edge[tot]=Edge(v, head[u]);
head[u]=tot++;
}
void dfs(int u, int fa)
{
for(int i=head[u]; ~i; i=edge[i].nxt)
{
if((i^1)==fa)continue ;
int v=edge[i].v;
if(color[v]==color[u])
{
odd=1;
++cnt;
int x=v;
while(!even)
{
if(~bel[x])
{
even=true;
break;
}
bel[x]=cnt;
x=pre[x];
if(x==u || x==-1)break;
}
}
if(color[v]==3-color[u])
even=1;
if(!color[v])
{
color[v]=3-color[u];
pre[v]=u;
dfs(v, i);
}
}
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &m);
init();
while(m--)
{
int u, v;
scanf("%d%d", &u, &v);
AddEdge(u, v);
AddEdge(v, u);
}
for(int i=1; i<=n; i++)
if(!color[i])
{
color[i]=1;
pre[i]=-1;
dfs(i, -1);
}
puts(odd?"YES":"NO");
puts(even?"YES":"NO");
}
return 0;
}

40
HDOJ/5216_autoAC.cpp Normal file
View File

@ -0,0 +1,40 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100010;
int N,M;
int A[maxn],B[maxn];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&M);
for(int i=1;i<=M;i++)scanf("%d",&A[i]);
for(int i=1;i<=M;i++)scanf("%d",&B[i]);
sort(A+1,A+1+M);
sort(B+1,B+1+M);
bool flag=true;
for(int i=1;i<=M;i++)
if(A[i]>B[i]){flag=false;break;}
if(!flag){printf("Stupid BrotherK!\n");}
else
{
int ans=max(A[1]-1,N-B[M]);
int last=0;
for(int i=1;i<=M;i++)
{
if(A[i]>last)
{
ans=max(ans,A[i]-last-1);
}
last=B[i];
}
printf("%.6lf\n",double(ans));
}
}
return 0;
}

178
HDOJ/5217_autoAC.cpp Normal file
View File

@ -0,0 +1,178 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 200010
char str[N];
struct Node
{
Node(int l=0,int r=0){lnum=l;rnum=r;}
int lnum,rnum;
}node[N<<2];
void pushup(int rt)
{
int matched=min(node[rt<<1].lnum,node[rt<<1|1].rnum);
node[rt].lnum=node[rt<<1].lnum+node[rt<<1|1].lnum-matched;
node[rt].rnum=node[rt<<1].rnum+node[rt<<1|1].rnum-matched;
}
void build(int l,int r,int rt)
{
if(l==r)
{
node[rt].lnum=node[rt].rnum=0;
if(str[l]=='(')node[rt].lnum=1;
else node[rt].rnum=1;
return ;
}
int mid=(l+r)/2;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
pushup(rt);
}
Node query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
return node[rt];
int mid=(l+r)/2;
Node ret;
if(L<=mid)ret=query(L,R,l,mid,rt<<1);
if(R>mid)
{
Node tmp=query(L,R,mid+1,r,rt<<1|1);
int matched=min(ret.lnum,tmp.rnum);
ret.lnum+=tmp.lnum-matched;
ret.rnum+=tmp.rnum-matched;
}
return ret;
}
void change(int x,int l,int r,int rt)
{
if(l==r&&r==x)
{
swap(node[rt].lnum,node[rt].rnum);
return;
}
int mid=(l+r)/2;
if(x<=mid)change(x,l,mid,rt<<1);
else change(x,mid+1,r,rt<<1|1);
pushup(rt);
return;
}
int lsearch(int L,int R,int K,Node &Val,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
int mid=(l+r)/2;
if(l==r)
{
int matched=min(Val.lnum,node[rt].rnum);
int lnum=Val.lnum+node[rt].lnum-matched;
int rnum=Val.rnum+node[rt].rnum-matched;
if(rnum==K)return l;
else
{
Val.lnum=lnum;
Val.rnum=rnum;
return 0;
}
}
int matched=min(Val.lnum,node[rt<<1].rnum);
int lnum=Val.lnum+node[rt<<1].lnum-matched;
int rnum=Val.rnum+node[rt<<1].rnum-matched;
if(K<=rnum)
{
return lsearch(L,R,K,Val,l,mid,rt<<1);
}
else
{
Val.lnum=lnum;
Val.rnum=rnum;
return lsearch(L,R,K,Val,mid+1,r,rt<<1|1);
}
}
int mid=(l+r)/2;
if(R<=mid)return lsearch(L,R,K,Val,l,mid,rt<<1);
if(L>mid)return lsearch(L,R,K,Val,mid+1,r,rt<<1|1);
int ret=lsearch(L,R,K,Val,l,mid,rt<<1);
if(ret)return ret;
else return lsearch(L,R,K,Val,mid+1,r,rt<<1|1);
}
int rsearch(int L,int R,int K,Node &Val,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
int mid=(l+r)/2;
if(l==r)
{
int matched=min(Val.rnum,node[rt].lnum);
int lnum=Val.lnum+node[rt].lnum-matched;
int rnum=Val.rnum+node[rt].rnum-matched;
if(lnum==K)return l;
else
{
Val.lnum=lnum;
Val.rnum=rnum;
return 0;
}
}
int matched=min(Val.rnum,node[rt<<1|1].lnum);
int lnum=Val.lnum+node[rt<<1|1].lnum-matched;
int rnum=Val.rnum+node[rt<<1|1].rnum-matched;
if(K<=lnum)
{
return rsearch(L,R,K,Val,mid+1,r,rt<<1|1);
}
else
{
Val.lnum=lnum;
Val.rnum=rnum;
return rsearch(L,R,K,Val,l,mid,rt<<1);
}
}
int mid=(l+r)/2;
if(R<=mid)return rsearch(L,R,K,Val,l,mid,rt<<1);
if(L>mid)return rsearch(L,R,K,Val,mid+1,r,rt<<1|1);
int ret=rsearch(L,R,K,Val,mid+1,r,rt<<1|1);
if(ret)return ret;
else return rsearch(L,R,K,Val,l,mid,rt<<1);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,q;
scanf("%d%d",&n,&q);
scanf(" %s",&str[1]);
int len=strlen(&str[1]);
build(1,len,1);
for(int i=0;i<q;i++)
{
int type;
scanf("%d",&type);
if(type==1)
{
int x;
scanf("%d",&x);
change(x,1,len,1);
}
else
{
int a,b,k;
scanf("%d%d%d",&a,&b,&k);
Node tp(0,0),qry=query(a,b,1,len,1);
int cnt=qry.lnum+qry.rnum;
if(cnt<k){printf("-1\n");continue;}
if(k<=qry.rnum)
{
printf("%d\n",lsearch(a,b,k,tp,1,len,1));
}
else
{
printf("%d\n",rsearch(a,b,cnt-k+1,tp,1,len,1));
}
}
}
}
}

45
HDOJ/5218_autoAC.cpp Normal file
View File

@ -0,0 +1,45 @@
#include <iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
int arr[300];
int n,m;
int dp[300][300];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)scanf("%d",&arr[i]);
memset(dp,0,sizeof(dp));
dp[1][0]=1;
for(int i=2;i<=n;i++)
{
for(int j=0;j<i;j++)
{
for(int k=0;k<m;k++)
{
if((arr[k]-1)%i==j)continue;
if((arr[k]-1)%i<j)dp[i][j]=dp[i-1][j-(arr[k]-1)%i-1];
if((arr[k]-1)%i>j)dp[i][j]=dp[i-1][i-1-((arr[k]-1)%i-j)];
if(dp[i][j])break;
}
}
}
int ok=1,ans=0;
for(int i=0;i<n;i++)if(dp[n][i])ans++;
printf("%d\n",ans);
for(int i=0;i<n;i++)
if(dp[n][i])
{
if(ok)ok=0;
else printf(" ");
printf("%d",i+1);
}
printf("\n");
}
return 0;
}

183
HDOJ/5219_autoAC.cpp Normal file
View File

@ -0,0 +1,183 @@
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;
#define ws Ws
#define rank rrrank
const int maxn = 100200 + 1;
int prime[maxn], vis[maxn], mu[maxn];
int tot;
void Mobius(){
mu[1] = 1;
for( int i = 2 ; i < maxn ; i ++ ){
if( !vis[i] )
prime[++tot] = i, mu[i] = -1;
for( int j = 1 ; j <= tot && i * prime[j] < maxn ; j ++ ){
vis[i*prime[j]] = 1;
if( i % prime[j] == 0 ){
mu[i*prime[j]] = 0;
break;
}
else
mu[i*prime[j]] = - mu[i];
}
}
return;
}
int wa[maxn], wb[maxn], wv[maxn], ws[maxn];
int cmp(int *r, int a, int b, int l)
{
return r[a] == r[b] && r[a + l] == r[b + l];
}
void da(int *r, int *sa, int n, int m)
{
int *x = wa, *y = wb, *t, i, j, p;
for(i = 0; i < m; i++) ws[i] = 0;
for(i = 0; i < n; i++) ws[x[i] = r[i]]++;
for(i = 1; i < m; i++) ws[i] += ws[i - 1];
for(i = n - 1; i >= 0; i--) sa[--ws[x[i]]] = i;
for(j = 1, p = 1; p < n; j <<= 1, m = p)
{
for(p = 0, i = n - j; i < n; i++) y[p++] = i;
for(i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i] - j;
for(i = 0; i < n; i++) wv[i] = x[y[i]];
for(i = 0; i < m; i++) ws[i] = 0;
for(i = 0; i < n; i++) ws[wv[i]]++;
for(i = 1; i < m; i++) ws[i] += ws[i - 1];
for(i = n - 1; i >= 0; i--) sa[--ws[wv[i]]] = y[i];
for(t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i++)
x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++;
}
return;
}
int rank[maxn], height[maxn];
void calheight(int *r, int *sa, int n)
{
int i, j, k = 0;
for(i = 1; i <= n; i++) rank[sa[i]] = i;
for(i = 0; i < n; height[rank[i++]] = k)
for(k ? k-- : 0, j = sa[rank[i] - 1]; r[i + k] == r[j + k]; k++);
return;
}
int rank2[maxn], height2[maxn];
void calheight2(int *r, int *sa, int n)
{
int i, j, k = 0;
for(i = 1; i <= n; i++) rank2[sa[i]] = i;
for(i = 0; i < n; height2[rank2[i++]] = k)
for(k ? k-- : 0, j = sa[rank2[i] - 1]; r[i + k] == r[j + k]; k++);
return;
}
int dp[maxn][20];
void initRMQ(int n)
{
for(int i = 1; i <= n; i++) dp[i][0] = height[i];
for(int j = 1; (1 << j) <= n; j++)
for(int i = 1; i + (1 << j) - 1 <= n; i++)
dp[i][j] = min(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
return;
}
int dp2[maxn][20];
void initRMQ2(int n)
{
for(int i = 1; i <= n; i++) dp2[i][0] = height2[i];
for(int j = 1; (1 << j) <= n; j++)
for(int i = 1; i + (1 << j) - 1 <= n; i++)
dp2[i][j] = min(dp2[i][j - 1], dp2[i + (1 << (j - 1))][j - 1]);
return;
}
int askRMQ(int a, int b)
{
int ra = rank[a], rb = rank[b];
if(ra > rb) swap(ra, rb);
int k = 0;
while((1 << (k + 1)) <= rb - ra) k++;
return min(dp[ra + 1][k], dp[rb - (1 << k) + 1][k]);
}
int len;
int askRMQ2(int a, int b)
{
a = len - a - 1;
b = len - b - 1;
int ra = rank2[a], rb = rank2[b];
if(ra > rb) swap(ra, rb);
int k = 0;
while((1 << (k + 1)) <= rb - ra) k++;
return min(dp2[ra + 1][k], dp2[rb - (1 << k) + 1][k]);
}
char in[maxn];
int s[maxn], sa[maxn];
int main()
{
Mobius();
int T;
scanf("%d", &T);
while(T--)
{
scanf("%s", in);
len = strlen(in);
for(int i = 0; i < len; i++)
s[i] = in[i] - 'a' + 1;
s[len] = 0;
da(s, sa, len + 1, 28);
calheight(s, sa, len);
initRMQ(len);
for(int i = 0; i < len; i++)
s[i] = in[len - 1 - i] - 'a' + 1;
s[len] = 0;
da(s, sa, len + 1, 28);
calheight2(s, sa, len);
initRMQ2(len);
lint ans = 0;
for(int L = 1; L <= (len >> 1); L++)
{
int n = len;
int blocks = n / L + (n % L != 0);
int now = 1;
while(now < blocks)
{
if(now + 1 < blocks)
{
int len2 = askRMQ2((now + 1)*L - 1, now*L - 1);
int len1 = askRMQ(now*L, (now + 1)*L);
int totlen = L + len2 + len1;
int cnt = totlen / L;
for(int i = 2; i <= cnt; i++)
{
ans += mu[i]*(totlen - L*i + 1);
}
now = now + (len1 / L) + 1;
}
else
{
if(n % L != 0) break;
int totlen = (len - now*L) + askRMQ2(len - 1, now*L - 1);
int cnt = totlen / L;
for(int i = 2; i <= cnt; i++)
ans += mu[i]*(totlen - L*i + 1);
now = blocks;
}
}
}
printf("%I64d\n", (lint)len*(len + 1LL) / 2LL + ans);
}
return 0;
}

66
HDOJ/5220_autoAC.cpp Normal file
View File

@ -0,0 +1,66 @@
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
using namespace std;
typedef long long LL;
#pragma comment(linker, "/STACK:102400000,102400000")
#define pb push_back
#define mp make_pair
#define F first
#define S second
const int N = 33;
int f[N], c[N], m[N], d[N], w[N];
vector<int> a[N], b[N];
typedef pair<int, int> pii;
map<pii, LL> g[2];
map<pii, LL>:: iterator it;
int main() {
int n, T;
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
for (int i = 1; i <= 32; ++i) a[i].clear(), b[i].clear();
memset(w, 0, sizeof(w));
memset(d, 0, sizeof(d));
g[0].clear(), g[1].clear();
for (int i = 0; i < n; ++i) {
scanf("%d%d%d", &f[i], &c[i], &m[i]);
a[f[i]].pb(i), b[c[i]].pb(i);
}
bool ok = 1;
int mask = 0, cnt = 0;
for (int i = 1; i <= 32; ++i) {
if (b[i].size() == 1) {
if (d[f[b[i][0]]]) ok = 0;
mask ^= m[b[i][0]];
d[f[b[i][0]]] = 1;
}
else if (b[i].size() > 1) w[i] = cnt++;
}
if (!ok) {
puts("0");
continue;
}
int now = 0;
g[now].insert(mp(mp(0, 0), 1));
for (int i = 1; i <= 32; ++i) {
if (!a[i].size() || d[i]) continue;
g[now ^ 1] = g[now];
for (it = g[now].begin(); it != g[now].end(); it++) {
pii st = it -> F;
LL d = it -> S;
for (int j = 0; j < a[i].size(); ++j) {
int t = a[i][j];
pii tmp = mp(st.F ^ m[t], st.S | (1 << w[c[t]]));
g[now ^ 1][tmp] += d;
}
}
now ^= 1;
}
printf("%I64d\n", g[now][mp(mask, (1 << cnt) - 1)]);
}
return 0;
}

222
HDOJ/5221_autoAC.cpp Normal file
View File

@ -0,0 +1,222 @@
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
#include <vector>
#define L(x) (x*2)
#define R(x) (x*2 + 1)
using namespace std;
const int INF = 0x3f3f3f3f ;
const int MX = 100000 + 5 ;
int n ,m ,num ,idx ,sum ;
int dep[MX] ,top[MX] ,V[MX] ,ti[MX] ,to[MX] ,head[MX] ,father[MX] ,siz[MX] ,son[MX] ;
struct Edge
{
int v ,next ;
}E[MX*2] ;
void add_edge(int u ,int v)
{
E[num].v = v ; E[num].next = head[u] ; head[u] = num++ ;
E[num].v = u ; E[num].next = head[v] ; head[v] = num++ ;
}
void dfs_find(int u ,int fa)
{
dep[u] = dep[fa] + 1 ;
father[u] = fa ;
siz[u] = 1 ;
son[u] = 0 ;
for(int i = head[u] ;i != -1 ; i = E[i].next)
{
int v = E[i].v ;
if(v == fa) continue ;
dfs_find(v ,u) ;
siz[u] += siz[v] ;
if(siz[son[u]] < siz[v]) son[u] = v ;
}
}
void dfs_time(int u ,int fa)
{
ti[u] = idx++ ;
top[u] = fa ;
if(son[u]) dfs_time(son[u] ,top[u]) ;
for(int i = head[u] ;i != -1 ;i = E[i].next)
{
int v = E[i].v ;
if(v == father[u] || v == son[u]) continue ;
dfs_time(v ,v) ;
}
to[u] = idx-1 ;
}
void init()
{
sum = 0 ;
num = 0 ;
memset(head ,-1 ,sizeof(head)) ;
}
struct node
{
int le ,rt ,sum ,flag ;
}T[MX*4] ;
void build(int x ,int le ,int rt)
{
T[x].sum = 0 ;
T[x].flag = true ;
T[x].le = le ; T[x].rt = rt ;
if(le == rt)
return ;
int Mid = (le + rt)>>1 ;
build(L(x) ,le ,Mid) ;
build(R(x),Mid+1 ,rt) ;
}
void push_up(int x)
{
T[x].sum = T[L(x)].sum + T[R(x)].sum ;
T[x].flag = true ;
}
void push_down(int x)
{
if(!T[x].flag)
{
T[L(x)].sum = T[R(x)].sum = 0 ;
T[L(x)].flag = T[R(x)].flag = false ;
T[x].sum = 0 ;
}
}
bool Findpos(int x ,int pos)
{
if(T[x].le == T[x].rt && T[x].le == pos)
{
if(!T[x].flag) T[x].sum = 0 ;
return T[x].flag ;
}
push_down(x) ;
int Mid = (T[x].le + T[x].rt)>>1 ;
if(pos <= Mid) return Findpos(L(x) ,pos) ;
else return Findpos(R(x) ,pos) ;
push_up(x) ;
}
void insert(int x ,int pos ,int w)
{
if(T[x].le == T[x].rt && T[x].le == pos)
{
T[x].sum = w ;
T[x].flag = true ;
return ;
}
push_down(x) ;
int Mid = (T[x].le + T[x].rt)>>1 ;
if(pos <= Mid) insert(L(x) ,pos ,w) ;
else insert(R(x) ,pos ,w) ;
push_up(x) ;
}
void update(int x ,int Le ,int Rt)
{
if(T[x].le == Le && T[x].rt == Rt)
{
T[x].flag = false ;
T[x].sum = 0 ;
return ;
}
push_down(x) ;
int Mid = (T[x].le + T[x].rt)>>1 ;
if(Le > Mid) update(R(x) ,Le ,Rt) ;
else if(Rt <= Mid) update(L(x) ,Le ,Rt) ;
else
{
update(L(x) ,Le ,Mid) ;
update(R(x) ,Mid+1 ,Rt) ;
}
push_up(x) ;
}
int Query(int x ,int Le ,int Rt)
{
if(T[x].le == Le && T[x].rt == Rt)
{
if(!T[x].flag) T[x].sum = 0 ;
int temp = T[x].sum ;
T[x].sum = 0 ;
T[x].flag = false ;
return temp ;
}
push_down(x) ;
int Mid = (T[x].le + T[x].rt)>>1 ;
if(Le > Mid) return Query(R(x) ,Le ,Rt) ;
else if(Rt <= Mid) return Query(L(x) ,Le ,Rt) ;
else
return Query(L(x) ,Le ,Mid) + Query(R(x) ,Mid+1 ,Rt) ;
}
void LCA(int u ,int v)
{
int temp ;
while(top[u] != top[v])
{
if(dep[top[u]] < dep[top[v]])
swap(u ,v) ;
temp = Query(1 ,ti[top[u]] ,ti[u]) ;
update(1 ,ti[top[u]] ,ti[u]) ;
sum += temp ;
u = father[top[u]] ;
}
if(dep[u] > dep[v])
swap(u ,v) ;
temp = Query(1 ,ti[u] ,ti[v]) ;
update(1 ,ti[u] ,ti[v]) ;
sum += temp ;
}
int main()
{
int Tx ,u ,v ;
scanf("%d" ,&Tx) ;
while(Tx--)
{
init() ;
scanf("%d" ,&n) ;
for(int i = 1 ;i <= n ; ++i)
scanf("%d" ,&V[i]) ;
for(int i = 1 ;i < n ; ++i)
{
scanf("%d%d" ,&u ,&v) ;
add_edge(u ,v) ;
}
dep[1] = siz[0] = 0 ;
dfs_find(1 ,1) ;
idx = 1 ;
dfs_time(1 ,1) ;
build(1 ,1 ,n) ;
for(int i = 1 ;i <= n ; ++i)
insert(1 ,ti[i] ,V[i]) ;
scanf("%d" ,&m) ;
int type ;
for(int i = 0 ;i < m ; ++i)
{
scanf("%d%d" ,&type ,&u) ;
if(type == 1)
{
scanf("%d" ,&v) ;
LCA(u ,v) ;
}
else if(type == 2)
{
bool flag = Findpos(1 ,ti[u]) ;
if(!flag)
{
insert(1 ,ti[u] ,V[u]) ;
sum -= V[u] ;
}
}
else if(type == 3)
{
sum += Query(1 ,ti[u] ,to[u]) ;
update(1 ,ti[u] ,to[u]) ;
}
cout<<sum<<endl ;
}
}
return 0 ;
}

74
HDOJ/5222_autoAC.cpp Normal file
View File

@ -0,0 +1,74 @@
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
const int maxn = 1000005;
int N,M1,M2,cnt;
int fa[maxn],vis[maxn];
vector<int>G[maxn];
int find_father(int u){
return u == fa[u] ? u : fa[u] = find_father(fa[u]);
}
void init(){
for(int i = 1; i <= N; i++) fa[i] = i;
for(int i = 1; i <= N; i++) G[i].clear();
}
bool dfs(int v){
vis[v] = -1;
for(int i = 0; i < G[v].size(); i++){
int e = G[v][i];
if(vis[e] < 0) return false;
if(!vis[e] && !dfs(e)) return false;
}
vis[v] = 1;
return true;
}
int solve(){
memset(vis,0,sizeof(vis));
for(int i = 1; i <= N; i++){
int e = find_father(i);
if(!vis[e]){
if(!dfs(e)) return true;
}
}
return false;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&N,&M1,&M2);
int u,v,ok = 0;
init();
getchar();
for(int i = 0; i < M1; i++){
scanf("%d%d",&u,&v);
int f1 = find_father(u);
int f2 = find_father(v);
if(f1 == f2)
ok = 1;
else
fa[f1] = f2;
}
for(int i = 0; i < M2; i++){
scanf("%d%d",&u,&v);
if(!ok){
int f1 = find_father(u);
int f2 = find_father(v);
G[f1].push_back(f2);
}
}
if(ok)
printf("YES\n");
else{
ok = solve();
if(ok)
printf("YES\n");
else
printf("NO\n");
}
}
return 0;
}

51
HDOJ/5223_autoAC.cpp Normal file
View File

@ -0,0 +1,51 @@
#include<cstdio>
#include<string>
#include<queue>
#include<cstring>
#include<iostream>
#include<map>
#include<stack>
#include<malloc.h>
#include<algorithm>
using namespace std;
const int maxn = 1005;
int T, n, m, l[maxn], r[maxn], ans[maxn], x[maxn];
int gcd(int x, int y)
{
if (x%y) return gcd(y, x%y);
return y;
}
int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) x[i] = 1;
int F = 1;
for (int i = 1; i <= m; i++)
{
scanf("%d%d%d", &l[i], &r[i], &ans[i]);
for (int j = l[i]; j <= r[i]; j++)
{
x[j] = x[j] / gcd(ans[i], x[j]);
if ((double)x[j] * ans[i] > 1e9) F = 0;
x[j] = x[j] * ans[i];
}
}
if (F)
for (int i = 1, u; i <= m; i++)
{
u = x[l[i]];
for (int j = l[i]; j <= r[i]; j++) u = gcd(u, x[j]);
if (u != ans[i]) { F = 0; break; }
}
if (F)
{
for (int i = 1; i < n; i++) printf("%d ", x[i]);
printf("%d\n", x[n]);
}
else printf("Stupid BrotherK!\n");
}
return 0;
}

23
HDOJ/5224_autoAC.cpp Normal file
View File

@ -0,0 +1,23 @@
#include<cstdio>
#include<cmath>
#include<queue>
#include<map>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int T, n;
int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
int k = sqrt(1.0*n), ans = 0x7FFFFFFF;
for (int i = 1; i <= k; i++)
if (n%i == 0) ans = min(ans, 2 * (i + n / i));
printf("%d\n", ans);
}
return 0;
}

103
HDOJ/5225_autoAC.cpp Normal file
View File

@ -0,0 +1,103 @@
#include <stdio.h>
#include <iostream>
using namespace std;
#define mod 1000000007
#define llt long long
llt cal[110],a[110],A[110];
llt tree[330];
template<class T>
inline char read(T &n){
T x = 0, tmp = 1; char c = getchar();
while((c < '0' | c > '9') && c != '-' && c != EOF) c = getchar();
if(c == '-') c = getchar(), tmp = -1;
while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
n = x*tmp;
return c;
}
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);
}
void create(llt l,llt r,llt rt)
{
tree[rt]=0;
if(l==r) return;
llt mid=(l+r)>>1;
create(l,mid,rt<<1);
create(mid+1,r,rt<<1|1);
}
llt query(llt ll,llt rr,llt l,llt r,llt rt)
{
if(l==ll&&r==rr) return tree[rt];
llt mid=(l+r)>>1;
if(rr<=mid) return query(ll,rr,l,mid,rt<<1);
else if(ll>mid) return query(ll,rr,mid+1,r,rt<<1|1);
else
{
llt ans=query(ll,mid,l,mid,rt<<1);
ans+=query(mid+1,rr,mid+1,r,rt<<1|1);
return ans;
}
}
void update(llt l,llt r,llt rt,llt x)
{
if(l==x&&r==x)
{
tree[rt]++;
return;
}
llt mid=(l+r)>>1;
if(x<=mid) update(l,mid,rt<<1,x);
else update(mid+1,r,rt<<1|1,x);
tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
void init()
{
cal[0]=0,cal[1]=0,cal[2]=1;
A[1]=1;A[0]=1;
for(int i=2;i<=100;i++)
{A[i]=(A[i-1]*i)%mod;
}
for(int i=3;i<=100;i++)
{
cal[i]=((i*cal[i-1])%mod+(i*(i-1)*A[i-1]/2)%mod)%mod;
}
}
llt sum(llt x)
{
return ((x-1)*x)/2;
}
int main()
{
init();llt n;
while(scanf("%I64d",&n)!=EOF)
{
llt ans=0;
llt i,j,pre=0,tmp=0;
llt k;
create(1,100,1);
for(i=1;i<=n;i++)
{
read(a[i]);
tmp=query(a[i]+1>100?100:a[i]+1,100,1,100,1);
j=n-i-(n-a[i]-tmp);
if(j<=0) continue;
k=A[n-i];llt p=cal[n-i];
ans=(ans+((p*j)%mod+(pre*k*j)%mod+(sum(j)*k)%mod)%mod)%mod;
update(1,100,1,a[i]);
pre+=j;
}
printf("%I64d\n",ans);
}
return 0;
}

53
HDOJ/5226_autoAC.cpp Normal file
View File

@ -0,0 +1,53 @@
#include <stdio.h>
#include <iostream>
using namespace std;
#define maxn 101000
#define ll long long
ll A[maxn],inv[maxn];
ll mod;
ll Pow(ll a,ll b)
{
ll ret=1;
a%=mod;
while(b)
{
if(b&1) ret=ret*a%mod;
a=a*a%mod;
b>>=1;
}
return ret%mod;
}
void init()
{
A[0]=1,A[1]=1;
inv[1]=1;inv[0]=1;
for(int i=2;i<maxn;i++)
{A[i]=A[i-1]*(ll)i%mod;inv[i]=Pow(A[i],mod-2);}
}
ll C(ll a,ll b)
{
if(a<b) return 0;
if(a<mod&&b<mod) return (A[a]*inv[b]%mod)*inv[a-b]%mod;
return C(a/mod,b/mod)*C(a%mod,b%mod)%mod;
}
int main()
{
ll x,xx,y,yy;
while(scanf("%lld%lld%lld%lld%lld",&x,&y,&xx,&yy,&mod)!=EOF)
{
init();
ll ans=0;
if(y>x&&yy>xx&&y>xx) {printf("0\n");continue;}
if(y>x) x=y;
if(yy>xx) yy=xx;
ll st=x,en=xx,h=y;
while(h<=yy&&st<=en)
{
if(st<h) st=h;
ans=(ans+C(en+1,h+1)-C(st,h+1)+mod)%mod;
h++;
}
printf("%lld\n",ans);
}
return 0;
}

154
HDOJ/5227_autoAC.cpp Normal file
View File

@ -0,0 +1,154 @@
#include <stdio.h>
#include <string.h>
#include <map>
#include <algorithm>
using namespace std ;
typedef long long LL ;
#define clr( a , x ) memset ( a , x , sizeof a )
#define cpy( a , x ) memcpy ( a , x , sizeof a )
const int MAXN = 10005 ;
const int MAXE = 20005 ;
struct Edge {
int v , n ;
Edge () {}
Edge ( int v , int n ) : v ( v ) , n ( n ) {}
} ;
Edge E[MAXE] ;
int H[MAXN] , cntE ;
int phi[MAXN] ;
LL val[MAXN] ;
LL S[MAXN] , top ;
LL ct[MAXN] ;
int ans ;
int n ;
int Q[MAXN] , head , tail ;
map < LL , int > mp ;
int vis[MAXN] ;
int siz[MAXN] ;
int pre[MAXN] ;
int gcd ( int a , int b ) {
return b ? gcd ( b , a % b ) : a ;
}
void init () {
ans = 0 ;
cntE = 0 ;
clr ( H , -1 ) ;
clr ( vis , 0 ) ;
}
void addedge ( int u , int v ) {
E[cntE] = Edge ( v , H[u] ) ;
H[u] = cntE ++ ;
}
LL get ( int n , int m ) {
LL res = 0 ;
if ( n > m ) swap ( n , m ) ;
for ( int i = 1 , j ; i <= n ; i = j + 1 ) {
j = min ( n / ( n / i ) , m / ( m / i ) ) ;
res += ( LL ) ( phi[j] - phi[i - 1] ) * ( n / i ) * ( m / i ) ;
}
return res ;
}
void preprocess () {
for ( int i = 0 ; i <= 10000 ; ++ i ) {
phi[i] = i ;
}
for ( int i = 2 ; i <= 10000 ; ++ i ) if ( phi[i] == i ) {
for ( int j = i ; j <= 10000 ; j += i ) {
phi[j] = phi[j] / i * ( i - 1 ) ;
}
}
for ( int i = 2 ; i <= 10000 ; ++ i ) {
phi[i] += phi[i - 1] ;
}
for ( int t = 1 ; t <= 10000 ; ++ t ) {
ct[t] = get ( t , t ) + ct[t - 1] ;
}
}
int get_root ( int s ) {
head = tail = 0 ;
Q[tail ++] = s ;
pre[s] = 0 ;
while ( head != tail ) {
int u = Q[head ++] ;
for ( int i = H[u] ; ~i ; i = E[i].n ) {
int v = E[i].v ;
if ( vis[v] || v == pre[u] ) continue ;
pre[v] = u ;
Q[tail ++] = v ;
}
}
int root_siz = tail , root = s ;
while ( head ) {
int u = Q[-- head] , cnt = 1 ;
siz[u] = 1 ;
for ( int i = H[u] ; ~i ; i = E[i].n ) {
int v = E[i].v ;
if ( vis[v] || v == pre[u] ) continue ;
siz[u] += siz[v] ;
if ( siz[v] > cnt ) cnt = siz[v] ;
}
cnt = max ( cnt , tail - siz[u] ) ;
if ( cnt < root_ ) {
root_siz = cnt ;
root = u ;
}
}
return root ;
}
void get_val ( int u , int f , LL value ) {
S[top ++] = value ;
for ( int i = H[u] ; ~i ; i = E[i].n ) {
int v = E[i].v ;
if ( vis[v] || v == f ) continue ;
get_val ( v , u , value ^ val[v] ) ;
}
}
void dfs ( int u ) {
int root = get_root ( u ) ;
vis[root] = 1 ;
mp.clear () ;
if ( !val[root] ) ++ ans ;
for ( int i = H[root] ; ~i ; i = E[i].n ) {
int v = E[i].v ;
if ( vis[v] ) continue ;
top = 0 ;
get_val ( v , root , val[v] ) ;
for ( int j = 0 ; j < top ; ++ j ) {
map < LL , int > :: iterator it = mp.find ( S[j] ^ val[root] ) ;
if ( it != mp.end () ) ans += it->second * 2 ;
if ( !( S[j] ^ val[root] ) ) ans += 2 ;
}
for ( int j = 0 ; j < top ; ++ j ) {
mp[S[j]] ++ ;
}
}
for ( int i = H[root] ; ~i ; i = E[i].n ) {
int v = E[i].v ;
if ( vis[v] ) continue ;
dfs ( v ) ;
}
}
void solve () {
int t , a , b , c ;
init () ;
for ( int i = 1 ; i < n ; ++ i ) {
scanf ( "%d%d" , &a , &b ) ;
addedge ( a , b ) ;
addedge ( b , a ) ;
}
for ( int i = 1 ; i <= n ; ++ i ) {
scanf ( "%d%d%d%d" , &t , &a , &b , &c ) ;
val[i] = ct[t - 1] + c - 1 ;
val[i] += get ( a - 1 , t ) ;
val[i] += get ( a , b - 1 ) - get ( a - 1 , b - 1 ) ;
}
dfs ( 1 ) ;
ans = n * n - ans ;
int x = gcd ( n * n , ans ) ;
printf ( "%d/%d\n" , ans / x , n * n / x ) ;
}
int main () {
preprocess () ;
while ( ~scanf ( "%d" , &n ) ) solve () ;
return 0 ;
}

46
HDOJ/5228_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int has[13*4];
char s[5];
int main()
{
int t;
cin>>t;
while(t--)
{
memset(has,0,sizeof(has));
for(int i=0;i<5;i++)
{
scanf("%s",s);
int cur;
if(strlen(s)==3)
{
cur=(s[1]-'0')*10+s[2]-'0';
}
else
{
cur=s[1]-'0';
}
cur--;
has[cur*4+s[0]-'A']++;
}
int ans=5;
for(int i=0;i<10;i++)
{
for(int j=0;j<4;j++)
{
int cnt=5;
for(int k=0;k<5;k++)
{
int num=(i+k)*4+j;
if(has[num%(52)]) cnt--;
}
ans=min(ans,cnt);
}
}
printf("%d\n",ans);
}
}

47
HDOJ/5229_autoAC.cpp Normal file
View File

@ -0,0 +1,47 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<string>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){
for(;a>0&&b>0;a>b?a%=b:b%=a);
return a+b;
}
int main(){
int T;
while(scanf("%d",&T)!=EOF){
while(T--){
int n;
scanf("%d",&n);
map<string,int>m;
ll ji=0,ou=0;
m.clear();
int i;
for(i=1;i<=n;i++){
string a;
cin>>a;
m[a]++;
if(a.length()%2){
ji++;
}
else ou++;
}
ll cnt=0;
cnt=ji*ou;
for(map<string,int>::iterator it=m.begin();it!=m.end();it++){
if(it->second>1){
cnt+=((it->second)*((it->second)-1)/2);
}
}
ll sum=n*(n-1)/2;
if(cnt==0)printf("0/1\n");
else{
ll g=gcd(cnt,sum);
printf("%lld/%lld\n",cnt/g,sum/g);
}
}
}
return 0;
}

44
HDOJ/5230_autoAC.cpp Normal file
View File

@ -0,0 +1,44 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MOD 998244353
using namespace std;
int Now[101005][2],Next[101005][2],s[101005];
void add(int &x,int y)
{
if (y>MOD) y-=MOD;
x+=y;
if (x>MOD) x-=MOD;
}
int main()
{
int i,j,T,c,n,l,r;
memset(s,0,sizeof(s));
Next[1][1]=1;
for (i=1;i<=450;i++)
{
for (j=1;j<=100000;j++)
{
Now[j][0]=Next[j][0]; Next[j][0]=0;
Now[j][1]=Next[j][1]; Next[j][1]=0;
}
for (j=i;j<=100000;j++)
{
add(Now[j+i][0],Now[j][0]+Now[j][1]);
add(Next[j+1][1],Now[j][0]);
}
for (j=1;j<=100000;j++) add(s[j],Now[j][0]+Now[j][1]);
}
s[0]=1;
for (i=1;i<=100000;i++) add(s[i],s[i-1]);
scanf("%d",&T);
while (T--)
{
scanf("%d%d%d%d",&n,&c,&l,&r);
l-=c; r-=c; c=s[r];
if (l!=0) c=s[r]-s[l-1];
if (c<0) c+=MOD;
printf("%d\n",c);
}
return 0;
}

63
HDOJ/5232_autoAC.cpp Normal file
View File

@ -0,0 +1,63 @@
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
const int inf=0x3f3f3f3f;
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
int g[300][300];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(g,0,sizeof(g));
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
g[i][j]=read();
int ans=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(g[i][j])
{
ans++;
g[i][j]=0;
g[j][i]=0;
}
}
}
cout<<(ans+n)*2<<endl;
}
}

49
HDOJ/5234_autoAC.cpp Normal file
View File

@ -0,0 +1,49 @@
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <sstream>
#include <algorithm>
#define Max 2147483647
#define INF 0x7fffffff
#define N 110
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define repu(i, a, b) for(int i = (a); i < (b); i++)
const double PI=-acos(-1.0);
using namespace std;
int dp[N][N][N];
int w[N][N];
int main()
{
int n,m,k;
while(~scanf("%d%d%d",&n,&m,&k))
{
repu(i,0,1+n)
repu(j,0,m+1)
repu(p,0,k+1)
dp[i][j][p] = 0;
repu(i,1,1+n)
repu(j,1,1+m)
scanf("%d",&w[i][j]);
repu(i,1,n+1)
{
repu(j,1,m+1)
{
for(int p = w[i][j]; p<=k; p++)
{
int t = max(dp[i-1][j][p],dp[i][j-1][p]);
int s = max(dp[i-1][j][p-w[i][j]]+w[i][j],dp[i][j-1][p-w[i][j]]+w[i][j]);
dp[i][j][p] = max(t,s);
}
}
}
int maxn = 0;
repu(p,0,k+1)
maxn = max(dp[n][m][p],maxn);
printf("%d\n",maxn);
}
return 0;
}

67
HDOJ/5236_autoAC.cpp Normal file
View File

@ -0,0 +1,67 @@
#include <cstdio>
#include <limits>
#include <algorithm>
using namespace std;
const int MAXN = 100005;
const int MAXS = 30;
int N, X, S;
double P, F[MAXS];
void CalcF(){
int i;
double a = 1, b = 1. / (P - 1.);
S = numeric_limits<int>::max();
F[0] = 0.;
F[1] = - b / a;
for(i = 2; i <= N && i <= S; i ++){
a = (a - P) / (1 - P);
b = (b - 1) / (1 - P);
F[i] = - b / a;
if(F[i] > F[i >> 1] + F[i - (i >> 1)] + X){
F[i] = F[i >> 1] + F[i - (i >> 1)] + X;
S = i;
}
}
}
double BFSolve(){
static double dp[MAXN];
int i, j;
dp[0] = 0;
for(i = 1; i <= N; i ++){
if(i <= S){
dp[i] = F[i] + X;
}else{
dp[i] = numeric_limits<double>::max();
}
for(j = 1; j <= S && j < i; j ++){
dp[i] = min(dp[i], dp[i - j] + dp[j]);
}
}
return dp[N];
}
double Solve(){
int i, a, b;
double ans = numeric_limits<double>::max();
if(N < S)
return F[N] + X;
for(i = 1; i <= S; i ++){
a = N / i;
b = N % i;
if(a >= b && i < S){
ans = min(ans, (a - b) * F[i] + b * F[i + 1] + a * X);
}
if(a + b + 1 >= i){
ans = min(ans, (a + 1 - i + b) * F[i] + (i - b) * F[i - 1] + (a + 1) * X);
}
}
return ans;
}
int main(){
int T, i;
scanf("%d", &T);
for(i = 1; i <= T; i ++){
scanf("%d%lf%d", &N, &P, &X);
CalcF();
printf("Case #%d: %.6lf\n", i, Solve());
}
return 0;
}

55
HDOJ/5237_autoAC.cpp Normal file
View File

@ -0,0 +1,55 @@
#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
char S[1000],Q[1000];
char Std[65]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
void Make(int x)
{
Q[strlen(Q)]=Std[x];
}
void f(int n)
{
if(n) f(n/2);
else return;
printf("%d",n%2);
}
int main()
{
int t,tm;
scanf("%d",&tm);t=tm;
while(t--)
{
int k,i,n;
scanf("%d %s",&n,S);
while(n--)
{
memset(Q,0,sizeof(Q));
int l=strlen(S);
for(i=0;i<l;i++)
{
if(i%3==0)
Make(S[i]/4);
if(i%3==1)
Make((S[i-1]%4)*16+S[i]/16);
if(i%3==2)
{
Make((S[i-1]%16)*4+S[i]/64);
Make(S[i]%64);
}
}
if(l%3)
{
int key=(S[l-1]%4)*16;
if(l%3==2) key=(S[l-1]%16)*4;
Make(key);
strcat(Q,"=");
if(l%3==1) strcat(Q,"=");
}
Q[strlen(Q)]='\0';
strcpy(S,Q);
}
printf("Case #%d: %s\n",tm-t,S);
}
return 0;
}

111
HDOJ/5238_autoAC.cpp Normal file
View File

@ -0,0 +1,111 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std ;
typedef long long LL ;
#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
const int MAXN = 50005 ;
const int MOD = 29393 ;
int mod[4] = { 7 , 13 , 17 , 19 } ;
int p[4][20][MOD] ;
int f[MAXN << 2][4][20] ;
int n , m ;
void preprocess () {
for ( int i = 0 ; i < 4 ; ++ i ) {
for ( int j = 0 , m = mod[i] ; j < m ; ++ j ) {
p[i][j][0] = 1 ;
for ( int k = 1 ; k < MOD ; ++ k ) {
p[i][j][k] = p[i][j][k - 1] * j % m ;
}
}
}
}
void upd ( int o , char op , int x ) {
for ( int i = 0 ; i < 4 ; ++ i ) {
for ( int j = 0 , m = mod[i] ; j < m ; ++ j ) {
if ( op == '+' ) f[o][i][j] = ( j + x ) % m ;
else if ( op == '*' ) f[o][i][j] = ( j * x ) % m ;
else f[o][i][j] = p[i][j][x] ;
}
}
}
void push_up ( int o ) {
for ( int i = 0 ; i < 4 ; ++ i ) {
for ( int j = 0 , m = mod[i] ; j < m ; ++ j ) {
f[o][i][j] = f[rs][i][f[ls][i][j]] ;
}
}
}
void build ( int o , int l , int r ) {
if ( l == r ) {
char c ;
int x ;
scanf ( " %c%d" , &c , &x ) ;
upd ( o , c , x ) ;
return ;
}
int m = mid ;
build ( lson ) ;
build ( rson ) ;
push_up ( o ) ;
}
void update ( int x , char c , int v , int o , int l , int r ) {
if ( l == r ) {
upd ( o , c , v ) ;
return ;
}
int m = mid ;
if ( x <= m ) update ( x , c , v , lson ) ;
else update ( x , c , v , rson ) ;
push_up ( o ) ;
}
int exgcd ( int a , int b , int &x , int &y ) {
if ( !b ) {
x = 1 ;
y = 0 ;
return a ;
}
int ans = exgcd ( b , a % b , y , x ) ;
y -= a / b * x ;
return ans ;
}
int crt ( int v ) {
int ans = 0 ;
for ( int i = 0 ; i < 4 ; ++ i ) {
int t = MOD / mod[i] , x , y ;
exgcd ( t , mod[i] , x , y ) ;
ans = ( ans + f[1][i][v % mod[i]] * t * ( x % mod[i] ) ) % MOD ;
}
return ( ans + MOD ) % MOD ;
}
void solve () {
int op , x , v ;
char c ;
scanf ( "%d%d" , &n , &m ) ;
build ( root ) ;
while ( m -- ) {
scanf ( "%d%d" , &op , &x ) ;
if ( op == 1 ) {
printf ( "%d\n" , crt ( x ) ) ;
} else {
scanf ( " %c%d" , &c , &v ) ;
update ( x , c , v , root ) ;
}
}
}
int main () {
int T ;
preprocess () ;
scanf ( "%d" , &T ) ;
for ( int i = 1 ; i <= T ; ++ i ) {
printf ( "Case #%d:\n" , i ) ;
solve () ;
}
return 0 ;
}

125
HDOJ/5239_autoAC.cpp Normal file
View File

@ -0,0 +1,125 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
struct uint64{
static const ull MODN = 9223372034707292160;
ull n;
uint64(ull n = 0):n(n){}
static uint64 ass(ull n){
if(n >= MODN)
return uint64(n - MODN);
else
return uint64(n);
}
inline uint64 operator-() const{
return uint64(MODN - n);
};
friend inline uint64 operator*(const uint64& a, const uint64& b){
uint64 tmp;
ull a1 = (a.n >> 32), a0 = (a.n & 0xffffffff);
ull b1 = (b.n >> 32), b0 = (b.n & 0xffffffff);
tmp = ass(a1 * b1);
tmp = tmp + ass(a1 * b0);
tmp = tmp + ass(a0 * b1);
for(int i = 0; i < 32; i ++)
tmp = tmp + tmp;
tmp = tmp + ass(a0 * b0);
return tmp;
}
friend inline uint64 operator+(const uint64& a, const uint64& b){
ull n = a.n + b.n;
return uint64((n < a.n || n >= MODN) ? (n - MODN) : n);
}
friend inline uint64 operator-(const uint64& a, const uint64& b){
return (a.n < b.n) ? (a.n + MODN - b.n) : (a.n - b.n);
}
inline uint64 sqr(){
return (*this) * (*this);
}
};
const int MAXN = 131072;
ull arr[MAXN];
int N, M;
class Segtree{
public:
static const int MAXK = 30;
struct NODE{
int flg;
uint64 d[MAXK];
}node[MAXN<<1];
inline void popup(int id){
int j;
for(j = 0; j < MAXK; j ++)
node[id].d[j] = node[id<<1].d[j] + node[id<<1|1].d[j];
}
inline void shift(int id, int k){
node[id].flg += k;
for(int j = 0; j <= MAXK - 2; j ++){
node[id].d[j] = node[id].d[min(j + k, MAXK - 1)];
}
}
inline void pushdown(int id){
int flg = node[id].flg;
if(flg > 0){
shift(id<<1, flg);
shift(id<<1|1, flg);
node[id].flg = 0;
}
}
void build(int id, int l, int r){
int mid = (l + r) >> 1;
if(l == r){
node[id].d[0] = uint64::ass(arr[l]);
node[id].flg = 0;
for(int j = 1; j < MAXK; j ++)
node[id].d[j] = node[id].d[j - 1].sqr();
}else{
node[id].flg = 0;
build(id<<1, l, mid);
build(id<<1|1, mid+1, r);
popup(id);
}
}
uint64 query(int id, int l, int r, int L, int R){
uint64 ret;
if(l == L && r == R){
ret = node[id].d[0];
shift(id, 1);
return ret;
}
int mid = (L + R) >> 1;
pushdown(id);
if(l > mid){
ret = query(id<<1|1, l, r, mid + 1, R);
}else if(r <= mid){
ret = query(id<<1, l, r, L, mid);
}else{
ret = query(id<<1, l, mid, L, mid) + query(id<<1|1, mid + 1, r, mid + 1, R);
}
popup(id);
return ret;
}
}tree;
int main(){
int i, T, l, r;
uint64 ans;
scanf("%d", &T);
for(i = 1; i <= T; i ++){
printf("Case #%d:\n", i);
scanf("%d%d", &N, &M);
for(l = 1; l <= N; l ++){
scanf("%I64u", &arr[l]);
}
tree.build(1, 1, N);
ans = 0;
while(M --){
scanf("%d%d", &l, &r);
ans = ans + tree.query(1, l, r, 1, N);
printf("%I64u\n", ans.n);
}
}
return 0;
}

63
HDOJ/5240_autoAC.cpp Normal file
View File

@ -0,0 +1,63 @@
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-4;
struct node
{
long long r,s,l;
}exam[111111];
bool cmp(node a,node b)
{
return a.s<b.s;
}
int main()
{
int T;
scanf("%d",&T);
for(int ncase=1;ncase<=T;ncase++)
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%I64d%I64d%I64d",&exam[i].r,&exam[i].s,&exam[i].l);
sort(exam,exam+n,cmp);
for(int i=n-1;i>0;i--)
exam[i].s-=(exam[i-1].s+exam[i-1].l);
int flag=1,i=0,j=0;
while(i<n)
{
if(exam[i].s<exam[j].r)
{
if(i==j)
{
flag=0;
break;
}
exam[j].r-=exam[i].s;
i++;
}
else
{
exam[i].s-=exam[j].r;
j++;
}
}
if(flag)
printf("Case #%d: YES\n",ncase);
else
printf("Case #%d: NO\n",ncase);
}
return 0;
}

65
HDOJ/5241_autoAC.cpp Normal file
View File

@ -0,0 +1,65 @@
#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
int a[10000],b[10000],c[10000];
void SP()
{
memset(c,0,sizeof(c));
int i,j;
for(i=1;i<=a[0];i++)
for(j=1;j<=b[0];j++)
c[i+j-1]+=a[i]*b[j];
i=1;
for(i=1;i<9999;i++)
{
c[i+1]+=c[i]/10;
a[i]=c[i]%10;
if(a[i]) a[0]=i;
}
}
void PP()
{
memset(c,0,sizeof(c));
int i,j;
for(i=1;i<=b[0];i++)
for(j=1;j<=b[0];j++)
c[i+j-1]+=b[i]*b[j];
i=1;
for(i=1;i<9999;i++)
{
c[i+1]+=c[i]/10;
b[i]=c[i]%10;
if(b[i]) b[0]=i;
}
}
int main()
{
int t,tm;
scanf("%d",&t);tm=t;
while(t--)
{
int n;
scanf("%d",&n);
printf("Case #%d: ",tm-t);
if(!n) printf("1\n");
else
{
n*=5;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
a[0]=1;a[1]=1;
b[0]=1;b[1]=2;
while(n)
{
if(n%2)
SP();
PP();
n/=2;
}
for(int i=a[0];i;i--) printf("%d",a[i]);
printf("\n");
}
}
return 0;
}

55
HDOJ/5242_autoAC.cpp Normal file
View File

@ -0,0 +1,55 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define LL __int64
const int maxn=1e5+100;
vector<LL>V[maxn];
priority_queue<LL>Q;
LL a[maxn];
LL dp[maxn];
LL dfs(int cur){
int i,v;
LL tmp;
dp[cur]=a[cur];
for(i=0;i<V[cur].size();i++){
v=V[cur][i];
tmp=dfs(v);
if(dp[cur]>tmp+a[cur]){
Q.push(tmp);
}else{
Q.push(dp[cur]-a[cur]);
dp[cur]=tmp+a[cur];
}
}
return dp[cur];
}
int main(){
int t ,n,m,i,j,k,u,v,cnt=0;
LL ans;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&k);
for(i=1;i<=n;i++)
scanf("%lld",&a[i]);
for(i=1;i<n;i++){
scanf("%d%d",&u,&v);
V[u].push_back(v);
}
dfs(1);
ans=dp[1];
for(i=1;i<k;i++){
ans+=Q.top();
Q.pop();
}
printf("Case #%d: %lld\n",++cnt,ans);
while(!Q.empty())
Q.pop();
for(i=0;i<=n;i++){
V[i].clear();
}
}
return 0;
}

198
HDOJ/5243_autoAC.cpp Normal file
View File

@ -0,0 +1,198 @@
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
#define V P
const double eps = 1e-6;
inline int dcmp (double x) {
return x < -eps ? -1 : x > eps;
}
struct P {
double x, y;
void scan() {
scanf("%lf%lf", &x, &y);
}
P(double _x = 0, double _y = 0) : x(_x), y(_y) { }
V operator + (V a) const {
return V(x + a.x, y + a.y);
}
V operator - (V a) const {
return V(x - a.x, y - a.y);
}
V operator * (double p) const {
return V(p * x, p * y);
}
V operator / (double p) const {
return V(x / p, y / p);
}
bool operator < (P a) const {
return x < a.x || (dcmp(x - a.x) == 0 && y < a.y);
}
bool operator == (P a) const {
return dcmp(x - a.x) == 0 && dcmp(y - a.y) == 0;
}
};
inline double dot(V a, V b) {
return a.x * b.x + a.y * b.y;
}
inline double len(V a) {
return sqrt(dot(a, a));
}
inline double dis(P a, P b) {
return len(b - a);
}
inline double ang(V a, V b) {
return acos(dot(a, b) / len(a) / len(b));
}
inline double cross(V a, V b) {
return a.x * b.y - a.y * b.x;
}
inline double area(P a, P b, P c) {
return cross(b - a, c - a);
}
V rot(V a, double p) {
return V(a.x * cos(p) - a.y * sin(p), a.x * sin(p) + a.y * cos(p));
}
V normal(V a) {
double L = len(a);
return V(-a.y / L, a.x / L);
}
P inter(P p, V v, P q, V w) {
V u = p - q;
double t = cross(w, u) / cross(v, w);
return p + v * t;
}
double dis(P p, P a, P b) {
V v1 = b - a, v2 = p - a;
return fabs(cross(v1, v2)) / len(v1);
}
double dis2(P p, P a, P b) {
if (a == b) return len(p - a);
V v1 = b - a, v2 = p - a, v3 = p - b;
if (dcmp(dot(v1, v2)) < 0) return len(v2);
else if (dcmp(dot(v1, v3)) > 0) return len(v3);
else return fabs(cross(v1, v2)) / len(v1);
}
P proj(P p, P a, P b) {
V v = b - a;
return a + v * (dot(v, p - a) / dot(v, v));
}
bool isInter(P a1, P a2, P b1, P b2) {
double c1 = cross(a2 - a1, b1 - a1), c2 = cross(a2 - a1, b2 - a1),
c3 = cross(b2 - b1, a1 - b1), c4 = cross(b2 - b1, a2 - b1);
return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
}
bool onSeg(P p, P a1, P a2) {
return dcmp(cross(a1 - p, a2 - p)) == 0 && dcmp(dot(a1 - p, a2 - p)) < 0;
}
double area(P* p, int n) {
double s = 0;
p[n] = p[0];
for (int i = 1; i < n; i ++)
s += cross(p[i] - p[0], p[i + 1] - p[0]);
return s / 2;
}
int graham(P* p, int n, P* ch) {
sort(p, p + n);
int m = 0;
for (int i = 0; i < n; i ++) {
while (m > 1 && cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m --;
ch[m ++] = p[i];
}
int k = m;
for (int i = n - 2; i >= 0; i --) {
while (m > k && cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m --;
ch[m ++] = p[i];
}
if (n > 1) m --;
return m;
}
struct L {
P p;
V v;
double ang;
L() {}
L(P _p, V _v) : p(_p), v(_v) { ang = atan2(v.y, v.y); }
bool operator < (const L& L) const {
return ang < L.ang;
}
};
inline int get(P a) {
if( a.x > 0 && a.y >= 0) return 1;
if( a.x <= 0 && a.y > 0) return 2;
if( a.x < 0 && a.y <= 0) return 3;
if( a.x >= 0 && a.y < 0) return 4;
return 0;
}
inline bool cmp2 (L a, L b) {
return get(a.v) < get(b.v) || (get(a.v) == get(b.v) && dcmp( cross(a.v, b.v) ) >0);
}
bool onLeft(L l, P p) {
return cross(l.v, p - l.p) > 0;
}
P inter(L a, L b) {
return inter(a.p, a.v, b.p, b.v);
}
int half(L* l, int n, P* po) {
sort(l, l + n, cmp2);
int h, t;
P *p = new P[n];
L *q = new L[n];
q[h = t = 0] = l[0];
for (int i = 1; i < n; i ++) {
while (h < t && !onLeft(l[i], p[t - 1])) t --;
while (h < t && !onLeft(l[i], p[h])) h ++;
q[++ t] = l[i];
if (dcmp(cross(q[t].v, q[t - 1].v)) == 0) {
t --;
if (onLeft(q[t], l[i].p)) q[t] = l[i];
}
if (h < t) p[t - 1] = inter(q[t - 1], q[t]);
}
while (h < t && !onLeft(q[h], p[t - 1])) t --;
if (t - h <= 1) return 0;
p[t] = inter(q[t], q[h]);
int m = 0;
for (int i = h; i <= t; i ++) po[m ++] = p[i];
return m;
}
inline bool cmp (V a, V b) {
return get(a) < get(b) || (get(a) == get(b) && dcmp( cross(a, b) ) >0);
}
const int N = 101000;
int n;
P a[N], b[N], res[N];
L l[N];
int main() {
int T;
scanf("%d", &T);
for (int cas = 1; cas <= T; cas ++) {
scanf("%d", &n);
for (int i = 0; i < n; i ++)
a[i].scan();
int bound = n / 3 - 1, l_c = 0;
for (int i = 0; i < n; i ++) {
int cnt = 0;
for (int j = 0; j < n; j ++)
if (j != i)
b[cnt ++] = a[j] - a[i];
sort(b, b + cnt, cmp);
int t = 0, sum = 0;
for (int j = 0; j < cnt; j ++) {
while ((dcmp(cross(b[j], b[(t + 1) % cnt])) == 1) ||
(dcmp(cross(b[j], b[(t + 1) % cnt])) == 0 &&
dcmp(dot(b[j], b[(t + 1) % cnt])) == -1)) t = (t + 1) % cnt, sum ++;
if (cnt - (sum + 1) == bound) l[l_c ++] = L(a[i], b[j]);
if (t == j) t ++;
else {
while (dcmp(cross(b[j], b[(j + 1) % cnt])) == 0) j ++, sum --;
sum --;
}
}
}
int ans = half(l, l_c, res);
printf("Case #%d: %.6lf\n", cas, area(res, ans));
}
return 0;
}

92
HDOJ/5244_autoAC.cpp Normal file
View File

@ -0,0 +1,92 @@
#include <cstdio>
using namespace std;
const int MAXK = 20;
const int MAXN = (1 << MAXK);
typedef long long ll;
typedef unsigned long ul;
struct BI{
ul d[4];
BI(const ll& a = 0){
d[0] = a & 0xffffffff;
d[1] = a >> 32;
d[2] = a >> 64;
d[3] = d[2];
}
BI operator+(const BI& B)const{
BI ret(*this);
int flg = 0;
ll tmp;
for(int i = 0; i < 4; i ++){
tmp = ll(ret.d[i]) + ll(B.d[i]) + ll(flg);
ret.d[i] = tmp & 0xffffffff;
flg = (tmp >> 32) & 1;
}
return ret;
}
BI operator-()const{
BI ret(*this);
int flg = 1;
ll tmp;
for(int i = 0; i < 4; i ++){
tmp = ll(~ret.d[i]) + ll(flg);
ret.d[i] = tmp & 0xffffffff;
flg = (tmp >> 32) & 1;
}
return ret;
}
inline BI operator-(const BI& rhs)const{
return (*this) + (- rhs);
}
void operator>>=(int k){
int i;
for(i = 0; i < 3; i ++)
d[i] = (d[i] >> k) | (d[i+1] << (32 - k));
d[3] = d[3] >> k;
};
ll val(){
return (ll(d[1]) << 32) + ll(d[0]);
}
};
ll B[MAXN];
BI A[MAXN];
int N, K;
void CalcA(){
int i, j, b;
for(j = 0; j < N; j ++)
A[j] = B[j];
for(i = 1; i <= K; i ++){
for(b = 0; b < N; b += (1 << i)){
int step = (1 << (i - 1));
for(j = b; j < b + step; j ++){
BI t1, t2;
t1 = A[j + step] + A[j];
t2 = A[j + step] - A[j];
A[j] = t1;
A[j + step] = t2;
}
}
}
if(K > 0){
for(j = 0; j < N; j ++){
A[j] >>= (K - 1);
}
A[0] = A[0] - B[N - 1];
}
}
int main(){
int i, j, T;
scanf("%d", &T);
for(i = 1; i <= T; i ++){
scanf("%d", &K);
N = 1 << K;
for(j = 0; j < N; j ++){
scanf("%I64d", &B[j]);
}
CalcA();
printf("Case #%d:", i);
for(j = 0; j < N; j ++)
printf(" %I64d", A[j].val());
printf("\n");
}
return 0;
}

32
HDOJ/5245_autoAC.cpp Normal file
View File

@ -0,0 +1,32 @@
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
int t,tm,k;
double n,m;
scanf("%d",&t);tm=t;
while(t--)
{
scanf("%lf%lf%d",&n,&m,&k);
double ans=0;
for(double i=1;i<=n;i++)
for(double j=1;j<=m;j++)
{
double p=m*n;
p+=(i-1)*(j-1)*(n-i+1)*(m-j+1);
p+=(i-1)*(m-j)*(n-i+1)*j;
p+=(j-1)*(n-i)*(m-j+1)*i;
p+=(n-i)*(m-j)*i*j;
p+=(i-1)*m*(n-i+1);
p+=(m-j)*n*j;
p+=(n-i)*m*i;
p+=(j-1)*n*(m-j+1);
p=p/n/n/m/m;
ans+=1-(pow(1-p,k));
}
printf("Case #%d: %d\n",tm-t,int(ans+0.5));
}
return 0;
}

51
HDOJ/5246_autoAC.cpp Normal file
View File

@ -0,0 +1,51 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 10005;
long long n, m, k;
long long s[N];
int main() {
int t, cas = 1;
scanf("%d", &t);
while (t--) {
scanf("%lld%lld%lld", &n, &m, &k);
for (int i = 0; i < n; i++)
scanf("%lld", &s[i]);
sort(s, s + n);
if (s[0] > m) {
printf("Case #%d:\nmadan!\n", cas++);
continue;
}
if (s[n - 1] <= m) {
printf("Case #%d:\nwhy am I so diao?\n", cas++);
continue;
}
for (int i = 0; i < n; i++) {
if (s[i] > m) {
int flag = 1, flag2 = 0;
m = s[i - 1];
for (int j = i; j < n; j++) {
if (m + k >= s[j])
continue;
else {
m = s[j - 1];
if (k > 0)
k--;
if (m + k < s[j]) {
flag = 0;
break;
}
j--;
}
}
if (!flag)
printf("Case #%d:\nmadan!\n", cas++);
else
printf("Case #%d:\nwhy am I so diao?\n", cas++);
break;
}
}
}
return 0;
}

41
HDOJ/5247_autoAC.cpp Normal file
View File

@ -0,0 +1,41 @@
#include<cstdio>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn = 10005;
set<LL>s;
set<LL>::iterator it1,it2;
int n,m;
int num[maxn] = {0};
LL arr[maxn];
int main(){
scanf("%d%d",&n,&m);
for(int i = 0; i < n; i++)
scanf("%I64d",&arr[i]);
for(int i = 0; i < n; i++){
s.clear();
s.insert(arr[i]);
num[1] ++;
for(int j = i + 1; j < n; j++){
if(!s.count(arr[j]))
s.insert(arr[j]);
else
break;
it1 = s.begin();
it2 = s.end();
it2 --;
LL e1 = *it1,e2 = *it2;
if(e2 - e1 == s.size() - 1){
num[s.size()] ++;
}
}
}
printf("Case #1:\n");
while(m--){
int k;
scanf("%d",&k);
printf("%d\n",num[k]);
}
return 0;
}

77
HDOJ/5248_autoAC.cpp Normal file
View File

@ -0,0 +1,77 @@
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
#define MOD 1000000007
const int INF=0x3f3f3f3f;
const double eps=1e-5;
typedef long long ll;
#define cl(a) memset(a,0,sizeof(a))
#define ts printf("*****\n");
const int MAXN=100005;
int n,m,tt;
int a[MAXN],b[MAXN];
bool check(int mid)
{
for(int i=1;i<=n;i++)
{
b[i]=a[i];
}
for(int i=1;i<=n;i++)
{
if(i==1)
{
b[i]-=mid;
continue;
}
if(b[i]<=b[i-1])
{
if((b[i-1]+1)-b[i]<=mid)
b[i]=b[i-1]+1;
else
{
return 0;
}
}
if(b[i]-mid>b[i-1])
{
b[i]=b[i]-mid;
}
else
{
b[i]=b[i-1]+1;
}
}
return 1;
}
int main()
{
int i,j,k;
int ca=1;
scanf("%d",&tt);
while(tt--)
{
printf("Case #%d:\n",ca++);
scanf("%d",&n);
for(i=1;i<=n;i++) scanf("%d",a+i);
int l=0;
int r=1000005;
int ans=0;
while(l<=r)
{
int mid=(l+r)>>1;
if(check(mid))
{
ans=mid;
r=mid-1;
}
else
l=mid+1;
}
printf("%d\n",ans);
}
}

54
HDOJ/5249_autoAC.cpp Normal file
View File

@ -0,0 +1,54 @@
#include <stdio.h>
#include <string.h>
#include <set>
#include <queue>
#include <algorithm>
using namespace std;
set<int> A, B;
queue<int> q;
int n, cas = 1;
int main() {
while (scanf("%d", &n) == 1) {
printf("Case #%d:\n", cas++);
while (!q.empty())
q.pop();
A.clear();
B.clear();
char str[10];
int m = 0, d;
while (n--) {
scanf("%s", str);
if (str[0] == 'i') {
scanf("%d", &d);
q.push(d);
if (A.size() == 0 || d <= *A.rbegin())
A.insert(d);
else
B.insert(d);
m++;
}
else if (str[0] == 'o') {
int temp = q.front();
q.pop();
if (A.find(temp) == A.end())
B.erase(temp);
else
A.erase(temp);
m--;
}
else
printf("%d\n", *A.rbegin());
while (m > 0 && A.size() < m / 2 + 1) {
int temp = *B.begin();
A.insert(temp);
B.erase(temp);
}
while (m > 0 && A.size() > m / 2 + 1) {
int temp = *A.rbegin();
B.insert(temp);
A.erase(temp);
}
}
}
return 0;
}

173
HDOJ/5251_autoAC.cpp Normal file
View File

@ -0,0 +1,173 @@
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef double typev;
const double eps = 1e-8;
const int N = 50005;
int sign(double d){
return d < -eps ? -1 : (d > eps);
}
struct point{
typev x, y;
point operator-(point d){
point dd;
dd.x = this->x - d.x;
dd.y = this->y - d.y;
return dd;
}
point operator+(point d){
point dd;
dd.x = this->x + d.x;
dd.y = this->y + d.y;
return dd;
}
}ps[N];
//int n, cn;
double dist(point d1, point d2){
return sqrt(pow(d1.x - d2.x, 2.0) + pow(d1.y - d2.y, 2.0));
}
double dist2(point d1, point d2){
return pow(d1.x - d2.x, 2.0) + pow(d1.y - d2.y, 2.0);
}
bool cmp(point d1, point d2){
return d1.y < d2.y || (d1.y == d2.y && d1.x < d2.x);
}
typev xmul(point st1, point ed1, point st2, point ed2){
return (ed1.x - st1.x) * (ed2.y - st2.y) - (ed1.y - st1.y) * (ed2.x - st2.x);
}
typev dmul(point st1, point ed1, point st2, point ed2){
return (ed1.x - st1.x) * (ed2.x - st2.x) + (ed1.y - st1.y) * (ed2.y - st2.y);
}
struct poly{
static const int N = 50005;
point ps[N+5];
int pn;
poly() { pn = 0; }
void push(point tp){
ps[pn++] = tp;
}
int trim(int k){
return (k+pn)%pn;
}
void clear(){ pn = 0; }
};
poly graham(point* ps, int n){
sort(ps, ps + n, cmp);
poly ans;
if(n <= 2){
for(int i = 0; i < n; i++){
ans.push(ps[i]);
}
return ans;
}
ans.push(ps[0]);
ans.push(ps[1]);
point* tps = ans.ps;
int top = -1;
tps[++top] = ps[0];
tps[++top] = ps[1];
for(int i = 2; i < n; i++){
while(top > 0 && xmul(tps[top - 1], tps[top], tps[top - 1], ps[i]) <= 0) top--;
tps[++top] = ps[i];
}
int tmp = top;
for(int i = n - 2; i >= 0; i--){
while(top > tmp && xmul(tps[top - 1], tps[top], tps[top - 1], ps[i]) <= 0) top--;
tps[++top] = ps[i];
}
ans.pn = top;
return ans;
}
point getRoot(point p, point st, point ed){
point ans;
double u=((ed.x-st.x)*(ed.x-st.x)+(ed.y-st.y)*(ed.y-st.y));
u = ((ed.x-st.x)*(ed.x-p.x)+(ed.y-st.y)*(ed.y-p.y))/u;
ans.x = u*st.x+(1-u)*ed.x;
ans.y = u*st.y+(1-u)*ed.y;
return ans;
}
point change(point st, point ed, point next, double l){
point dd;
dd.x = -(ed - st).y;
dd.y = (ed - st).x;
double len = sqrt(dd.x * dd.x + dd.y * dd.y);
dd.x /= len, dd.y /= len;
dd.x *= l, dd.y *= l;
dd = dd + next;
return dd;
}
double getMinAreaRect(point* ps, int n, point* ds){
int cn, i;
double ans;
point* con;
poly tpoly = graham(ps, n);
con = tpoly.ps;
cn = tpoly.pn;
if(cn <= 2){
ds[0] = con[0]; ds[1] = con[1];
ds[2] = con[1]; ds[3] = con[0];
ans=0;
}else{
int l, r, u;
double tmp, len;
con[cn] = con[0];
ans = 1e40;
l = i = 0;
while(dmul(con[i], con[i+1], con[i], con[l])
>= dmul(con[i], con[i+1], con[i], con[(l-1+cn)%cn])){
l = (l-1+cn)%cn;
}
for(r=u=i = 0; i < cn; i++){
while(xmul(con[i], con[i+1], con[i], con[u])
<= xmul(con[i], con[i+1], con[i], con[(u+1)%cn])){
u = (u+1)%cn;
}
while(dmul(con[i], con[i+1], con[i], con[r])
<= dmul(con[i], con[i+1], con[i], con[(r+1)%cn])){
r = (r+1)%cn;
}
while(dmul(con[i], con[i+1], con[i], con[l])
>= dmul(con[i], con[i+1], con[i], con[(l+1)%cn])){
l = (l+1)%cn;
}
tmp = dmul(con[i], con[i+1], con[i], con[r]) - dmul(con[i], con[i+1], con[i], con[l]);
tmp *= xmul(con[i], con[i+1], con[i], con[u]);
tmp /= dist2(con[i], con[i+1]);
len = xmul(con[i], con[i+1], con[i], con[u])/dist(con[i], con[i+1]);
if(sign(tmp - ans) < 0){
ans = tmp;
ds[0] = getRoot(con[l], con[i], con[i+1]);
ds[1] = getRoot(con[r], con[i+1], con[i]);
ds[2] = change(con[i], con[i+1], ds[1], len);
ds[3] = change(con[i], con[i+1], ds[0], len);
}
}
}
return ans+eps;
}
int main ()
{
int t ,n ,i ,NN ,cas = 1;
point ds[10];
scanf("%d" ,&t);
while(t--)
{
scanf("%d" ,&NN);
int n = 0;
for(i = 1 ;i <= NN ;i ++)
{
for(int j = 1 ;j <= 4 ;j ++)
{
scanf("%lf %lf" ,&ps[n].x ,&ps[n].y);
n++;
}
}
double ans = getMinAreaRect(ps ,n ,ds);
printf("Case #%d:\n" ,cas ++);
printf("%.0lf\n" ,ans);
}
return 0;
}

78
HDOJ/5253_autoAC.cpp Normal file
View File

@ -0,0 +1,78 @@
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define N 1005
int father[N*N];
struct EDGE
{
int u,v;
int len;
bool operator<(const EDGE& a)const
{
return len<a.len;
}
};
EDGE edge[N*N*4];
int map[N][N],m,n,k;
int Find(int x)
{
return x==father[x]?x:father[x]=Find(father[x]);
}
bool Union(int x,int y)
{
int a,b;
a=Find(x);
b=Find(y);
if(a!=b)
{
father[a]=b;
return true;
}
return false;
}
int process()
{
int i;
int sum=0;
for(i=0;i<k;i++)
if(Union(edge[i].u,edge[i].v))
sum+=edge[i].len;
return sum;
}
int main()
{
int T,t=0,i,j;
EDGE e;
scanf("%d",&T);
t=0;
while(T--)
{
k=0;
scanf("%d%d",&m,&n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
father[i*n+j]=i*n+j;
scanf("%d",&map[i][j]);
if(j>0)
{
e.u=i*n+j;
e.v=i*n+j-1;
e.len=abs(map[i][j]-map[i][j-1]);
edge[k++]=e;
}
if(i>0)
{
e.u=i*n+j;
e.v=i*n+j-n;
e.len=abs(map[i][j]-map[i-1][j]);
edge[k++]=e;
}
}
sort(edge,edge+k);
printf ("Case #%d:\n", ++t);
printf ("%d\n", process());
}
return 0;
}

2
HDOJ/5254_autoAC.cpp Normal file
View File

@ -0,0 +1,2 @@
10
&amp;#8232;01&amp;#8232;

85
HDOJ/5255_autoAC.cpp Normal file
View File

@ -0,0 +1,85 @@
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstring>
#include<vector>
#include<cmath>
#include<string>
#define N 2000010
#define M 1000
using namespace std;
double n;
long long a,b,z,pp,qq,ans[N],P,p,q,o,k,tot,i,j,l;
long long gcd(long long a,long long b)
{
if (b==0) return a;
return gcd(b,a%b);
}
int check(long long p,long long q)
{
if (p==0) return 0;
if (p>=P) return 0;
long long a,b,c=1,pp;
a=p%10;
pp=p;
while (pp)
{
b=pp%10;
pp=pp/10;
c=c*10;
}
c=c/10;
p=p-a+b-b*c+a*c;
if (p==q) return 1;return 0;
}
int main()
{
int test,ii;
P=1000000;
P=P*10000;
scanf("%d",&test);
for (ii=1;ii<=test;ii++)
{
tot=0;
scanf("%lf",&n);
p=(n+0.0000005)*1000000;
q=p/gcd(p,1000000);
p=1000000/gcd(p,1000000);
o=abs(p-q);
b=10;a=0;
for (k=0;k<=8;k++)
{
for (i=0;i<=9;i++)
for (j=0;j<=9;j++)
{
z=b*i+a*10+j;
if (z%o==0)
{
z=z/o;
pp=p*z;qq=q*z;
if (check(pp,qq))
{
tot++;
ans[tot]=pp;
}
}
}
a=a*10+9;
b=b*10;
}
sort(ans+1,ans+1+tot);
j=0;ans[0]=-1;
for (i=1;i<=tot;i++)
if (ans[i]!=ans[i-1])
{
j++;ans[j]=ans[i];
}
tot=j;
printf("Case #%d:\n",ii);
printf("%I64d\n",tot);
for (i=1;i<=tot-1;i++)
printf("%I64d ",ans[i]);
if (tot)
printf("%I64d\n",ans[tot]);
}
}

29
HDOJ/5256_autoAC.cpp Normal file
View File

@ -0,0 +1,29 @@
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 100005;
int n, s[N], f[N];
int main() {
int t, cas = 1;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
int a;
for (int i = 0; i < n; i++) {
scanf("%d", &a);
s[i] = a - i;
}
int k = 0;
f[k++] = s[0];
for (int i = 1; i < n; i++) {
if (s[i] >= f[k - 1])
f[k++] = s[i];
else {
int pos = upper_bound(f, f + k, s[i]) - f;
f[pos] = s[i];
}
}
printf("Case #%d:\n%d\n", cas++, n - k);
}
return 0;
}

122
HDOJ/5258_autoAC.cpp Normal file
View File

@ -0,0 +1,122 @@
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 2000001
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
int g[50][50];
map<int,int> H1;
map<int,int> H2;
vector<int> x;
vector<int> y;
vector<int> kiss[50];
struct node
{
int x1,x2,y1,y2;
}a[100];
int main()
{
int t=read();
for(int cas=1;cas<=t;cas++)
{
memset(g,0,sizeof(g));
memset(a,0,sizeof(a));
H1.clear();
H2.clear();
x.clear();
y.clear();
for(int i=0;i<50;i++)
kiss[i].clear();
int n=read();
for(int i=0;i<n;i++)
{
a[i].x1=read(),a[i].y1=read(),a[i].x2=read(),a[i].y2=read();
if(a[i].x1>a[i].x2)
swap(a[i].x1,a[i].x2);
if(a[i].y1>a[i].y2)
swap(a[i].y1,a[i].y2);
x.push_back(a[i].x1);
x.push_back(a[i].x2);
y.push_back(a[i].y1);
y.push_back(a[i].y2);
}
sort(x.begin(),x.end());
sort(y.begin(),y.end());
x.erase(unique(x.begin(),x.end()),x.end());
y.erase(unique(y.begin(),y.end()),y.end());
for(int i=0;i<x.size();i++)
H1[x[i]]=i;
for(int i=0;i<y.size();i++)
H2[y[i]]=i;
for(int i=0;i<n;i++)
{
a[i].x1=H1[a[i].x1];
a[i].x2=H1[a[i].x2];
a[i].y1=H2[a[i].y1];
a[i].y2=H2[a[i].y2];
}
for(int i=0;i<n;i++)
if(a[i].x1==a[i].x2)
for(int j=a[i].y1;j<=a[i].y2;j++)
g[a[i].x1][j]=i+1;
for(int i=0;i<n;i++)
if(a[i].y1==a[i].y2)
for(int j=a[i].x1;j<=a[i].x2;j++)
if(g[j][a[i].y1]!=0)
kiss[g[j][a[i].y1]].push_back(i+1);
ll ans=0;
for(int i=0;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
int flag=0;
for(int k=0;k<kiss[i].size();k++)
{
for(int m=0;m<kiss[j].size();m++)
{
if(kiss[i][k]==kiss[j][m])
{
flag++;
break;
}
}
}
ans+=flag*(flag-1)/2;
}
}
printf("Case #%d:\n",cas);
cout<<ans<<endl;
}
}

76
HDOJ/5259_autoAC.cpp Normal file
View File

@ -0,0 +1,76 @@
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef long long LL;
const int MOD = 1e9 + 7;
const int INF = 10000000;
const int N = 5000 + 10;
struct State {
int a[4], b[4];
void read() {
for(int i = 0; i < 4; i++) {
scanf("%d%d", &a[i], &b[i]);
}
}
} s[N];
int seq[30][4], dp[N][30];
bool check(int b[], int q[]) {
for(int i = 1; i < 4; i++) {
if(b[q[i]] < b[q[i - 1]]) return false;
}
return true;
}
int calc(State& A, State& B, int q0[], int q1[]) {
int dis = 0;
for(int i = 0; i < 4; i++) {
dis += abs(A.a[q0[i]] - B.a[q1[i]]);
dis += abs(A.b[q0[i]] - B.b[q1[i]]);
}
return dis;
}
int main() {
int T, C = 1, n;
int A[4] = {0, 1, 2, 3};
int nq = 0;
do {
for(int i = 0; i < 4; i++) seq[nq][i] = A[i];
nq ++;
} while(next_permutation(A, A + 4));
scanf("%d", &T);
while(T--) {
printf("Case #%d:\n", C++);
scanf("%d" , &n);
for(int i = 1; i <= n; i++) {
s[i].read();
}
for(int i = 0; i < 4; i++)
s[0].b[i] = i + 1, s[0].a[i] = 0;
for(int i = 0; i < 24; i++) {
if(check(s[1].b, seq[i])) {
dp[1][i] = calc(s[0], s[1], seq[0], seq[i]);
} else {
dp[1][i] = INF;
}
}
for(int i = 2; i <= n; i++) {
for(int j = 0; j < 24; j++) {
dp[i][j] = INF;
if(!check(s[i].b, seq[j])) continue;
for(int k = 0; k < 24; k++) {
if(dp[i - 1][k] == INF) continue;
dp[i][j] = min(dp[i][j], dp[i - 1][k] + calc(s[i - 1], s[i], seq[k], seq[j]));
}
}
}
int ans = INF;
for(int i = 0; i < 24; i++) {
ans = min(ans, dp[n][i]);
}
printf("%d\n", ans);
}
return 0;
}

79
HDOJ/5260_autoAC.cpp Normal file
View File

@ -0,0 +1,79 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <bitset>
#include <complex>
#include <string>
#include <utility>
using namespace std;
typedef long long LL;
typedef pair<int, int> Pii;
typedef pair<LL, LL> Pll;
#define foreach(it,s) for(__typeof(s.begin()) it=s.begin();it!=s.end();it++)
const int maxn = 50 + 5;
const int maxe = 500 + 5;
int n, m, s, t;
struct Tedge {
int u, v;
string w;
Tedge(int u = 0, int v = 0, string w = ""):u(u), v(v), w(w){};
};
vector<Tedge> edge[maxn];
string dis[maxn];
bool vis[maxn];
string bf()
{
int clk = 0;
memset(vis, false, sizeof(vis));
dis[t] = ""; vis[t] = true;
while (1) {
bool update = false;
for (int ii=0;ii<n;ii++) {
foreach(i, edge[ii])
if (vis[i->v]) {
int u = i->u, v = i->v;
string tmp = i->w + dis[v];
if (!vis[u] || tmp < dis[u]) {
dis[u] = tmp;
update = vis[u] = true;
if (clk >= n - 1 && u == s) return "Tough way!";
}
}
}
if (!update || ++clk > n * 6) break;
}
return vis[s] ? dis[s]: "Tough way!";
}
int main()
{
int cas = 0;
int T;
scanf("%d", &T);
while (T--) {
scanf("%d%d%d%d", &n, &m, &s, &t);
for (int i=0;i<n;i++) edge[i].clear();
printf("Case #%d:\n", ++cas);
for (int i=0;i<m;i++) {
int u, v;
char st[8];
scanf("%d%d%s", &u, &v, st);
edge[u].push_back(Tedge(u, v, st));
}
puts(bf().c_str());
}
return 0;
}

129
HDOJ/5261_autoAC.cpp Normal file
View File

@ -0,0 +1,129 @@
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=200010;
int N;
int maxvid,maxvsum;
LL R;
int height[maxn];
struct IntervalTree
{
LL sum[maxn<<2];
int id[maxn<<2];
void build(int o,int l,int r)
{
if(l==r)
{
sum[o]=R*(l-1)+height[l];
id[o]=l;
return ;
}
int mid=(l+r)>>1;
build(o<<1,l,mid);
build(o<<1|1,mid+1,r);
pushup(o);
}
void pushup(int o)
{
if(sum[o<<1]<sum[o<<1|1])
{
sum[o]=sum[o<<1|1];
id[o]=id[o<<1|1];
}
else
{
sum[o]=sum[o<<1];
id[o]=id[o<<1];
}
}
void query(int o,int l,int r,int q1,int q2)
{
if(q1<=l&&r<=q2)
{
if(sum[o]>maxvsum)
{
maxvsum=sum[o];
maxvid=id[o];
}
return ;
}
int mid=(l+r)>>1;
if(q1<=mid)query(o<<1,l,mid,q1,q2);
if(q2>mid)query(o<<1|1,mid+1,r,q1,q2);
}
}tree;
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
scanf("%d%I64d",&N,&R);
for(int i=1;i<=N;i++)
scanf("%d",&height[i]);
for(int i=1;i<=N;i++)
height[i+N]=height[i];
tree.build(1,1,N*2);
LL anssum=0;
int x,y,p;
pair<int,int> ans=make_pair(N,N),tmp;
for(int i=1;i<=N*2;i++)
{
int r=i+N/2,l=i-N/2;
if(r>N*2||l<1)continue;
maxvid=maxvsum=0;
tree.query(1,1,N*2,i+1,r);
x=maxvid;
maxvid=maxvsum=0;
tree.query(1,1,N*2,l,i-1);
y=maxvid;
if((i-y)*R+height[i]+height[y]>anssum)
{
anssum=(i-y)*R+height[i]+height[y];
if(y>N)y-=N;
p=i;
if(p>N)p-=N;
tmp=make_pair(min(y,p),max(y,p));
ans=tmp;
}
else if((i-y)*R+height[i]+height[y]==anssum)
{
if(y>N)y-=N;
p=i;
if(p>N)p-=N;
tmp=make_pair(min(y,p),max(y,p));
if(tmp<ans)ans=tmp;
}
if((x-i)*R+height[x]+height[i]>anssum)
{
anssum=(x-i)*R+height[x]+height[i];
if(x>N)x-=N;
p=i;
if(p>N)p-=N;
tmp=make_pair(min(x,p),max(x,p));
ans=tmp;
}
else if((x-i)*R+height[x]+height[i]==anssum)
{
if(x>N)x-=N;
p=i;
if(p>N)p-=N;
tmp=make_pair(min(x,p),max(x,p));
if(tmp<ans)ans=tmp;
}
}
printf("Case #%d:\n",cas++);
printf("%d %d\n",ans.first,ans.second);
}
return 0;
}

59
HDOJ/5262_autoAC.cpp Normal file
View File

@ -0,0 +1,59 @@
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
using namespace std;
typedef long long LL;
const int maxn=100010;
const int MOD=1e9+7;
char str[maxn];
int dp[maxn];
int pos[30];
int nxt[maxn][30];
LL cnt[maxn];
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
scanf("%s",str);
int N=strlen(str);
for(int i=0;i<26;i++)pos[i]=N+1;
for(int i=N-1;i>=0;i--)
{
pos[str[i]-'a']=i+1;
for(int j=0;j<26;j++)
nxt[i][j]=pos[j];
}
dp[N+1]=0,dp[N]=1;
for(int i=N-1;i>=0;i--)
{
int minv=N+1;
for(int j=0;j<26;j++)
minv=min(minv,dp[nxt[i][j]]+1);
dp[i]=minv;
}
memset(cnt,0,sizeof(cnt));
cnt[N]=26,cnt[N+1]=1;
for(int i=N-1;i>=0;i--)
{
for(int j=0;j<26;j++)
if(dp[i]==dp[nxt[i][j]]+1)
{
cnt[i]+=cnt[nxt[i][j]];
if(cnt[i]>=MOD)cnt[i]-=MOD;
}
}
printf("Case #%d:\n%d %I64d\n",cas++,dp[0],cnt[0]);
}
return 0;
}

28
HDOJ/5264_autoAC.cpp Normal file
View File

@ -0,0 +1,28 @@
#include<cstdio>
#include<cstring>
#include<vector>
#include<iostream>
#include<queue>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
const int maxn = 1005;
int T, n, m;
string a, b, s;
int main()
{
scanf("%d", &T);
while (T--)
{
cin >> s;
b = a = "";
for (int i = 0; s[i]; i++)
{
if (i & 1) b = s[i] + b;
else a = a + s[i];
}
cout << a << endl;
cout << b << endl;
}
}

46
HDOJ/5265_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn=100000+5;
int data[maxn];
int main()
{
int m,p;
while(scanf("%d%d",&m,&p)!=-1)
{
for(int i=0; i<m; i++)
{
scanf("%d",&data[i]);
data[i]%=p;
}
sort(data,data+m);
if(p-data[m-1]>data[m-2])
printf("%d\n",data[m-1]+data[m-2]);
else
{
int now=0;
int ans=data[m-1]+data[m-2]-p;
for(int i=m-1; i>=1; i--)
{
for(int j=now; j<i; j++)
{
if(p-data[i]>data[j])
ans=max(data[i]+data[j],ans);
else
{
if(j>0)
now=j-1;
break;
}
if(ans==p-1)
break;
}
if(ans==p-1)
break;
}
printf("%d\n",ans);
}
}
return 0;
}

20
HDOJ/5268_autoAC.cpp Normal file
View File

@ -0,0 +1,20 @@
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int T,a,b;
cin>>T;
for(int k=1; k<=T; k++)
{
int sum,ans=0;
for(int i=2; i<=5; i++)
{
cin>>a>>b;
sum=2*i*(250-a)-50*b;
ans+=max(sum,(int)(0.4*i*500));
}
cout<<"Case #"<<k<<": "<<ans<<endl;
}
return 0;
}

88
HDOJ/5269_autoAC.cpp Normal file
View File

@ -0,0 +1,88 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <stack>
#include <sstream>
#include <cmath>
#include <queue>
#include <string>
#include <vector>
#define zeros(a) memset(a,0,sizeof(a))
#define ones(a) memset(a,-1,sizeof(a))
#define sc(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d%d",&a,&b)
#define rep(i,n) for(int i=0;i<n;i++)
#define lson (step<<1)
#define rson (lson+1)
#define esp 1e-6
#define oo 0x3fffffff
using namespace std;
typedef long long ll;
int n;
const int mod=998244353;
const int maxn=50000+10;
int bit[31];
ll ans;
struct trie
{
int next[30*maxn][2];
int tot[maxn*30];
int cnt,root;
int newNode()
{
tot[cnt]=0;
next[cnt][0]=next[cnt][1]=-1;
cnt++;
return cnt-1;
}
void init()
{
cnt=0;
root=newNode();
}
void Insert(int val)
{
int temp=root;
int id;
for(int i=0;i<30;i++)
{
if(val&bit[i])
id=1;
else
id=0;
if(next[temp][id^1]!=-1)
ans=(ans+((ll)bit[i]*tot[next[temp][id^1]])%mod)%mod;
if(next[temp][id]==-1)
next[temp][id]=newNode();
temp=next[temp][id];
tot[temp]++;
}
}
}tree;
int main()
{
int T_T;
scanf("%d",&T_T);
bit[0]=1;
for(int i=1;i<=30;i++)
{
bit[i]=bit[i-1]<<1;
}
for(int kase=1;kase<=T_T;kase++)
{
scanf("%d",&n);
int val;
ans=0;
tree.init();
for(int i=1;i<=n;i++)
{
sc(val);
tree.Insert(val);
}
ans=(ans<<1)%mod;
printf("Case #%d: %I64d\n",kase,ans);
}
return 0;
}

66
HDOJ/5270_autoAC.cpp Normal file
View File

@ -0,0 +1,66 @@
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#define cl(a,b) memset(a,b,sizeof(a))
#define ll long long
#define ull unsigned long long
using namespace std;
#define maxn 200005
ull aa[maxn],bb[maxn];
ull taa[maxn],tbb[maxn];
ull cc[64],be[64],en[64];
int n;
void initcc()
{
cl(cc,0);
cc[0]=1;
be[0]=1;
en[0]=2;
for(int i=1;i<=62;i++)
{
cc[i]=cc[i-1]*2+1;
be[i]=be[i-1]*2;
en[i]=en[i-1]*2;
}
}
int main()
{
initcc();
int tt,ii;
cin>>tt;
ii=0;
while(tt--)
{
scanf("%d",&n);
int i,j,k;
for(i=0;i<n;i++)scanf("%llu",&aa[i]);
for(i=0;i<n;i++)scanf("%llu",&bb[i]);
ull res=0,bitc;
int s1,s2,e1,e2;
for(k=0;k<62;k++)
{
for(i=0;i<n;i++)
{
taa[i]=aa[i]&cc[k];
tbb[i]=bb[i]&cc[k];
}
sort(taa,taa+n);
sort(tbb,tbb+n);
s1=n-1;e1=n-1;bitc=0;
s2=n-1;e2=n-1;
for(i=0;i<n;i++)
{
while(s1>=0&&tbb[s1]+taa[i]>=be[k])s1--;
while(e1>=0&&tbb[e1]+taa[i]>=en[k])e1--;
while(s2>=0&&tbb[s2]+taa[i]>=be[k]+en[k])s2--;
while(e2>=0&&tbb[e2]+taa[i]>=en[k+1])e2--;
bitc+=e1-s1;
bitc+=e2-s2;
}
res+=be[k]*(bitc%2);
}
printf("Case #%d: %llu\n",++ii,res);
}
return 0;
}

55
HDOJ/5272_autoAC.cpp Normal file
View File

@ -0,0 +1,55 @@
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 2000001
#define mod 10007
#define eps 1e-5
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int main()
{
int t=read();
while(t--){
ll n=read();
int ans=0;
int flag=1;
while(n)
{
if(flag)
{
if(n&1)
{
ans++;
flag=0;
}
}
else if(!(n&1))
{
flag=1;
}
n>>=1;
}
cout<<ans<<endl;
}
}

28
HDOJ/5273_autoAC.cpp Normal file
View File

@ -0,0 +1,28 @@
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
const int N = 1005;
#define LL __int64
int n,q,a[N],l,r,dp[N][N];
int main()
{
while(scanf("%d%d",&n,&q)>0){
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
memset(dp,0,sizeof(dp));
for(int k=1; k<n; k++)
for(int i=1; i+k<=n; i++){
int j=i+k;
dp[i][j]=dp[i][j-1]+dp[i+1][j]-dp[i+1][j-1];
if(a[i]>a[j])
dp[i][j]++;
}
while(q--){
scanf("%d%d",&l,&r);
printf("%d\n",dp[l][r]);
}
}
return 0;
}

130
HDOJ/5274_autoAC.cpp Normal file
View File

@ -0,0 +1,130 @@
#pragma comment(linker,"/STACK:1024000000,1024000000")
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<map>
using namespace std;
const int N = 100005;
#define LL __int64
struct EDG{
int to,next;
}edg[N*2];
int eid,head[N];
int fath[N],deep[N],top[N],num[N],son[N],p[N],pos;
void init(){
pos=0; eid=0;
memset(head,-1,sizeof(head));
}
void addEdg(int u,int v){
edg[eid].to=v; edg[eid].next=head[u]; head[u]=eid++;
edg[eid].to=u; edg[eid].next=head[v]; head[v]=eid++;
}
void dfs(int u,int pre,int d){
fath[u]=pre; num[u]=1; deep[u]=d; son[u]=-1;
for(int i=head[u]; i!=-1; i=edg[i].next){
int v=edg[i].to;
if(v==pre)continue;
dfs(v,u,d+1);
num[u]+=num[v];
if(son[u]==-1||num[son[u]]<num[v])
son[u]=v;
}
}
void getpos(int u,int root){
top[u]=root;
p[u]=++pos;
if(son[u]==-1)
return ;
getpos(son[u],root);
for(int i=head[u]; i!=-1; i=edg[i].next){
int v=edg[i].to;
if(son[u]==v||v==fath[u])
continue;
getpos(v,v);
}
}
int root[N*3];
int a[N];
void build(int l,int r,int k){
if(l==r){
root[k]=a[l];
return ;
}
int m=(l+r)>>1;
build(l,m,k<<1);
build(m+1,r,k<<1|1);
root[k]=root[k<<1]^root[k<<1|1];
}
void updata(int l,int r,int k,int id,int vule){
if(l==r){
root[k]=vule;
return ;
}
int m=(l+r)>>1;
if(id<=m)
updata(l,m,k<<1,id,vule);
else
updata(m+1,r,k<<1|1,id,vule);
root[k]=root[k<<1]^root[k<<1|1];
}
int query(int l,int r,int k,int L,int R){
if(L<=l&&r<=R){
return root[k];
}
int m=(l+r)>>1 , ans=0;
if(L<=m)
ans^= query(l,m,k<<1,L,R);
if(m<R)
ans^=query(m+1,r,k<<1|1,L,R);
return ans;
}
void swp(int &aa,int &bb){
int tt=aa; aa=bb; bb=tt;
}
int Operat(int u,int v){
int fu=top[u] , fv=top[v],ans=0;
while(fu!=fv){
if(deep[fu]<deep[fv]){
swp(fu,fv); swp(u,v);
}
ans^= query(1,pos,1,p[fu],p[u]);
u=fath[fu]; fu=top[u];
}
if(deep[u]>deep[v])
swp(u,v);
ans^=query(1,pos,1,p[u],p[v]);
return ans;
}
int main()
{
int T,n,q,val[N],u,v;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&q);
init();
for(int i=1; i<n; i++)
{
scanf("%d%d",&u,&v);
addEdg(u,v);
}
for(int i=1; i<=n; i++){
scanf("%d",&val[i]);
val[i]++;
}
dfs(1,1,1);
getpos(1,1);
for(int i=1; i<=n; i++)
a[p[i]]=val[i];
build(1,pos,1);
while(q--){
int op,x,y;
scanf("%d%d%d",&op,&x,&y);
if(op==0)
updata(1,pos,1,p[x],y+1);
else{
printf("%d\n",Operat(x,y)-1);
}
}
}
return 0;
}

54
HDOJ/5275_autoAC.cpp Normal file
View File

@ -0,0 +1,54 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <bitset>
#include <complex>
#include <string>
#include <utility>
using namespace std;
typedef long long Int;
const int M=1e9+7;
int rev[250002];
int dp[3002][3002];
int x[3002],y[3002];
inline int getrev(int x){return x<0?-rev[-x]:rev[x];}
int main()
{
rev[1]=1;
for(int i=2;i<=250000;i++)rev[i]=(M-M/i)*(Int)rev[M%i]%M;
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)scanf("%d%d",x+i,y+i);
for(int i=1;i<=n;i++)dp[0][i]=y[i];
for(int i=1;i<=n;i++)
for(int j=1;j+i<=n;j++)
dp[i][j]=((dp[i-1][j+1]-dp[i-1][j])*(Int)getrev(x[i+j]-x[j])%M+M)%M;
int m;scanf("%d",&m);
while(m--)
{
int l,r,q;scanf("%d%d%d",&l,&r,&q);
int ans=0,cur=1;
for(int i=0;i<=r-l;i++)
{
ans+=dp[i][l]*(Int)cur%M;
if(ans>=M)ans-=M;
cur=(cur*(Int)(q-x[l+i])%M+M)%M;
}printf("%d\n",ans);
}
}
}

32
HDOJ/5276_autoAC.cpp Normal file
View File

@ -0,0 +1,32 @@
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
int h=0,m=0,s=0;
for (int i = 0; i < 12 * 60 * 60; i = i + 10)
{
int time1 = (i * 1200) % 4320000;
int time2 = (i * 100) % 4320000;
int time;
if (time1 > time2)
time = time1 - time2;
else
time = time2 - time1;
if (time > 2160000)
time = 4320000 - time;
if (time == n)
{
int h = i / (60 * 60);
int m = (i - h * (60 * 60)) / 60;
int s = (i % 60) - (i % 60) % 10;
printf("%02d:%02d:%02d\n", h, m, s);
}
}
}
return 0;
}

91
HDOJ/5277_autoAC.cpp Normal file
View File

@ -0,0 +1,91 @@
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 1005
#define mod 10007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
struct node
{
int x,y;
};
node a[maxn];
int g[maxn][maxn];
vector<int> G[maxn];
int one,two,three,four;
int n,m;
void init()
{
one=two=three=four=0;
memset(a,0,sizeof(a));
memset(g,0,sizeof(g));
for(int i=0;i<n;i++)
G[i].clear();
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
for(int i=0;i<n;i++)
a[i].x=read(),a[i].y=read();
for(int i=1;i<=m;i++)
{
int x=read(),y=read();
x--,y--;
g[x][y]=g[y][x]=1;
G[x].push_back(y);
G[y].push_back(x);
}
for(int i=0;i<n;i++)
{
for(int j=0;j<G[i].size();j++)
{
for(int k=0;k<G[i].size();k++)
{
if(G[i][j]!=G[i][k]&&g[G[i][j]][G[i][k]])
{
three++;
for(int t=0;t<G[i].size();t++)
{
if(G[i][j]!=G[i][k]&&G[i][j]!=G[i][t]&&G[i][k]!=G[i][t]&&g[G[i][j]][G[i][k]]&&g[G[i][j]][G[i][t]]&&g[G[i][k]][G[i][t]])
{
four++;
}
}
}
}
}
}
if(four)
cout<<"4 "<<four/24<<endl;
else if(three)
cout<<"3 "<<three/6<<endl;
else if(m)
cout<<"2 "<<m<<endl;
else
cout<<"1 "<<n<<endl;
}
}

82
HDOJ/5278_autoAC.cpp Normal file
View File

@ -0,0 +1,82 @@
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
using namespace std;
typedef long long ll;
const ll mod=998244353;
bool a[1000][1000];
int n,m,au[1000][9],p[1000][1000],ee,fa[900],num[900];
ll two[1000];
struct edge {
int x,y,next;
}e[8888888];
void addedge(int x,int y,int u,int v) {
if(x==y) return;
if(x>y) {x=x^y;y=x^y;x=x^y;}
e[ee].x=u;e[ee].y=v;e[ee].next=p[x][y];p[x][y]=ee++;
}
void init() {
int i,j,k,x,y;
queue<int> qx,qy;ee=0;
for(i=1;i<=n;i++) {fa[i]=i;num[i]=1;}
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
for(k=1;k<=m;k++)
addedge(au[i][k],au[j][k],i,j);
for(i=1;i<=n;i++) {
a[0][i]=true;qx.push(0);qy.push(i);
}
while (!qx.empty()) {
i=qx.front();j=qy.front();qx.pop();qy.pop();
k=p[i][j];
while(k!=-1) {
x=e[k].x;y=e[k].y;
if(a[x][y]) {k=e[k].next;continue;}
else {
a[x][y]=true;
qx.push(e[k].x);qy.push(e[k].y);
}
}
}
}
int find_fa(int u) {
if(u==fa[u]) return u;
return fa[u]=find_fa(fa[u]);
}
ll cal () {
int i,j;ll ans=two[n]-1;
for(i=1;i<n;i++) for(j=i+1;j<=n;j++) {
if(!a[i][j]&&find_fa(i)!=find_fa(j)) {
num[find_fa(i)]+=num[find_fa(j)];
fa[fa[j]]=fa[i];
}
}
for(i=1;i<=n;i++) {
if(i==fa[i]) {
ans=(ans+mod-two[num[i]]+1)%mod;
}
}
return ans;
}
int main ()
{
int i,j;
two[0]=1;for(i=1;i<=900;i++) two[i]=2*two[i-1]%mod;
while(~scanf("%d%d",&n,&m)) {
for(i=1;i<=n;i++) for(j=1;j<=m;j++) scanf("%d",&au[i][j]);
memset(a,false,sizeof(a));
for(i=0;i<n;i++) for(j=i+1;j<=n;j++) p[i][j]=-1;
init();
printf("%I64d\n",cal());
}
}

108
HDOJ/5279_autoAC.cpp Normal file
View File

@ -0,0 +1,108 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std ;
typedef long long LL ;
#define clr(a,x); memset ( a , x , sizeof a ) ;
const int MAXN = 1 << 17 ;
const int mod = 998244353 ;
const int g = 3 ;
LL f[MAXN] , invf[MAXN] ;
LL tcnt[MAXN] ;
LL dp[MAXN] , dp2[MAXN] ;
LL x1[MAXN] , x2[MAXN] , x3[MAXN] ;
int n ;
LL power ( LL a , int b ) {
LL res = 1 , tmp = a ;
while ( b ) {
if ( b & 1 ) res = res * tmp % mod ;
tmp = tmp * tmp % mod ;
b >>= 1 ;
}
return res ;
}
void NTT ( LL y[] , int n , int rev ) {
for ( int i = 1 , j , k , t ; i < n ; ++ i ) {
for ( j = 0 , k = n >> 1 , t = i ; k ; k >>= 1 , t >>= 1 ) {
j = j << 1 | ( t & 1 ) ;
}
if ( i < j ) swap ( y[i] , y[j] ) ;
}
for ( int s = 2 , ds = 1 ; s <= n ; ds = s , s <<= 1 ) {
LL wn = power ( g , ( mod - 1 ) / s ) ;
if ( !rev ) wn = power ( wn , mod - 2 ) ;
for ( int k = 0 ; k < n ; k += s ) {
LL w = 1 , t ;
for ( int i = k ; i < k + ds ; ++ i , w = w * wn % mod ) {
y[i + ds] = ( y[i] - ( t = y[i + ds] * w % mod ) + mod ) % mod ;
y[i] = ( y[i] + t ) % mod ;
}
}
}
if ( !rev ) {
LL invn = power ( n , mod - 2 ) ;
for ( int i = 0 ; i < n ; ++ i ) {
y[i] = y[i] * invn % mod ;
}
}
}
void cdq_fz ( int l , int r ) {
if ( l == r ) return ;
if ( l + 1 == r ) {
dp[r] = ( dp[r] + dp[l] ) % mod ;
return ;
}
int m = ( l + r ) >> 1 , n1 = 1 ;
cdq_fz ( l , m ) ;
while ( n1 <= r - l + 1 ) n1 <<= 1 ;
for ( int i = 0 ; i < n1 ; ++ i ) {
x1[i] = l + i <= m ? dp[l + i] * invf[l + i] % mod : 0 ;
x2[i] = l + i <= r ? tcnt[i + 1] * invf[i] % mod : 0 ;
x3[i] = l + i <= r ? tcnt[i + 2] * invf[i] % mod : 0 ;
}
NTT ( x1 , n1 , 1 ) ;
NTT ( x2 , n1 , 1 ) ;
NTT ( x3 , n1 , 1 ) ;
for ( int i = 0 ; i < n1 ; ++ i ) {
x3[i] = x1[i] * x3[i] % mod ;
x2[i] = x1[i] * x2[i] % mod ;
}
NTT ( x2 , n1 , 0 ) ;
NTT ( x3 , n1 , 0 ) ;
for ( int i = m + 1 ; i <= r ; ++ i ) {
dp[i] = ( dp[i] + f[i - 1] * x2[i - l - 1] % mod ) % mod ;
dp2[i] = ( dp2[i] + f[i - 2] * x3[i - l - 2] % mod ) % mod ;
}
cdq_fz ( m + 1 , r ) ;
}
void preprocess () {
f[0] = invf[0] = 1 ;
dp[0] = dp2[0] = dp2[1] = 1 ;
for ( int i = 1 ; i <= 100000 ; ++ i ) {
f[i] = f[i - 1] * i % mod ;
invf[i] = power ( f[i] , mod - 2 ) ;
if ( i >= 2 ) tcnt[i] = power ( i , i - 2 ) ;
}
tcnt[1] = 1 ;
cdq_fz ( 0 , 100000 ) ;
}
void solve () {
int x ;
scanf ( "%d" , &n ) ;
int ans1 = power ( 2 , n ) , ans2 = 1 ;
for ( int i = 1 ; i <= n ; ++ i ) {
scanf ( "%d" , &x ) ;
ans1 = ans1 * dp[x] % mod ;
if ( n > 2 ) ans2 = ans2 * dp2[x] % mod ;
}
printf ( "%d\n" , ( ans1 - ans2 + mod ) % mod ) ;
}
int main () {
int T ;
preprocess () ;
scanf ( "%d" , &T ) ;
for ( int i = 1 ; i <= T ; ++ i ) {
solve () ;
}
return 0 ;
}

50
HDOJ/5280_autoAC.cpp Normal file
View File

@ -0,0 +1,50 @@
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair <int, int> PII;
const int N = 1e5 + 7;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-6;
const double PI = acos(-1.0);
int a[N];
LL l[N], r[N];
int main(void){
int T;
scanf("%d", &T);
while (T--) {
int n, p;
scanf("%d%d", &n, &p);
for (int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
l[0] = r[0] = l[n + 1] = r[n + 1] = 0;
for (int i = 1; i <= n; ++i)
l[i] = max(l[i - 1] + a[i], (LL)a[i]);
for (int i = n; i; --i)
r[i] = max(r[i + 1] + a[i], (LL)a[i]);
LL ans = -INF;
for (int i = 1; i <= n; ++i) {
ans = max(ans, max(l[i - 1], 0LL) + max(r[i + 1], 0LL) + p);
}
for (int i = 1; i < n; ++i) {
ans = max(ans, l[i]);
}
for (int i = n; i > 1; --i) {
ans = max(ans, r[i]);
}
printf("%I64d\n", ans);
}
return 0;
}

49
HDOJ/5281_autoAC.cpp Normal file
View File

@ -0,0 +1,49 @@
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define maxn 100000 + 10
#define INF 0x7fffffff
long long mmap[maxn];
long long temp[maxn];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%lld",&mmap[i]);
}
for(int i=0;i<m;i++)
{
scanf("%lld",&temp[i]);
}
sort(mmap,mmap+n);
sort(temp,temp+m);
long long sum = 0;
long long ans = 0;
int p = 0;
for(int i=n-1;i>=0;i--)
{
if(p>=m) break;
if(mmap[i] >= temp[p])
{
ans += mmap[i] - temp[p];
sum = max(sum,ans);
p++;
}
else
{
p++;
i++;
}
}
printf("%lld\n",sum);
}
return 0;
}

62
HDOJ/5282_autoAC.cpp Normal file
View File

@ -0,0 +1,62 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define MAX_N 1100
char a[MAX_N],b[MAX_N];
int dp[MAX_N+1][MAX_N+1];
int dd[MAX_N+1][MAX_N+1];
int wei[MAX_N+1][26];
__int64 f[MAX_N+1][MAX_N+1];
int mod=1e9+7;
int main()
{
int n,m,tem,t;
scanf("%d",&t);
while(t--)
{
scanf("%s%s",a,b);
memset(dp,0,sizeof(dp));
n=strlen(a);
m=strlen(b);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(a[i]==b[j])
dp[i+1][j+1]=max(dp[i][j]+1,max(dp[i+1][j],dp[i][j+1]));
else
dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]);
}
}
memset(wei,0,sizeof wei);
for(int i=1;i<=m;i++)
{
for(int j=0;j<26;j++)
wei[i][j]=wei[i-1][j];
wei[i][b[i-1]-'a']=i;
}
memset(f,0,sizeof f);
for(int i=0;i<=n;i++)
{
for(int j=0;j<=m;j++)
{
if(dp[i][j]==0)
{
f[i][j]=1;
continue;
}
if(dp[i-1][j]==dp[i][j])
f[i][j]=(f[i][j]+f[i-1][j])%mod;
int p=wei[j][a[i-1]-'a'];
if(p)
{
if(dp[i-1][p-1]+1==dp[i][j])
f[i][j]=(f[i][j]+f[i-1][p-1])%mod;
}
}
}
printf("%I64d\n",f[n][m]%mod);
}
return 0;
}

144
HDOJ/5283_autoAC.cpp Normal file
View File

@ -0,0 +1,144 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
#include <limits>
using namespace std;
typedef long long LL;
const int MAXN=100000+100;
const LL LL_INF=numeric_limits<LL>::max();
#define lson L,mid,rt<<1
#define rson mid+1,R,rt<<1|1
long long X[4],Y[4];
long long tx[MAXN],ty[MAXN];
struct Tree {
long long maX[4],maY[4],addx,addy,sum[4];
}tr[MAXN<<2];
void pushup(int rt){
for(int i=0;i<4;i++){
tr[rt].maX[i]=max(tr[rt<<1].maX[i],tr[rt<<1|1].maX[i]);
tr[rt].maY[i]=max(tr[rt<<1].maY[i],tr[rt<<1|1].maY[i]);
tr[rt].sum[i]=tr[rt<<1].sum[i]+tr[rt<<1|1].sum[i];
}
}
void pushdown(int rt){
if(tr[rt].addx){
tr[rt<<1].addx+=tr[rt].addx;
tr[rt<<1|1].addx+=tr[rt].addx;
for(int i=0;i<4;i++){
tr[rt<<1].maX[i]+=tr[rt].addx;
tr[rt<<1|1].maX[i]+=tr[rt].addx;
}
tr[rt].addx=0;
}
if(tr[rt].addy){
tr[rt<<1].addy+=tr[rt].addy;
tr[rt<<1|1].addy+=tr[rt].addy;
for(int i=0;i<4;i++){
tr[rt<<1].maY[i]+=tr[rt].addy;
tr[rt<<1|1].maY[i]+=tr[rt].addy;
}
tr[rt].addy=0;
}
}
void build(int L,int R,int rt){
tr[rt].addy=tr[rt].addx=0;
if(L==R){
for(int i=0;i<4;i++){
tr[rt].maX[i]=tx[L];
tr[rt].maY[i]=ty[L];
tr[rt].sum[i]=1;
}
return ;
}
int mid=(L+R)>>1;
build(lson);
build(rson);
pushup(rt);
}
void update(int L,int R,int rt,int l,int r,long long v,int flag){
if(l<=L&&R<=r){
if(flag==0){
for(int i=0;i<4;i++) tr[rt].maX[i]+=v;
tr[rt].addx+=v;
}
else {
for(int i=0;i<4;i++) tr[rt].maY[i]+=v;
tr[rt].addy+=v;
}
return ;
}
pushdown(rt);
int mid=(L+R)>>1;
if(l<=mid)update(lson,l,r,v,flag);
if(r>mid)update(rson,l,r,v,flag);
pushup(rt);
}
void adjust(int L,int R,int rt){
int mid=(L+R)>>1;
for(int i=0;i<4;i++){
if(tr[rt].maX[i]>X[i]||tr[rt].maY[i]>Y[i]){
if(L==R){
tr[rt].maX[i]=LL_INF+1;
tr[rt].maY[i]=LL_INF+1;
tr[rt].sum[i]=0;
continue;
}
pushdown(rt);
adjust(lson);adjust(rson);
pushup(rt);
}
}
}
LL query(int L,int R,int rt,int l,int r,int f){
if(l<=L&&R<=r)return tr[rt].sum[f];
int mid=(L+R)>>1;
LL ans=0;
if(l<=mid)ans+=query(lson,l,r,f);
if(r>mid)ans+=query(rson,l,r,f);
return ans;
}
int main()
{
int n,m,t;
int xx1,xx2,yy1,yy2;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
scanf("%d%d%d%d",&xx1,&yy1,&xx2,&yy2);
X[0]=xx2;Y[0]=yy2;
X[1]=xx1-1;Y[1]=yy2;
X[2]=xx2;Y[2]=yy1-1;
X[3]=xx1-1;Y[3]=yy1-1;
for(int i=1;i<=n;i++){
scanf("%I64d%I64d",&tx[i],&ty[i]);
}
build(1,n,1);
int type,d,tl,tr;
scanf("%d",&m);
while(m--){
scanf("%d",&type);
if(type==1){
scanf("%d%d%d",&tl,&tr,&d);
update(1,n,1,tl,tr,(LL)d,0);
}
if(type==2){
scanf("%d%d%d",&tl,&tr,&d);
update(1,n,1,tl,tr,(LL)d,1);
}
if(type==3){
adjust(1,n,1);
scanf("%d%d",&tl,&tr);
LL ans=query(1,n,1,tl,tr,0)-query(1,n,1,tl,tr,1)-query(1,n,1,tl,tr,2)+query(1,n,1,tl,tr,3);
printf("%I64d\n",ans);
}
}
}
return 0;
}

51
HDOJ/5284_autoAC.cpp Normal file
View File

@ -0,0 +1,51 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 3145750;
const int inf = 1000000000;
char s[N];
int main()
{
int t,i;
scanf("%d",&t);
while(t--)
{
scanf("%s",s);
for(i=0;s[i+1]!='\0';i++)
if(s[i]=='w'||s[i]=='v'&&s[i+1]=='v')
break;
if(s[i+1]=='\0')
{
puts("No");
continue;
}
for(;s[i]!='\0';i++)
if(s[i]=='y')
break;
if(s[i]=='\0')
{
puts("No");
continue;
}
for(;s[i]!='\0';i++)
if(s[i]=='h')
break;
if(s[i]=='\0')
puts("No");
else
puts("Yes");
}
return 0;
}

91
HDOJ/5285_autoAC.cpp Normal file
View File

@ -0,0 +1,91 @@
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
using namespace std;
int vis[100005];
vector <int> v[100005];
int bfs(int u)
{
queue <int> q;
q.push(u);
vis[u] = 0;
int res[2] = {1, 0};
while(!q.empty())
{
int tmp = q.front();
q.pop();
int len = v[tmp].size();
for(int i = 0; i < len; i++)
{
if(vis[v[tmp][i]] == -1)
{
vis[v[tmp][i]] = !vis[tmp];
q.push(v[tmp][i]);
res[vis[v[tmp][i]]]++;
}
else if(vis[v[tmp][i]] == vis[tmp])
return -1;
}
}
return max(res[0], res[1]);
}
int main()
{
int T;
while(~scanf("%d", &T))
{
while(T--)
{
int n, m;
for(int i = 0; i < 100005; i++)
v[i].clear();
memset(vis, -1, sizeof vis);
scanf("%d%d", &n, &m);
for(int i = 0; i < m; i++)
{
int a, b;
scanf("%d%d", &a, &b);
v[a].push_back(b);
v[b].push_back(a);
}
int ans = 0;
for(int j = 1; j <= n; j++)
{
if(vis[j] == -1)
{
int tmp = bfs(j);
if(tmp == -1)
{
ans = -1;
break;
}
else
{
ans += tmp;
}
}
}
if(ans == -1 || (ans == n && n < 2))
{
puts("Poor wyh");
}
else if(ans != n)
printf("%d %d\n", ans, n - ans);
else
printf("%d %d\n", ans - 1, n - ans + 1);
}
}
return 0;
}

176
HDOJ/5286_autoAC.cpp Normal file
View File

@ -0,0 +1,176 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>
#include<map>
using namespace std;
const int maxn=51005;
const int mod=1000000007;
struct pi{
int sum;
int lson;
int rson;
}pp[maxn*17];
int root[maxn],tot;
void build(int cnt,int l,int r){
pp[cnt].sum=0;
if(l==r) return;
pp[cnt].lson=tot+1;
tot++;
build(tot,l,(l+r)/2);
pp[cnt].rson=tot+1;
tot++;
build(tot,(l+r)/2+1,r);
}
void merg(int qq,int cnt,int n,int p,int k){
int le,ri,mid;
le=1;
ri=n;
while(le<=ri){
mid=(le+ri)/2;
pp[cnt]=pp[qq];
pp[cnt].sum+=k;
if (le==ri) break;
if(p<=mid){
pp[cnt].lson=tot+1;
tot++;
ri=mid;
cnt=tot;
qq=pp[qq].lson;
}
else{
pp[cnt].rson=tot+1;
tot++;
le=mid+1;
cnt=tot;
qq=pp[qq].rson;
}
}
}
int query(int cnt,int le,int ri,int l,int r){
int s=0;
int mid;
if(le>=l&&ri<=r){
return pp[cnt].sum;
}
mid=(le+ri)/2;
if(l<=mid) s+=query(pp[cnt].lson,le,mid,l,r);
if(r>mid) s+=query(pp[cnt].rson,mid+1,ri,l,r);
return s;
}
int a[maxn],b[maxn],c[maxn];
int l[maxn],vis[maxn],r[505],le[505];
int g[315][315];
vector<int>gg[maxn],g1;
int d[maxn][300];
int main()
{
int i,j,n,m,t;
cin>>t;
while(t--){
scanf("%d%d",&n,&m);
memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
b[i]=a[i];
gg[i].clear();
}
sort(b+1,b+1+n);
memset(d,0,sizeof(d));
for(i=1;i<=n;i++){ a[i]=lower_bound(b+1,b+1+n,a[i])-b;
vis[a[i]]++;
}
for(i=1;i<=n;i++){
long long q=1;
gg[i].push_back(0);
for(j=1;j<=vis[i];j++){
q=q*b[i];
q%=mod;
gg[i].push_back(q);
}
}
int p=sqrt(n),f=0;
for(i=1;i<=n;i++){
f++;
int q=min(i+p-1,n);
r[f]=q;
le[f]=i;
for(j=1;j<=n;j++){
d[j][f]=d[j][f-1];
}
for(j=i;j<=q;j++){
l[j]=f;
d[a[j]][f]++;
}
i=q;
}
memset(g,0,sizeof(g));
for(i=1;i<=n;i++){
memset(vis,0,sizeof(vis));
for(j=i;j<=n;j++){
vis[a[j]]++;
if(j==i||l[j]!=l[j-1]){
g[l[i]][l[j]]=(g[l[i]][l[j]-1]+gg[a[j]][vis[a[j]]])%mod-gg[a[j]][vis[a[j]]-1];
g[l[i]][l[j]]%=mod;
}
else{
g[l[i]][l[j]]+=(gg[a[j]][vis[a[j]]]-gg[a[j]][vis[a[j]]-1])%mod;
g[l[i]][l[j]]%=mod;
}
}
i+=p-1;
}
memset(vis,0,sizeof(vis));
int la=0;
for(i=0;i<m;i++){
int x,y,ll,rr;
scanf("%d%d",&x,&y);
ll=x;
rr=y;
x=min((ll^la)%n+1,(rr^la)%n+1);
y=max((ll^la)%n+1,(rr^la)%n+1);
g1.clear();
if(l[y]-l[x]<=1){
for(j=x;j<=y;j++){
if(vis[a[j]]==0) g1.push_back(a[j]);
vis[a[j]]++;
}
int s=0;
int p=g1.size();
for(j=0;j<p;j++){
s+=gg[g1[j]][vis[g1[j]]];
vis[g1[j]]=0;
s%=mod;
}
s=(s+mod)%mod;
printf("%d\n",s);
la=s;
}
else{
int s=0;
s=g[l[x]+1][l[y]-1];
for(j=x;j<=r[l[x]];j++){
if(!vis[a[j]]) g1.push_back(a[j]);
vis[a[j]]++;
}
for(j=le[l[y]];j<=y;j++){
if(!vis[a[j]]) g1.push_back(a[j]);
vis[a[j]]++;
}
int p=g1.size();
for(j=0;j<p;j++){
int v=g1[j];
int q=d[v][l[y]-1]-d[v][l[x]];
s+=(gg[v][q+vis[v]]-gg[v][q])%mod;
s%=mod;
vis[v]=0;
}
s=(s%mod+mod)%mod;
printf("%d\n",s);
la=s;
}
}
}
}

124
HDOJ/5288_autoAC.cpp Normal file
View File

@ -0,0 +1,124 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <set>
using namespace std;
int num[100010];
int zuo[100010];
int you[100010];
int has[10010];
int Scan()
{
int res = 0, ch, flag = 0;
if((ch = getchar()) == '-')
flag = 1;
else if(ch >= '0' && ch <= '9')
res = ch - '0';
while((ch = getchar()) >= '0' && ch <= '9' )
res = res * 10 + ch - '0';
return flag ? -res : res;
}
vector<int>my[10010];
int main()
{
int n;
for(int i=1;i<=10000;i++)
{
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
my[i].push_back(j);
}
}
}
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
{
num[i]=Scan();
}
memset(zuo,-1,sizeof zuo);
memset(you,-1,sizeof you);
/*
memset(has,0,sizeof has);
for(int i=1;i<=n;i++)
{
if(has[num[i]]!=0)
zuo[i]=has[num[i]]+1;
has[num[i]]=i;
}
memset(has,0,sizeof has);
for(int i=n;i>=1;i--)
{
if(has[num[i]]!=0)
you[i]=has[num[i]]-1;
has[num[i]]=i;
}
*/
memset(has,0,sizeof has);
for(int i=1;i<=n;i++)
{
for(int j=0;j<my[num[i]].size();j++)
{
int tt=my[num[i]][j];
if(has[tt])
{
if(num[i]%tt==0)
{
if(zuo[i]!=-1)
zuo[i]=max(zuo[i],has[tt]+1);
else
zuo[i]=has[tt]+1;
}
}
}
has[num[i]]=i;
}
memset(has,0,sizeof has);
for(int i=n;i>=1;i--)
{
for(int j=0;j<my[num[i]].size();j++)
{
int tt=my[num[i]][j];
if(has[tt])
{
if(num[i]%tt==0)
{
if(you[i]!=-1)
you[i]=min(you[i],has[tt]-1);
else
you[i]=has[tt]-1;
}
}
}
has[num[i]]=i;
}
for(int i=1;i<=n;i++)
{
if(zuo[i]==-1)
zuo[i]=1;
if(you[i]==-1)
you[i]=n;
}
__int64 ans=0;
for(int i=1;i<=n;i++)
{
__int64 l,r;
l=(__int64)(i-zuo[i]+1);
r=(__int64)(you[i]-i+1);
ans=(ans+l*r)%1000000007;
}
printf("%I64d\n",ans);
}
return 0;
}

55
HDOJ/5289_autoAC.cpp Normal file
View File

@ -0,0 +1,55 @@
#include <iostream>
#include <cstdio>
#include <cmath>
#include <set>
using namespace std;
int a[100005];
multiset<int> s;
int main()
{
int T;
scanf("%d", &T);
int n, k;
while(T--) {
s.clear();
scanf("%d %d", &n, &k);
for(int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
if(k == 0) {
printf("0\n");
continue;
}
s.insert(a[0]);
int l = 0, r = 1;
int cnt = 1;
long long ans = 0;
int minn, maxn;
while(1) {
if(s.size()) {
minn = *s.begin();
maxn = *s.rbegin();
if(abs(a[r]-minn) < k && abs(a[r]-maxn) < k) {
ans += (long long)s.size();
s.insert(a[r]);
r++;
if(r == n) break;
}
else {
if(s.size()) {
s.erase(s.find(a[l]));
}
l++;
}
}
else {
l = r;
s.insert(a[r]);
r++;
if(r == n) break;
}
}
printf("%I64d\n", ans+n);
}
return 0;
}

172
HDOJ/5290_autoAC.cpp Normal file
View File

@ -0,0 +1,172 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <bitset>
#include <complex>
#include <string>
#include <utility>
#include <assert.h>
using namespace std;
typedef long long ll;
#define prt(k) cout<<#k" = "<<k<<endl;
const int N = 100007;
const int W = 107;
int f[N][107], g[N][107];
int father[N];
int n;
int w[N];
int F[N][107], G[N][107];
int maxw;
struct edge
{
int v, next;
}e[N<<1];
int head[N], mm;
void add(int u, int v)
{
e[mm].v = v;
e[mm].next = head[u];
head[u] = mm++;
}
struct P
{
int d, id;
P() {}
P(int _id, int _d=0) { d=_d, id=_id; }
bool operator < (P b) const
{
return d > b.d;
}
}p[N];
bool vis[N];
void bfs()
{
queue<P> q;
q.push(P(1,0));
memset(vis,0,sizeof vis);
vis[1] = true;
father[1] = 1;
while (!q.empty()) {
P u = q.front(); q.pop();
int now = u.id;
p[now].id = now;
p[now].d = u.d + 1;
for (int i=head[now];~i;i=e[i].next) {
int v = e[i].v;
if (!vis[v]) {
q.push(P(v, u.d + 1));
vis[v] = true;
father[v] = now;
}
}
}
}
void gao(int u, int fa)
{
assert(!vis[u]);
vis[u] = true;
bool flag = false;
g[u][0] = 0;
for (int i=head[u];~i;i=e[i].next) {
int v = e[i].v;
if (v==fa) continue;
flag = true;
g[u][0] += f[v][0];
assert(vis[v]);
}
if (!flag) {
g[u][0] = 0;
f[u][w[u]] = 1;
}
for (int j=0;j<=maxw;j++) {
ll sum=0;
for (int i=head[u];~i;i=e[i].next) {
int v = e[i].v;
if (v==fa) continue;
int t = F[v][j+1];
if (j > 0)
t = min(t, G[v][j-1]);
sum += t;
if (sum >= n) { sum = n; break; }
}
if (sum < n ) for (int i=head[u];~i;i=e[i].next) {
int v = e[i].v;
if (v==fa) continue;
int t = F[v][j+1];
if (j > 0) t = min(t, G[v][j-1]);
if (0<=sum && sum < n) f[u][j] = min(f[u][j], (int)(f[v][j+1] + sum - t ) ) ;
}
sum = 0;
int t;
for (int i=head[u];~i;i=e[i].next) {
int v = e[i].v;
if (v==fa) continue;
int t = F[v][j];
if (j > 0) t = min(t, G[v][j-1]);
sum += t;
if (sum >= n) { sum = n; break; }
}
if (j>0 && sum < n) for (int i=head[u];~i;i=e[i].next) {
int v = e[i].v;
if (v==fa) continue;
int t = F[v][j];
if (j > 0) t = min(t, G[v][j-1]);
if (0<=sum && sum < n) g[u][j]=min(g[u][j], (int)(g[v][j-1] + sum - t ) );
}
}
ll sum = 0;
for (int i=head[u];~i;i=e[i].next) {
int v = e[i].v;
if (v==fa) continue;
sum += min(F[v][w[u]+1], w[u]>0 ? G[v][w[u]-1] : n);
}
if (0<=sum && sum<n) f[u][w[u]]=min(f[u][w[u]], int(1 + sum ));
for (int i=100;i>=0;i--) f[u][i] = min(f[u][i], f[u][i+1]);
for (int i=1;i<=100;i++) g[u][i] = min(g[u][i], g[u][i-1]);
F[u][0] = f[u][0];
G[u][0] = g[u][0];
for (int j=1;j<=maxw+1;j++) {
F[u][j] = min(F[u][j-1], f[u][j]);
G[u][j] = min(G[u][j-1], g[u][j]);
}
}
int main()
{
while (scanf("%d", &n)==1) {
maxw = 0;
for (int i=1;i<=n;i++) scanf("%d", w+i), maxw=max(maxw, w[i]);
mm=0; memset(head,-1,sizeof head);
for (int i=1;i<=n-1;i++)
{
int u, v; scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
memset(f, 63, sizeof f);
memset(g, 63, sizeof g);
bfs();
sort(p+1, p+n+1);
memset(vis, 0, sizeof vis);
for (int i=1;i<=n;i++) {
gao(p[i].id, father[p[i].id]);
}
int ans = n;
for (int i=0;i<=maxw;i++)
ans = min(ans, f[1][i]);
printf("%d\n", ans);
}
}

59
HDOJ/5291_autoAC.cpp Normal file
View File

@ -0,0 +1,59 @@
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
#define ll long long
int dp[90000],sum[2][90000];
int mod = 1000000007;
int modx = -mod;
int num[300];
int main(){
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int total = 0;
for(int i = 1; i <= n; i++){
scanf("%d",&num[i]);
total += num[i];
}
if(total & 1) total++;
memset(dp,0,sizeof(dp));
memset(sum,0,sizeof(sum));
dp[total] = 1;
int tt = total*2;
for(int i = 1; i <= n; i++){
sum[0][0] = dp[0];
sum[1][0] = 0;
for(int j = 1;j <= tt; j++){
sum[0][j] = sum[0][j-1];
sum[1][j] = sum[1][j-1];
sum[j&1][j] += dp[j];
sum[j&1][j] %= modx;
}
ll ans = 0;
for(int j = 0;j <= num[i]; j++){
ans += (ll)dp[j]*((num[i]-j)/2+1);
ans %= mod;
}
int p = (num[i]&1)^1;
int res = ans;
for(int j = 0;j <= tt; j++){
dp[j] = res;
int u = j-num[i]-1;
u = max(u,0);
res += (sum[p][j+1+num[i]] - sum[p][j])%mod;
res %= mod;
p ^= 1;
res -= (sum[p][j] - sum[p][u])%mod;
res %= mod;
}
}
int res = dp[total];
res %= mod;
res = (res+mod)%mod;
cout<<res<<endl;
}
return 0;
}

53
HDOJ/5292_autoAC.cpp Normal file
View File

@ -0,0 +1,53 @@
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <set>
#include <algorithm>
using namespace std;
#define ll long long
int main(){
int t;
cin>>t;
int cas=0;
while(t--){
cas++;
int ans=0;
for(int i=1;i<=24;i++){
char color[2];
scanf("%s",color);
if(color[0]=='w'||color[0]=='y'){
switch(i){
case 5:
case 7:
case 9:
case 12:
case 14:
case 16:
case 21:
case 24:
ans++;
break;
case 6:
case 8:
case 10:
case 11:
case 13:
case 15:
case 22:
case 23:
ans+=2;
break;
}
}
}
printf("Case #%d: ",cas);
if(ans%3==0){
printf("YES\n");
}else{
printf("NO\n");
}
}
return 0;
}

74
HDOJ/5293_autoAC.cpp Normal file
View File

@ -0,0 +1,74 @@
#pragma comment(linker,"/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<math.h>
#include<algorithm>
#define ll int
using namespace std;
const int maxn = 100000+10;
int t,n,m;
vector<int> g[maxn];
int fa[maxn][25],deep[maxn];
int cnt,l[maxn],r[maxn];
vector<int> lis[maxn];
void dfs(int u,int f,int d){
l[u]=++cnt,deep[u]=d,fa[u][0]=f;
for(int i=0;i<g[u].size();i++){
int v=g[u][i];
if(v==f) continue;
dfs(v,u,d+1);
}
r[u]=++cnt;
}
void init(){
cnt=0; dfs(1,0,1);
for(int i=1;i<=20;i++) for(int j=1;j<=n;j++) fa[j][i]=fa[fa[j][i-1]][i-1];
for(int i=1;i<=n;i++) lis[i].clear();
}
int lca(int x, int y) {
if (deep[x] < deep[y]) swap(x, y);
int delta = deep[x] - deep[y];
for(int j=0;j<=20;j++) if ((1<<j) & delta) x = fa[x][j];
if (x == y) return x;
for(int j=20;j>=0;j--) if (fa[x][j] != fa[y][j]) x = fa[x][j], y = fa[y][j];
return fa[x][0];
}
int st[maxn],en[maxn],quan[maxn];
int dp[maxn],sum[maxn],sd[maxn<<1],ss[maxn<<1];
int lowbit(int x) { return x&(-x); }
void Add(int id,int x,int c[]) { for(int i=id;i<=2*n;i+=lowbit(i)) c[i]+=x; }
int Sum(int id,int c[]) { int tmp=0; for(int i=id;i>0;i-=lowbit(i)) tmp+=c[i]; return tmp; }
void dfs2(int u,int f){
for(int i=0;i<g[u].size();i++){
int v=g[u][i];
if(v==f) continue;
dfs2(v,u);
sum[u]+=dp[v];
}
dp[u]=sum[u];
for(int i=0;i<lis[u].size();i++){
int id=lis[u][i],s=st[id],t=en[id],w=quan[id];
int tmp=Sum(l[s],ss)+Sum(l[t],ss)-Sum(l[s],sd)-Sum(l[t],sd)+sum[u];
dp[u]=max(dp[u],tmp+w);
}
Add(l[u],dp[u],sd),Add(r[u],-dp[u],sd),Add(l[u],sum[u],ss),Add(r[u],-sum[u],ss);
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) g[i].clear();
for(int i=1;i<n;i++) { int u,v; scanf("%d%d",&u,&v); g[u].push_back(v); g[v].push_back(u); }
init();
for(int i=1;i<=m;i++){
scanf("%d%d%d",&st[i],&en[i],&quan[i]);
int tmp=lca(st[i],en[i]); lis[tmp].push_back(i);
}
memset(dp,0,sizeof(dp)); memset(sum,0,sizeof(sum)); memset(sd,0,sizeof(sd)); memset(ss,0,sizeof(ss));
dfs2(1,0);
printf("%d\n",dp[1]);
}
return 0;
}

187
HDOJ/5294_autoAC.cpp Normal file
View File

@ -0,0 +1,187 @@
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int MAXN=2010;
int N,M;
struct Edge{
int from,to,cap,flow;
};
struct Dinic{
int n,m,s,t;
vector<Edge> edges;
vector<int> G[MAXN];
bool vis[MAXN];
int d[MAXN];
int cur[MAXN];
void clear_all(int n){
for(int i=0;i<n;i++) G[i].clear();
edges.clear();
}
void clear_flow(){
int len=edges.size();
for(int i=0;i<len;i++) edges[i].flow=0;
}
void add_edge(int from,int to,int cap){
edges.push_back((Edge){from,to,cap,0});
edges.push_back((Edge){to,from,0,0});
m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool BFS(){
memset(vis,0,sizeof(vis));
queue<int> q;
q.push(s);
d[s]=0;
vis[s]=1;
while(!q.empty()){
int x=q.front();
q.pop();
int len=G[x].size();
for(int i=0;i<len;i++){
Edge& e=edges[G[x][i]];
if(!vis[e.to]&&e.cap>e.flow){
vis[e.to]=1;
d[e.to]=d[x]+1;
q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x,int a){
if(x==t||a==0) return a;
int flow=0,f,len=G[x].size();
for(int& i=cur[x];i<len;i++){
Edge& e=edges[G[x][i]];
if(d[x]+1==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>0){
e.flow+=f;
edges[G[x][i]^1].flow-=f;
flow+=f;
a-=f;
if(a==0) break;
}
}
return flow;
}
int maxflow(int s,int t){
this->s=s;
this->t=t;
int flow=0;
while(BFS()){
memset(cur,0,sizeof(cur));
flow+=DFS(s,INF);
}
return flow;
}
int mincut(){
int ans=0;
int len=edges.size();
for(int i=0;i<len;i++){
Edge& e=edges[i];
if(vis[e.from]&&!vis[e.to]&&e.cap>0) ans++;
}
return ans;
}
void reduce(){
int len=edges.size();
for(int i=0;i<len;i++) edges[i].cap-=edges[i].flow;
}
}solver;
int d[MAXN];
vector<int> p[MAXN];
struct HeapNode{
int u,d;
bool operator < (const HeapNode& rhs) const{
return d>rhs.d;
}
};
struct Edge2{
int u,v,dist;
};
struct Dijkstra{
int n,m;
vector<Edge2> edges;
vector<int> G[MAXN];
bool done[MAXN];
void init(int n){
this->n=n;
for(int i=0;i<n;i++) G[i].clear();
edges.clear();
}
void add_edge(int u,int v,int dist){
edges.push_back((Edge2){u,v,dist});
m=edges.size();
G[u].push_back(m-1);
}
void dijkstra(int s){
priority_queue<HeapNode> q;
for(int i=0;i<n;i++) p[i].clear();
for(int i=0;i<n;i++) d[i]=INF;
d[s]=0;
memset(done,0,sizeof(done));
q.push((HeapNode){s,0});
while(!q.empty()){
HeapNode x=q.top();
q.pop();
int u=x.u;
if(done[u]) continue;
done[u]=true;
int L=G[u].size();
for(int i=0;i<L;i++){
Edge2& e=edges[G[u][i]];
if(d[e.v]>d[u]+e.dist){
d[e.v]=d[u]+e.dist;
p[e.v].clear();
p[e.v].push_back(u);
q.push((HeapNode){e.v,d[e.v]});
}
else if(d[e.v]==d[u]+e.dist){
p[e.v].push_back(u);
q.push((HeapNode){e.v,d[e.v]});
}
}
}
}
}solver2;
int num[MAXN];
int dfs(int u){
if(u==0) return 0;
if(num[u]!=INF) return num[u];
int len=p[u].size();
for(int i=0;i<len;i++){
int fa=p[u][i];
solver.add_edge(fa,u,1);
solver.add_edge(u,fa,1);
num[u]=min(num[u],dfs(fa)+1);
}
return num[u];
}
int main(){
while(scanf("%d%d",&N,&M)!=EOF){
solver2.init(N+1);
int u,v,t;
for(int i=0;i<M;i++){
scanf("%d%d%d",&u,&v,&t);
u--;
v--;
solver2.add_edge(u,v,t);
solver2.add_edge(v,u,t);
}
solver2.dijkstra(0);
memset(num,INF,sizeof(num));
solver.clear_all(N+1);
int n=dfs(N-1);
solver.maxflow(0,N-1);
int ans1=solver.mincut(),ans2=M-num[N-1];
printf("%d %d\n",ans1,ans2);
}
return 0;
}

130
HDOJ/5295_autoAC.cpp Normal file
View File

@ -0,0 +1,130 @@
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-6;
int sgn(double x){
if(fabs(x) <= eps) return 0;
return x > 0 ? 1 : -1;
}
struct P{
double x , y;
P(double _x = 0,double _y = 0){
x = _x , y = _y;
}
void set(double _x,double _y){
x = _x , y = _y;
}
void out(){
printf("%.6lf %.6lf\n",x,y);
}
P operator + (P b){
return P(x+b.x,y+b.y);
}
P operator - (P b){
return P(x-b.x,y-b.y);
}
P operator / (double k){
return P(x/k , y/k);
}
bool operator == (const P&b) const{
return sgn(x - b.x) == 0 && sgn(y - b.y) == 0;
}
double lenq(){
return x*x + y*y;
}
double len(){
return sqrt(lenq());
}
};
double dis(P a,P b){
return (a-b).len();
}
struct Cir{
P o;
double r;
Cir(P _o,double _r = 0){
o = _o , r = _r;
}
void set(P _o,double _r){
o = _o , r = _r;
}
bool operator == (const Cir&b) const{
return o == b.o && sgn(r - b.r) == 0;
}
};
void c2point(Cir c1,Cir c2,P &rp1,P &rp2){
if(c1 == c2){
rp1 = c1.o + P(0,c1.r);
rp2 = c1.o - P(0,c1.r);
return;
}
P p1 = c1.o , p2 = c2.o;
double r1 = c1.r , r2 = c2.r;
double a = p2.x - p1.x , b = p2.y - p1.y , r = (a*a + b*b + r1*r1 - r2*r2) / 2;
double tmp;
if(a == 0 && b != 0){
rp1.y = rp2.y = r / b;
tmp = r1 * r1 - rp1.y * rp1.y;
if(sgn(tmp) <= 0) tmp = 0;
rp1.x = sqrt(tmp);
rp2.x = -rp1.x;
}
else if(a != 0 && b == 0){
rp1.x = rp2.x = r / a;
tmp = r1 * r1 - rp1.x * rp1.x;
if(sgn(tmp) <= 0) tmp = 0;
rp1.y = sqrt(tmp);
rp2.y = -rp1.y;
}
else if(a != 0 && b != 0){
double delta = b*b*r*r - (a*a + b*b) * (r*r - r1*r1*a*a);
if(sgn(delta) <= 0) delta = 0;
rp1.y = (b*r + sqrt(delta)) / (a*a + b*b);
rp2.y = (b*r - sqrt(delta)) / (a*a + b*b);
rp1.x = (r - b*rp1.y) / a;
rp2.x = (r - b*rp2.y) / a;
}
rp1.x += p1.x , rp1.y += p1.y;
rp1.x += p1.x , rp2.y += p1.y;
}
int T;
double AB , BC , CD , DA , EF;
P A , B , C , D , A2 , G;
void graph_construct(){
C.set(0,0);
B.set(BC,0);
Cir O1(C,DA) , O2(B,2 * EF);
P discard;
c2point(O1,O2,A2,discard);
G = A2 + (B - C);
O1.set(C,CD);O2.set(G,AB);
c2point(O1,O2,D,discard);
A = D + (C - A2);
}
const double eps2 = 1e-4;
int sgn2(double x){
if(fabs(x) <= eps2) return 0;
return 1;
}
void spj(){
bool ff = true;
if(sgn2(AB - dis(A,B)) != 0) ff = false;
if(sgn2(BC - dis(B,C)) != 0) ff = false;
if(sgn2(CD - dis(C,D)) != 0) ff = false;
if(sgn2(DA - dis(D,A)) != 0) ff = false;
if(sgn2(EF - dis((A+B)/2,(C+D)/2)) != 0) ff = false;
}
int main(){
scanf("%d",&T);
for(int _=1;_<=T;++_){
scanf("%lf%lf%lf%lf%lf",&AB,&BC,&CD,&DA,&EF);
graph_construct();
printf("Case #%d:\n",_);
A.out();B.out();C.out();D.out();
spj();
}
return 0;
}

156
HDOJ/5296_autoAC.cpp Normal file
View File

@ -0,0 +1,156 @@
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf (-((LL)1<<40))
#define lson k<<1, L, (L + R)>>1
#define rson k<<1|1, ((L + R)>>1) + 1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define rep(i, a, b) for(int i = (a); i <= (b); i ++)
#define dec(i, a, b) for(int i = (a); i >= (b); i --)
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; }
typedef long long LL;
const int MAXN = 100000 + 100;
const int MAXM = 2000000 + 100;
const double eps = 1e-8;
LL MOD = 1000000007;
struct Edge {
int v, w;
Edge(int _v = 0, int _w = 0) {
v = _v; w = _w;
}
};
struct LCA {
int idx[MAXN << 1];
int dep[MAXN << 1];
int dp[MAXN << 1][20];
int K[MAXN << 1];
int node_cnt;
vector<Edge>G[MAXN];
int P[MAXN];
int dis[MAXN];
void init(int n) {
mem0(dep); mem0(K);
node_cnt = 0;
rep (i, 0, n) G[i].clear();
}
void add_edge(int u, int v, int w) {
G[u].push_back(Edge(v, w));
G[v].push_back(Edge(u, w));
}
void dfs(int u, int fa, int height, int dist) {
idx[++node_cnt] = u;
dep[node_cnt] = height;
P[u] = node_cnt;
dis[u] = dist;
int sz = G[u].size();
rep (i, 0, sz - 1) {
int v = G[u][i].v;
if(v == fa) continue;
dfs(v, u, height + 1, dist + G[u][i].w);
idx[++node_cnt] = u;
dep[node_cnt] = height;
}
}
void init_st_table() {
dfs(1, -1, 0, 0);
int n = node_cnt;
rep (i, 1, n) {
dp[i][0] = i;
while((1 << (K[i] + 1)) <= i) K[i] ++;
}
for(int j = 1; (1 << j) <= n; j ++) {
for(int i = 1; i + (1 << j) - 1 <= n; i ++) {
int l_pos = dp[i][j - 1], r_pos = dp[i + (1 << (j - 1))][j - 1];
dp[i][j] = dep[l_pos] < dep[r_pos] ? l_pos : r_pos;
}
}
}
int rmq_query(int L, int R) {
if(L > R) swap(L, R);
int len = R - L + 1, k = K[len];
return dep[dp[L][k]] < dep[dp[R - (1 << k) + 1][k]] ? dp[L][k] : dp[R - (1 << k) + 1][k];
}
int lca_query(int u, int v) {
int id = rmq_query(P[u], P[v]);
return idx[id];
}
}lca;
struct SegTree {
int s[MAXN << 3];
void update(int k, int L, int R, int p, int v) {
if(L == R) { s[k] = v; return ; }
if(((L + R) >> 1) >= p) update(lson, p, v);
else update(rson, p, v);
s[k] = s[k << 1] + s[k << 1 | 1];
}
int query_sum(int k, int L, int R, int p) {
if(R <= p) return s[k];
if( ((L + R) >> 1) >= p ) return query_sum(lson, p);
return s[k << 1] + query_sum(rson, p);
}
int query_pos(int k, int L, int R, int x) {
if(L == R) return L;
if(s[k << 1] >= x) return query_pos(lson, x);
return query_pos(rson, x - s[k << 1]);
}
}st;
int t, n, m, cas = 0;
int u, v, w;
bool vis[MAXN << 1];
int main()
{
cin >> t;
while(t--) {
scanf("%d %d", &n, &m);
lca.init(n);
rep (i, 2, n) {
scanf("%d %d %d", &u, &v, &w);
lca.add_edge(u, v, w);
}
lca.init_st_table();
mem0(st.s);
mem0(vis);
int ans = 0;
printf("Case #%d:\n", ++cas);
while(m --) {
scanf("%d %d", &u, &v);
if( (u == 1 && !vis[v]) || (u == 2 && vis[v]) ) {
vis[v] = !vis[v];
if(u == 2) st.update(1, 1, lca.node_cnt, lca.P[v], 0);
if( st.s[1] ) {
int x, y;
int sum = st.query_sum(1, 1, lca.node_cnt, lca.P[v]);
if( !sum || sum == st.s[1] ) x = 1, y = st.s[1];
else x = sum, y = sum + 1;
x = lca.idx[st.query_pos(1, 1, lca.node_cnt, x)];
y = lca.idx[st.query_pos(1, 1, lca.node_cnt, y)];
int xv = lca.lca_query(x, v);
int yv = lca.lca_query(y, v);
int xy = lca.lca_query(x, y);
ans += (u == 1 ? 1 : -1) * (lca.dis[v] - lca.dis[xv] - lca.dis[yv] + lca.dis[xy]);
}
else ans = 0;
if(u == 1) st.update(1, 1, lca.node_cnt, lca.P[v], 1);
}
printf("%d\n", ans);
}
}
return 0;
}

60
HDOJ/5297_autoAC.cpp Normal file
View File

@ -0,0 +1,60 @@
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int prime[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67};
int cnt[100]={19,0,1,1,1,1,2,1,1,1,2,1,2,1,2,2,1,1,2,1,2,2,2,1,2,1,2,1,2,1,3,1,1,2,2,2,2,1,2,2
,2,1,3,1,2,2,2,1,2,1,2,2,2,1,2,2,2,2,2,1,3,1,2,2,1};
long long considrt[6000000],nconsidrt;
void GetConsider(int r)
{
nconsidrt=0;
int i,j,sz;
for(i=0;prime[i]<=r;i++)
{
sz=nconsidrt;
for(j=0;j<sz;j++)
{
if(considrt[j]*prime[i]<=63)
considrt[nconsidrt++]=considrt[j]*prime[i];
}
considrt[nconsidrt++]=prime[i];
}
}
long long cal(long long x)
{
long long ret(0);
for(int i=0;i<nconsidrt;i++)
{
if(cnt[considrt[i]]&1)
ret+=(long long)pow(x+0.5,1.0/considrt[i])-1;
else
ret-=(long long)pow(x+0.5,1.0/considrt[i])-1;
}
return x-ret-1;
}
long long solve(long long n,int r)
{
GetConsider(r);
long long ans = n;
while(1)
{
long long temp = cal(ans);
if(temp == n)
break;
ans += n - temp;
}
return ans;
}
int main()
{
int ncase,r;
long long n;
scanf("%d",&ncase);
while(ncase--)
{
scanf("%I64d%d",&n,&r);
printf("%I64d\n",solve(n,r));
}
return 0;
}

45
HDOJ/5298_autoAC.cpp Normal file
View File

@ -0,0 +1,45 @@
#include <stdio.h>
typedef long long LL ;
struct Node {
LL x , y , z , d ;
} a[100] , b[10] ;
int n , m , p , q ;
LL f ( LL x ) {
return ( LL ) x * x ;
}
bool calc ( int x , int y , int z ) {
bool t = 0 ;
for ( int i = 0 ; i < n ; ++ i ) if ( a[i].x * x + a[i].y * y + a[i].z * z + a[i].d > 0 ) t ^= 1 ;
for ( int i = 0 ; i < m ; ++ i ) if ( f ( b[i].d ) > f ( b[i].x - x ) + f ( b[i].y - y ) + f ( b[i].z - z ) ) t ^= 1 ;
return t ;
}
void solve () {
int x , y , z , ok = 1 , mark = -1 ;
scanf ( "%d%d%d%d" , &n , &m , &p , &q ) ;
for ( int i = 0 ; i < n ; ++ i ) scanf ( "%lld%lld%lld%lld" , &a[i].x , &a[i].y , &a[i].z , &a[i].d ) ;
for ( int i = 0 ; i < m ; ++ i ) scanf ( "%lld%lld%lld%lld" , &b[i].x , &b[i].y , &b[i].z , &b[i].d ) ;
for ( int i = 0 ; i < p ; ++ i ) {
scanf ( "%d%d%d" , &x , &y , &z ) ;
if ( !ok ) continue ;
bool tmp = calc ( x , y , z ) ;
if ( mark == -1 ) mark = tmp ;
else if ( mark != tmp ) ok = 0 ;
}
if ( !ok ) puts ( "Impossible" ) ;
for ( int i = 0 ; i < q ; ++ i ) {
scanf ( "%d%d%d" , &x , &y , &z ) ;
if ( !ok ) continue ;
bool tmp = calc ( x , y , z ) ;
if ( mark == -1 ) puts ( "Both" ) ;
else puts ( mark == tmp ? "Y" : "R" ) ;
}
}
int main () {
int T ;
scanf ( "%d" , &T ) ;
for ( int i = 1 ; i <= T ; ++ i ) {
if ( i > 1 ) puts ( "" ) ;
solve () ;
}
return 0 ;
}

76
HDOJ/5299_autoAC.cpp Normal file
View File

@ -0,0 +1,76 @@
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
struct Point
{
int x,y,r;
}point[20010];
bool cmp(Point a,Point b)
{
return a.r<b.r;
}
struct Edge
{
int to,next;
}edge[20010];
int head[20010],tail;
void add(int from,int to)
{
edge[tail].to=to;
edge[tail].next=head[from];
head[from]=tail++;
}
int dfs(int from)
{
int ans=0;
for(int i=head[from];i!=-1;i=edge[i].next)
ans^=dfs(edge[i].to)+1;
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d%d%d",&point[i].x,&point[i].y,&point[i].r);
sort(point,point+n,cmp);
tail=0;
memset(head,-1,sizeof(head));
for(int i=0;i<n;i++)
{
bool flag=0;
for(int j=i+1;j<n;j++)
if(ll(point[j].r*point[j].r)>ll(point[i].x-point[j].x)*(point[i].x-point[j].x)+(point[i].y-point[j].y)*(point[i].y-point[j].y))
{
flag=1;
add(j,i);
break;
}
if(!flag)
add(n,i);
}
if(dfs(n)!=0)
puts("Alice");
else
puts("Bob");
}
}

33
HDOJ/5301_autoAC.cpp Normal file
View File

@ -0,0 +1,33 @@
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
int main(void) {
ll n, m, x, y;
while (scanf ("%I64d%I64d%I64d%I64d", &n, &m, &x, &y) == 4) {
if (n > m) {
swap (n, m); swap (x, y);
}
if (x > n - x + 1) {
x = n - x + 1;
}
if (y > m - y + 1) {
y = m - y + 1;
}
ll hm = (m + 1) / 2, hn = (n + 1) / 2;
ll ans = min (hm, hn);
if (m == n) {
if (m % 2 == 1) {
if (x == hn && y == hm) ans--;
}
}
else {
ll t = min (y, n - x);
ans = max (ans, t);
ans = min (ans, hm);
}
printf ("%I64d\n", ans);
}
return 0;
}

41
HDOJ/5302_autoAC.cpp Normal file
View File

@ -0,0 +1,41 @@
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
#define sf scanf
int T;
int a[5],b[5];
int d[1000005];
int main()
{
sf("%d",&T);
while(T--)
{
for(int i = 0;i<3;i++)sf("%d",a+i);
for(int i = 0;i<3;i++)sf("%d",b+i);
int sum = 0;
for(int i = 0;i<3;i++)sum+=a[i];
if( (a[1]&1) || (b[1]&1) )
{
puts("-1");
continue;
}
if(sum==4)
{
puts("4\n1 2 0\n1 3 0\n2 3 1\n3 4 1");
continue;
}
printf("%d\n",a[1]/2+a[2]+b[1]/2+b[2]);
int t = 1;
while(a[2]!=-1){printf("%d %d 0\n",t,t+1);t++;a[2]--;}t++;
while(a[1]!=2){printf("%d %d 0\n",t,t+1);t+=2;a[1]-=2;}
int tt = 0;
for(int i = 1;i<=sum;i+=2) d[tt++] = i;
for(int i = 2;i<=sum;i+=2)d[tt++] = i;
t = 0;
while(b[2]!=-1){printf("%d %d 1\n",min(d[t],d[t+1]),max(d[t],d[t+1]));t++;b[2]--;}t++;
while(b[1]!=2){printf("%d %d 1\n",min(d[t],d[t+1]),max(d[t],d[t+1]));t+=2;b[1]-=2;}
}
}

54
HDOJ/5303_autoAC.cpp Normal file
View File

@ -0,0 +1,54 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int t,n,m,l,num1,num2,cnt;
long long left[500010],right[500010];
long long sum1[500010],sum2[500010];
long long apple[500010];
long long ans;
int main()
{
int i,j,k,a,b;
scanf("%d",&t);
while(t--)
{
cnt=0;num1=num2=0;
memset(apple,0,sizeof(apple));
memset(sum1,0,sizeof(sum1));
memset(sum2,0,sizeof(sum2));
scanf("%d%d%d",&l,&n,&k);
for(i=0;i<n;i++)
{
scanf("%d%d",&a,&b);
for(j=1;j<=b;j++){
apple[++cnt]=a;
}
}
k=min(k,cnt);
for(i=1;i<=cnt;i++){
if(apple[i]*2<=l){
left[++num1]=apple[i];
}
else{
right[++num2]=l-apple[i];
}
}
sort(left+1,left+num1+1);
sort(right+1,right+num2+1);
for(i=1;i<=num1;i++){
if(i<=k) sum1[i]=left[i];
else sum1[i]=sum1[i-k]+left[i];
}
for(i=1;i<=num2;i++){
if(i<=k) sum2[i]=right[i];
else sum2[i]=sum2[i-k]+right[i];
}
ans=(long long)(sum1[num1]+sum2[num2])*2;
for(i=0;i<=num1&&i<=k;i++){
ans=min(ans,(long long)(l+2*sum1[num1-i]+2*sum2[num2-(k-i)]));
}
printf("%lld\n",ans);
}
return 0;
}

88
HDOJ/5304_autoAC.cpp Normal file
View File

@ -0,0 +1,88 @@
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int mo=998244353;
struct atom{
int x,y;
};
atom operator - (const atom k1,const atom k2){
return (atom){(k1.x-k2.x+mo)%mo,(k1.y-k2.y+mo)%mo};
}
atom operator * (const atom k1,const int k2){
return (atom){1ll*k1.x*k2%mo,1ll*k1.y*k2%mo};
}
int x[20][20],n,m,A[20][20],dp[1<<16][17],f[1<<16],pd[20];
int gauss(int n){
int pd=1; n--;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++) x[i][j]=(x[i][j]+mo)%mo;
for (int i=1;i<=n;i++){
int r=0;
for (int j=i;j<=n;j++) if (x[j][i]){r=j; break;}
for (int j=1;j<=n;j++) swap(x[i][j],x[r][j]);
if (r!=i) pd=-pd;
for (int j=i+1;j<=n;j++){
int k1=x[i][i],k2=x[j][i];
atom xx=(atom){1,0},y=(atom){0,1};
while (k2){
int k4=k1/k2;
atom z=y; y=xx-y*k4; xx=z;
k4=k2; k2=k1%k2; k1=k4; pd=-pd;
}
for (int k=1;k<=n;k++){
int k3=x[i][k],k4=x[j][k];
x[i][k]=(1ll*xx.x*k3+1ll*xx.y*k4)%mo;
x[j][k]=(1ll*y.x*k3+1ll*y.y*k4)%mo;
}
}
}
for (int i=1;i<=n;i++) pd=1ll*pd*x[i][i]%mo;
return (pd+mo)%mo;
}
int solve(){
memset(A,0x00,sizeof A);
for (;m;m--){
int k1,k2; scanf("%d%d",&k1,&k2); A[k1][k2]=1; A[k2][k1]=1;
}
memset(f,0x00,sizeof f);
for (int s=1;s<=n;s++){
memset(dp,0x00,sizeof dp);
dp[(1<<s-1)][s]=1;
for (int i=1;i<(1<<n);i++)
for (int j=1;j<=n;j++)
if (dp[i][j]){
for (int k=s+1;k<=n;k++)
if ((i&(1<<k-1))==0&&A[j][k])
dp[i|(1<<k-1)][k]=(dp[i|(1<<k-1)][k]+dp[i][j])%mo;
if (A[j][s]) f[i]=(f[i]+dp[i][j])%mo;
}
}
int ans=0;
for (int i=1;i<=n;i++)
for (int j=i+1;j<=n;j++)
if (A[i][j]) f[(1<<i-1)|(1<<j-1)]=(f[(1<<i-1)|(1<<j-1)]-1+mo)%mo;
int inv2=(mo+1)/2;
for (int i=0;i<(1<<n);i++) f[i]=1ll*f[i]*inv2%mo;
for (int now=0;now<(1<<n);now++) if (f[now]){
for (int i=1;i<=n;i++) pd[i]=0;
for (int i=1;i<=n;i++) if (now&(1<<i-1)) pd[i]=1;
int sign=1;
for (int i=1;i<=n;i++) if (pd[i]==0) pd[i]=++sign;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++) x[i][j]=0;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
if (A[i][j]&&pd[i]!=pd[j]){
x[pd[i]][pd[j]]--; x[pd[i]][pd[i]]++;
}
ans=(ans+1ll*f[now]*gauss(sign))%mo;
}
return ans;
}
int main(){
while (scanf("%d%d",&n,&m)!=EOF) printf("%d\n",solve());
return 0;
}

70
HDOJ/5305_autoAC.cpp Normal file
View File

@ -0,0 +1,70 @@
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
int ans;
int n,m;
int cnt[10];
struct node{
int x,y;
void in(){
scanf("%d%d",&x,&y);
if(x>y)swap(x,y);
cnt[x]++;
cnt[y]++;
}
friend bool operator < (node a,node b){
if(a.x==b.x)return a.y<b.y;
return a.x<b.x;
}
}p[30];
void dfs(int t,int on[],int off[]){
if(t==m){
ans++;
return ;
}
int x=p[t].x;
int y=p[t].y;
on[x]++ , on[y]++;
if(on[x]<=cnt[x]/2 && off[x]<=cnt[x]/2 && on[y]<=cnt[y]/2 && off[y]<=cnt[y]/2 ){
dfs(t+1,on,off);
}
on[x]-- , on[y]--;
off[x]++ , off[y]++;
if(on[x]<=cnt[x]/2 && off[x]<=cnt[x]/2 && on[y]<=cnt[y]/2 && off[y]<=cnt[y]/2 ){
dfs(t+1,on,off);
}
off[x]-- , off[y]-- ;
}
int main(){
int T;scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
int on[10];
int off[10];
memset(cnt,0,sizeof cnt);
memset(on,0,sizeof on);
memset(off,0,sizeof off);
for(int i=0;i<m;i++){
p[i].in();
}
bool ok=false;
for(int i=1;i<=n;i++){
if(cnt[i]&1){
ok=true;
break;
}
}
if(ok){
printf("0\n");
continue;
}
sort(p,p+m);
ans=0;
dfs(0,on,off);
printf("%d\n",ans);
}
return 0;
}

175
HDOJ/5306_autoAC.cpp Normal file
View File

@ -0,0 +1,175 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long int LL;
const int maxn=1000030;
int n,m;
int a[maxn];
struct Node
{
LL ss;
int mx,tag,cv;
void toString() {
printf("ss: %lld mx: %d tag: %d cv: %d\n",ss,mx,tag,cv);
}
}T[maxn<<2];
#define lrt (rt<<1)
#define rrt (rt<<1|1)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
void push_up(int rt)
{
T[rt].ss=T[lrt].ss+T[rrt].ss;
T[rt].mx=max(T[lrt].mx,T[rrt].mx);
T[rt].cv=T[lrt].cv+T[rrt].cv;
}
void pnc(int t,int l,int r,int rt)
{
if(T[rt].tag!=0&&T[rt].tag<=t) return ;
int all=r-l+1;
if(T[rt].cv!=all)
{
T[rt].ss+=(LL)t*(all-T[rt].cv);
T[rt].tag=T[rt].mx=t;
T[rt].cv=all;
}
}
void push_down(int l,int r,int rt)
{
if(T[rt].tag)
{
int m=(l+r)/2;
pnc(T[rt].tag,lson); pnc(T[rt].tag,rson);
}
}
void fix(int t,int l,int r,int rt)
{
if(T[rt].mx<t) return ;
if(T[rt].tag>=t) T[rt].tag=0;
if(l==r)
{
T[rt].ss=T[rt].mx=T[rt].tag;
T[rt].cv=T[rt].tag!=0;
}
else
{
push_down(l,r,rt);
int m=(l+r)/2;
fix(t,lson); fix(t,rson);
push_up(rt);
}
}
void build(int l,int r,int rt)
{
if(l==r)
{
T[rt].ss=T[rt].mx=T[rt].tag=a[l];
T[rt].cv=1;
return ;
}
T[rt].tag=0;
int m=(l+r)/2;
build(lson); build(rson);
push_up(rt);
}
void update(int L,int R,int t,int l,int r,int rt)
{
if(T[rt].mx<=t) return ;
if(L<=l&&r<=R)
{
fix(t,l,r,rt);
if(l==r)
{
T[rt].ss=T[rt].mx=T[rt].tag=t;
T[rt].cv=1;
}
else push_up(rt);
pnc(t,l,r,rt);
}
else
{
push_down(l,r,rt);
int m=(l+r)/2;
if(L<=m) update(L,R,t,lson);
if(R>m) update(L,R,t,rson);
push_up(rt);
}
}
int query_max(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R) return T[rt].mx;
push_down(l,r,rt);
int m=(l+r)/2;
int ret=0;
if(L<=m) ret=max(ret,query_max(L,R,lson));
if(R>m) ret=max(ret,query_max(L,R,rson));
return ret;
}
LL query_sum(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R) return T[rt].ss;
push_down(l,r,rt);
int m=(l+r)/2;
LL ret=0;
if(L<=m) ret+=query_sum(L,R,lson);
if(R>m) ret+=query_sum(L,R,rson);
return ret;
}
void show(int l,int r,int rt)
{
printf("rt: %d %d <---> %d\n ",rt,l,r);
T[rt].toString();
if(l==r) return ;
int m=(l+r)/2;
show(lson); show(rson);
}
char *ch,buf[40*1024000+5];
void nextInt(int& x)
{
x=0;
for(++ch;*ch<=32;++ch);
for(x=0;'0'<=*ch;ch++) x=x*10+*ch-'0';
}
int main()
{
ch=buf-1;
fread(buf,1,1000*35*1024,stdin);
int T_T;
nextInt(T_T);
while(T_T--)
{
nextInt(n); nextInt(m);
for(int i=1;i<=n;i++) nextInt(a[i]);
build(1,n,1);
int k,l,r,t;
while(m--)
{
nextInt(k);
if(k==0)
{
nextInt(l); nextInt(r); nextInt(t);
update(l,r,t,1,n,1);
}
else if(k==1)
{
nextInt(l); nextInt(r);
printf("%d\n",query_max(l,r,1,n,1));
}
else if(k==2)
{
nextInt(l); nextInt(r);
printf("%I64d\n",query_sum(l,r,1,n,1));
}
}
}
return 0;
}

94
HDOJ/5307_autoAC.cpp Normal file
View File

@ -0,0 +1,94 @@
#include <cstdio>
#include <string.h>
using namespace std;
#define P 50000000001507329LL
#define G 3
int T;
int n, s[110000];
long long ans[140000], a[140000], b[140000], c[140000], x[140000], w[140000];
long long pu[110000];
int nn;
long long Mul(long long x, long long y) {
return (x*y-(long long)(x/(long double)P*y+1e-3)*P+P)%P;
}
long long Pow(long long x, long long y) {
long long i, ans = 1;
for (i = 1; i <= y; i *= 2, x = Mul(x, x)) if (y & i) ans = Mul(ans, x);
return ans;
}
void DFT(long long *a, int n) {
int m, i, d, p, q;
for (m = 1; (1 << m) <= n; m++){
for (i = 0; i < (n >> m); i++)
for (q = 0, d = p = i; d < n; q += (n >> m), d += (n >> (m - 1)), p += (n >> m)){
x[p] = (Mul(a[d + (n >> m)], w[q]) + a[d]) % P;
x[p + n / 2] = (Mul(a[d + (n >> m)], w[q + n / 2]) + a[d]) % P;
}
for (i = 0; i < n; i++) a[i] = x[i];
}
}
void DFT1(long long *a, int n){
int m, i, d, p, q;
for (m = 1; (1 << m) <= n; m++) {
for (i = 0; i < (n >> m); i++)
for (q = 0, d = p = i; d < n; q += (n >> m), d += (n >> (m - 1)), p += (n >> m)){
x[p] = (Mul(a[d + (n >> m)], w[n - q]) + a[d]) % P;
x[p + n / 2] = (Mul(a[d + (n >> m)], w[n / 2 - q]) + a[d]) % P;
}
for (i = 0; i < n; i++) a[i] = x[i];
}
}
void doit() {
long long S = Pow(n, P - 2);
DFT(a, n);
DFT(b, n);
for (int i = 0; i < n; i++)
c[i] = Mul(a[i], b[i]);
DFT1(c, n);
for (int i = 0; i < n; i++)
c[i] = Mul(c[i], S);
for (int i = 0; i < n; i++)
ans[i] = (ans[i] + c[i]) % P;
}
int main() {
scanf("%d", &T);
n = 131072;
for (int i = 0; i <= n; i++)
w[i] = Pow(G, (P - 1) / n * i);
s[0] = 0;
while (T--) {
scanf("%d", &nn);
for (int i = 1; i <= nn; i++)
scanf("%d", &s[i]), s[i] += s[i - 1];
memset(ans, 0, sizeof ans);
memset(a, 0, sizeof a);
memset(b, 0, sizeof b);
for (int i = 1; i <= nn; i++)
a[s[i]] += i;
for (int i = 1; i <= nn; i++)
b[s[nn] - s[i - 1]]++;
doit();
memset(a, 0, sizeof a);
memset(b, 0, sizeof b);
for (int i = 0; i <= 50000; i++)
a[i] = b[i] = 0;
for (int i = 1; i <= nn; i++)
a[s[i]] = (a[s[i]] + P - 1) % P;
for (int i = 1; i <= nn; i++)
b[s[nn] - s[i - 1]] += i - 1;
doit();
long long ans0 = 0;
int q, h;
for (int i = 1; i <= nn; i++)
pu[i] = pu[i - 1] + 1LL * i * (i + 1) / 2;
for (q = 1; q <= nn; q = h + 1)
if (s[q] != s[q - 1]) h = q;
else {
for (h = q; h < nn && s[h + 1] == s[q]; h++);
ans0 += pu[h - q + 1];
}
printf("%lld\n", ans0);
for (int i = 1; i <= s[nn]; i++)
printf("%lld\n", ans[i + s[nn]]);
}
}

46
HDOJ/5308_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
int n;
while(~scanf("%d",&n)){
if(n<=3){puts("-1");continue;}
if(n==4){puts("1 * 2");puts("5 + 3");puts("6 + 4");continue; }
if(n==5){puts("2 / 3");puts("6 / 4");puts("1 - 7");puts("8 * 5");continue; }
if(n==6){puts("1 + 2");puts("7 + 3");puts("8 + 4");puts("9 + 5");puts("10 - 6");continue; }
if(n==7){puts("1 + 2");puts("8 + 3");puts("9 / 4");puts("10 + 5");puts("11 + 6");puts("12 + 7");continue; }
if(n==8){puts("1 + 2");puts("9 + 3");puts("10 + 4");puts("5 + 6");puts("11 / 12");puts("13 * 7");puts("14 + 8");continue; }
if(n==9){puts("1 + 2");puts("10 + 3");puts("4 + 5");puts("12 + 6");puts("13 / 7");
puts("11 - 14");puts("15 * 8");puts("16 / 9");continue;}
if(n==10){puts("1 + 2");puts("11 / 3");puts("12 + 4");puts("5 / 6");puts("7 / 8");
puts("14 + 15");puts("13 * 16");puts("17 * 9");puts("18 / 10");continue; }
if(n==11){puts("1 / 2");puts("12 + 3");puts("4 / 5");puts("6 / 7");puts("14 + 15");puts("13 * 16");
puts("17 * 8");puts("18 * 9");puts("19 / 10");puts("20 / 11");continue; }
if(n&1){
puts("1 + 2");printf("%d / 3\n",n+1);
puts("4 + 5");printf("%d / 6\n",n+3);
puts("7 + 8");printf("%d / 9\n",n+5);
puts("10 + 11");printf("%d + 12\n",n+7);printf("%d / 13\n",n+8);
printf("%d * %d\n",n+2,n+4);printf("%d * %d\n",n+10,n+6);
printf("%d * %d\n",n+11,n+9);
int x=n+12,y=14;
for(int i=1;i<=(n-13)/2;i++){
printf("%d * %d\n",x++,y++);
printf("%d / %d\n",x++,y++);
}
}else{
puts("1 + 2");printf("%d + 3\n",n+1);printf("%d + 4\n",n+2);printf("%d / 5\n",n+3);
puts("6 + 7");
for(int i=5;i<=8;i++)
printf("%d + %d\n",n+i,i+3);
printf("%d / 12\n",n+9);
printf("%d * %d\n",n+4,n+10);
int x=n+11,y=13;
for(int i=1;i<=(n-12)/2;i++){
printf("%d * %d\n",x++,y++);
printf("%d / %d\n",x++,y++);
}
}
}
return 0;
}

91
HDOJ/5309_autoAC.cpp Normal file
View File

@ -0,0 +1,91 @@
#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
#define sf scanf
#define mx 100000000
struct node{
int id,v;
//friend bool operator<(node a,node b){return (a.v==b.v)?(a.id>b.id):(a.v>b.v);}
bool operator < (const node &a)const {
if(a.v==v)return id>a.id;
return v<a.v;
}
};
priority_queue<node> que1,que2;
queue<int> que;
int n,m,k;
int g[1000000],sum[1000000],a[1000000],r[1000000];
int main()
{
while(~scanf("%d%d%d",&n,&m,&k))
{
while(!que1.empty()) que1.pop();
while(!que2.empty())que2.pop();
for(int i = 1;i<=n;i++) {sf("%d",a+i);sum[i]=sum[i-1]+a[i];}
int t = 0;
for(int i = 0;i<=n;i++){while(t<=n&&sum[t]<i*k + m) t++;r[i] = t-1;}
for(int i = n;i>0;i--)
{
g[i] = 0;
if(r[i] == n) g[i] = mx + i;
else
{
while(!que1.empty()){
node tmp = que1.top();
if(tmp.id>r[i])que1.pop();
else if(tmp.v*2>=g[tmp.id] + i)
{
que2.push(node{tmp.id,g[tmp.id]-tmp.id});
que1.pop();
}
else break;
}
while(!que2.empty())
{
node tmp = que2.top();
if(tmp.id>r[i])que2.pop();
else break;
}
if(!que1.empty())g[i] = max(g[i],que1.top().v);
if(!que2.empty())g[i] = max(g[i],que2.top().v+i);
}
if(g[i]>i) que1.push((node){i,i});
else que2.push((node){i,g[i]-i});
}
for(int i = 1;i<=n;i++)g[i]-=i;
g[0] = 0;
for(int i = 1;i<=r[0];i++) g[0] = max(g[0],g[i]);
if(g[0] > n) puts("Poor JRY!");
else if(!g[0]) puts("Poor Hero!");
else
{
printf("%d\n",g[0]);
int i = 1;
while(g[i]!=g[0]) i++;
que.push(i);
while(g[i]!=mx)
{
int j=i+g[0];
while(g[j]<g[0]) {
j++;
}
i = j;
que.push(i);
}
printf("%d\n",que.size());
while(!que.empty())
{
int tmp = que.front();
printf("%d",tmp);
que.pop();
if(que.size())printf(" ");
else printf("\n");
}
}
}
return 0;
}

25
HDOJ/5310_autoAC.cpp Normal file
View File

@ -0,0 +1,25 @@
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
int m,n,q,t,p;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d",&n,&m ,&p,&q);
int sum=0;
double op=q/m;
if(op<p)
{
sum+=(n/m)*q;
int pl=n%m;
sum+=min(p*pl,q);
cout<<sum<<endl;
}
else
cout<<p*n<<endl;
}
return 0;
}

104
HDOJ/5311_autoAC.cpp Normal file
View File

@ -0,0 +1,104 @@
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MAX_N = 200 + 10;
int Next[MAX_N];
const char *str = "anniversary";
char pat[MAX_N], pat_sub[MAX_N];
char sub_1[MAX_N], sub_2[MAX_N], sub_3[MAX_N];
void get_nextval(char *W, int Next[])
{
int i = 0, j = -1;
int Len_W = strlen(W);
Next[0] = -1;
if (W[0] == '\0')
return;
while (i < Len_W)
{
if (j == -1 || W[i] == W[j])
{
++i;
++j;
Next[i] = j;
} else
j = Next[j];
}
}
int KMP_Matcher(char T[], char P[], int Next[])
{
int i = 0, j = 0;
int TLen = strlen(T);
int PLen = strlen(P);
if (TLen == 0)
return -2;
while (i < TLen && j < PLen)
{
if (j == -1 || T[i] == P[j])
{
i++;
j++;
} else
j = Next[j];
}
if (j == PLen)
return i - j;
else
return -1;
}
int main()
{
int case_times, len;
scanf("%d", &case_times);
len = strlen(str);
while (case_times--)
{
int len_s;
bool ans = false;
scanf("%s", pat);
strcpy(pat_sub, pat);
len_s = strlen(pat);
for (int i = 1; i < len; ++i)
{
if (ans)
break;
for (int j = i + 1; j < len; ++j)
{
int m_1, m_2, m_3;
strncpy(sub_1, str, i);
sub_1[i] = '\0';
strncpy(sub_2, str + i, j - i);
sub_2[j - i] = '\0';
strncpy(sub_3, str + j, len - j);
sub_3[len - j] = '\0';
strcpy(pat_sub, pat);
get_nextval(sub_1, Next);
m_1 = KMP_Matcher(pat_sub, sub_1, Next);
if (m_1 >= 0)
{
for (int k = 0; k <= len_s - i; ++k)
pat_sub[k] = pat[m_1 + i + k];
}
get_nextval(sub_2, Next);
m_2 = KMP_Matcher(pat_sub, sub_2, Next);
if (m_2 >= 0)
{
for (int k = 0; k <= len_s - j; ++k)
pat_sub[k] = pat_sub[m_2 + j - i + k];
}
get_nextval(sub_3, Next);
m_3 = KMP_Matcher(pat_sub, sub_3, Next);
if (m_1 >= 0 && m_2 >= 0 && m_3 >= 0)
{
ans = true;
break;
}
}
}
if (ans)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

63
HDOJ/5312_autoAC.cpp Normal file
View File

@ -0,0 +1,63 @@
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
ll a[1000000];
int count1;
int main(){
mem0(a);
int m;
for(int i=1;(ll)3*i*(i-1)+1<=1000010000;i++){
a[i]=3*i*(i-1)+1;
m=i;
}
int t;
int n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
if(*lower_bound(a+1,a+m+1,n)==n){
printf("1\n");
continue;
}
int p=1;
int flag=0;
if((n-2)%6==0){
while(2*a[p]<=n){
if(*lower_bound(a+1,a+m+1,n-a[p])==n-a[p]){
printf("2\n");
flag=1;
break;
}
p++;
}
}
if(flag==1)
continue;
for(int i=3;;i++){
if((n-i)%6==0){
printf("%d\n",i);
break;
}
}
}
return 0;
}

Some files were not shown because too many files have changed in this diff Show More