2022.4.7

Codeforces Round #739 (Div. 3)

A. Dislike of Threes

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e9;
int vis[N];
int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    int t;
    cin >> t;
    for (int i = 1; i <= N;i++)
    {
        int x = i % 10;
        if(i%3==0)
        {
            vis[i] = 1;
        }
        if(x==3)
        {
            vis[i] = 1;
        }
    }
        while (t--)
        {
            int x,cnt=0;
            cin >> x;
            for (int i = 1; i <= N;i++)
            {
                if(vis[i])
                    continue;
                cnt++;
                if(cnt==x)
                {
                    cout << i << '\n';
                    break;
                }
            }
        }

    return 0;
}

B. Who's Opposite?

经过观察发现两个人对视的距离*2即为这个圆的周长,那么对于a,b,c而言如果有某个的编号大于周长显然是不合理的。排除不合理的情况剩下的只需要让c加加上这段距离即可,如果超过需要取模。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e9;
int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--)
    {
        int a, b, c;
        cin >> a >> b >> c;
        int dis = 2 * abs(a - b);
        if(a>dis||b>dis||c>dis)
        {
            cout << -1 << '\n';
        }
        else
        {
            int ans = (abs(a - b) + c);
            if(ans>dis)
                ans -= dis;
            cout << ans << '\n';
        }
    }
      
    return 0;
}

C. Infinity Table

一开始观察想直接暴力搜索确定数的位置,前1万个还行都后面就搜不了了,于是开始观察找规律,它是一层一层往外扩展的,想了想如果需要确定一个数的位置首先得知道它是在那一层的,于是打表把每个对角线的点都打出来,在枚举第一个等于大于它的点。此时有两种情况,第一:当前点如果大于当前的这一层的中心点的第一行的数的话,说明它是在这一层的,否则肯定是在上一层并且还大于上一层的中心点,发现这个性质后就很好解决了

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e9;
int x;
ll a[31625];
ll row[31625];
int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    int t,d=2,dd=1;
    cin >> t;
    row[1]=a[1] = 1;
    for (int i = 2; i <= 31624;i++)
    {
        a[i] = d + a[i - 1];
        d += 2;
        row[i] = dd +row[i - 1];
        dd += 2;
    }
        while (t--)
        {
            int i;
            cin >> x;
            for (i = 1; i <= 31624; i++)
            {
                if (a[i] >= x)
                {
                    break;
                }
            }
            if (x >= row[i])
            {
                cout << i-(a[i] - x)<<' ';
                cout << i << "\n";
            }
            else
            {
                i--;
                cout << i << ' ';
                cout << i-(x-a[i]) << '\n';
            }
        }
    return 0;
}

D. Make a Power of Two

预处理出2的幂的数,转化成字符串存储。找到转化最小次数可以枚举每个2次幂的字符串,输入字符串转换成当前的字符串的次数就是(a − 共有数字 + b − 共有数字)枚举的过程中更新最小值。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e9;
vector<string> s;
void db()
{
    for (ll i = 1; i <= 1e18;i<<=1)
    {
        s.push_back(to_string(i));
    }
}
int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    db();
    int t;
    cin >> t;
    while(t--)
    {
        string x;
        cin >> x;
        int len = x.size() + 1;
        for(auto t:s)
        {
            int xsz = x.size(), tsz = t.size();
            int same = 0, cnt1 = 0, cnt2 = 0;
            while(cnt1<xsz&&cnt2<tsz)
            {
                if(x[cnt1]==t[cnt2])
                {
                    same++;
                    cnt2++;
                }
                cnt1++;
            }
            len = min(len, xsz + tsz - 2 * same);
        }
        cout << len << '\n';
    }

    return 0;
}
posted @ 2022-04-08 11:04  menitrust  阅读(17)  评论(0编辑  收藏  举报