• 2019河北省大学生程序设计竞赛部分题题解


    2019河北省大学生程序设计竞赛

    B. Icebound and Sequence

    Icebound and Sequence

    快速乘&快速幂&逆元
    在这里插入图片描述
    了解逆元

    #include
    using namespace std;
    
    ll mod,q,n;
    
    ll ksc(ll x,ll y)
    {
    	ll ans=0;
    	while(y)
    	{
    		if(y&1) ans=(ans+x)%mod;
    		x=2*x%mod;
    		y/=2;
    	}	
    	return ans;
    } 
    
    ll ksm(ll x,ll y)
    {
    	ll ans=1;
    	while(y)
    	{
    		if(y&1)ret=ksc(ret,x)%mod;
    		x=ksc(x,x)%mod;
    		y/=2;
    	}
    	return ret;
    } 
    
    int main()
    {
    	int t;
    	scanf("%d",&t);
    	while(t--)
    	{
    		scanf("%lld%lld%lld",&q,&n,&mod);	
    		mod=(q-1)*mod;
    		ll ans=(fp(q,n+1)-q+mod)%mod/(q-1);
    		printf("%lld\n",ans);		
    	}	
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    C. 分治

    分治

    区间DP,逆向思考。

    #include
    using namespace std;
    int dp[110][110],cost[110],T,n;
    
    int main()
    {
    	cin>>T;
    	while(T--)
    	{
    		cin>>n;
    		memset(dp,0x3f,sizeof(dp));
    		for(int i=1;i<=n;i++)
    			scanf("%d",&cost[i]),dp[i][i]=0;
    		for(int len=2;len<=n;len++)
    		{
    			for(int l=1;l+len-1<=n;l++)
    			{
    				int r=l+len-1;
    				for(int k=l;k<=r;k++)
    				{
    					if(k==l) dp[l][r]=min(dp[l][r],dp[k+1][r]+cost[l]*(r-l));
    					else if(k==r) dp[l][r]=min(dp[l][r],dp[l][k-1]+cost[r]*(r-l));
    					else dp[l][r]=min(dp[l][r],dp[l][k-1]+dp[k+1][r]+cost[k]*(r-l));
    				}
    			}
    		}
    		cout<<dp[1][n]<<endl;
    	}
        return 0;
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    E. Paper Plane Fly Away

    Paper Plane Fly Away

    树状数组求逆序对。博客

    #include
    using namespace std;
    const int N=200010;
    int n,pos[N],love[N],seat[N],c[N],ans[N];
    
    int lowbit(int x)
    {
    	return x&(-x);
    }
    
    void add(int p)
    {
    	for(int i=p;i<=n;i+=lowbit(i))
    		c[i]++;
    }
    
    int get(int p)
    {
    	int sum=0;
    	for(int i=p;i;i-=lowbit(i))
    		sum+=c[i];
    	return sum;
    }
    
    int main()
    {
    	cin>>n;
    	for(int i=1;i<=n;i++)
    	{
    		int x;
    		scanf("%d%d",&x,&love[i]);
    		pos[x]=i;
    	}
    	for(int i=1;i<=n;i++)
    		seat[i]=pos[love[i]];
    	for(int i=n;i;i--)
    	{
    		ans[i]+=get(seat[i]);
    		add(seat[i]);
    	}
    	memset(c,0,sizeof(c));
    	for(int i=1;i<=n;i++)
    	{
    		ans[i]+=get(n)-get(seat[i]);
    		add(seat[i]);
    		printf("%d\n",ans[i]);
    	}
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    J. 舔狗

    舔狗

    拓扑排序,从入度小的地方开始(应该算是一种贪心?),一对点一对点地拆。

    #include
    using namespace std;
    
    const int N=1e6+10;
    int n,a[N],ind[N],vis[N];
    
    struct node
    {
    	int x,p;
    };
    bool operator < (node a,node b){return a.p>b.p;}
    priority_queue<node> q;
    
    int main()
    {
    	cin>>n;
    	for(int i=1;i<=n;i++)	
    		scanf("%d",&a[i]),ind[a[i]]++;
    	for(int i=1;i<=n;i++)	
    		q.push({i,ind[i]});
    	while(q.size())
    	{
    		node u=q.top();	
    		q.pop();
    		if(vis[u.x]||vis[a[u.x]]) continue;
    		n-=2;	
    		vis[u.x]=vis[a[u.x]]=1;
    		int now=a[a[u.x]];	
    		ind[now]--;
    		q.push({now,ind[now]});
    	}
    	cout<<n;
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    L. smart robot

    smart robot

    从0开始往上暴力搜索,本以为会TLE却没有,或许答案必然在一个较小的范围内是可证的?

    #include
    using namespace std;
    int mp[100][100],n,num[100];
    int dx[4]={0,-1,0,1};
    int dy[4]={1,0,-1,0};
    
    struct node
    {
    	int x,y,step;
    };
    
    bool bfs(int x)
    {
    	int len=0;
    	if(!x) num[++len]=1;
    	while(x)
    	{
    		num[++len]=x%10;
    		x/=10;
    	}
    	queue<node>q;
    	for(int i=1;i<=n;i++)
    		for (int j=1;j<=n;j++)
    			if(mp[i][j]==num[1])
    				q.push(node{i,j,1});
    	while(!q.empty())
    	{
    		node temp=q.front();
    		q.pop();
    		if(temp.step==len) return true;
    		for(int i=0;i<4;i++)
    		{
    			int xx=temp.x+dx[i];
    			int yy=temp.y+dy[i];
    			if(xx<1||xx>n||yy<1||yy>n) continue;
    			if(mp[xx][yy]!=num[temp.step+1]) continue;
    			q.push(node{xx,yy,temp.step+1});	
    		}
    	}
    	return false;			
    }
    int main()
    {
    	cin>>n;
    	for(int i=1;i<=n;i++)
    		for(int j=1;j<=n;j++)
    			cin>>mp[i][j];
    	for(int i=0;;i++)
    	{
    		if(!bfs(i))
    		{
    			cout<<i<<endl;
    			return 0;
    		} 
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
  • 相关阅读:
    基于JAVASketch2Mod网站计算机毕业设计源码+系统+lw文档+部署
    cocos2dx 3D物理相关知识点汇总
    GIS前端—地图标注
    领航杯2022年-Crypto-rsa
    再学Blazor——概述
    校招面试数据库原理知识复习总结三之SQL语句
    Java学习路线图—精心整理 java进阶
    JS中面向对象的程序设计
    C++——多态
    利用iptable实现ssh端口复用后门
  • 原文地址:https://blog.csdn.net/m0_60630928/article/details/126234874