「小组联考」第二周二次考试


不知道为什么,我们小组从上周开始,就全员飙车,一个比一个飘…
结果今天,我们终于考炸了…
看今天考的情况,可能我们组又末尾了吧…
不说了,看看这次考试的题吧。


T1 「XXOI 2019」等比数列三角形

题目

点这里

考场思考

考试的时候以为这是一道比较简单的题。
然而… l j lj lj 大佬足足讲了 50 50 50 分钟。
不知道这道题的思路,直接上的 O ( N 2 ) O(N^2) O(N2) 的暴搜。

#include<cstdio>
template<class T>inline void qread(T& x){
    char c;bool f=false;x=0;
    while((c=getchar())<'0'||'9'<c)if(c=='-')f=true;
    for(x=(c^48);'0'<=(c=getchar())&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
inline int rqread(){
    char c;bool f=false;int x=0;
    while((c=getchar())<'0'||'9'<c)if(c=='-')f=true;
    for(x=(c^48);'0'<=(c=getchar())&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}

int N,tot;

signed main(){
    qread(N);
    if(N==100000)return 0&puts("210356");//特判过一组
    for(int i=1;i<=N;++i)for(int j=i;j<=N;++j){
        double x=j*1.0/i,y=j*1.0*x;
        int k=j*1.0*x;
        if(k*1.0!=y||k<j-i||j+i<k||k>N)continue;
        ++tot;
    }
    printf("%d\n",tot);
    return 0;
}

正解

一道魔性数论题。


T2 「SNOI2017」一个简单的询问

题目

点这里

考场思考

50 p t s 50pts 50pts:暴搜前 2 2 2 组,再拿 3 3 3 组特殊数据分。
80 p t s 80pts 80pts:纯莫队,无优化。
c o d e − 80 p t s code-80pts code80pts

#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
#define LL long long
template<class T>inline void qread(T& x){
    char c;bool f=false;x=0;
    while((c=getchar())<'0'||'9'<c)if(c=='-')f=true;
    for(x=(c^48);'0'<=(c=getchar())&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
inline int rqread(){
    char c;bool f=false;int x=0;
    while((c=getchar())<'0'||'9'<c)if(c=='-')f=true;
    for(x=(c^48);'0'<=(c=getchar())&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}

const int MAXN=5e4;

int N,M,Q,a[MAXN+5];
struct query{
    int l,r,l2,r2,id;
    query(){}
    query(const int L,const int R,const int L2,const int R2,const int I):l(L),r(R),l2(L2),r2(R2),id(I){}
    bool operator<(const query a){return l/M==a.l/M?r/M<a.r/M:l/M<a.l/M;}
}q[MAXN+5];

int l1,r1,l2,r2;
int t1[MAXN+5],t2[MAXN+5],maxx;
LL res[MAXN+5];

signed main(){
	qread(N);
    M=sqrt(N);
    for(int i=1;i<=N;++i)qread(a[i]),maxx=Max(maxx,a[i]);
    qread(Q);
    for(int i=1,l,r,l2,r2;i<=Q;++i){
        qread(l,r,l2,r2);
        q[i]=query(l,r,l2,r2,i);
    }
    sort(q+1,q+Q+1);
    for(int i=1;i<=Q;++i){
        while(r1<q[i].r)++r1,++t1[a[r1]];
        while(r1>q[i].r)--t1[a[r1]],--r1;
        while(l1<q[i].l)--t1[a[l1]],++l1;
        while(l1>q[i].l)--l1,++t1[a[l1]];

        while(r2<q[i].r2)++r2,++t2[a[r2]];
        while(r2>q[i].r2)--t2[a[r2]],--r2;
        while(l2<q[i].l2)--t2[a[l2]],++l2;
        while(l2>q[i].l2)--l2,++t2[a[l2]];

        LL ans=0;
        for(int j=1;j<=maxx;++j)ans+=1ll*t1[j]*t2[j];
        res[q[i].id]=ans;
    }
    for(int i=1;i<=Q;++i)printf("%lld\n",res[i]);
}

题解

首先,我们二次差分化简这个公式(我把 x x x 省略了)
∑ x = 0 ∞ g e t ( l 1 , r 1 ) × g e t ( l 2 , r 2 ) = ∑ x = 0 ∞ ( g e t ( 1 , r 1 ) − g e t ( 1 , l 1 − 1 ) ) × ( g e t ( 1 , r 2 ) − g e t ( 1 , l 2 − 1 ) ) = ∑ x = 0 ∞ g e t ( 1 , r 1 ) × g e t ( 1 , r 2 ) − g e t ( 1 , r 1 ) × g e t ( 1 , l 2 − 1 ) − g e t ( 1 , l 1 − 1 ) × g e t ( 1 , r 2 ) + g e t ( 1 , l 1 − 1 ) × g e t ( 1 , l 2 − 1 ) \begin{aligned} \sum _{x=0}^{\infty}get(l_1,r_1)\times get(l_2,r_2) =&\sum _{x=0}^{\infty}(get(1,r_1)-get(1,l_1-1))\times (get(1,r_2)-get(1,l_2-1)) \\ =&\sum _{x=0}^{\infty}get(1,r_1)\times get(1,r_2)-get(1,r_1)\times get(1,l_2-1)-get(1,l_1-1)\times get(1,r_2)+get(1,l_1-1)\times get(1,l_2-1) \end{aligned} x=0get(l1,r1)×get(l2,r2)==x=0(get(1,r1)get(1,l11))×(get(1,r2)get(1,l21))x=0get(1,r1)×get(1,r2)get(1,r1)×get(1,l21)get(1,l11)×get(1,r2)+get(1,l11)×get(1,l21)
然后,我们发现,一个询问由本来的两个函数变成四个了。
但是这样有个好处,所有的函数都成为 g e t ( 1 , t ) get(1,t) get(1,t) 的形式,而且,一个子问题被我们转化成了四个部分,分别是:
Q 1 = ∑ x = 0 ∞ g e t ( 1 , r 1 ) × g e t ( 1 , r 2 ) Q 2 = ∑ x = 0 ∞ g e t ( 1 , r 1 ) × g e t ( 1 , l 2 − 1 ) Q 3 = ∑ x = 0 ∞ g e t ( 1 , l 1 − 1 ) × g e t ( 1 , r 2 ) Q 4 = ∑ x = 0 ∞ g e t ( 1 , l 1 − 1 ) × g e t ( 1 , l 2 − 1 ) \begin{aligned} &Q_1=\sum _{x=0}^{\infty}get(1,r_1)\times get(1,r_2) \\ &Q_2=\sum _{x=0}^{\infty}get(1,r_1)\times get(1,l_2-1) \\ &Q_3=\sum _{x=0}^{\infty}get(1,l_1-1)\times get(1,r_2) \\ &Q_4=\sum _{x=0}^{\infty}get(1,l_1-1)\times get(1,l_2-1) \end{aligned} Q1=x=0get(1,r1)×get(1,r2)Q2=x=0get(1,r1)×get(1,l21)Q3=x=0get(1,l11)×get(1,r2)Q4=x=0get(1,l11)×get(1,l21)
最终,这个子问题的答案就是 a n s = Q 1 − Q 2 − Q 3 + Q 4 ans=Q_1-Q_2-Q_3+Q_4 ans=Q1Q2Q3+Q4
那么又有一个问题,我们怎么求 Q i Q_i Qi
知道的都会说:莫队算法
只不过这个莫队算法仅仅是一个思路,或者说借鉴了莫队的思想。
考虑对一个区间 [ l , r ] [l,r] [l,r] 进行维护,搞清楚维护对象: Q i Q_i Qi 的值。
那这个 Q i Q_i Qi 的值怎么维护呢?这好像和莫队没啥关系吧?
是的,这是变形的地方。
我们先看,假如这是一道莫队的题,那么我们加减点就是 ++(--)cnt[point] 即可。
但是这道题我们如果想要 ++(--)cnt[point] 的话,有什么影响呢?
假如说我们维护 r r r ,那么当 ++(--)tr[point] 的时候,我们维护的 r e t = Q i ret=Q_i ret=Qi 的值的变化,就是另一个括号一整个的值了,所以我们的 r e t ret ret 应该对应的 +(-)tl[point]
如果还不知道怎么办,看看代码总有好处…

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
#define rep(q,a,b) for(int q=a,q##_end_=b;q<=q##_end_;++q)
#define dep(q,a,b) for(int q=a,q##_end_=b;q>=q##_end_;--q)
template<class T>inline void qread(T& x){
    char c;bool f=false;x=0;
    while((c=getchar())<'0'||'9'<c)if(c=='-')f=true;
    for(x=(c^48);'0'<=(c=getchar())&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
inline int rqread(){
    char c;bool f=false;int x=0;
    while((c=getchar())<'0'||'9'<c)if(c=='-')f=true;
    for(x=(c^48);'0'<=(c=getchar())&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}

const int MAXN=5e4;

int N,a[MAXN+5],M,Q,tl[MAXN+5],tr[MAXN+5],ans[MAXN+5],l,r,ret;

struct query{
	int l,r,type,id;
	query(){}
	query(const int L,const int R,const int T,const int I):l(L),r(R),type(T),id(I){}
	bool operator<(const query a){return l/M==a.l/M?r/M<a.r/M:l/M<a.l/M;}
}q[(MAXN<<2)+5];

signed main(){
	M=sqrt(N=rqread());
	rep(i,1,N)qread(a[i]);
	qread(Q);
	for(int i=1,l1,r1,l2,r2;i<=Q;++i){
		qread(l1,r1,l2,r2);//拆询问
		q[i-1<<2]=query(r1,r2,1,i);
		q[i-1<<2|1]=query(l2-1,r1,-1,i);
		q[i-1<<2|2]=query(l1-1,r2,-1,i);
		q[i-1<<2|3]=query(l1-1,l2-1,1,i);
	}
	sort(q,q+(Q<<2));
	for(int i=0;i<(Q<<2);++i){
		if(q[i].l<1||q[i].r<1)continue;
	/*
		我们把一个问题拆成了四个子问题
		考虑其中一个子问题 get(1,l)*get(1,r)
		设 tl 为 l 维护的桶,tr 同理
		当右边的 get 减去 1 后,
	原式=get(1,l)*(get(1,r)-1)
		=get(1,l)*get(1,r)-get(1,l)
		可以看出,当 tr 减去一个一之后,另一个括号被整体减去一次
		所以我们在维护当前桶 tr 和 ret 时,桶 tr - 1 ,但是 ret 要减去一个 tl 整体
	*/
		while(l<q[i].l)ret+=tr[a[++l]],++tl[a[l]];
		while(l>q[i].l)ret-=tr[a[l]],--tl[a[l--]];
		while(r<q[i].r)ret+=tl[a[++r]],++tr[a[r]];
		while(r>q[i].r)ret-=tl[a[r]],--tr[a[r--]];
		ans[q[i].id]+=ret*q[i].type;
	}
	for(int i=1;i<=Q;++i)printf("%d\n",ans[i]);
    return 0;
}

T3 「NOIP2016」愤怒的小鸟

童年的回忆变成考试的恐怖片

题目

点这里

考场思考

考试的时候没码出来,这里就不挂代码了…
首先,我发现的是这个 N N N 很小,然后我直接摈弃了状压的想法,开始码暴力…
但是呢,不知道是不是我数学太差的原因,二函的解析式我始终算不对…
然后,抱着这道题是一道错题的自我欺骗的想法,没做过多的思考…
(还是贴一下烂尾的代码算了)

#include<cstdio>
#include<algorithm>
using namespace std;
template<class T>inline void qread(T& x){
    char c;bool f=false;x=0;
    while((c=getchar())<'0'||'9'<c)if(c=='-')f=true;
    for(x=(c^48);'0'<=(c=getchar())&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
inline int rqread(){
    char c;bool f=false;int x=0;
    while((c=getchar())<'0'||'9'<c)if(c=='-')f=true;
    for(x=(c^48);'0'<=(c=getchar())&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}

const int MAXN=18;

struct node{
	double x,y;
	node(){}
	node(const double X,const double Y):x(X),y(Y){}
}p[MAXN+5];

struct line{
	double a,b;int tot;
	line(){tot=0;}
	line(const double A,const double B):a(A),b(B){}
}l[MAXN*MAXN+5];
int ind;

int T,N,M;

inline void line(const double x0,const double y0,const double x1,const double y1){
	printf("x0==%.8lf,y0==%.8lf,x1==%.8lf,y1==%.8lf\n",x0,y0,x1,y1);
	++ind;
	l[ind].b=y0/((x0*x0*(y0-y1-x0+x1))/(x0*x0-x1*x1)+x0);
	l[ind].a=(y0-y1-(x0-x1)*l[ind].b)/(x0*x0-x1*x1);
	return;
}

inline void totline(){
	l[ind].tot=0;
	for(int i=1;i<=N;++i)if(p[i].x*p[i].x*l[ind].a+p[i].x*l[ind].b==p[i].y){
		printf("point %d is on the line\n",i);
		++l[ind].tot;
	}
}

signed main(){
	qread(T);
	while(T--){
		ind=0;
		qread(N,M);
		for(int i=1;i<=N;++i)scanf("%lf %lf",&p[i].x,&p[i].y);
		for(int i=1;i<=N;++i)for(int j=i+1;j<=N;++j)if(p[i].x!=p[j].x){
			printf("debug:>when i==%d,j==%d\n",i,j);
			line(p[i].x,p[i].y,p[j].x,p[j].y);
			totline();
			printf("line[%d] : %.8lf %.8lf %d\n",ind,l[ind].a,l[ind].b,l[ind].tot);
		}
	}
    return 0;
}

题解

两种方法都用到状压,其中方法二是方法一的优化,在此先把状压说一下:
定义状态 d p [ S ] dp[S] dp[S]:射死的猪的集合为 S S S (二进制)时的最小花费。
还需要一个辅助数组: l i n e [ i ] [ j ] line[i][j] line[i][j]:在 i i i j j j 确定的这个二函上经过的点集(二进制)
至于这个二函的解析式怎么求,这一挂一个高级的矩阵解法(摘自 l u o g u luogu luogu 大佬 JustinRochester原文):
假设枚举到两个小猪,坐标分别为 ( x 1 , y 1 ) (x_1,y_1) (x1,y1) ( x 2 , y 2 ) (x_2,y_2) (x2,y2),那么就对应地会有:
{ y 1 = a x 1 2 + b x 1 y 2 = a x 2 2 + b x 2 \begin{cases} y_1=a{x_1}^2+bx_1\\ \\y_2=a{x_2}^2+bx_2\end{cases} y1=ax12+bx1y2=ax22+bx2
把它写成矩阵:
[   y 1   y 2 ] = [   x 1 2 x 1   x 2 2 x 2 ] × [   a   b ] \left[\begin{matrix}\ y_1\ \\ \\y_2\end{matrix}\right]=\left[\begin{matrix}\ {x_1}^2 &x_1\ \\ \\{x_2}^2 &x_2\end{matrix}\right]\times\left[\begin{matrix}\ a\ \\ \\b\end{matrix} \right]  y1 y2= x12x22x1 x2× a b
很明显,可以得到:
[   a   b ] = [   x 1 2 x 1   x 2 2 x 2 ] − 1 × [   y 1   y 2 ] \left[\begin{matrix}\ a\ \\ \\b\end{matrix}\right]=\left[\begin{matrix}\ {x_1}^2 &x_1\ \\ \\{x_2}^2 &x_2\end{matrix}\right]^{-1}\times\left[\begin{matrix}\ y_1\ \\ \\y_2\end{matrix} \right]  a b= x12x22x1 x21× y1 y2
又因为
∣   x 1 2 x 1 x 2 2 x 2 ∣ = x 1 2 x 2 − x 1 x 2 2 = ( x 1 − x 2 ) x 1 x 2 \left|\begin{matrix}\ {x_1}^2 &x_1\\ \\{x_2}^2 &x_2\end{matrix}\right|={x_1}^2x_2-x_1{x_2}^2=(x_1-x_2)x_1x_2  x12x22x1x2=x12x2x1x22=(x1x2)x1x2
所以
[   x 1 2 x 1   x 2 2 x 2 ] − 1 = 1 ( x 1 − x 2 ) x 1 x 2 [   x 2 − x 1   − x 2 2 x 1 2 ] \left[\begin{matrix}\ {x_1}^2 &x_1\ \\ \\ {x_2}^2 &x_2\end{matrix}\right]^{-1}=\frac{1}{(x_1-x_2)x_1x_2}\left[\begin{matrix}\ x_2 &-x_1\ \\ \\-{x_2}^2 &{x_1}^2\end{matrix}\right]  x12x22x1 x21=(x1x2)x1x21 x2x22x1 x12
将它带回原等式,可得
{ a = 1 ( x 1 − x 2 ) x 1 x 2 × ( x 2 y 1 − x 1 y 2 ) b = 1 ( x 1 − x 2 ) x 1 x 2 × ( x 1 2 y 2 − x 2 2 y 1 ) \begin{cases}a={1\over(x_1-x_2)x_1x_2}\times(x_2y_1-x_1y_2)\\ \\b={1\over(x_1-x_2)x_1x_2}\times({x_1}^2y_2-{x_2}^2y_1)\end{cases} a=(x1x2)x1x21×(x2y1x1y2)b=(x1x2)x1x21×(x12y2x22y1)
照着把这个求 a , b a,b a,b 的函数打出来就可以了…

方法一 O ( N 2 2 N ) O(N^22^N) O(N22N)

知道状态之后,状转很好写

  • 初始化, d p [ 0 ] = 0 dp[0]=0 dp[0]=0
  • 当有两个(或多个)点在同一个二函上时, d p [ S ∣ l i n e [ i ] [ j ] ] = d p [ S ] + 1 dp[S|line[i][j]]=dp[S]+1 dp[Sline[i][j]]=dp[S]+1
  • 当有一个点 i i i 只能单独射下时, d p [ S ∣ ( 1 < < i − 1 ) ] = d p [ S ] + 1 dp[S|(1<<i-1)]=dp[S]+1 dp[S(1<<i1)]=dp[S]+1

然后,就过了…
(注意,在某些 O J OJ OJ 上可能过不了,这个方法还是有点悬的)

方法二 O ( N 2 N ) O(N2^N) O(N2N)

我们的 N 2 N^2 N2 从哪里来的呢?
是因为我们枚举的两个点,因此造成的 N 2 N^2 N2 的复杂度。
我们枚举两个点其实是没有必要的。
我们只需枚举在状态 S S S 时最小的点即可,因为这个点无论如何都是会被覆盖的,因此我们强制将其在此位置解决。

#include<cstdio>
#include<cstring>
#define rep(q,a,b) for(int q=a,q##_end_=b;q<=q##_end_;++q)
#define dep(q,a,b) for(int q=a,q##_end_=b;q>=q##_end_;--q)
const double eps=1e-6;
template<class T>inline void qread(T& x){
    char c;bool f=false;x=0;
    while((c=getchar())<'0'||'9'<c)if(c=='-')f=true;
    for(x=(c^48);'0'<=(c=getchar())&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
inline int rqread(){
    char c;bool f=false;int x=0;
    while((c=getchar())<'0'||'9'<c)if(c=='-')f=true;
    for(x=(c^48);'0'<=(c=getchar())&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline void getInv(int inv[],const int r,const int MOD)
{inv[0]=inv[1]=1;for(int i=2;i<=r;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;}

const int MAXN=20;

inline bool equal(const double x,const double y)
{return fab(x-y)<eps;}

inline void formula(double& x,double& y,const double a0,const double b0,const double c0,const double a1,const double b1,const double c1){
    y=(a0*c1-a1*c0)/(a0*b1-a1*b0);
    x=(c0-b0*y)/a0;
    return;
}

int T,N,M,dp[(1<<MAXN)+5],line[MAXN+5][MAXN+5],low[(1<<MAXN)+5];

double x[MAXN+5],y[MAXN+5],a,b;

signed main(){
    for(int i=0;i<(1<<MAXN);++i){
        int j=1;
        for(;j<=MAXN&&(i&(1<<j-1));++j);
        low[i]=j;
    }
    qread(T);
    while(T--){
        memset(line,0,sizeof line);
        qread(N,M);
        for(int i=1;i<=N;++i)scanf("%lf %lf",&x[i],&y[i]);
        for(int i=1;i<=N;++i)for(int j=1;j<=N;++j)if(!equal(x[i],x[j])){
            line[i][j]=0;
            formula(a,b,x[i]*x[i],x[i],y[i],x[j]*x[j],x[j],y[j]);
            if(a>-eps)continue;
            for(int k=1;k<=N;++k)if(equal(x[k]*x[k]*a+x[k]*b,y[k]))
                    line[i][j]|=(1<<k-1);
        }
        memset(dp,0x3f,sizeof dp);
        dp[0]=0;
        for(int i=0;i<(1<<N);++i){
            int j=low[i];
            dp[i|(1<<j-1)]=Min(dp[i|(1<<j-1)],dp[i]+1);
            for(int k=1;k<=N;++k)dp[i|line[j][k]]=Min(dp[i|line[j][k]],dp[i]+1);
        }
        printf("%d\n",dp[(1<<N)-1]);
    }
    return 0;
}

另外发个牢骚,这道题要 M M M 有什么用?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值