7/15 总结

· · 个人记录

A:

数据小,从x遍历到y就行

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int x, y;
ll gcd(ll a, ll b)
{
    return (a % b) ? gcd(b, a % b) : b;
}
int main()
{
    cin >> x >> y;
    ll m = x * y;
    ll cnt = 0;
    for (int i = x; i <= y; ++i)
    {
        if (m % i == 0)
        {
            if (m / i >= x && m / i <= y && gcd(i, m / i) == x)
                ++cnt;
        }
    }
    cout << cnt << endl;
    return 0;
}

B:

只要写出90度翻转和反射就行

180度=2*90度

270度=3*90度

#include <bits/stdc++.h>
using namespace std;
int n, m;
const int maxn = 105;
char a[maxn][maxn]; // 原数组 
char b[maxn][maxn]; // 中间旋转时候需要用到的数组(临时数组) 
char c[maxn][maxn]; // 目标数组 
// 旋转90度
void rotate()  // n行n列 
{
    for (int i=1;i<=n;++i) // n行 
    {
        for (int j=1;j<=n;++j) // n列 
        { 
            b[j][n-i+1] = a[i][j];
            // 原来的第i行,变成了现在的第n - i + 1 列
            // 原来的第j列,变成了现在的 j行 
            a[i][j] = 0; 
        }   
    } 
    for (int i=1;i<=n;++i) // n行 
    {
        for (int j=1;j<=n;++j) // n列 
        { 
            a[i][j] = b[i][j];  // 将b数组(旋转之后的)放回a数组之中 
        }   
    } 
} 
bool check() // 检查两个二维数组是否完全相等
{
    for (int i=1;i<=n;++i) // n行 
    {
        for (int j=1;j<=n;++j) // n列 
        { 
            if(a[i][j] != c[i][j])
            {
                return 0;// 说明不相等 
            } 
        }   
    } 
    return 1;// 说明相等 
} 
void Transform()// 水平变换
{
    for (int i=1;i<=n;++i) // n行 
    {
        for (int j=1;j<=n;++j) // n列 
        { 
            b[i][n - j + 1] = a[i][j];
            // 原来的第i行,变成了现在的第i行 
            // 原来的第j列,变成了现在的 n - j + 1列 
            a[i][j] = 0;
        }   
    } 
    // 将b数组之中的放回到a数组 
    for (int i=1;i<=n;++i) // n行 
    {
        for (int j=1;j<=n;++j) // n列 
        { 
            a[i][j] = b[i][j];  // 将b数组(旋转之后的)放回a数组之中 
        }   
    } 
} 
int main()
{
    cin >> n;
    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= n; ++j)
        {
            cin >> a[i][j]; 
        }   
    } 
    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= n; ++j)
        {
            cin >> c[i][j]; 
        }   
    } 
    rotate() ;// 旋转90度
    if (check())
    {
        cout << 1 << endl;
        return 0;   
    } 
    rotate() ;// 再旋转90度,等于旋转180度 
    if (check())
    {
        cout << 2 << endl;
        return 0;   
    } 
    rotate() ;// 再旋转90度,等于旋转270度 
    if (check())
    {
        cout << 3 << endl;
        return 0;   
    } 
    rotate() ;// 再旋转90度,等于旋转360度 
    Transform();// 水平翻转
    if (check())
    {
        cout << 4 << endl;
        return 0;   
    } 
    for (int i = 1; i <= 3; ++i) // 旋转3次,如果出现了相等,就说明是5答案 
    {
        rotate() ;// 再旋转90度
        if(check())
        {
            cout << 5 << endl;
            return 0;
        } 
    }
    Transform();// 水平翻转  记得翻转回去 
    rotate() ;// 再旋转90度,变成了360度
    if(check())
    {
        cout << 6 << endl;
        return 0;
    } 
    cout << 7 << endl;
    return 0;
}

C:

考虑差分

目标:

所有数字相等,就是差分数组除去diff[1] 和 diff[n+1]不是0之外,其余所有差分值都是0

[l,r]区间修改直接套公式:

[l,r] 内全体数字+1,相当于diff[l]+=1,diff[r+1] -= 1

[l,r] 内全体数字-1,相当于diff[l]-=1,diff[r+1]+=1

——————————

正解思路:

上述的操作就是可以让差分数组左侧-/+,右侧+/-

我们的想要减少操作次数,假设diff[2]<0,diff[3] > 0 ,那么左侧增加1,右侧减少1,同时执行

最终的操作次数,由所有diff值不为0的统一决定,此时记得跳过diff[1],因为它并不要求为0。

第一问的答案是 max(diff[i]大于0的数值之和, abs(diff[i] < 0) 的数值之和)。

第二问求解的最终得到的数列有多少种,其实取决于diff[1]的种类

因为 ∑ 1 n+1 ​ diff i ​ =0

所以diff[1] + (第二个到第n个刚刚的差值) + diff[n + 1] = 0

此时有x+1种选择

第二个到第n个刚刚的差值和diff[1] 做x次左侧和右侧的变化,然后立马和diff[n + 1]进行左侧和右侧的变化,

diff[1]会产生第二个到第n个刚刚的差值 + 1种 变化次数

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int MAXN = 100010;
ll n, x, y, a[MAXN], diff[MAXN];
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        diff[i] = a[i] - a[i - 1];
    }
    for (int i = 2; i <= n; i++)
    {
        if (diff[i] > 0)
            x += diff[i];
        else
            y -= diff[i];
    }
    cout << max(x, y) << endl
         << abs(x - y) + 1 << endl;
}

D:

dp开不了那么大,bfs队列会乱

可以考虑dfs

但数据容易越界,

所以可以反向:

*2-> /2

*3-> /3

*5-> /5

如果x存在余数,我们有两种方法:

x−x%3 的方式,减去x%3这么多个1。

x+3−x%3的方式,加上3−x%3个1 。

同时可以使用map记忆化进一步提速

#include <bits/stdc++.h>
#define int unsigned long long
using namespace std;
const int N = 1e18;
map<int, int> mp;
int n, a, b, c, d;
int dfs(int x)
{
    if (mp.count(x))
    {
        return mp[x];
    } 
    int ans = x * d;
    if (x % 2)  
    {
        ans = min(ans, min(dfs((x - x % 2) / 2), dfs((x + 2 - x % 2) / 2)) + a + d);
    }
    else
    {
        ans = min(ans, dfs(x / 2) + a);
    }
    if (x % 3)
    {
        ans = min(ans, min(dfs((x - x % 3) / 3) + (x % 3) * d, dfs((x + 3 - x % 3) / 3) + (3 - x % 3) * d) + b);
    }
    else
    {
        ans = min(ans, dfs(x / 3) + b);
    }

    if (x % 5)
    {
        ans = min(ans, min(dfs((x - x % 5) / 5) + (x % 5) * d, dfs((x + 5 - x % 5) / 5) + (5 - x % 5) * d) + c);
    }
    else
    {
        ans = min(ans, dfs(x / 5) + c);
    }
    mp[x] = ans;
    return ans;
}
signed main()
{
    int T;
    cin >> T;
    while (T--)
    {
        cin >> n >> a >> b >> c >> d;
        mp.clear();
        mp[0] = 0;
        mp[1] = d;
        cout << dfs(n) << "\n";
    }
    return 0;
}