A
题面
You have an initially empty cauldron, and you want to brew a potion in it. The potion consists of two ingredients: magic essence and water. The potion you want to brew should contain exactly k % k\ \% k %magic essence and ( 100 − k ) % (100 - k)\ \% (100−k) % water.
In one step, you can pour either one liter of magic essence or one liter of water into the cauldron. What is the minimum number of steps to brew a potion? You don’t care about the total volume of the potion, only about the ratio between magic essence and water in it.
A small reminder: if you pour e e e liters of essence and w w w liters of water ( e + w > 0 e + w > 0 e+w>0 ) into the cauldron, then it contains e e + w ⋅ 100 % \frac{e}{e + w} \cdot 100\ \% e+we⋅100 % (without rounding) magic essence and w e + w ⋅ 100 % \frac{w}{e + w} \cdot 100\ \% e+ww⋅100 % water
翻译
t
t
t
(
1
≤
t
≤
100
)
(1\le t\le100)
(1≤t≤100)组数据,每组数据输入一个数
k
k
k
(
1
≤
k
≤
100
)
(1\le k\le 100)
(1≤k≤100)
输出至少要多少份的液体(两种相加)使浓度到
k
%
k\%
k%
解析
可以发现无论如何,
a
n
s
w
e
r
≤
100
answer\le100
answer≤100
而当它小于
100
100
100时,一定是
k
k
k和
100
−
k
100-k
100−k有公因数时;
所以
a
n
s
w
e
r
=
100
g
c
d
(
k
,
100
−
k
)
answer=\frac{100}{gcd(k,100-k)}
answer=gcd(k,100−k)100
代码
#include<bits/stdc++.h>
using namespace std;
int T,N;
int gcd(int a,int b){return (b>a?b^=a^=b^=a:0),b?gcd(b,a%b):a;}
int main(){
scanf("%d",&T);
while(T--){scanf("%d",&N);printf("%d\n",100/gcd(N,100-N));}
return 0;
}
B
题面
You are given a permutation aa consisting of nn numbers 1 1 1 , 2 2 2 , …, n n n (a permutation is an array in which each element from 1 1 1 to n n n occurs exactly once).
You can perform the following operation: choose some subarray (contiguous subsegment) of aa and rearrange the elements in it in any way you want. But this operation cannot be applied to the whole array.
For example, if
a
=
[
2
,
1
,
4
,
5
,
3
]
a = [2, 1, 4, 5, 3]
a=[2,1,4,5,3] and we want to apply the operation to the subarray
a
[
2
,
4
]
a[2, 4]
a[2,4] (the subarray containing all elements from the
2
n
d
2^{nd}
2nd to the
4
t
h
4^{th}
4th), then after the operation, the array can become
a
=
[
2
,
5
,
1
,
4
,
3
]
a = [2, 5, 1, 4, 3]
a=[2,5,1,4,3] or, for example,
a
=
[
2
,
1
,
5
,
4
,
3
]
a = [2, 1, 5, 4, 3]
a=[2,1,5,4,3]
task is to calculate the minimum number of operations described above to sort the permutation aa in ascending order.
翻译
T T T ( 1 ≤ T ≤ 2000 ) (1\le T\le 2000) (1≤T≤2000)组数据,给定一个长度为 N N N ( 3 ≤ N ≤ 50 ) (3\le N \le 50) (3≤N≤50)的序列,有 1 − N 1-N 1−N, N N N个数字,每次可以把一个小于 N N N的连续区间中进行排序,问要几次后才能使序列有序
解析
可以发现,每次排序后最多可以把除去首尾的任意一个外的其他排序,就可以分类讨论:
条件 | 结论 |
---|---|
a i = i ( 1 ≤ i ≤ N ) a_i=i(1\le i\le N) ai=i(1≤i≤N) | 0 |
a 1 = 1 a_1=1 a1=1 | 1 |
a N = N a_N=N aN=N | 1 |
a 1 = N , a N = 1 a_1=N,a_N=1 a1=N,aN=1 | 3 |
others | 2 |
代码
#include <bits/stdc++.h>
using namespace std;
int a[139];
int N,T;
void print(int x){printf("%d\n",x);}
int main() {
scanf("%d",&T);
while(T--){
bool f=1;scanf("%d",&N);
for(int i=1;i<=N;i++) {scanf("%d",&a[i]);if(a[i]!=i){f = 0;}}
if(f){print(0);} else if(a[1]==1||a[N]==N){print(1);} else if(a[1]==N&&a[N]==1){print(3);} else{print(2);}
}
return 0;
}