codeforces1997(div.3)E F
E.Novice’s Mistake
找满足条件的所有 (a,b)(a,b)(a,b) 的值,约束条件有:
1<=a<=100001<=a<=100001<=a<=10000 ,
1<=b<=min(10000,n∗a)1<=b<=min(10000,n*a)1<=b<=min(10000,n∗a)
字符串长度b<=ls(字符串长度)字符串长度b<=ls(字符串长度)字符串长度b<=ls(字符串长度) ,n∗a−b=100∗104−1n*a-b=100*10^4-1n∗a−b=100∗104−1 b的最大长度为ls−6<=bls-6<=bls−6<=b
#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]==0x\%a[i]==0x%a[i]==0&&vis[x/a[i]]==1vis[x/a[i]]==1vis[x/a[i]]==1 就需要分割。
不需要分割就扩展,当前元素*当前段已有因子,生成新的可能因子,并标记可能因子。
#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();}
}
