202312-1仓库规划
202312-2 因子化简
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
#define maxn 100000//因为一个数的因数最大是它的开方
#define ll long long
int times, q;
ll n;
vector<int> prime;
vector<bool>isprime(maxn+1, true);
void oval() {
int count = 0;
prime.push_back(0);
isprime[1] = false;
for (int i = 2; i <=maxn; i++) {
if (isprime[i]){
prime.push_back(i);
count++;
}
for (int j = 1; j<=count&&prime[j]<= maxn/ i; j++) {
isprime[i * prime[j]] = false;
if (i % prime[j] == 0) {
break;
}
}
}
}
ll query(){
ll ans = 1;//注意ans可能就是n 会发生溢出
for (int i = 1; i <prime.size(); i++) {
int pow = 0;
if (n <= 0) {
break;
}
while(n % prime[i] == 0) {
n = n / prime[i];
pow++;
}
if (pow < q) {
continue;
}
ans =ans*std::pow(prime[i],pow);
}
return ans;
}
int main() {
cin >> times;
oval();
for (int i = 0; i < times; i++) {
cin >> n >> q;
cout<<query()<<endl;
}
}
202312-3 树上搜索
参考:树上搜索题解----csp202312-3-CSDN博客
#include<iostream>
#include<vector>
#define ll long long
using namespace std;
struct node {
int value;
int parent;
vector<int>children;//多叉树
}a[2002],b[2002];
//b 是输入的树形 在a上可以做查询过程的删除等操作
int n, m;
ll w[2002];
//记录每个节点的后代节点权重和
ll sum(int root){
ll ans=a[root].value;
for (int c : a[root].children) {
ans += sum(c);
}
return ans;
}
void wsub(ll total, int root) {
w[root] = abs(total - 2 * sum(root));
for (int c : a[root].children) {
wsub(total, c);
}
return;
}
int findmin(int root){
int min =root ;
for (int c : a[root].children) {
int temp = findmin(c);
if (w[temp] < w[min]||(w[temp]==w[min])&&temp<min) {
min = temp;
}
}
return min;
}
bool intree(int q,int min) {
if (q == min) {
return true;
}
for (int c : a[min].children) {
if (intree(q, c)) {
return true;
}
}
return false;
}
int main(){
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> b[i].value;
}
for (int i = 2; i <= n; i++) {
cin >> b[i].parent;
b[b[i].parent].children.push_back(i);
}
int q;
for(int k=0;k<m;k++){//每一次查询
int root = 1;
cin >> q;
for (int i = 1; i <= n; i++) {
a[i].value = b[i].value;
a[i].parent = b[i].parent;
for (int j = 0; j < b[i].children.size(); j++) {
a[i].children = b[i].children;
}
}
//开始一次查询
while (a[root].children.size()!=0) {
//计算所有点的wsigma
//w西格玛 = totalweight - 2 * 后代节点的权重和
ll total=sum(root);
wsub(total,root);
//找到有最小wΣ的节点
int min = findmin(root);
//查询是否在以min为根的子树中
cout << min << " ";
if (intree(q, min)) {
//如果在 root=min
root = min;
}
else {
//如果不在 去除以min为根的子树
int temp = a[min].parent;
for (int i = 0; i < a[temp].children.size(); i++) {
if (a[temp].children[i] == min) {
a[temp].children.erase(a[temp].children.begin() + i);
break;
}
}
}
}
cout << endl;
}
return 0;
}