ACM日常训练日记——7.27(快速幂,贪心)
- SMU Summer 2024 Contest Round 8
1.Ubiquity
考数学,我的想法是排列组合,在n个空位中选任意两个插入0和9,其他空位任意插入,存在问题,考虑不够全面
正确做法的思想是容斥原理没怎么写过这类题,长见识了
#include <iostream>
#include <cmath>
using namespace std;
const int MOD = 1000000007;
// 快速幂函数
long long power(long long x, long long y, long long mod) {
long long result = 1;
while (y > 0) {
if (y % 2 == 1) {
result = (result * x) % mod;
}
x = (x * x) % mod;
y /= 2;
}
return result;
}
// 计算满足条件的序列数量
long long count_sequences(int N) {
long long ten_power_N = power(10, N, MOD);
long long nine_power_N = power(9, N, MOD);
long long eight_power_N = power(8, N, MOD);
long long result = (ten_power_N - 2 * nine_power_N + eight_power_N) % MOD;
if (result < 0) {
result += MOD;
}
return result;
}
int main() {
int N;
cin >> N;
cout << count_sequences(N) << endl;
return 0;
}
2.Left Right Operation
这道题我的思路是前缀和加上后缀和,然后每次比较前后缀和选的l,r比较,但是这样做超时了,正确做法是
贪心,每次从前往后取每次比较换取的值更新,从前往后一次,从后往前一次
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N=200200;
LL a[N];
LL f[N],g[N];
int main()
{
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
LL n,L,R;
cin>>n>>L>>R;
for(LL i=1;i<=n;i++)
cin>>a[i];
f[0]=0;
for(LL i=1;i<=n;i++)
f[i]=min(f[i-1]+a[i],i*L);
g[n+1]=0;
for(LL i=n;i>=1;i--)
g[i]=min(g[i+1]+a[i],(n-i+1)*R);
LL minn=1e18;
for(LL i=0;i<=n;i++)
{
minn=min(minn,f[i]+g[i+1]);
}
cout<<minn<<endl;
return 0;
}