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\%a[i]==0 x%a[i]==0&& v i s [ x / a [ i ] ] = = 1 vis[x/a[i]]==1 vis[x/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();
}
}