寒假训练——第一周(枚举)

目录

A - 火柴棒等式 

B - 砝码称重 

C - 4 values whose sum is 0

E - Yet Another Card Deck


A - 火柴棒等式 

A - 火柴棒等式

打表,枚举每一种情况

0时特判为6,其他打表的出结论,查表判断是否成立

#include <bits/stdc++.h>

#define fast ios::sync_with_stdio(false), cin.tie(nullptr); cout.tie(nullptr)

#define x first
#define y second
#define int long long

using namespace std;

typedef pair<int, int> PII;

const int N = 2e6 + 10, M = N * N;
const int mod = 1e11 + 3;
const double eps = 1e-8;

int n, m;
int num[] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
int get_num[3010];

int get(int x)
{
    int res = 0;
    
    while(x)
    {
        res += num[x % 10];
        x /= 10;
    }
    
    return res;
}

void init()
{
    get_num[0] = 6;
    for (int i = 1; i <= 3000; i ++ )
        get_num[i] = get(i);
}

signed main()
{
    fast;
    
    cin >> n;
    init();
    
    int res = 0;
    for (int i = 0; i <= 1000; i ++ )
        for (int j = 0; j <= 1000; j ++ )
                if(get_num[i] + get_num[j] + get_num[i + j] + 4 == n)
                    res ++;
    
    cout << res << endl;
    
    return 0;
}

B - 砝码称重 

B - 砝码称重

枚举每一种砝码的情况判断即可

#include <bits/stdc++.h>

#define fast ios::sync_with_stdio(false), cin.tie(nullptr); cout.tie(nullptr)

#define x first
#define y second
//#define int long long

using namespace std;

typedef pair<int, int> PII;

const int N = 2e6 + 10, M = 1010;
const int mod = 1e9 + 7;
const double eps = 1e-8;

int n, m;
int nums[10];
int weight[] = {0, 1, 2, 3, 5, 10, 20};
unordered_set<int> ww;

signed main()
{
    fast;
    
    for (int i = 1; i <= 6; i ++ )
        cin >> nums[i];
    
    for (int i1 = 0; i1 <= nums[1]; i1 ++ )
        for (int i2 = 0; i2 <= nums[2]; i2 ++ )
            for (int i3 = 0; i3 <= nums[3]; i3 ++ )
                for (int i4 = 0; i4 <= nums[4]; i4 ++ )
                    for (int i5 = 0; i5 <= nums[5]; i5 ++ )
                        for (int i6 = 0; i6 <= nums[6]; i6 ++ )
                        {
                            int res = i1 * weight[1] + i2 * weight[2] + i3 * weight[3] 
                                    + i4 * weight[4] + i5 * weight[5] + i6 * weight[6];
                            if(res) ww.insert(res);
                        }
    
    cout << "Total=" << ww.size() << endl;
    
    return 0;
}

C - 4 values whose sum is 0

C - 4 values whose sum is 0

折半查找,先枚举两个数的和,保存在 hash 表中,再枚举另外两个数的和

必须手写哈希,STL 会 TLE

#include <bits/stdc++.h>

#define fast ios::sync_with_stdio(false), cin.tie(nullptr); cout.tie(nullptr)

#define x first
#define y second
//#define int long long

using namespace std;

typedef pair<int, int> PII;

const int N = 4050, M = 16000057 * 2, null = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;

int n, m;
int a[N], b[N], c[N], d[N];
PII cnt[M];

int find0(int x)
{
    int t = (x % M + M) % M;

    while (cnt[t].x != null && cnt[t].x != x)
    {
        t ++;
        if(t == M) t = 0;
    }

    return t;
}

signed main()
{
    fast;
    cin >> n;

    for (int i = 0; i <= M; i ++ )
        cnt[i].x = null;

    for (int i = 1; i <= n; i ++ )
        cin >> a[i] >> b[i] >> c[i] >> d[i];

    for (int i = 1; i <= n; i ++ )
        for (int j = 1; j <= n; j ++ )
        {
        	//cout << a[i] + b[j] << " " << find0(a[i] + b[j]) << endl;
        	cnt[find0(a[i] + b[j])].x = a[i] + b[j];
        	cnt[find0(a[i] + b[j])].y ++;
        	//cout << find0(a[i] + b[j]) << endl;
        }

    int res = 0;
    for (int i = 1; i <= n; i ++ )
        for (int j = 1; j <= n; j ++ )
        {

            int t = c[i] + d[j];
            if(cnt[find0(-t)].x != null)
            {
               // cout << find0(-t) << endl;
                res += cnt[find0(-t)].y;
            }
        }

    cout << res << endl;

    return 0;
}

E - Yet Another Card Deck

E - Yet Another Card Deck

map保存最近的一次,每一次询问输出map[x],并更新其他数

#include <bits/stdc++.h>

#define fast ios::sync_with_stdio(false), cin.tie(nullptr); cout.tie(nullptr)

#define x first
#define y second
#define int long long

using namespace std;

typedef pair<int, int> PII;

const int N = 4050, M = 16000057 * 2, null = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;

int n, m;
map<int, int> mp;

signed main()
{
    fast;
    cin >> n >> m;
    
    int x;
    for (int i = 1; i <= n; i ++ )
    {
        cin >> x;
        if(!mp[x]) mp[x] = i;
    }
    
    int xx;
    while ( m -- )
    {
        cin >> xx;
        cout << mp[xx] << " ";
        int t = mp[xx];
        for (auto it : mp)
        {
            if(it.y < t) mp[it.x] ++;
            else if(it.y == t) mp[it.x] = 1;
        }
    }

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AC自动寄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值