题目的大致意思为:已知正整数 l, r, m 求任意一组 a, b, c 使得存在正整数 n 满足 n * a + b - c = m 且a, b, c 均在 [l, r] 这个范围之内,让你构造一个a,b,c使得na+b-c==m。
题解: 这道题它确保了至少有一个a,b,c能够使得na+b-c=m,所以我只要让c=l,或者c=r,然后去遍历一遍l到r之间的值就行了。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
int main()
{
int t;
cin >> t;
while(t--){
LL l, r;
LL m;
LL min1 = INF;
LL min2 = -INF;
LL minl=-1;
LL minr=-1;
cin >> l >> r >> m;
for (int i = (int)l;i<=(int)r;i++){
if(m/i!=0){
int n1 = m - m / i * i;
int n2 = m - (m / i + 1) * i;
if (min1 > n1)
{
min1 = n1;
minl = i;
}
if(min2 < n2){
min2 = n2;
minr = i;
}
}
else {
int n2 = m - (m / i + 1) * i;
if(min2 < n2){
min2 = n2;
minr = i;
}
}
}
if(abs(min2) < min1)
cout << minr << " " << r + min2 << " " << r << endl;
else
cout << minl << " " << l + min1 << " " << l << endl;
}
}
Xenia and Bit Operations
**题目的大致意思就是:**给你2 ^ n个数a[n^2],让你去构建一个二叉树,从底层开始进行或,异或,或,异或的操作,给你m次询问,给你两个数p,b。让你将将a[p]的值改为b,让你去计算修改以后根节点的值。
思路: 每个数即为线段数的节点,每更改一次,其父节点的值也跟着更改,最终输出根节点的值即可。
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6;
int ah[maxn];int court;
struct Tree{
int l,r;int val;int h;
}tree[maxn<<2];
void Pushup(int i){
tree[i].h=tree[i<<1].h*(-1);
if(tree[i].h==-1)tree[i].val=tree[i<<1].val|tree[(i<<1)|1].val;
else tree[i].val=tree[i<<1].val^tree[(i<<1)|1].val;
}
void build(int i,int l,int r){
tree[i].l=l;tree[i].r=r;
if(l==r){
tree[i].h=1;
tree[i].val =ah[l];
return;
}
int mid=(l+r)/2;
build(i<<1,l,mid);
build((i<<1)|1,mid+1,r);
Pushup(i);
}
void update(int i,int k,int v){
if(tree[i].l==k&&tree[i].r==k){
tree[i].val=v;
return;
}
int mid=(tree[i].l+tree[i].r)/2;
if(k<=mid)update(i<<1,k,v);
else update((i<<1)|1,k,v);
Pushup(i);
}
int main(){
int n;int m;int c=1;int b,a;
scanf("%d%d",&n,&m);
court=n;
for(int i=1;i<=n;i++){
c=c*2;
}
for(int i=1;i<=c;i++){
scanf("%d",&ah[i]);
}
build(1,1,c);
for(int i=1;i<=m;i++){
scanf("%d%d",&a,&b);
update(1,a,b);
printf("%d\n",tree[1].val);
}
}
Big Vova
Alexander is a well-known programmer. Today he decided to finally go out and play football, but with the first hit he left a dent on the new Rolls-Royce of the wealthy businessman Big Vova. Vladimir has recently opened a store on the popular online marketplace “Zmey-Gorynych”, and offers Alex a job: if he shows his programming skills by solving a task, he’ll work as a cybersecurity specialist. Otherwise, he’ll be delivering some doubtful products for the next two years.
You’re given n positive integers a1,a2,…,an. Using each of them exactly at once, you’re to make such sequence b1,b2,…,bn that sequence c1,c2,…,cn is lexicographically maximal, where ci=GCD(b1,…,bi) - the greatest common divisor of the first i elements of b.
Alexander is really afraid of the conditions of this simple task, so he asks you to solve it.
A sequence a is lexicographically smaller than a sequence b if and only if one of the following holds:
a is a prefix of b, but a≠b;
in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤10^3). Description of the test cases follows.
The first line of each test case contains a single integer n (1≤n≤10^3) — the length of the sequence a.
The second line of each test case contains n integers a1,…,an (1≤ai≤10^3) — the sequence a.
It is guaranteed that the sum of n over all test cases does not exceed 10^3.
Output
For each test case output the answer in a single line — the desired sequence b. If there are multiple answers, print any.
Example
input
7
2
2 5
4
1 8 2 3
3
3 8 9
5
64 25 75 100 50
1
42
6
96 128 88 80 52 7
5
2 4 8 16 17
output
5 2
8 2 1 3
9 3 8
100 50 25 75 64
42
128 96 80 88 52 7
17 2 4 8 16
Note
In the first test case of the example, there are only two possible permutations b — [2,5] and [5,2]: for the first one c=[2,1], for the second one c=[5,1].
In the third test case of the example, number 9 should be the first in b, and GCD(9,3)=3, GCD(9,8)=1, so the second number of b should be 3.
In the seventh test case of the example, first four numbers pairwise have a common divisor (a power of two), but none of them can be the first in the optimal permutation b.
这道题题目意思有点难理解,读了半天,大致意思是给你有n个数序列a,让你去构造一个序列b,使得序列c的最大,c的第i个元素等于gcd(b1,b2,……,bi)。
序列大小判断跟字符串的大小判断差不多,这里就不说了。
我的思路是b的第一个元素一定是a中最大的元素,b的第一个元素确认,GCD=b1,然后去遍历a数组去找与GCD的最小公因数最大,最大的就是b2,此时GCD=gcd(b1,b2),这样下去,如果GCD=1,那么说明剩下的数这么输出都没事,就去遍历一遍将为加入的数输出就可以了。
代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=1e3+10;
int a[maxn];
int b[maxn];
bool com(int a,int b){
return a>b;
}
int gcd(int a,int b){
return b==0?a:gcd(b,a%b);
}
int main(){
int t;
cin>>t;
while(t--){
memset(b,0,sizeof b);
int n;
cin>>n;
int cnt=n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
sort(a+1,a+1+n,com);
b[1]=1;
cout<<a[1]<<" ";
int max1=1;
int gcd1=a[1];
int i1=1;
cnt=cnt-1;
for(int i=1;i<n;i++){
max1=1;
for(int j=1;j<=n;j++){
if(gcd(gcd1,a[j])>max1&&!b[j]){
max1=gcd(gcd1,a[j]);
i1=j;
}
}
b[i1]=1;
gcd1=max1;
if(max1==1){
for(int j=1;j<=n;j++){
if(!b[j]){
printf("%d",a[j]);
if(cnt==1)cout<<endl;
else{cnt--;cout<<" ";}
}
}
break;
}
else {printf("%d",a[i1]);
if(cnt==1)cout<<endl;
else {cnt--;cout<<" ";}
}
}
}
}