小红的基环树
题解:明显最优为一朵花的形状,那么任意俩俩之间的距离都为2,除了3是1其他都是2
实现代码
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
if(n == 3) cout << 1 << endl;
else cout << 2 << endl;
return 0;
}
小红的回文串
题解:首先回文串那么就对半遍历,若n为奇数,则多处理一次最中间,由题目可知,?能代替所有字母,对半遍历的时候,如果两边不相等且都不为?那么直接输出0,若有一个为?那么这个?只能相对应变前成这个,如果为俩问号,则乘以26(不开LL见祖宗);
实现代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int Mod = 1e9 + 7;
int main()
{
string s;
cin >> s;
int n = s.size();
int sum = 0;
for(int i = 0 ; i < n / 2 ; i ++ )
if(s[i] == '?' && s[n - i - 1] == '?') sum ++;
else if(s[i] != s[n - 1 - i])
{
if(s[i] != '?' && s[n - i - 1] != '?')
{
cout << 0 << endl;
return 0;
}
}
if(n & 1 && s[n / 2] == '?') sum ++;
long long ans = 1;
for(int i = 0 ; i < sum ; i ++ ) ans = (ans * 26) % Mod;
cout << ans << endl;
}
小红的数组操作(easy version)
设a为加x次数,b为减y次数 , sum为元素总和。
(sum + ax - by) % n = 0
(sum % n - by % n) % n = -ax % n;
n - (sum % n - by % n) % n = ax % n = a % n * x % n
所以只需枚举减y的数量,其x的数量就是左半边 /
实现代码
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 100010;
int a[N];
int main()
{
LL n , p , x , q , y;
cin >> n >> p >> x >> q >> y;
LL sum = 0;
for(int i = 0 ; i < n ; i ++ )
{
cin >> a[i];
sum += a[i];
}
sum %= n;
LL ans = 2e18;
for(int i = 0 ; i < n ; i ++ )
{
LL c = y * i % n;
LL s = (n - ((sum - c) % n)) % n;
ans = min(ans , i * q + s * p);
}
cout << ans << endl;
return 0;
}