#Day1
孤独一生(sprung)
Description
下课了,Polo来到球场,但他到了之后才发现…..被放了飞机…… 无事可做的他决心找点乐子,比方说……跳台阶…… 球场边有N个台阶拍成一行,第i个台阶的高度是Hi(0<Hi<=10^9),第0个台阶,也就是地面的高度为0。Polo打算把这N个台阶分成两个集合Sa,Sb(可以为空),对于一个台阶集合S={P1,P2,…P|S|},其中P1<P2<…<P|S|,他需要花费的体力值来完成。 现在他希望两次跳跃所需的总体力值最小,你能帮帮他吗?
input
第一行一个数N。
第二行N个整数Hi。
ouput
一行一个整数,表示最小的总体力值。
sample input
3
1 3 1
sample output
4
hint
对于10%的数据N<=20。
对于20%的数据N<=100。
对于50%的数据N<=5000。
对于100%的数据1<=N<=500000。
solution
等补坑
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 500020;
struct data{
int h,id;
};
data a[N];
int n;
int g[N],f1[N],f2[N],sum[N],rk[N],h[N];
int ans;
inline int lowbit(int x)
{
return x&(-x);
}
inline int query1(int x)
{
int ret = 2e16;
for(;x;x-=lowbit(x)) ret = min(ret,f1[x]);
return ret;
}
inline int query2(int x)
{
int ret = 2e16;
for(;x;x-=lowbit(x)) ret = min(ret,f2[x]);
return ret;
}
inline void add1(int i,int p)
{
for(;i<=n+1;i+=lowbit(i))
f1[i] = min(f1[i],p);
}
inline void add2(int i,int p)
{
for(;i<=n+1;i+=lowbit(i))
f2[i] = min(f2[i],p);
}
bool cmp(data c,data d)
{
return c.h>d.h;
}
signed main()
{
scanf("%lld",&n);
for(int i=1;i<=n;i++)
scanf("%lld",&a[i].h),a[i].id=i,h[i] = a[i].h;
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++)
rk[a[i].id] = i;
for(int i=1;i<=n;i++)sum[i] = sum[i-1] + abs(h[i]-h[i-1]),f1[i] = f2[i] = 2e16;
g[1] = h[1];
ans = sum[n];
rk[0] = n+1;
add1(rk[0],g[1] + h[0] - sum[1]);
add2(n-rk[0]+2,g[1] - h[0] - sum[1]);
for(int i=2;i<=n;i++)
{
g[i] = min(query1(rk[i]) - h[i],query2(n-rk[i]+2) + h[i]) + sum[i-1];
ans = min(sum[n] - sum[i] + g[i],ans);
add1(rk[i-1],g[i] + h[i-1] - sum[i] );
add2(n-rk[i-1]+2,g[i] - h[i-1] - sum[i]);
}
printf("%lld\n",ans);
return 0;
} 地壳运动
Description
JZ是一个坐落在地壳运动活跃的山区的城市,常受地质灾害的袭击。 城市中建立了N个应急避难所以躲避灾害,这些避难所从1~N编号。此外有M条道路连接这些避难所,所有避难所间均可通过这M条道路直接或间接到达。由于是在规划良好的市区,道路可以由若干个平行于x或y坐标轴的线段组成,所以避难所xi和yi之间的道路可以用(ui,vi)来表示,道路的长度为ui+vi。由于地壳运动会导致地面拉伸或收缩,可用两个实数k1,k2描述城市的伸缩程度,此时某条道路的实际长度变为uik1+vik2。有若干个独立的询问,每次询问给出k1和k2,政府都希望在此地壳运动前提下,以最小的花费维护其中一些道路,使得只用这些被维护的道路仍能使所有避难所间相互连通。因为花费与道路的实际总长成正比,所以你需要对每一次询问求出被维护道路的最短实际总长。
input
第一行三个整数N,M,Q,分别表示避难所数量、道路数量、询问数量。
接下来M行每行四个整数xi,yi,ui,vi。xi,yi表示道路连接的两个避难所编号,ui,vi意义如上文所述。
最后Q行每行两个实数,表示每次询问的的k1和k2。
ouput
输出Q行,每行一个实数,表示实际总长,保留三位小数。
sample input
4 8 3
2 1 3 6
3 2 0 7
4 1 7 0
1 4 4 6
2 1 2 7
1 2 2 10
2 2 5 5
4 4 8 9
0.626436771146 0.472537839745
0.977631137354 0.190235819672
0.418883351791 0.221987861358
sample output
hint
对于30%的数据,N<=30,M<=3000,Q<=3000。
对于另外30%的数据,N<=20,M<=25000,Q<=10000
对于100%的数据,N<=35,M<=25000,Q<=200000,1<=xi,yi<=N,0<=ui,vi<=10^6。
solution
#include<bits/stdc++.h>
using namespace std;
typedef double db;
const int N = 1e5+20;
struct edge{
int a,b;
db x,y;
};
struct mst{
db k1,k2,a,b;
};
db k1,k2;
bool cmp(edge a,edge b)
{
return a.x*k1 + a.y*k2 < b.x*k1+b.y*k2;
}
int f[N];
int find(int x)
{
if(f[x] != x) f[x] = find(f[x]);
return f[x];
}
edge a[N];
mst tu[N];
int n,m;
void krs(mst &u)
{
k1=u.k1,k2=u.k2;
u.a=u.b=0;
sort(a+1,a+m+1,cmp);
int lim = 0;
for(int i=1;i<=n;i++) f[i] = i;
for(int i=1;i<=m;i++)
{
int b =find(a[i].a),c=find(a[i].b);
if(b!=c)
{
f[b] = c;
u.a += a[i].x;
u.b += a[i].y;
lim ++ ;
}
if(lim == n-1) break;
}
}
db xielv(mst x,mst y)
{
return (double)(x.a-y.a)/(x.b-y.b);
}
int cnt;
mst fir,las;
void dfs(mst l,mst r)
{
mst mid;
mid.k1 = fabs(l.b-r.b);
mid.k2 = fabs(l.a-r.a);
krs(mid);
if(l.b==r.b||l.b==mid.b||xielv(l,r) == xielv(l,mid))
return ;
dfs(l,mid);
tu[++cnt] = mid;
dfs(mid,r);
}
int main()
{
int q;
scanf("%d%d%d",&n,&m,&q);
for(int i=1;i<=m;i++)
scanf("%d%d%lf%lf",&a[i].a,&a[i].b,&a[i].x,&a[i].y);
fir.k1 = las.k2 = 1;
fir.k2 = las.k1 = 0;
krs(fir);
krs(las);
tu[++cnt] = fir;
dfs(fir,las);
tu[++cnt] = las;
for(int i=1;i<=q;i++)
{
scanf("%lf%lf",&k1,&k2);
db ans = 20000000000000.00;
int l=1,r=cnt,mid1,mid2;
while(l<r)
{
if(r-l<=2) break;
else
{
mid1= l+(r-l+1)/3;
mid2= l+(r-l+1)/3*2;
if(tu[mid1].a*k1+tu[mid1].b*k2 < tu[mid2].a*k1 + tu[mid2].b*k2)
r = mid2; else l = mid1;
}
}
for(int j=l;j<=r;j++)
ans = min(ans,tu[j].a*k1+tu[j].b*k2);
printf("%.3lf\n",ans);
}
return 0;
}树的计数(counting)
Description
给出两个整数n和d,求出有n个节点并且两个节点间最长距离为d的标号树的个数。
标号树即是树上每个结点都标有一个不同的编号。
input
每行包含两个整数n和d。
每个文件包含多行,读入直到文件结束。
ouput
对于每一行,输出一行包含一个整数。
sample input
2 1
3 1
3 2
4 3
sample output
1
0
3
12
hint
对于所有数据,1≤n≤50,0≤d≤n。
solution
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int m = 10;
const ll w = 1e8;
int n,d;
struct big{
ll a[12];
big()
{
for(int i=0;i<=m;i++) a[i] = 0;
}
};
big operator + (big a,big b)
{
for(int i=0;i<=m;i++)
a.a[i] += b.a[i];
for(int i=0;i<=m;i++)
a.a[i+1] += a.a[i] / w,a.a[i] %= w;
return a;
}
big operator - (big a,big b)
{
for(int i=0;i<=m;i++)
a.a[i]-=b.a[i];
for(int i=0;i<=m;i++) if(a.a[i]<0){
a.a[i] += w;
a.a[i+1] --;
}
return a;
}
big operator * (big a,big b)
{
big c=big();
for(int i=0;i<=m;i++)
for(int j=0;j<=m-i;j++)
c.a[i+j] += a.a[i] * b.a[j];
for(int i=0;i<=m;i++)
for(int i=0;i<=m;i++)
c.a[i+1] += c.a[i]/w , c.a[i]%=w;
return c;
}
big operator * (big a,ll b)
{
for(int i=0;i<=m;i++)
a.a[i] *= b;
for(int i=0;i<=m;i++)
{
a.a[i+1] += a.a[i]/w;
a.a[i] %= w;
}
return a;
}
big operator / (big a,ll b)
{
ll y = 0;
for(int i=m;i>=0;i--)
{
a.a[i] += y*w;
y = a.a[i] % b;
a.a[i] /= b;
}
return a;
}
void print(big a)
{
int a0 = m;
while(a0>=0 && !a.a[a0]) a0--;
if(a0<0)
printf("0");
else
{
printf("%lld",a.a[a0]);
for(int i=a0-1;i>=0;i--)
printf("%08lld",a.a[i]);
}
printf("\n");
}
const int N = 52;
big c[N][N],f[N][N],g[N][N];
big ans[N][N];
int main()
{
n=50;
for(int i=0;i<=n;i++)
{
c[i][0].a[0] = 1;
for(int j=1;j<=i;j++)
c[i][j] = c[i-1][j] + c[i-1][j-1];
}
for(int i=0;i<=n;i++)
f[1][i].a[0] = g[0][i].a[0] = 1;
for(int i=2;i<=n;i++)
for(int j=1;j<=n;j++)
for(int x=1;x<=i-1;x++)
f[i][j] = f[i][j] + f[x][j-1] *f[i-x][j] * c[i-2][x-1] * i / (i-x);
for(int i=0;i<=n;i++)
{
for(int j=1;j<=n;j++)
g[i][j] = f[i][j] - f[i][j-1];
g[i][0] = f[i][0];
}
for(n=1;n<=50;n++)
{
if(n==1) ans[n][0].a[0] = 1;
for(d=1;d<=n;d++)
{
int w = d/2;
if(d&1)
{
for(int x=1;x<=n;x++)
ans[n][d] = ans[n][d] + g[x][w] * g[n-x][w] * c[n-1][x-1];
}
else{
ans[n][d] = g[n][w];
for(int x=0;x<=n;x++) ans[n][d]=ans[n][d] - g[x][w-1]*f[n-x][w-1]*c[n][x];
}
}
}
while(~scanf("%d%d",&n,&d))
print(ans[n][d]);
return 0;
}物语(monogatari)
Description
某一天,少年邂逅了同病相连的IA。见面后,IA一把牵起少年的手,决定和他一起逃离部落,离开这个无法容身的是非之地。
要逃离部落,少年和IA就需要先选择一条耗时最少的路线,从而避免被部落的大人们抓到。部落可以大致分为N个区域,少年和IA在区域1,部落的出口设在区域N。此外部落还有M条连接两个区域道路。道路是无向的,没有一条道路的两端连接相同的区域,也没有两条道路所连接的两个区域完全相同。对于其中前(M−1)条道路,其通过时间是确定的,但最后一条道路,由于地理因素,通过其的时间会不断变化。
现在,少年和IA得知了在K个不同的时段里,通过第M条道路的时间,请您分别计算出在这K 个时段中逃离部落的最少时间,以帮助他们确定行动的时刻。
input
第一行三个整数N,M,K,分别表示区域数,道路数,询问数。
接下来M−1行每行三个整数ui,vi,wi(ui≠vi,1≤ui,vi≤N,0<wi≤10^9),表示这条道路连接的区域和通过时间。
紧接着是两个整数ui,vi(ui≠vi,1≤ui,vi≤N),表示第M条道路连接的区域。
最后K行,每行一个正整数xi(0<xi≤10^9),表示目前第M条道路的通过时间。
ouput
输出共计K行,每行一个整数,表示对应时段逃离部落的最短时间。如果在该时段内无法逃离,输出“+Inf”。
sample input
输入1:
4 5 4
1 2 7
1 3 4
2 4 3
3 4 6
2 3
1
2
4
6
输入2:
4 3 1
1 2 7
1 3 4
2 3
9
sample output
输出1:
8
9
10
10
输出2:
+Inf
hint
N<= 200000 , M<= 500000 ,K<=30000
solution
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 200020;
const int M = 1000002;
int nex[M],head[N],to[M];
ll dis[2][N],w[M];
int vis[N];
int n,m,qu;
const ll inf = 2e16;
struct node{
int x;
ll dis;
node(int a,ll b)
{
x=a;
dis=b;
}
bool operator <(const node &a)const
{
return a.dis<dis;
}
};
int cnt;
int s,t;
priority_queue<node> q;
void ae(int x,int y,ll z)
{
nex[++cnt] = head[x];
to[cnt] = y;
w[cnt] = z;
head[x] = cnt;
}
void dij(int s,int d)
{
for(int i=1;i<=n;i++)
dis[d][i] = inf,vis[i] = 0;
q.push(node(s,0));
dis[d][s] = 0;
while(!q.empty())
{
int u = q.top().x;
q.pop();
if(vis[u]) continue;
for(int i=head[u];i;i=nex[i])
{
int v = to[i];
if(dis[d][v] > dis[d][u] + w[i])
{
dis[d][v] = dis[d][u] + w[i];
q.push(node(v,dis[d][v]));
}
}
}
}
ll sol(int u)
{
return dis[u][s] + dis[1-u][t];
}
int main()
{
freopen("monogatari.in","r",stdin);
freopen("monogatari.out","w",stdout);
scanf("%d%d%d",&n,&m,&qu);
int x,y;
ll z;
for(int i=1;i<m;i++)
scanf("%d%d%lld",&x,&y,&z),ae(x,y,z),ae(y,x,z);
scanf("%d%d",&s,&t);
dij(1,0);
dij(n,1);
ll fans = min(sol(0),sol(1)),tans = dis[0][n];
if(fans >= inf && tans >= inf)
{
while(qu--)printf("+Inf\n");
return 0;
}
while(qu--)
{
scanf("%lld",&z);
printf("%lld\n",min(tans,fans+z));
}
return 0;
}消息传递(news)
Description
H国的社会等级森严,除了国王之外,每个人均有且只有一个直接上级,当然国王没有上级。如果A是B的上级,B是C的上级,那么A就是C的上级。绝对不会出现这样的关系:A是B的上级,B也是A的上级。
最开始的时刻是0,你要做的就是用1单位的时间把一个消息告诉某一个人,让他们自行散布消息。在任意一个时间单位中,任何一个已经接到消息的人,都可以把消息告诉他的一个直接上级或者直接下属。
现在,你想知道:
1.到底需要多长时间,消息才能传遍整个H国的所有人?
2.要使消息在传递过程中消耗的时间最短,可供选择的人有那些?
input
输入文件的第一行为一个整数N(N≤200000),表示H国人的总数,假如人按照1到n编上了号码,国王的编号是1。
第2行到第N行(共N-1行),每一行一个整数,第i行的整数表示编号为i的人直接上级的编号。
ouput
输出共计两行:
第一行为一个整数,表示最后一个人接到消息的最早时间。
第二行有若干个数,表示可供选择人的编号,按照编号从小到大的顺序输出,中间用一个空格隔开。
sample input
输入1:
4
1
1
1
输入2:
8
1
1
3
4
4
4
3
sample output
输出1:
4
1 2 3 4
输出2:
5
3 4 5 6 7
hint
对于20%的数据,满足N<=3000;
对于50%的数据,满足N<=20000;
对于100%的数据,满足N<=200000;
solution
#include<bits/stdc++.h>
using namespace std;
const int N = 202000;
int ti[N];
int ct[N];
vector<int> son[N],pre[N],succ[N];
int ans[N];
int fans;
int rk[N];
bool cmp(int a,int b)
{
return ti[a]>ti[b];
}
void dfs1(int u)
{
int maxx = 0;
for(int i=0;i<ct[u];i++)
dfs1(son[u][i]);
sort(son[u].begin(),son[u].end(),cmp);
for(int i=0;i<ct[u];i++)
{
pre[u][i] = maxx;
maxx = max(maxx,i + ti[son[u][i]] + 1);
}
maxx = 0;
for(int i=ct[u]-1;i>=0;i--)
{
succ[u][i] = maxx;
maxx = max(maxx,i + ti[son[u][i]] + 1);
}
ti[u] = maxx;
}
void dfs2(int u,int pree)
{
ans[u] = ti[u];
fans = min(ans[u],fans);
int bre=0,aft=0;
if(ct[u]!=0)for(int i=rk[u];i<ct[u];i++) aft = max(aft,ti[son[u][i]]+i+1);
for(int i=0;i<ct[u];i++)
{
int v = son[u][i];
if(u==1)
{
ti[u] = max(pre[u][i],succ[u][i]-1);
}
else
{
if(i<rk[u])
{
ti[u] = max(pre[u][i],succ[u][i]-1);
ti[u] = max(ti[u],aft);
ti[u] = max(ti[u],ti[pree] + rk[u] + 1 - 1);
}
else
{
ti[u] = max(pre[u][i],succ[u][i]);
ti[u] = max(ti[u],bre);
ti[u] = max(ti[u],ti[pree] + rk[u] + 1 );
bre = max(bre,ti[v]+i+1+1);
}
}
int l = 0,r=ct[v];
if(ct[v] == 0)
{
ti[v] = ti[u] + 1;
}
else
{
while(l<r)
{
int mid = l + r >>1;
if(ti[son[v][mid]]>ti[u]) l = mid + 1;
else r = mid ;
}//l:<=u
if(!l)
{
ti[v] = max(ti[u]+1,ti[v]+1);
}
else if(l==ct[v])
{
ti[v] = max(ti[v],ti[u] + ct[v] + 1);
}
else
{
ti[v] = max(pre[v][l],l+1+ti[u]);
ti[v] = max(ti[v],succ[v][l-1] + 1);
}
rk[v] = l;
}
dfs2(v,u);
ti[u] = ans[u];
}
}
int main()
{
freopen("news.in","r",stdin);
freopen("news.out","w",stdout);
int n,x;
scanf("%d",&n);
for(int i=2;i<=n;i++)
scanf("%d",&x),son[x].push_back(i),
pre[x].push_back(0),succ[x].push_back(0),ct[x] ++;
dfs1(1);
fans =2e9;
dfs2(1,0);
printf("%d\n",1+fans);
for(int i=1;i<=n;i++) if(ans[i] == fans) printf("%d ",i);
return 0;
}随机数生成器
Description
NOI 2014solution
#include<bits/stdc++.h>
using namespace std;
const int maxn = 5020;
int N,n,m,qu;
int a,b,c,d,x[maxn*maxn],t[maxn*maxn];
bool bo[maxn][maxn];
int fx[4],fy[4];
struct node{
int x,y;
inline node(int a,int b)
{
x=a;
y=b;
}
};
queue<node> q;
inline bool pd(int x,int y)
{
if(x<1 || y<1 || x>n || y>m) return 0;
return 1;
}
inline void bfs(int x,int y,int f)
{
if(!pd(x,y) || bo[x][y]) return;
q.push(node(x,y));
while(!q.empty())
{
x = q.front().x,y=q.front().y;
q.pop();
for(register int i=0;i<2;i++)
{
int nx = x + fx[i+f],ny = y + fy[i+f];
if(pd(nx,ny) && !bo[nx][ny])
{
bo[nx][ny] = 1;
q.push(node(nx,ny));
}
}
}
}
int main()
{
long long l;
scanf("%lld%d%d%d%d",&l,&a,&b,&c,&d);
scanf("%d%d%d",&n,&m,&qu);
N = n*m;
fx[0] = 1;fy[0] = 0;
fx[2] = -1;fy[2] = 0;
fx[1] = 0;fy[1] = -1;
fx[3] = 0;fy[3] = 1 ;
for(register int i=1;i<=N;i++) t[i]=i;
for(register int i=1;i<=N;i++)
l=(l*l*a+l*b+c)%d,x[i]=l;
for(register int i=1;i<=N;i++)
swap(t[i],t[x[i]%i+1]);
int y,z;
for(register int i=1;i<=qu;i++)
{
scanf("%d%d",&y,&z);
swap(t[y],t[z]);
}
for(register int i=1;i<=N;i++)
x[t[i]] = i;
int ux,uy;
for(register int i=1;i<=N;i++)
{
ux = (x[i] - 1) /m + 1;
uy = (x[i] - 1) % m + 1;
if(!bo[ux][uy])
{
printf("%d ",i);
bfs(ux+1,uy-1,0),bfs(ux-1,uy+1,2);
}
}
return 0;
}
/*
654321 209 111 23 70000001
10 10 0
*/PPMM
Description
请实现一个队列,支持如下四种操作。
PUSH X:将整数X加入到队尾。
POP:将队首的数字出队。
MINUS:队列中所有数字都变为其相反数,即X←-X。
MAX:返回队列中最大的数。
对于操作POP、MINUS和MAX,如果队列为空,则直接忽略它们。
现在给定一个操作序列,对于其中的每个MAX操作,如果它没有被忽略,请输出它所返回的数。
input
输入第一行为正整数N,表示操作的个数。
接下来N行,每行为一个操作。所有操作都用大写字母表示。PUSH操作后会接着一个正整数X。
ouput
对于每个没有被忽略的MAX操作,输出其返回的数字,每个数字各占一行。
sample input
6
PUSH -2
MINUS
PUSH -1
MAX
POP
MAX
sample output
2
-1
hint
-2^31 < X < 2^31
对于20%的数据有 N ≤ 100
对于50%的数据有N ≤ 10,000
对于100%的数据有N ≤ 2,000,000
solution
#include<bits/stdc++.h>
using namespace std;
int n;
int lag;
char s[10];
const int N = 2e6+20;
int qmax[N],qmin[N],h1,h2,t1,t2;
int s1[N],s2[N];
int cnt,del;
int main()
{
scanf("%d",&n);
lag = 1;
h1=h2 = 1;
t1=t2 = 0;
for(int i=1;i<=n;i++)
{
scanf("%s",&s);
if(s[1] == 'U')
{
int x;
scanf("%d",&x);
x*= lag;
++cnt;
while(h1<=t1 && x>=qmax[t1]) t1--;
qmax[++t1] = x;
s1[t1] = cnt;
while(h2<=t2 && x<=qmin[t2]) t2--;
qmin[++t2] = x;
s2[t2] = cnt;
}
else if(s[1] == 'O')
{
del++;
if(del > cnt) del = cnt;
while(s1[h1] <= del && h1<=t1) h1++;
while(s2[h2] <= del && h2<=t2) h2++;
}
else if(s[1] == 'I')
lag *= -1;
else
{
if(lag>0)
{
if(h1<=t1)printf("%d\n",qmax[h1]);
}
else if(h2<=t2)printf("%d\n",-qmin[h2]);
}
}
return 0;
}Distinct Paths
Description
小首有一个n*m的木板,一些块已经被涂上给出的k种颜色中的一种。你需要把每个没涂色的块涂色使得从左上角到右下角的每条路径都不会经过两个颜色一样的块。路径只能向右或向下走。 输出答案%1000000007。
input
第一行3个整数n,m,k。 接下来n行,每行m个整数表示木板,其中第一行表示最上的m块,每行第一个数表示最左列的块。 对于每个整数,如果为0,表示未涂色,否则表示涂上颜色的编号。
ouput
一个答案表示方案数。
sample input
输入1:
2 2 4
0 0
0 0
输入2:
2 2 4
1 2
2 1
输入3:
2 6 10
1 2 3 4 5 6
0 0 0 0 0 0
输入4:
5 6 10
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
sample output
输出1:
48
输出2:
0
输出3:
4096
输出4:
3628800
hint
对于40%的数据,1<=n,m<=3
对于100%的数据,1<=n,m<=1000,1<=k<=10
solution
#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;
int f[22][22];
int col[22][22];
int cnt[12];
int n,m,k;
int dfs(int x,int y)
{
if(x==n+1) return 1;
int ty = y+1,tx=x,can=0;
if(ty>m) ty=1,tx++;
f[x][y] = f[x-1][y] | f[x][y-1];
for(int i=1;i<=k;i++)
if(!(f[x][y] & (1<<(i-1)))) can ++;
if(can < n+m-x-y+1) return 0;
int rec=0,lag=0,ret=0;
if(!col[x][y])
{
for(int i=1;i<=k;i++)
if(!(f[x][y] & (1<<(i-1))))
{
if(!cnt[i])
{
if(lag) ret+=rec,ret%=mod;
else
{
lag = 1;
cnt[i]++;
f[x][y] |= (1<<(i-1));
rec = dfs(tx,ty);
f[x][y] ^= (1<<(i-1));
cnt[i]--;
ret += rec;
ret %= mod;
}
}
else
{
f[x][y] |= (1<<(i-1));
ret += dfs(tx,ty);
ret %= mod;
f[x][y] ^= (1<<(i-1));
}
}
}
else
{
if((f[x][y] & (1<<(col[x][y]-1)))==0)
{
f[x][y] |= 1<<(col[x][y]-1);
ret += dfs(tx,ty);
ret %=mod;
f[x][y] ^= 1<<(col[x][y]-1);
}
}
return ret;
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
if(n+m-1>k)
{
printf("0\n");
return 0;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&col[i][j]),cnt[col[i][j]]++;
printf("%d\n",dfs(1,1));
return 0;
} Philips and Calculator
Description
Philips得到了一个计算器,这个计算器有两个整数单元,一开始,第一个单元包含数字1,第二个单元包含数字0。 这个计算器支持一以下两种操作:
1.假设第一个单元的数字为a,第二个单元的数字为b,那么将第二个单元的数字改成b+1。
2.假设第一个单元的数字为a,第二个单元的数字为b,那么将第一个单元的数字改成a*b。
现在Philips想知道,有多少个正整数x(l<=x<=r)满足, 存在一种方式从计算器初始状态开始,操作不超过p步之后使得第一个单元中的数字为x。
input
共一行,为三个空格隔开的正整数(2<=l<=r<=10^9, 1<=p<=100)
ouput
输出仅一行一个整数ans,即为问题所求。
sample input
输入1:
2 10 3
输入2:
2 111 100
输入3:
2 111 11
sample output
输出1:
1
输出2:
106
输出3:
47
hint
对于30的数据p<=16。
对于100%的数据2<=l<=r<=10^9, 1<=p<=100。
solution
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
const int N = 3e6+20; //2944730
int cnt1,cnt;
int pri[120];
int a[N],ans[N],f[N];
int l,r;
inline int read()
{
int x=0; char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9'){ x=(x<<3)+(x<<1)+(ch^48); ch=getchar(); }
return x;
}
inline void dfs(int x,int k)
{
a[++cnt1] = x;
for(register int j=k;j<=cnt;j++)
if(1LL * x * pri[j] <= r) dfs(x*pri[j],j);
else return;
}
int main()
{
register int p;
l=read();r=read();p=read();
for(register int i=2;i<=p;i++)
{
int flag = 0;
for(register int j=2;j*j<=i;j++) if(i%j==0)
{
flag = 1;
break;
}
if(!flag) pri[++cnt] = i;
}
dfs(1,1);
sort(a+1,a+cnt1+1);
for(register int i=1;i<=cnt1;i++) f[i] = 1e9;
f[1] = 0;
for(register int i=2;i<=p-1;i++)
{
register int k=1;
for(register int j=i;j<=cnt1;j++) if(a[j]%i==0)
{
while(a[k]*i<a[j]) k++;
if(k>cnt1) break;
f[j] = min(f[j],f[k]+1);
if(f[j] + i <= p) ans[j] = 1;
}
}
register int sum = 0;
for(register int i=1;i<=cnt1;i++)
if(ans[i] && l<=a[i] && a[i]<=r) sum++;
printf("%d\n",sum);
return 0;
}数列
Description
Xn = a^2 Xn-1 + bXn-1 + c(mod m)
求Xn
input
一行,六个整数,分别表示 x0, a, b, c, n, m。
ouput
一行,一个整数,表示 xn mod m 的值。
sample input
1 1 1 1 5 1000000000
sample output
133904603
hint
对于30%的数据,n<=1e6,m<=1e9 对于另外30%的数据,m<=1e6,n<=1e9 对于另外40%的数据,n<=1e9,m<=1e9, 2a|b, m是质数,4ac = b^2-2b
solution
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6+20;
int s[N],pos[N];
int ksm(int x,int y,int p)
{
int ret = 1;
for(;y;y>>=1)
{
if(y&1)ret = ret * x % p;
x=x*x%p;
}
return ret;
}
signed main()
{
int x,a,b,c,n,m,t;
scanf("%lld%lld%lld%lld%lld%lld",&x,&a,&b,&c,&n,&m);
x%=m ; a%= m;
b%=m ; c%= m;
if(n<=1e6)
{
for(int i=1;i<=n;i++) x=(a*x%m*x%m+b*x%m+c)%m;
printf("%lld\n",x);
}
else
if(m<=1e6)
{
s[0] = x;
memset(pos,-1,sizeof(pos));
pos[x] = 0;
int dis = 1e9;
for(int i=1;i<=n;i++)
{
x=(a*x%m*x%m+b*x%m+c)%m;
if(pos[x]!=-1)
{
dis = i - pos[x];
break;
}
s[i] = x;
pos[x] = i;
}
n=(n-pos[x])%dis+pos[x];
printf("%lld\n",s[n]);
}
else
{
int tep = b/(2*a);
int x1 = ksm(2,n,m-1),x2;
x2 = ksm(x+tep,x1,m);
x1 = (x1-1+m-1)%(m-1);
x1= ksm(a,x1,m);
x2 = x1 * x2 %m;
x2 = x2 - tep + m ;
x2 %= m;
printf("%lld\n",x2);
}
return 0;
}
圈地为王
Description
在 n 行 m 列的网格中,你要圈一些地。
你从左上角出发,最后返回左上角,路径内部的区域视为被你圈住。 你不可以进入网格内部, 只能在边上行走。 你的路径不能在左上角以外自交, 但是边足够宽, 你可以重复经过而不自交。
网格中有一些格子对你很重要,你要尽量圈住它;而另一些格子对你有坏处,你不能圈住它。
求圈住 i 个重要的格子的最小路径长度。
input
n 行,每行 m 个字符。
‘I’表示重要的格子, ‘X’表示有坏处的格子, ’.’表示其他格子。
ouput
输出重要的格子数行, 第 i 行表示圈住 i 个重要的格子的最小路径长度。
sample input
X.I
.I.
I..
sample output
8
10
14
hint
n<=50,m<=50,非’.‘的格子数<=10
solution
#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <cstring>
const int N=54;
const int inf=0x3f3f3f3f;
const int fx[4]={1,0,-1,0};
const int fy[4]={0,1,0,-1};
using namespace std;
int n,m,cnt1,cnt2;
int f[N][N][1050];
int ans[20];
struct node{
int x,y,s;
node()
{
x=y=s=0;
}
node(int a,int b,int c)
{
x=a;
y=b;
s=c;
}
};
struct data{
int x,y;
data()
{
x=y=0;
}
data(int a,int b)
{
x=a;
y=b;
}
}g[11],g1[11];
queue <node> q;
int calc(int x)
{
int sum=0;
while (x>0)
{
if (x%2) sum++;
x/=2;
}
return sum;
}
int main()
{
char s[100];
while (scanf("%s",s)!=EOF)
{
n++;
m=strlen(s);
for (int j=1;j<=m;j++)
{
if (s[j-1]=='X') g1[++cnt2]=data(n,j);
if (s[j-1]=='I') g[++cnt1]=data(n,j);
}
}
node c=node(0,0,0);
memset(f,inf,sizeof(f));
memset(ans,inf,sizeof(ans));
f[0][0][0]=0;
q.push(c);
while (!q.empty())
{
node u=q.front();
q.pop();
int x=u.x,y=u.y,s=u.s;
for (int i=0;i<=3;i++)
{
int xx=x+fx[i],yy=y+fy[i];
if ((xx>=0) && (yy>=0) && (xx<=n) && (yy<=m))
{
int sub=s;
if (i==1)
{
for (int j=1;j<=cnt1;j++)
{
if ((g[j].y==y+1) && (g[j].x>=x+1)) sub^=1<<(j-1);
}
for (int j=1;j<=cnt2;j++)
{
if ((g1[j].y==y+1) && (g1[j].x>=x+1)) sub^=1<<(cnt1+j-1);
}
}
if (i==3)
{
for (int j=1;j<=cnt1;j++)
{
if ((g[j].y==y) && (g[j].x>=x+1)) sub^=1<<(j-1);
}
for (int j=1;j<=cnt2;j++)
{
if ((g1[j].y==y) && (g1[j].x>=x+1)) sub^=1<<(cnt1+j-1);
}
}
if (f[xx][yy][sub]>f[x][y][s]+1)
{
f[xx][yy][sub]=f[x][y][s]+1;
node u=node(xx,yy,sub);
q.push(u);
}
}
}
}
for (int i=1;i<1<<(cnt1);i++)
{
int d=calc(i);
ans[d]=min(ans[d],f[0][0][i]);
}
for (int i=1;i<=cnt1;i++) printf("%d\n",ans[i]);
}逃跑
Description
Konrad, Delfador 和 Kalenz 一行人又喜闻乐见地被追杀了。
他们面临的是一条有 N 个地点的路, 他们从 0 号地点出发, 要逃到 N 号地点去。每个地点的战斗都有一定的金币收入 Ai,也有一定的部队损失 Bi。
为了更好地逃生, Delfador 还弄到了一块传送宝石,这样一行人就能向后传送不超过 L 的距离。从一个地点传送到另一个地点时,Konrad 会选择路径上除起点外的地形指数 Ci 最大的地点进行战斗,地形指数相同时选择最靠后的。
作为优秀的领导者, Konrad 希望总金币收入与总部队损失的比值最大。
input
第一行,两个整数 N, L。
接下来 N 行,每行两个整数,分别表示 Ai, Bi, Ci。
ouput
一行,一个实数,表示答案。
答案请使用科学计数法输出,保留 9 位小数,具体参见输出样例。指数为 0 时,最后应当输出’0.000000000e+000’。
sample input
5 3
1 1 1
1 2 2
2 3 1
1 9 2
1 1 1
sample output
3.750000000e-001
hint
N<=3e4
Ai<=1e6
Bi<=1e4
Ci<=1e8
solution
#include <iostream>
#include <cstdio>
#include <cmath>
#define LL long long
const int N=1e5+7;
const double inf=1564918894981;
using namespace std;
double a[N],b[N],c[N],f[N];
double ans;
int n,L,e,h,t;
struct que{
double x,c,w,num,las,nex;
}q[N];
bool check(double x)
{
f[0]=0;
h=1; t=1;
q[1]=(que){0,0,0,0,0,0};
for (int i=1;i<=n;i++)
{
double w=a[i]-b[i]*x;
f[i]=-inf;
while (q[h].num+L<i)
{
if (q[h].nex!=h+1) q[h+1].nex=q[h].nex;
h++;
}
int z,g=h;
for (int j=t;j>=h;j--)
{
if (q[j].c<=c[i]) q[j].c=c[i],q[j].w=w;
else {g=j+1; break;}
}
int las=q[g].las;
q[g].las=0; q[g].nex=0;
z=g;
for (int k=g+1;k<=t;k++)
{
q[k].las=0; q[k].nex=0;
q[++z]=q[k];
while ((q[z].x>=q[z-1].x) && (z>g)) q[z-1]=q[z],z--;
}
t=z;
q[g].las=las;
q[las].nex=g;
int j=h;
while (j>0)
{
f[i]=max(f[i],q[j].x+q[j].w);
j=q[j].nex;
}
q[++t]=(que){f[i],0,0,i,t-1,0};
q[g].nex=t; q[t].las=g;
}
if (f[n]>=0) return 1;
else return 0;
}
int main()
{
scanf("%d%d",&n,&L);
for (int i=1;i<=n;i++) scanf("%lf%lf%lf",&a[i],&b[i],&c[i]);
LL k=1e8;
for (int i=1;i<=n;i++) a[i]=a[i]*k;
for (int i=-7;i<=7;i++)
{
for (int j=1;j<=n;j++)
{
if (i>0) b[j]*=10;
else a[j]/=10;
}
if (!check(10)) {e=i; break;};
}
double l=1,r=9.9999999999;
while (r-l>1e-12)
{
double mid=(l+r)/2;
if (check(mid)) l=mid,ans=mid;
else r=mid;
}
printf("%.9lf",ans);
if (e<0) printf("e-00%d",-e);
else printf("e+00%d",e);
}