题解

A.小红的7

取一下值就好了

int s  ; cin >> s ; 
        for(int i = 1 ; i <= 6 ; i++)
        {
            double a = i * 1.000000 / 7 ;
            int c = a * 1000000 ; 
            if(c == s)
            {
                cout << i << endl ; 
                break ; 
            }
        }

B.小红的回文串

手模一下,因为去掉一个之后补上来的要和删掉对应的这个相等

void work()
{
   int n ; cin >> n ; 
    string s ; cin >> s ; 
    s = " " + s ; 
    int c = n / 2 + 1 ; 
    int l = c ; 
    int r = c ; 
    while(l - 1 > 0 && s[l - 1] == s[l])
    {
        l-- ; 
    }
    while(r + 1 <= n && s[r + 1] == s[r])
    {
        r++ ; 
    }
    int z = r - l + 1 ; 
    cout << z << endl ; 
}

C.小红的gcd位运算构造

做到按位与是很容易的,所以按照按位寻找就行

void work()
{
   int x ; cin >> x ;
    int sum = 0 ; 
    for(int i = 0 ; i <= 64 ; i++)
    {
        if((x >> i) & 1)
        {
            continue ;
        }
        else
        {
            sum = sum + (1LL << i) ; 
            if(gcd(sum , x) == 1)
            {
                cout << sum << endl ; 
                return ; 
            }
        }
    }
}

D.i在西元前

回忆等比数列的性质,第一项×第三项等于第二项的平方,验证一下就行

void work()
{
   int n ; cin >> n ; 
    vi a(n) , b(n) ; 
    auto cal = [&](int z1 , int z2 , int z3 , int z4)->pair<int , int>{
        int c1 = z1 * z3 - (z2 * z4) ; 
        int c2 = z1 * z4 + z2 * z3 ; 
        return {c1 , c2} ; 
    };
    for(int i = 0 ; i < n ; i++)cin >> a[i] >> b[i] ; 
    for(int i = 0 ; i + 2 < n ; i++)
    {
        int a1 = a[i] ; 
        int a2 = b[i] ; 
        
        int b1 = a[i + 1] ; 
        int b2 = b[i + 1] ;
        
        int c1 = a[i + 2] ; 
        int c2 = b[i + 2] ;
        auto [md1 , md2] = cal(a1 , a2 , c1 , c2) ; 
        auto [md3 , md4] = cal(b1 , b2 , b1 , b2) ;
        if(md1 == md3 && md2 == md4)
        {
            continue ; 
        }
        else
        {
            cout << "No" << endl ;
            return ; 
        }
    }
    cout << "Yes" << endl ; 
}

E.小红打游戏

如你所想,01背包和完全背包分开操作就行

void work()
{
   int n , k ; cin >> n >> k ; 
    int x , y ; cin >> x >> y ; 
    vii dp(x + 1 , vi(y + 1 , inf)) ; 
    dp[0][0] = 0 ; 
    for(int i = 1 ; i <= n ; i++)
    {
        int a , b , c ; cin >> a >> b >> c ;
        if(i <= k)
        {
            for(int j = x ; j >= 0 ; j--)
            {
                for(int k = y ; k >= 0 ; k--)
                {
                    if(dp[j][k] == inf)continue ;
                    int nx = min(x , j + a) ; 
                    int ny = min(y , k + b) ; 
                    dp[nx][ny] = min(dp[nx][ny] , dp[j][k] + c) ; 
                }
            }
        }
        else
        {
            for(int j = 0 ; j <= x ; j++)
            {
                for(int k = 0 ; k <= y ; k++)
                {
                    if(dp[j][k] == inf)continue ;
                    int nx = min(x , j + a) ; 
                    int ny = min(y , k + b) ; 
                    dp[nx][ny] = min(dp[nx][ny] , dp[j][k] + c) ; 
                }
            }
        }
    }
    if(dp[x][y] == inf)cout << -1 << endl ; 
    else cout << dp[x][y] << endl ; 
}