2019年山东省复赛-组队赛补题报告
C-Wandering Robot
DreamGrid creates a programmable robot to explore an infinite two-dimension plane. The robot has a basic instruction sequence a1,a2,…ana1,a2,…an
and a “repeating parameter” kk
, which together form the full instruction sequence s1,s2,…,sn,sn+1,…,snks1,s2,…,sn,sn+1,…,snk
and control the robot.There are 4 types of valid instructions in total, which are ‘U’ (up), ‘D’ (down), ‘L’ (left) and ‘R’ (right). Assuming that the robot is currently at (x,y)(x,y)
, the instructions control the robot in the way below: U: Moves the robot to (x,y+1)(x,y+1)
.D: Moves the robot to (x,y−1)(x,y−1)
.L: Moves the robot to (x−1,y)(x−1,y)
.R: Moves the robot to (x+1,y)(x+1,y)
.The full instruction sequence can be derived from the following equations {si=aisi=si−nif 1≤i≤notherwise{si=aiif 1≤i≤nsi=si−notherwise
The robot is initially at (0,0)(0,0)
and executes the instructions in the full instruction sequence one by one. To estimate the exploration procedure, DreamGrid would like to calculate the largest Manhattan distance between the robot and the start point (0,0)(0,0)
during the execution of the nknk
instructions.Recall that the Manhattan distance between (x1,y1)(x1,y1)
and (x2,y2)(x2,y2)
is defined as |x1−x2|+|y1−y2|
Input
There are multiple test cases. The first line of the input contains an integer TT indicating the number of test cases. For each test case:
The first line contains two integers nn and kk (1≤n≤105,1≤k≤1091≤n≤105,1≤k≤109), indicating the length of the basic instruction sequence and the repeating parameter.
The second line contains a string A=a1a2…anA=a1a2…an (|A|=n|A|=n, ai∈{‘L’,‘R’,‘U’,‘D’}ai∈{‘L’,‘R’,‘U’,‘D’}), where aiai indicates the ii-th instruction in the basic instruction sequence.
It’s guaranteed that the sum of |A||A| of all test cases will not exceed 2×1062×106
Output
For each test case output one line containing one integer indicating the answer.
2
3 3
RUL
1 1000000000
D
Sample Output
4
1000000000
Hint
For the first sample test case, the final instruction sequence is “RULRULRUL” and the route of the robot is (0, 0) - (1, 0) - (1, 1) - (0, 1) - (1, 1) - (1, 2) - (0, 2) - (1, 2) - (1, 3) - (0, 3). It’s obvious that the farthest point on the route is (1, 3) and the answer is 4.
题意:给出一串长为n的基本指令,使机器人重复该串动作k次,有效指令共四种:U-上,R-右,D-下,L-左,机器人最初位于(0,0)处,计算出机器人在执行期间与起点的最大距离是多少
思路:最坑的就是要进行第一次计算,在第一次循环时也要看是否是最大距离,再直接看最后一次循环是否最大就行,进行比较
#include<bits/stdc++.h> using namespace std; #define ll long long int main() { ll t; cin>>t; while(t--) { ll n,k,i,sum=0,x=0,y=0; scanf("%lld%lld",&n,&k); ll maxn=0; string s; cin>>s; for(i=0;i<n;i++) { if(s[i]=='U')++y; else if (s[i]=='D')--y; else if(s[i]=='R')++x; else if(s[i]=='L')--x; //cout<<x<<" "; maxn=max((abs(x)+abs(y)),maxn); } // cout<<maxn<<endl; x=x*(k-1); y=y*(k-1); // cout<<x<<" "<<y<<endl; maxn=max(maxn,(abs(x)+abs(y))); // cout<<maxn<<"*"<<endl; for(i=0;i<n;i++) { if(s[i]=='U')++y; else if (s[i]=='D')--y; else if(s[i]=='R')++x; else --x; maxn=max(maxn,(abs(x)+abs(y))); // cout<<maxn<<" "; } printf("%lld\n",maxn); } }
D-Game on a Graph
There are K people playing a game on a connected undirected simple graph with n(n>=2) vertices (numbered from 0 to (n-1)) and edges. These people, numbered from 0 to(k-1) , are divided into two groups and the game goes as follows:
- They take turns to make the move. That is to say, person number 0 will make the 1st move, person number 1 will make the 2nd move, ..., person number(i mod k) will make the (i+1)-th move.
- During a move, the current player MUST select an edge from the current graph and remove it. If the graph is no longer connected after removing the edge, the group this person belongs to loses the game (and of course their opponents win), and the game ends immediately.
Given the initial graph when the game starts, if all people use the best strategy to win the game for their groups, which group will win the game?
Recall that a simple graph is a graph with no self loops or multiple edges.
Input
There are multiple test cases. The first line of the input contains an integer T , indicating the number of test cases. For each test case:
The first line contains an integer k (2<=k<=10^5), indicating the number of people.
The second line contains a string s0 s1...sk-1 of length k
indicates that person number i belongs to the 1st group, and si='2' indicates that person number belongs to the 2nd group.
The third line contains two integers n and m (2<=n<=10^5,n-1<=m<=10^5 ), indicating the number of vertices and edges of the initial graph.
The following lines each contains two integers ui and vi (0<=ui,vi<=n), indicating that there is an edge connecting vertex and in the initial graph.
It's guaranteed that:
- The initial graph is a connected undirected simple graph.
- There exist two people who belong to different groups.
- The sum of k , the sum of and the sum of in all test cases will not exceed 10^6.
Output
For each test case output one line containing one integer. If the 1st group wins, output "1" (without quotes); If the 2nd group wins, output "2" (without quotes).
Sample Input
3 5 11212 4 6 0 1 0 2 0 3 1 2 1 3 2 3 5 11121 5 7 0 2 1 3 2 4 0 3 1 2 3 2 4 1 3 121 4 3 0 1 0 2 1 3
Sample Output
2 1 2
题意:给出一个含n个点,m条边的连通图,将k个人按照输入分成两组,按顺序取边,当取走一条边后图不再连通,该队就输了,要求输出赢的队的编号。
有K个人在一个有n个顶点(编号从0到(n-1))和边的连通无向简单图上玩游戏。这些人,编号从0到(k-1),被分成两组,游戏进行如下:他们轮流走一步。
也就是说,0号人会迈出第一步,1号人会迈出第二步,……,i mod k将进行第(i+1)次移动。在移动过程中,当前玩家必须从当前图形中选择
一条边并移除它。如果在移除边缘后图不再连接,这个人所属的组就会输掉这场游戏(当然他们的对手会赢),游戏立即结束。根据游戏开始时
的初始图,如果所有人都用最佳策略为自己的组赢得比赛,哪组将赢得比赛?回想一下,一个简单的图是没有自循环或多条边的图。
输入
有多个测试用例。输入的第一行包含一个整数T,指示测试用例的数量。对于每个测试用例:
第一行包含一个整数k (2<=k<=10^5),表示人数。
第二行包含一个字符串s0 s1…长度k的sk-1表示编号i属于第一组,si='2'表示编号i属于第二组。
第三行包含两个整数n和m (2<=n<=10^5,n-1<=m<=10^5),表示初始图的顶点数和边数。
下面的每一行都包含两个整数ui和vi (0<=ui,vi<=n),表示初始图中有一条连接顶点和的边。
保证:初始图是连通无向简单图。有两个人属于不同的群体。
所有测试用例中k的和的和的和的和不会超过10^6。
每个测试用例的
输出
输出一行包含一个整数。
如果第一组获胜,输出“1”(不带引号);如果第二组获胜,输出“2”(不带引号)。
题解:其实就是连接这n个点,形成连通的图形,那就可以知道要达到这个图形则至少需要n-1
条边,所以当边数为n-1时拿掉这条边的队伍就输了
#include<bits/stdc++.h> using namespace std; int main() { long long t; cin>>t; while(t--) { long long k,n,m,i,j,u,v; cin>>k; char s[k+5]={0}; cin>>s; //int n,m; cin>>n>>m; //int u,v; for(i=0;i<m;i++) { cin>>u>>v; } //m条边,需留下(n-1)条边,所以可以拿去m-(n-1)条, //s从0开始所以s[(m-n+1)%k]表示的队伍输 if(m-n+1>0) { //cout<<m-n+1<<" "<<s[(m-n+1)%k]<<"*"<<endl; if(s[(m-n+1)%k]=='1')//注意1要加 ‘ ’ cout<<"2"<<endl; else cout<<"1"<<endl; } else//当图形原本就不连通时 { //cout<<s[0]<<"*"<<endl; if(s[0]=='1')//注意1要加 ‘ ’ cout<<"2"<<endl; else cout<<"1"<<endl; } } }
K-Happy Equation
Little Sub has just received an equation, which is shown below, as his birthday gift.

Given the value of a, please help Little Sub count the number of x(1<=x<=2^p) which satisfies the equation.
Input
There are multiple test cases. The first line of the input contains an integer T(about 1000), indicating the number of test cases. For each test case:
The first and only line contains two integers a and p (1<=a<=10^9,1<=p<=30 ).
Output
For each test case output one line containing one integer, indicating the answer.
Sample Input
2 6 12 8 16
Sample Output
1023 16383
题意:给定一个a值和p值,计算出满足方程式中x的数量
输入第一行为T 表示测试用例数量,每个测试用例第一行包含两个整数a,p
输出一行包含一个整数,表示答案
题解:(数论题型)看了很长时间也不太懂
打表可以找到a为奇数时x也为奇数,此时答案总为1,如:
a=1,x=1;
a=3,x=3;
x=5,x=5;
......
当a为偶数时 x 为偶数,x的个数可以分为两部分来求:
百度数论四大定理查到该式子,简单来说是 a^x%2^p=x^a%2^p
设a=2*t,a^x=2^x*t^x;
设x=2^k*t;
x^a=2^ka*t^a;
1.当p很小时,直接枚举x<p时的解:
2.当x>=p时,a^x%2^p=0,此时只要使 x^a%2^p也为0就可,当x^a为2^p的倍数时等式成立,所以此时ka>=p,t^a肯定不会整除,所以就是k>=p/a,看t的个数,
也是就看看2^p是2^(p/a)的倍数是多少
两部分会有重复的解,需要除去第二种情况下第一种情况的重复部分
#include<bits/stdc++.h> //a^x 与x^a // 1<=x<=2^p using namespace std; #define ll long long ll show(int x,int n,ll mod) { ll m=1; ll t=x; while(n) { if(n&1)//判断n的最后一位是否为1,看最终是否为1//(n&1)可以判断n是否为偶数,如果为偶数则返回0,否则返回1;作用与n%2相同,但效率快 m=(m*t)%mod; t=(t*t)%mod; n>>=1;//将n的二进制位右移1位,舍去n的最后一位,再将最后结果赋给n,如1000,右移一位变为0100 x=(x*x);//将x平方 } return m%mod; } int main() { ll a,p; int t; while(~scanf("%d",&t)) { while(t--) { scanf("%lld%lld",&a,&p); if(a%2!=0)//a为奇数时 { cout<<"1"<<endl; continue; } //a为偶数 ll up=(1LL<<p);//把p转换为long long 类型再赋值给up ll ct=0; for(int i=1;i<=p;i++){//x<p时 if(show(a,i,up)==show(i,a,up))ct++; } //x>=p ll w=ceil(p/(a+0.0));//ceil取大于等于结果的最小整数 ct+=(up>>w)-(p>>w); cout<<ct<<endl; } } }

中
浙公网安备 33010602011771号