2021-06-14
Posted skywalker767
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021-06-14相关的知识,希望对你有一定的参考价值。
HPU20级算协积分赛01
目录
宏定义用的比较多,主要是这俩
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define pre(i, b, a) for (int i = (b); i >= (a); --i)
int类型读入基本都使用的是快读,换成scanf理论上不会有任何问题;
A - Doin’ Time
题目思路:
算法:区间DP,快速幂求逆元。
状态转移方程:
f
[
i
]
[
j
]
=
m
a
x
(
f
[
i
]
[
j
]
,
f
[
i
]
[
k
]
+
f
[
k
+
1
]
[
j
]
+
(
x
−
y
)
2
)
f[i][j] = max(f[i][j] , f[i][k] + f[k + 1][j] + (x - y) ^ 2)
f[i][j]=max(f[i][j],f[i][k]+f[k+1][j]+(x−y)2)
AC代码:
#include <bits/stdc++.h>
#define lowbit(x) (x & (-x))
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define pre(i, b, a) for (int i = (b); i >= (a); --i)
using namespace std;
int read() {int x = 0, w = 1;char ch = 0;while (ch < '0' || ch > '9') { if (ch == '-') w = -1;ch = getchar();}while (ch >= '0' && ch <= '9') { x = x * 10 + (ch - '0');ch = getchar(); }return x * w;}
typedef long long LL;
typedef pair<int , int > PII;
const int N = 310;
const int mod = 1000003;
const int inf = 0x3f3f3f3f;
LL f[N][N] , a[N];
int n , cost[N] , incost[N];
int qmi(int a , int k)
{
int res = 1;
while(k)
{
if(k & 1) res = (LL)res * a % mod;
a = (LL)a * a % mod;
k >>= 1;
}
return res;
}
int main()
{
n = read(); cost[0] = 1 , incost[0] = 1;
rep(i , 1 , n)
{
scanf("%lld" , &a[i]);
cost[i] = cost[i - 1] * a[i] % mod;
incost[i] = qmi(cost[i] , mod - 2);
}
rep(len , 2 , n)
{
for(int i = 1;i + len - 1 <= n;i ++)
{
int j = i + len - 1;
for(int k = i;k < j;k ++)
{
LL x = (LL)cost[k] * incost[i - 1] % mod;
LL y = (LL)cost[j] * incost[k] % mod;
f[i][j] = max(f[i][j] , f[i][k] + f[k + 1][j] + (x - y) * (x - y));
}
}
}
printf("%lld" , f[1][n]);
return 0;
}
B - Function
题目思路:
算法:欧拉筛
思路:
众所周知:欧拉筛相对于埃氏筛法的优化在于每个数只会被最小质因数筛掉,也正是这一步,把时间复杂度提升到了
o
(
n
)
o(n)
o(n),对欧筛稍作修改,此题就可以AC了。
- f[1 and 单个素数] = 1,这个很容易晒出来,只需要在
if(!st[i]) prmes[cnt ++] = i
后面加一句f[i] = 1
; - 如果要筛掉
primes[j] * i
这个数,那么又分为两种情况。
注意:这个时候,primes[j]一定是<= prmes[j] ∗ * ∗ i的最小质因子
1.i
含有primes[j]
,那么属于第二类, priems[j]是其最小质因子,算出i*primes[j]
中prmes[j]
的次数,根据奇偶,就可以从f[i]
推出f[primes[j] * i]
的值,如果是次数是奇数次,再乘一下,就会多一个primes[j]
的值,如果是偶数,则f[primes[j] * i]
=f[i]
2.如果不含有primes[j]
,那么primes[j]
一定是i * primes[j]
的最小质因子,且次数一定等于1,那么f[primes[j] * i]
=i(第三种)。
AC代码:
#include <bits/stdc++.h>
#define lowbit(x) (x & (-x))
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define pre(i, b, a) for (int i = (b); i >= (a); --i)
using namespace std;
int read() {int x = 0, w = 1;char ch = 0;while (ch < '0' || ch > '9') { if (ch == '-') w = -1;ch = getchar();}while (ch >= '0' && ch <= '9') { x = x * 10 + (ch - '0');ch = getchar(); }return x * w;}
typedef long long LL;
typedef pair<int , int > PII;
const int N = 1e7 + 10;
int primes[N] , cnt , n;
LL f[N] , sum[N];
bool st[N];
LL init(int n)
{
LL ans = 0;
f[1] = 1;
for(int i = 2;i <= n;i ++)
{
if(!st[i])
{
primes[cnt ++ ] = i;
f[i] = 1;
//第一种
}
for(int j = 0;primes[j] <= n / i;j ++)
{
st[primes[j] * i] = true;
//第二种
if(i % primes[j] == 0)
{
int count = 0;
int ri = i;
while(ri % primes[j] == 0)
{
count ++;
ri /= primes[j];
}
if(count & 1) f[primes[j] * i] = f[i] * primes[j];
else f[primes[j] * i] = f[i];
break;
}
//第三种
f[primes[j] * i] = i;
}
}
rep(i , 1 , n) ans += f[i];
return ans;
}
int main()
{
n = read();
printf("%lld" , init(n));
return 0;
}
C - Hack DSU!
题目思路:
AC代码:
#include <bits/stdc++.h>
#define lowbit(x) (x & (-x))
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define pre(i, b, a) for (int i = (b); i >= (a); --i)
using namespace std;
int read() {int x = 0, w = 1;char ch = 0;while (ch < '0' || ch > '9') { if (ch == '-') w = -1;ch = getchar();}while (ch >= '0' && ch <= '9') { x = x * 10 + (ch - '0');ch = getchar(); }return x * w;}
typedef long long LL;
typedef pair<int , int > PII;
int n , T;
int main()
{
n = read() , T = read();
printf("%d %d\\n" , n , 1);
pre(i , n - 1 , 2) printf("%d %d\\n" , i , i - 1);
printf("%d %d\\n" , 1 , n);
return 0;
}
D - JOJO’s Factory
题目思路:
AC代码:
#include <bits/stdc++.h>
#define lowbit(x) (x & (-x))
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define pre(i, b, a) for (int i = (b); i >= (a); --i)
using namespace std;
int read() {int x = 0, w = 1;char ch = 0;while (ch < '0' || ch > '9') { if (ch == '-') w = -1;ch = getchar();}while (ch >= '0' && ch <= '9') { x = x * 10 + (ch - '0');ch = getchar(); }return x * w;}
typedef long long LL;
typedef pair<int , int > PII;
int n , m , counter_1 , counter_2;
unordered_map<int , int> bj1 , bj2;
int main()
{
n = read() , m = read();
rep(i , 1 , m)
{
int a , b;
a = read() , b = read();
bj1[a] ++; bj2[b] ++;
if(bj1[a] == n) counter_1 ++;
if(bj2[b] == n) counter_2 ++;
}
printf("%d" , min(n - counter_1 , n - counter_2));
return 0;
}
E - Keep Eating
题目思路:
AC代码:
#include <bits/stdc++.h>
#define lowbit(x) (x & (-x))
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define pre(i, b, a) for (int i = (b); i >= (a); --i)
using namespace std;
int read() {int x = 0, w = 1;char ch = 0;while (ch < '0' || ch > '9') { if (ch == '-') w = -1;ch = getchar();}while (ch >= '0' && ch <= '9') { x = x * 10 + (ch - '0');ch = getchar(); }return x * w;}
typedef long long LL;
typedef pair<int , int > PII;
LL sum;
int n , k , x;
int main()
{
n = read() , k = read();
rep(以上是关于2021-06-14的主要内容,如果未能解决你的问题,请参考以下文章
《安富莱嵌入式周报》第217期:2021.06.14--2021.06.20