codeforces1997(div.3)E F

E.Novice's Mistake

找满足条件的所有 ( a , b ) (a,b) (a,b) 的值,约束条件有:

1 < = a < = 10000 1<=a<=10000 1<=a<=10000 ,

1 < = b < = m i n ( 10000 , n ∗ a ) 1<=b<=min(10000,n*a) 1<=b<=min(10000,n∗a)

字符串长度 b < = l s ( 字符串长度 ) 字符串长度b<=ls(字符串长度) 字符串长度b<=ls(字符串长度) , n ∗ a − b = 100 ∗ 1 0 4 − 1 n*a-b=100*10^4-1 n∗a−b=100∗104−1 b的最大长度为 l s − 6 < = b ls-6<=b ls−6<=b

c++ 复制代码
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
void solve() {
	int n;
	cin >> n;
	string s;
	string sn = to_string(n);
	int ln = sn.size();
	vector<pair<int, int>>p;
	for (int a = 1; a <= 10000; a++) {
		int ls = ln*a;
		//计算b的有效范围
		int minb = max(1, ls - 6);
		int maxb = min(min(10000, a*n), ls - 1);
		if (minb > maxb) continue;

		for (int b = minb; b <= maxb; b++) {
			int k = ls - b;
			int pos = 0;
			string x = "";
			for (int i = 0; i < k; i++) {//前k个字符
				x += sn[pos];
				pos = (pos + 1) % ln;
			}
			int x2 = a*n - b;
			if (x2 == stoi(x)) {
				p.push_back({a, b});
			}
		}
	}
	cout << p.size() << endl;
	for (auto i : p) {
		cout << i.first << " " << i.second << endl;
	}
}
int main() {
	int T;
	cin >> T;
	while (T--) {
		solve();
	}
}

F. Valuable Cards

只有 x 的因子相乘才可以得到 x 。不是 x 的因子直接跳过

用数组来标记当前段中已存在的因子,如果当前元素满足 x % a i = = 0 x\%ai==0 x%ai==0&& v i s x / a \[ i ] = = 1 visx/a\[i]==1 visx/a\[i]==1 就需要分割。

不需要分割就扩展,当前元素*当前段已有因子,生成新的可能因子,并标记可能因子。

c++ 复制代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve() {
	int n, x;
	cin >> n >> x;
	vector<int>a(n);
	vector<int>vis(x + 1, 0);
	for (int i = 0; i < n; i++) cin >> a[i];
	a.push_back(x);
	int ans = 0;
	vector<int>t;//存当前段中x的因子
	for (int i = 0; i <= n; i++) {
		if ((x % a[i] == 0 && vis[x / a[i]])||a[i]==x) {
			ans++;
			vis.assign(x + 1, 0);
			t.clear();

			if ( a[i] <= x &&!vis[a[i]] && x % a[i] == 0) { //将当前数加入新段
				t.push_back(a[i]);
				vis[a[i]] = 1;
			}
		} else {
			// 用当前元素与t中已有乘积相乘,生成新的可能乘积
			vector<int>temp;
			for (auto j : t) {
				int xin = j*a[i];
				if (xin <= x && !vis[xin] && x % xin == 0) {
					vis[xin] = 1;
					temp.push_back(xin);
				}
			}
			t.insert(t.end(),temp.begin(),temp.end());
			if ( a[i] <= x &&!vis[a[i]] && x % a[i] == 0) {
				vis[a[i]] = 1;
				t.push_back(a[i]);
			}
		}
	}
	cout<<ans<<endl;
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int T;
	cin >> T;
	while (T--) {
		solve();
	}
}
相关推荐
8Qi82 小时前
回文子串(Palindromic Substrings)—— 题解
算法·leetcode·职场和发展·动态规划
小宋加油啊7 小时前
机械臂抓取物体 PVN3D算法调研学习
学习·算法·3d
lqqjuly7 小时前
前沿算法深度解析(一)
算法
小欣加油7 小时前
leetcode1926 迷宫中离入口最近的出口
数据结构·c++·算法·leetcode·职场和发展
happymaker06269 小时前
LeetCodeHot100——42.接雨水
算法
阿正的梦工坊10 小时前
【Rust】07-错误处理:Option、Result 与 ? 运算符
开发语言·算法·rust
八解毒剂11 小时前
数据结构-平衡二叉树——对二叉搜索树的优化
数据结构·c++·算法
运行时记录12 小时前
别再手动写提示词了 — SkillOpt 让技能文档自己进化
算法
啦啦啦啦啦zzzz12 小时前
算法总结(二分查找、双指针)
c++·算法