2024牛客五一集训派对day2

 这套题目我总结一下就是  python嘎嘎乱杀,真的嘎嘎乱杀,我真的长见识了,我真的,真的佩服

A.  Groundhog and 2-Power Representatio

链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
 

Groundhog took a math class. In this class, his math teacher said:

Any positive integer can be represented by the power of 2{2}2. For example:137=27+23+20137=2^7+2^3+2^0137=27+23+20.

And powers are expressed in parentheses.That is ,a(b){a(b)}a(b) stands for ab{a^b}ab.Therefore,137{137}137 can be expressed as 137=2(7)+2(3)+2(0)137={2(7)+2(3)+2(0)}137=2(7)+2(3)+2(0).

Further more,for 7=22+2+207=2^2+2+2^07=22+2+20(212^121is expressed with 2{2}2),3=2+203=2+2^03=2+20,137 can be finally expressed as 137=2(2(2)+2+2(0))+2(2+2(0))+2(0){137=2(2(2)+2+2(0))+2(2+2(0))+2(0)}137=2(2(2)+2+2(0))+2(2+2(0))+2(0).

Another example:1315=210+28+25+2+1=2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)1315=2^{10}+2^8+2^5+2+1 = 2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)1315=210+28+25+2+1=2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0).

Groundhog feels amazing and wants you to write a program to simulate the above content.You need to read in an expression that is a power of 2{2}2 and calculate its value.

输入描述:

Given a string, indicating the power representation.

输出描述:

Output the original number.

示例1

输入

复制2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)

2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)

输出

复制1315

1315

备注:

The range of answers :[10,10180][10,10^{180}][10,10180],and the length of the input data shall not exceed 20000{20000}20000.

思路:这题就是纯坑c组,python水过

 

print(eval(str(input()).replace('(', '**(')))

 

#include "bits/stdc++.h"
using namespace std;
stack<int> st1;
stack<char> st2;
int main(){
	string s;
	cin>>s;
	long long ans = 0;
	for(int i = 0 ; i < s.length(); i ++){
		if(s[i] >= '0' && s[i] <= '9') st1.push(s[i] - '0');
		else if(s[i] == '+' || s[i] == '(') st2.push(s[i]);
		else {
			int x, n;
			while(st2.top() != '('){
				n = st1.top();
				st1.pop();
				x = st1.top();
				st1.pop();
				int ans = x + n;
				st1.push(ans);
				st2.pop();
				
			}	
				n = st1.top();
				st1.pop();
				x = st1.top();
				st1.pop();
				if(st2.top() == '('){
					int ans = pow(x, n);
					st1.push(ans);
					st2.pop();
				}
		}
	}
	int n, x;
	while(st1.size() > 1){
			n = st1.top();
			st1.pop();
			x = st1.top();
			st1.pop();
			st1.push(n + x);
	}
	cout<<st1.top()<<endl;
	return 0;
}

用c的话我大概写了一下算法,里面的加法和乘法还要换成高精度,我这里就不写了,总结一下啊就是,python牛

E. Groundhog Chasing Death

 链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
 

题目描述

As we all know,"Groundhog chasing death" means "GCD",while "GCD" stands for "greatest common divisor".

So you need to calculate  ∏i=ab∏j=cdgcd⁡(xi,yj)\displaystyle\prod_{i=a}^b\prod_{j=c}^d\gcd(x^i,y^j)i=a∏b​j=c∏d​gcd(xi,yj) modulo 998244353{998244353}998244353.

输入描述:

One line which contains six intergers a,b,c,d,x,y{a,b,c,d,x,y}a,b,c,d,x,y.

输出描述:

One line which contains ∏i=ab∏j=cdgcd⁡(xi,yj)\displaystyle \prod_{i=a}^b\prod_{j=c}^d\gcd(x^i,y^j)i=a∏b​j=c∏d​gcd(xi,yj) modulo 998244353{998244353}998244353.

示例1

输入

复制1 2 1 2 8 4

1 2 1 2 8 4

输出

复制2048

2048

示例2

输入

复制1 2 3 4 120 180

1 2 3 4 120 180

输出

复制235140177

235140177

备注:

0⩽a,b,c,d⩽3×106,0<x,y⩽109,a⩽b,c⩽d0\leqslant{a,b,c,d\leqslant 3\times10^6,0<x,y\leqslant10^9},a\leqslant b,c\leqslant d0⩽a,b,c,d⩽3×106,0<x,y⩽109,a⩽b,c⩽d.

思路:就是一个很全的数论,什么费马小定理,什么质因数分解,什么快速幂都用上了,反正不像你看起来这么简单咯,python也是会超时的咯,实践证明,最短的题就是最长的题,这里是看的大佬的博客。其实也就是将两个数分解出公共的质因数(我们其实在小学二年级就是这么计算公倍数的)然后因为外面有个连乘,所以我们可以把幂次提出来,数之间的相乘就是幂次的相加,这里的话就是直接对循环进行一个连加(其实就是连加一个相同的数,也可以用这个数,乘以连加的次数,这也就是这个公共质因数的贡献,当然要注意,我们要取最小的那个幂次作为公倍数的贡献(很容易想到)。在求解幂次的时候可以对mod - 1取余数,运用费马小定理,一个数的mod - 1次幂对mod取余是1.然后在计算求幂的时候,使用快速幂,因为已经是乘法了,所以当前进行对mod取余是不会影响最终的结果2020牛客暑期多校训练营(第九场)题解A、I、F、K、E_k a i f e-CSDN博客

 总结一下,其实就是对x和y进行取公共的质因数,然后找那个幂次较小的当作贡献,进行连乘,然后继续本循环的连乘,找下一个最小的幂次,记得优化(用费马小定理)

#include "bits/stdc++.h"
using namespace std;
#define int long long 
const int mod = 998244353;
int a, b, c, d, x, y;
int quick_pow(int x, int n){
	int t = 1;
	while(n){
		if(n & 1){
			t = t * x % mod;
		}
		 x = x * x % mod;
		 n >>= 1;
	}
	return t;
}
inline int cal(int x, int y){
	int ansx = x * a, ansy = y * c;
	__int128_t summ = 0;
	for(int i = a, j = c; i <= b && j <= d; ){
		if(ansx <= ansy) summ += ansx * (d - j + 1) , ansx += x, i ++;
		else summ += ansy *(b - i + 1), ansy += y, j ++;
	}
	return summ % (mod - 1);
}
void solve(){
	cin>>a>>b>>c>>d>>x>>y;
	int summ = 1;
	for(int i = 2; i * i <= x; i ++){
		
		if(x % i == 0){
			int ansx = 0, ansy = 0;
			while(x % i == 0) ansx ++, x /= i;
			while(y % i == 0) ansy ++, y /= i; 
			if(ansy) summ = summ * quick_pow(i, cal(ansx, ansy)) % mod;
		}
	}
	if(x != 1){
		int ansx = 1, ansy = 0;
		while(y % x == 0) ansy ++, y /= x;
		if(ansy) summ = summ * quick_pow(x, cal(ansx, ansy)) % mod;
	}
	cout<<summ<<endl;
}
signed main(){
	solve();
}

 

 F. Groundhog Looking Dowdy

链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

Groundhog finds that Apple seems to be less intimate than before.

He is very distressed for this.After pondering for a long time, Groundhog finds that he looks too dowdy.So, Groundhog decided to improve a little.

Because Groundhog is lazy, he only lists the clothes that can be worn for the next n{n}n days. On the ith{i^{th}}ith day, the jth{j^{th}}jth clothes have a dowdiness ai,ja_{i,j}ai,j​.On each day,he will choose one of the clothes and wear it.

And Groundhog should choose m{m}m days from n{n}n days to go out with Apple.

Groundhog wants to know the minimum difference between the maximum dowdiness and the minimum dowdiness in m{m}m days when Groundhog's choice is optimal.

Simplified: You should choose n clothes for each day and then choose m clothes from those n clothes, and the problem is to calculate the minimum difference between the maximum dowdiness and the minimum dowdiness.

输入描述:

The first line contains two integers n{n}n and m{m}m.

Then n{n}n lines follow,each line contains a integer kik_iki​,represents the number of the clothes that can be worn on ith{i^{th}}ith day.Then kik_iki​ integers ai,ja_{i,j}ai,j​ follow.

输出描述:

Print in one line the minimum difference.

示例1

输入

复制4 3 1 3 2 8 6 1 2 3 1 7 5

4 3
1 3
2 8 6
1 2
3 1 7 5

输出

复制2

2

说明

Apple will pay attention to Groundhog's clothes on day 1, 3, and 4 ,Groundhog will wear clothes with dowdiness of 3, 2, and 1 on day 1, 3, and 4,and the difference is 2.

备注:

1⩽ai,j⩽1091\leqslant a_{i,j}\leqslant 10^{9}1⩽ai,j​⩽109,1≤n⩽106,1≤m⩽n1\le n\leqslant  10^6,1 \le m\leqslant n1≤n⩽106,1≤m⩽n,the sum of clothes ⩽2⋅106\leqslant2\cdot10^6⩽2⋅106.ki≥1k_i \ge 1ki​≥1

 思路:这题看懂题目纯水题,其实就是每一组取最小,然后取前第一个和第k个的差

 

#include "bits/stdc++.h"
using namespace std;
const int N = 1e9;
vector<int> v;
int main(){
	int n, m;
	cin>>n>>m;
	while(n--){
		int a;
		cin>>a;
		int minn = N;
		while(a--){
			int x;
			cin>>x;
			minn = min(minn, x);
		
		}
		v.push_back(minn);
	}
	sort(v.begin(), v.end());
	cout<<v[m - 1] - v[0]<<endl;
	return 0;
}

 I The Crime-solving Plan of Groundhog

链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

Today, ZLZX has a mysterious case: Orange lost his down jacket hanging in his dorm room. Under the expectations of everyone, detective Groundhog took his small spoon of the artifact and started the journey to solve the case.
 

After an in-depth investigation of the northernmost mysterious room on each floor, Groundhog discovered  n{n}n  mysterious numbers. As long as the clues conveyed by these numbers are deciphered, he can reveal the truth of the matter.  The deciphering method is: using these numbers to generate two positive integers without leading zeros, and minimizing the product of these two positive integers is the final clue.

Then Groundhog wants to know: What is the smallest product?
 

As he continued to investigate in the room west of the new building, he gave you the question.

Concise meaning:Given n numbers between 0 and 9, use them to make two positive integers without leading zeros to minimize the product.

输入描述:

 
 

The first line of input is a single integer T{T}T,the number of test cases.
For each set of data:

Each test case begins with a single integer n{n}n , the count of numbers.

The next line are  n{n}n numbers.

输出描述:

 
 

For each set of Case, an integer is output, representing the smallest product.

示例1

输入

复制1 4 1 2 2 1

1
4
1 2 2 1

输出

复制122

122

示例2

输入

复制2 5 1 3 2 1 2 3 1 1 0

2
5
1 3 2 1 2
3
1 1 0

输出

复制1223 10

1223
10

备注:

1⩽T⩽1000,2⩽n⩽100000,1⩽∑n⩽1000000{ 1 \leqslant T \leqslant 1000}, 2 \leqslant n \leqslant 100000, { 1 \leqslant \sum n \leqslant 1000000}1⩽T⩽1000,2⩽n⩽100000,1⩽∑n⩽1000000

There are at least two Numbers that are guaranteed not to be zero.

The Numbers range between [0,9]{[0,9]}[0,9].

 思路:有点思维,但是看懂题就知道先把所有数排序,分成一个一位数和其他数是最小的乘积,然后就是一个高精乘法,这题也是python也是直接水过(ps这套题python选手不是嘎嘎乱杀,然后祭上队友的高精)

t=int(input())
for i in range(t):
    n=int(input())
    a=input().split()
    a.sort()
    pos=0
    while a[pos]=='0':
        pos=pos+1
    x=a[pos]
    y=a[pos+1]+'0'*pos+''.join(a[pos+2:])
    print(int(x)*int(y))

 

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
typedef long long ll;
ll a[N],n;
void xf(ll flag){
	int up=0;
	for(int i=n-1;i>=1;i--){
		int x=a[i];
		if(i!=1)
		a[i]=((x*flag)+up)%10;
		else
		a[i]=(x*flag)+up;
		up=((x*flag)+up)/10;
	}
}
int main(){
	ll T;
	cin>>T;
	while(T--){
		cin>>n;
		for(ll i=0;i<n;i++){
			cin>>a[i];
		}
		sort(a,a+n);
		ll flaa=0;
		for(ll i=0;i<n;i++){
			if(a[i]>0){
				flaa=a[i];
				a[i]=-1;
				break;
			}
		}
		sort(a,a+n);
		ll cn=0;
		for(ll i=1;i<n;i++){
			if(a[i]==0){
				cn++;
			} 
			else break;
		}
		if(cn>0){
			swap(a[1],a[cn+1]);
		}
//		for(int i=1;i<n;i++){
//			cout<<a[i];
//		}cout<<endl<<flaa<<endl;
//		for(ll i=0;i<cn;i++){
//			res=res*10;
//		}
//		for(ll i=cn+2;i<n;i++){
//			res=res*10+a[i];
//		}
        xf(flaa);
		for(int i=1;i<n;i++){
			cout<<a[i];
		}
		cout<<endl;
	}
	return 0;
}

 K. The Flee Plan of Groundhog

链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

Groundhog was especially careful after the outbreak,so he put on his mask in the 1st1^{st}1st bedroom early,and then walked on the way to the nth{n^{th}}nth dormitory to play with Orange. There are n{n}n dormitories in ZLZX, which are connected by n−1{n-1}n−1 corridors. Each dormitory can be reached to each other.The length of each corridor is 1{1}1.The walking speed of Groundhog is 1 m/s{1\ \mathrm{m/s}}1 m/s.

At that moment the bad news came: After Groundhog set out for t{t}t seconds,Orange took his temperature, and it turned out to be 41℃ !!! In addition to grief and indignation, Orange decided to run to Groundhog, to tell him the news at the speed of  2 m/s{2\ \mathrm{m/s}}2 m/s.

Groundhog had to run, of course, but he was running too slow at 1 m/s{1\ \mathrm{m/s}}1 m/s . As he ran, he had an idea: if he ran with the best strategy, how soon would Orange catch up  with him? Define that every second Groundhog moves and then Orange moves again. Groundhog can choose to stay put.


Groundhog would have solved that, of course, but he is running away now, so he give it to you, the smartest one.

输入描述:

The first line contains two integers n,t{n,t}n,t。
The next n−1{n-1}n−1 lines,each line contains two integers x,y{x,y}x,y, indicating there is a corridor between the  xth{x^{th}}xth dormitory and the yth{y^{th}}yth dormitory.

输出描述:

An integer, indicating the latest time for Orange to catch Groundhog.

示例1

输入

复制7 2 1 2 2 5 5 7 5 6 3 6 3 4

7 2
1 2
2 5
5 7
5 6
3 6
3 4

输出

复制1

1

说明

After t{t}t seconds, Groundhog is in the 5th5^{th}5th dormitory and Orange is in the 7th7^{th}7th dormitory.At this point, the best way for Groundhog is to goto the 2nd2^{nd}2nd dormitory or the 6th6^{th}6th dormitory.But wherever he goes, he will be immediately caught by Orange.

备注:

1≤n≤105,1≤t≤n−1,1≤x,y≤n1 \le n \le 10^5, 1\le t \le n-1 , 1\le x,y \le n1≤n≤105,1≤t≤n−1,1≤x,y≤n.

思路:他逃他追,他插翅难飞,这里就是一个你追我怕的恐怖故事(你说你没事你发烧了你还来找我干啥,果真“好朋友”,然后就是先找出鼠鼠第k秒所在位置(这里我用了一个最短路去朋友家,但是好像用不用都行,因为好像只有一条路),接着求鼠鼠和橙子分别从当前点到达每一个点的时间,(用dfs),最后再用一次dfs求从鼠鼠的位置开始跑,在哪些点能比橙子早到,我们旧记录那个我们最晚不被追到的点。(这里本来我们一直在想着用公式来做,但是真的就暴力,好吧,以后不要乱想,有时候暴力没准能过呢)

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const int N = 3e5 + 10;
const int INF = 0x3f3f3f3f3f3f;
int n, m;
vector<int> vv;
struct edge{
	int from, to;
	ll w;
	edge(int a, int b, int c):
		from(a), to(b), w(c){}
};
vector<edge> e[N];
struct s_node{
	int id;
	ll n_dis;
	s_node(int a, ll b):
		id(a), n_dis(b)
	{}
	bool operator < (const s_node  x)const{
		 return n_dis > x.n_dis;
	}
};
ll dis[N];
ll done[N];
int pre[N];
int vis1[N], vis2[N], vis[N];
priority_queue<s_node> Q;
void print_path(int s, int t){
	if(s == t){
//		cout<<s<<" ";
		vv.push_back(s);
		return ;
	}
	print_path(s, pre[t]);
//	cout<<t<<" ";
	vv.push_back(t);
}
void d(){
//	int s = 1, n_dis = 0; 
	
	for(int i = 1; i <= n; i ++){
		dis[i] = INF;
		done[i] = false;
	}
	int s = 1;
	dis[s] = 0;
	Q.push(s_node(s, dis[s]));
	while(!Q.empty()){
		s_node u = Q.top();
		Q.pop();
		if(done[u.id])  continue;
		done[u.id] = true;
		for(int i = 0; i < e[u.id].size(); i++){
			edge y = e[u.id][i];
			if(done[y.to]) continue;
			if(dis[y.to] > y.w + dis[u.id]) dis[y.to] = y.w + dis[u.id];
			Q.push(s_node(y.to, dis[y.to]));
			pre[y.to] = u.id;
		}
	}
	
}
void dfs1(int x, int ann){
	vis[x] = 1;
	vis1[x]  = ann;
	for(int i = 0; i < e[x].size(); i ++){
		
		if(!vis[e[x][i].to]){
			dfs1(e[x][i].to, ann + 1);
		} 
	}
	vis[x] = 0;
}
void dfs2(int x, int ann){
	vis[x] = 1;
	vis2[x]  = ann >> 1;
	for(int i = 0; i < e[x].size(); i ++){
		if(!vis[e[x][i].to]){
			dfs2(e[x][i].to, ann + 1);
		} 
	}
	vis[x] = 0;
}
int maxx = -1;
void dfs3(int x, int ann){
	vis[x] = 1;	
	maxx = max(maxx, vis2[x]);//没追上之前求最迟的时间 
	for(int i = 0; i < e[x].size(); i ++){
		if(!vis[e[x][i].to] && vis1[e[x][i].to] < vis2[e[x][i].to]) dfs3(e[x][i].to, ann + 1);
		else if(!vis[e[x][i].to] && vis1[e[x][i].to] >= vis2[e[x][i].to]){
			return;//当追上了,这条线就不通了,就可以返回了 
		}
	}
	vis[x] = 0;
}
int main(){
	cin>>n;
	int k;
	for(int i = 1; i <= n; i++ ) e[i].clear();
	cin>>k;
	for(int i = 0; i < n - 1; i ++){
		int a, b;
		ll c = 1;
		cin>>a>>b;
		e[a].push_back(edge(a, b, c)) ;
		e[b].push_back(edge(b, a, c)) ;
	}
	d();
	print_path(1, n);
	if(k >= vv.size()) cout<<0<<endl;
	else {
		int begin = vv[k];
		cout<<begin<<endl;
		dfs1(begin, 0);
		dfs2(n, 1);
		dfs3(begin, 0);
		cout<< maxx<<endl;
	}
	return 0;
}

 

  • 23
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小竹子14

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值