Progressive Square&&Product of Binary Decimals

文章分析了两道编程竞赛题目,第一题要求在给定条件下的数独解决方案,利用哈希优化避免TLE;第二题考查如何判断数组能否被0和1组成的数字整除,通过打表策略解决。
摘要由CSDN通过智能技术生成

Cf Round 938(Div.3)-B. Progressive Square

 题意:

        第一眼:行列式?

        第二眼:数独?

        每一行是等差数列,每一列也是等差数列。就这么简单。

真是太简单了哈哈哈。。。

         500*500的数组会出现TLE的情况就是写了个三循环或者以上的循环。这题一开始是用暴力和前缀和求解。记录第一个元素然后把第一行的所有元素优先算出来,再对于每一列进行等差计算。

        但是由于本题可以看作是在所给的数字中选择数字填入二维数组,当应该要填入的数字没有的时候,就输出“NO”。所以可以选择哈希优化。

AC代码:

#include <iostream>
#include <cstring>
#include <map>
#include <algorithm>
typedef long long ll;
const int M = 505;

using namespace std;

ll a[250005];
ll f[M][M];

void solve() {
	map<int, int>m;
	ll n, c, d;
	cin >> n >> c >> d;
	for (int i = 1; i <= n * n; i++) {
		cin >> a[i];
		m[a[i]]++;
	}
	sort(a + 1, a + n * n + 1);
	f[1][1] = a[1];
	for (int i = 2; i <= n; i++) {
		f[i][1] = f[i - 1][1] + c;
		if (m[f[i][1]] == 0) {
			cout << "NO" << '\n';
			return ;
		} 
        else
			m[f[i][1]]--;
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 2; j <= n; j++) {
			f[i][j] = f[i][j - 1] + d;
			if (m[f[i][j]] == 0) {
				cout << "NO" << '\n';
				return ;
			}
            else
				m[f[i][j]]--;
		}
	}
	cout << "YES" << '\n';
}

int main() {
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t;
	cin >> t;
	while (t--)
		solve();
	return 0;
}

Cf Round 937(Div.4)-D. Product of Binary Decimals

 题意:

        判断一个数组是否能被由0,1组成的数字整除。

        看 t 的范围,很小,直接打表。

打表:

#include <bits/stdc++.h>
typedef long long ll;

using namespace std;

void solve() {
	int n;
	bool f = 0;
	cin >> n;
	int bin = n;
	while (bin) {        //判断这个数是否只含有01数字
		int jud = bin % 10;
		if (jud != 0 && jud != 1) {
			f =  1;
			break;
		}
		bin /= 10;
	}
	if (f == 1) {
		if (n == 121||n == 1210||n == 12100||n == 1331||n == 13310
		||n == 14641||n == 10201||n == 12221||n == 11121||n == 12111
		||n == 11211||n == 1221||n == 12210||n == 13431||n == 12321)
			f = 0;
	}
	if (f == 1) {
		cout << "NO" << endl;
	} else
		cout << "YES" << endl;
}

int main() {
	int t;
	cin >> t;
	while (t--)
		solve();
	return 0;
}

(谁说打表不算好方法)

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值