2023.3.10
这几天讲的倍增捏(一开始以优化 dp 为主,现在几乎纯倍增,6)。然后发现我不会快速幂。
贴一个板子。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll ans = 1;
ll a, b, p;
ll aa, bb, mod;
int main()
{
scanf("%lld%lld%lld", &a, &b, &p);
aa = a;
bb = b;
mod = p;
while(b)
{
if(b % 2) ans = ans * a % p;
a = a * a % p;
b /= 2;
}
printf("%lld", ans % p);
return 0;
}
3.09 模拟赛 T2,洛谷:P3509 ZAB-Frog
正解是倍增 + 单调队列。但是对于倍增最后空间会卡,所以要开动态数组,这里没有太理解,想明白了再补过来思路。先存一下码。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1000010;
ll m;
int n, k;
ll a[N], nex[N], f[N], ans[N];
int main()
{
scanf("%d%d%lld", &n, &k, &m);
for(int i = 1; i <= n; i ++ ) scanf("%lld", &a[i]);
int head = 1, tail = k + 1;
nex[1] = k + 1;
for(int i = 2; i <= n; i ++ )
{
while(tail + 1 <= n && a[i] - a[head] > a[tail + 1] - a[i]) head ++, tail ++;
if(a[i] - a[head] >= a[tail] - a[i]) nex[i] = head;
else nex[i] = tail;
}
for(int i = 1; i <= n; i ++ ) ans[i] = i;
while(m > 0)
{
if(m % 2 == 1) for(int i = 1; i <= n; i ++ )ans[i] = nex[ans[i]];
m /= 2;
for(int i = 1; i <= n; i ++ ) f[i] = nex[i];
for(int i = 1; i <= n; i ++ ) nex[i] = f[f[i]];
}
for(int i = 1; i <= n; i ++ ) printf("%lld ", ans[i]);
return 0;
}