1001 Aaronson
简单模拟
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<bitset>
using namespace std;
int T;
int x,y,z,zz;
int flag;
int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%d %d",&x,&z);
zz=1;
flag=0;
for (int i=1;i<=z;i++)
{
zz*=2;
if (zz>=x)
{
flag=1;
break;
}
}
if (flag) y=0;
else y=x/zz,x-=x/zz*zz;
while (x) y+=x&1,x>>=1;
printf("%d\n",y);
}
return 0;
}
1002 Bellovin
很容易发现,字典序最小的答案就是
f
数组的值,所以此题就是裸的
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<bitset>
using namespace std;
int T,n;
int a[410000],f[410000];
int c[410000],tail;
int check(int x)
{
if (x>c[tail]) c[++tail]=x;
int l=1,r=tail,ans=0;
while (l<=r)
{
int mid=(l+r)/2;
if (c[mid]<x) ans=mid,l=mid+1;
else r=mid-1;
}
return ans;
}
int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
memset(c,0,sizeof(c));
tail=0;
for (int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if (i==1) c[++tail]=a[i],f[1]=1;
else
{
int pos=check(a[i]);
f[i]=pos+1;
if (c[pos+1]>a[i]) c[pos+1]=a[i];
}
}
for (int i=1;i<=n;i++)
{
printf("%d",f[i]);
if (i!=n) printf(" ");
else printf("\n");
}
}
return 0;
}
1003 Colmeraner
预处理出不同区间的 a∗b 的值,然后求下每个点向四个方向能延伸的最大距离,求和即可。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<bitset>
using namespace std;
typedef unsigned int ui;
int T;
int n,m;
int a[1100][1100];
ui l[1100][1100],r[1100][1100],u[1100][1100],d[1100][1100];
ui f[1100][1100];
int main()
{
for (ui i=1;i<=1000;i++)
for (ui j=1;j<=1000;j++)
f[i][j]=f[i][j-1]+(i*(2*j+i-1)/2);
scanf("%d",&T);
while (T--)
{
scanf("%d %d",&n,&m);
memset(l,0,sizeof(l));
memset(r,0,sizeof(r));
memset(u,0,sizeof(u));
memset(d,0,sizeof(d));
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
scanf("%u",&a[i][j]);
for (int i=1;i<=n;i++)
{
l[i][1]=1;
for (int j=2;j<=m;j++)
{
int now=j;
while (now!=1&&a[i][j]<a[i][now-1]) now=l[i][now-1];
l[i][j]=now;
}
}
for (int i=1;i<=n;i++)
{
r[i][m]=m;
for (int j=m-1;j>=1;j--)
{
int now=j;
while (now!=m&&a[i][j]<a[i][now+1]) now=r[i][now+1];
r[i][j]=now;
}
}
for (int i=1;i<=m;i++)
{
u[1][i]=1;
for (int j=2;j<=n;j++)
{
int now=j;
while (now!=1&&a[j][i]>a[now-1][i]) now=u[now-1][i];
u[j][i]=now;
}
}
for (int i=1;i<=m;i++)
{
d[n][i]=n;
for (int j=n-1;j>=1;j--)
{
int now=j;
while (now!=n&&a[j][i]>a[now+1][i]) now=d[now+1][i];
d[j][i]=now;
}
}
ui ans=0;
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
ans+=f[j-l[i][j]+1][r[i][j]-j+1]*f[i-u[i][j]+1][d[i][j]-i+1]*a[i][j];
cout<<ans<<endl;
}
return 0;
}
1004 Dertouzos
答案应该为
1
到
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<bitset>
using namespace std;
typedef long long LL;
#define maxn 2000010
bool valid[maxn];
int primm;
int prim[maxn];
void getPrime(int n)
{
memset(valid,true,sizeof(valid));
for (int i=2;i<=n;i++)
{
if (valid[i])
{
prim[++primm]=i;
for (int j=2;j<=n/i;j++)
valid[i*j]=0;
}
}
}
int n,d,g;
int main()
{
getPrime(maxn-1);
int T;
scanf("%d",&T);
while (T--)
{
scanf("%d %d",&n,&d);
int i;
for (i=1;i<=primm;i++)
{
if (d*prim[i]>=n) break;
if (d<prim[i]) break;
if (d%prim[i]==0) break;
}
if (d*prim[i]>=n||d<prim[i]) i--;
printf("%d\n",i);
}
return 0;
}
1005 Eade
先用ST表预处理数组,然后对于序列中的每个点二分出其能作为最大值的最大的区间
[l,r]
。将所有区间排序,对于区间
[l,r]
相同的点,其数值也一定相同,设其为
x
,且出现的位置为
这一部分可以用FFT快速求得。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<bitset>
using namespace std;
typedef long long LL;
const double PI = acos(-1.0);
struct complex
{
double r,i;
complex(double _r = 0.0,double _i = 0.0){r = _r; i = _i;}
complex operator +(const complex &b){return complex(r+b.r,i+b.i);}
complex operator -(const complex &b){return complex(r-b.r,i-b.i);}
complex operator *(const complex &b){return complex(r*b.r-i*b.i,r*b.i+i*b.r);}
}x1[400000],x2[400000];
void change(complex y[],int len)
{
int i,j,k;
for(i=1,j=len/2;i<len-1;i++)
{
if(i<j)swap(y[i],y[j]);
k=len/2;
while(j>=k)
{
j-=k;
k/=2;
}
if(j<k) j+=k;
}
}
void fft(complex y[],int len,int on)
{
change(y,len);
for(int h = 2; h <= len; h <<= 1)
{
complex wn(cos(on*2*PI/h),sin(on*2*PI/h));
for(int j = 0;j < len;j+=h)
{
complex w(1,0);
for(int k = j;k < j+h/2;k++)
{
complex u = y[k], t = w*y[k+h/2];
y[k] = u+t;
y[k+h/2] = u-t;
w = w*wn;
}
}
}
if(on == -1)
for(int i = 0;i < len;i++)
y[i].r /= len;
}
int T,n;
int a[61000];
const int MAX=100000;
struct node
{
int l,r,id;
}b[61000];
int cmp(node a,node b)
{
if (a.l!=b.l) return a.l<b.l;
else if (a.r!=b.r) return a.r<b.r;
else return a.id<b.id;
}
int stTable[MAX][32];
int preLog2[MAX];
void st_prepare(int n,int *array)
{
preLog2[1]=0;
for (int i=2;i<=n;i++)
{
preLog2[i]=preLog2[i-1];
if ((1<<preLog2[i]+1)==i)
{
preLog2[i]++;
}
}
for (int i=n;i>=1;i--)
{
stTable[i][0]=array[i];
for (int j=1;(i+(1<<j)-1)<=n;j++)
{
stTable[i][j]=max(stTable[i][j-1],stTable[i+(1<<j-1)][j-1]);
}
}
}
int query_max(int l,int r)
{
int len=r-l+1,k=preLog2[len];
return max(stTable[l][k],stTable[r-(1<<k)+1][k]);
}
int call(int x,int y)
{
int l=0,r=y-1,ans;
while (l<=r)
{
int mid=(l+r)/2;
if (query_max(y-mid,y)<=x) ans=mid,l=mid+1;
else r=mid-1;
}
return y-ans;
}
int calr(int x,int y)
{
int l=0,r=n-y,ans;
while (l<=r)
{
int mid=(l+r)/2;
if (query_max(y,y+mid)<=x) ans=mid,l=mid+1;
else r=mid-1;
}
return y+ans;
}
int num;
LL c[61000];
LL k[61000];
void work()
{
int N=1;
while (N<2*(num+1)) N*=2;
for (int i=0;i<=num;i++)
x1[i]=complex((double)c[i],0.0);
for (int i=num+1;i<N;i++)
x1[i]=complex(0.0,0.0);
for (int i=0;i<=num;i++)
x2[i]=complex((double)c[num-i],0.0);
for (int i=num+1;i<N;i++)
x2[i]=complex(0.0,0.0);
fft(x1,N,1);
fft(x2,N,1);
for (int i=0;i<N;i++)
x1[i]=x1[i]*x2[i];
fft(x1,N,-1);
for (int i=0;i<=num;i++)
{
LL d=(LL)(x1[num-i].r+0.5);
k[i]+=d;
}
}
int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
st_prepare(n,a);
memset(k,0,sizeof(k));
for (int i=1;i<=n;i++)
{
b[i].l=call(a[i],i);
b[i].r=calr(a[i],i);
b[i].id=i;
}
sort(b+1,b+1+n,cmp);
num=1;
c[0]=b[1].id-b[1].l+1;
for (int i=2;i<=n;i++)
{
if (b[i].l==b[i-1].l&&b[i].r==b[i-1].r)
{
c[num++]=b[i].id-b[i-1].id;
}
else
{
c[num]=b[i-1].r-b[i-1].id+1;
work();
num=1;
c[0]=b[i].id-b[i].l+1;
}
}
c[num]=b[n].r-b[n].id+1;
work();
LL ans=0;
for (LL i=1;i<=n;i++)
ans+=i^k[i];
cout<<ans<<endl;
}
return 0;
}