SDUT 2021 Autumn Team Contest 4th

2021/9/13 23:05:10

本文主要是介绍SDUT 2021 Autumn Team Contest 4th,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

文章目录

  • Problem A. Super-palindrome——思维
  • Problem J. Master of GCD——差分+快速幂
  • Problem C. Master of Phi——枚举找规律博弈
  • Problem B. Master of Phi——数论推公式
  • Problem D. Master of Random——逆元

以下题目出自 该网址

Problem A. Super-palindrome——思维

给定一个字符串,输出经过几步可以变成超级回文串
超级回文串定义:对于每一个奇数长度的子串,都是回文串
对于一个奇数n,长度为n的字符串是回文串,那么长度为n+2的字符串想要是回文串,就必须使奇数位上的数相同,偶数位上的数相同,得出结论:
符合超级回文串的字符串一定是 ababababab… 型的回文串,所以只要找到奇数/偶数位上的出现次数最多的字符串就行了

#include <iostream>
#include <string.h>
using namespace std;
int st[130];
int main()
{
	int t;
	cin >> t;
	while(t -- )
	{
		string a;
		cin >> a;
		int ans = 0;
		
		int mx = 0;
		memset(st, 0, sizeof(st));
		for(int i = 0; i < a.size(); i +=2 )
		{
			st[a[i]] ++ ;
			mx = max(st[a[i]], mx);
		}
		ans += (a.size() + 1) / 2 - mx;
		
		mx = 0;
		memset(st, 0, sizeof(st));
		for(int i = 1; i < a.size(); i +=2 )
		{
			st[a[i]] ++ ;
			mx = max(st[a[i]], mx);
		}
		ans += a.size() / 2 - mx;
		cout << ans << endl;
	}
}

Problem J. Master of GCD——差分+快速幂

一开始以为是线段树,但是嫌代码长细看了一会题,发现差分就可以实现题目要求,但是要用加减法模拟乘法,不然会爆longlong,题目中说了,只有2 和 3 ,所以开俩数组,一个存2,一个存3,然后将差分还原,最后算的时候只需要找出现次数最小的2和3就行了,然后求2和3的快速幂相乘就行了,记得取模,记得加速cin或者直接用scanf

#include <iostream>
#include <string.h>
using namespace std;
const int N = 1e5 + 10, mod = 998244353;
typedef long long ll;
int a[N], b[N];
ll f(ll a, ll b)
{
	ll res = 1;
	while(b)
	{
		if(b % 2) res = (res * a) % mod;
		a = a * a % mod; 
		b >>= 1;
	}
	return res;
}
int main()
{
	int t;
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> t;
	while(t -- )
	{
		memset(a, 0, sizeof(a));
		memset(b, 0, sizeof(b));
		int n, m;
		cin >> n >> m;
		while(m -- )
		{
			int l, r, c;
			cin >> l >> r >> c;
			if(c == 2)
			{
				a[l] ++;
				a[r + 1] --;
			}
			else
			{
				b[l] ++;
				b[r + 1] --;
			}
		}
		ll mn1, mn2;
		mn1 = mn2 = 0x3f3f3f3f;
		for(int i = 1; i <= n; i ++ )
		{
			a[i] += a[i - 1];
			b[i] += b[i - 1];
			mn1 = min(mn1, (ll)a[i]);
			mn2 = min(mn2, (ll)b[i]);
		}
		ll ans = 1;
		ans = f(2, mn1) * f(3, mn2) % mod;
		cout << ans << endl;
	}
}

Problem C. Master of Phi——枚举找规律博弈

傻逼题,恶心人,人家一猜就猜出来了,我们搁着磨叽半天打表才看出来,太恶心了

#include <iostream>
using namespace std;
int main()
{
    int t;
    cin >> t;
    while(t -- ) 
	{
        int n, d, ans, flag;
        ans = flag = 0;
        cin >> n >> d;
        for(int i = 0; i < n; i ++ )
		{
            int x;
            cin >> x;
            if(x != 1) flag ++;//计算堆中石子数量为1的堆数
        }
        if(flag == 0) //没有石子数为1的堆
		{
            int f = n % 3;//每三组为一对
            if(d == 1)
			{
                if(f == 1 || f == 2) cout << "Yes\n";
                else cout << "No\n";
            } 
			else
			{
                if(f == 0 || f == 2) cout << "Yes\n";
                else cout << "No\n";
            }
        }
		else //有石子数为1的堆
		{
            if(d == 1 || (d == 2 && flag >= 2)) cout << "Yes\n";
            else cout << "No\n";
        }
    }
    return 0;
}

Problem B. Master of Phi——数论推公式

这题队友推出来的,补题的时候看了个大佬的博客,居然让我这种数论废物都看懂了,而且公式推的很清晰,推荐大伙们去看看
大佬传送门

#include<iostream>
using namespace std;
const int N = 1e5 + 10, mod = 998244353;
typedef long long LL;
LL f(LL a, LL b)
{
	LL res = 1;
	while(b)
	{
		if(b % 2) res = res * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return res;
}

int main()
{
	int t;
	cin >> t;
	while(t -- )
	{
		int n;
		LL ans = 1;
		cin >> n;
		for(int i = 0; i < n; i ++ )
		{
			LL p, q;
			cin >> p >> q;
			//取模非常重要,注意减号,减一个数一定要 + mod 再取余 mod,不然可能有负数
			ans = (ans * f(p, q - 1) % mod * ( ( (p * q % mod + p) % mod - q + mod) % mod) ) % mod;
		}
		cout << ans << endl;
	}
}

Problem D. Master of Random——逆元

这题当时真没看懂意思,后来看着别的大佬博客里的题意介绍配合题目的样例解释终于读懂了,这题代码很简单,一读懂就会,没读懂就拉跨了
题意介绍:
给你 n 个点,第 i 个点只能让1 ~ i -1(比如,第4节点的父节点只能是1或2或3)的点当自己的父节点,由此可以建出一颗树,但是这棵树有很多种可能,算出每个节点的子树的权值总和的期望
看不懂没事,举个例子:
n = 4
编号1 2 3 4
权值1 2 3 4
2的父节点只能是1
3的父节点可能是1,可能是2
4的父节点可能是1,可能是2,可能是3
第一种情况
1节点的子树权值和 = 1 + 2 + 3 + 4 = 10
2节点的子树权值和 = 2
3节点的子树权值和 = 3
4节点的子树权值和 = 4
第二种情况
1节点的子树权值和 = 1 + 2 + 3 + 4 = 10
2节点的子树权值和 = 2 + 4 = 6
3节点的子树权值和 = 3
4节点的子树权值和 = 4
第三种情况
1节点的子树权值和 = 1 + 2 + 3 + 4 = 10
2节点的子树权值和 = 2
3节点的子树权值和 = 3 + 4 = 7
4节点的子树权值和 = 4
第四种情况
1节点的子树权值和 = 1 + 2 + 3 + 4 = 10
2节点的子树权值和 = 2 + 3 = 5
3节点的子树权值和 = 3
4节点的子树权值和 = 4
第五种情况
1节点的子树权值和 = 1 + 2 + 3 + 4 = 10
2节点的子树权值和 = 2 + 3 + 4 = 9
3节点的子树权值和 = 3
4节点的子树权值和 = 4
第六种情况
1节点的子树权值和 = 1 + 2 + 3 + 4 = 10
2节点的子树权值和 = 2 + 3 + 4 = 9
3节点的子树权值和 = 3 + 4 = 7
4节点的子树权值和 = 4

一共六种情况,每种情况都是求出4棵树的权值和,所以期望是24棵子树权值和相加除去24(也就是乘24的乘法逆元)
我们来统计一下每一个节点出现的次数我们可以得到以下规律
1——6次 = (3!)
2——12次 = (3!) + (3!)/1
3——15次 = (3!) + (3!)/1 + (3!)/2
4——17次 = (3!) + (3!)/1 + (3!)/2 + (3!)/3
而且24 = 4!
所以答案是 (1*6 + 2*12 + 3*15 + 4*17) / n!
最终答案 = 每个节点的权值 * 该节点的出现次数 / n!

#include<iostream>
using namespace std;
const int N = 1e5+10, mod = 998244353;
typedef long long LL;
LL qmi(LL a) // 求a的逆元
{
	LL b = mod - 2;
	LL p = mod;
	LL res = 1;
	while(b)
	{
		if(b & 1) res = res * a % p;
		b >>= 1;
		a = a * a % p;
	}
	return res;
}
int main()
{
    ios::sync_with_stdio(false);
	int t;
	cin >> t;
	while(t--)
	{
		LL n;
		LL ans = 0, res = 1, tmp = 0;//ans是最终答案,res是(n-1)!,tmp是每个节点出现的次数
		cin >> n;
		for(int i = 2; i <= n - 1; i ++ )
		{
			res = res * i % mod;
		}

		for(int i = 0; i < n; i ++ )
		{
			LL x;
			cin >> x;
			if(i == 0) 
			{
				ans = x * res % mod; 
				tmp = res; 
			}
			else
			{
				tmp = tmp + (res *qmi(i) % mod) % mod;//先计算该点出现次数
				ans = (ans + x * tmp % mod) % mod;//ans+出现次数*权值
			}
		}
		
		ans = ans * qmi(res * n % mod) % mod;//最后乘n!的逆元
		
		cout << ans << endl;
	}
}


这篇关于SDUT 2021 Autumn Team Contest 4th的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程