A. Sasha and His Trip
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Sasha is a very happy guy, that's why he is always on the move. There are nn cities in the country where Sasha lives. They are all located on one straight line, and for convenience, they are numbered from 11 to nn in increasing order. The distance between any two adjacent cities is equal to 11 kilometer. Since all roads in the country are directed, it's possible to reach the city yy from the city xx only if x<yx<y.
Once Sasha decided to go on a trip around the country and to visit all nn cities. He will move with the help of his car, Cheetah-2677. The tank capacity of this model is vv liters, and it spends exactly 11 liter of fuel for 11 kilometer of the way. At the beginning of the journey, the tank is empty. Sasha is located in the city with the number 11 and wants to get to the city with the number nn. There is a gas station in each city. In the ii-th city, the price of 11 liter of fuel is ii dollars. It is obvious that at any moment of time, the tank can contain at most vv liters of fuel.
Sasha doesn't like to waste money, that's why he wants to know what is the minimum amount of money is needed to finish the trip if he can buy fuel in any city he wants. Help him to figure it out!
Input
The first line contains two integers nn and vv (2≤n≤1002≤n≤100, 1≤v≤1001≤v≤100) — the number of cities in the country and the capacity of the tank.
Output
Print one integer — the minimum amount of money that is needed to finish the trip.
Examples
input
Copy
4 2
output
Copy
4
input
Copy
7 6
output
Copy
6
Note
In the first example, Sasha can buy 22 liters for 22 dollars (11 dollar per liter) in the first city, drive to the second city, spend 11 liter of fuel on it, then buy 11 liter for 22 dollars in the second city and then drive to the 44-th city. Therefore, the answer is 1+1+2=41+1+2=4.
In the second example, the capacity of the tank allows to fill the tank completely in the first city, and drive to the last city without stops in other cities.
题意:有n个城市,每个城市距离1km,从一个城市到另一个城市需要花费1容量的油,没个城市都有加油站问到n的最少花费。
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,v;
scanf("%d%d",&n,&v);
if(v>=n-1)
printf("%d\n",n-1);
else{
int ans=v,i=2,l=n-1;
while(l>0){
ans+=i;
l--;
i++;
if(v>=l)
break;
}
printf("%d\n",ans);
}
}
B. Sasha and Magnetic Machines
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
One day Sasha visited the farmer 2D and his famous magnetic farm. On this farm, the crop grows due to the influence of a special magnetic field. Maintaining of the magnetic field is provided by nn machines, and the power of the ii-th machine is aiai.
This year 2D decided to cultivate a new culture, but what exactly he didn't say. For the successful growth of the new culture, it is necessary to slightly change the powers of the machines. 2D can at most once choose an arbitrary integer xx, then choose one machine and reduce the power of its machine by xx times, and at the same time increase the power of one another machine by xx times (powers of all the machines must stay positive integers). Note that he may not do that if he wants. More formally, 2D can choose two such indices ii and jj, and one integer xx such that xx is a divisor of aiai, and change powers as following: ai=aixai=aix, aj=aj⋅xaj=aj⋅x
Sasha is very curious, that's why he wants to calculate the minimum total power the farmer can reach. There are too many machines, and Sasha can't cope with computations, help him!
Input
The first line contains one integer nn (2≤n≤5⋅1042≤n≤5⋅104) — the number of machines.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1001≤ai≤100) — the powers of the machines.
Output
Print one integer — minimum total power.
Examples
input
Copy
5 1 2 3 4 5
output
Copy
14
input
Copy
4 4 2 4 4
output
Copy
14
input
Copy
5 2 4 2 3 7
output
Copy
18
Note
In the first example, the farmer can reduce the power of the 44-th machine by 22 times, and increase the power of the 11-st machine by 22times, then the powers will be: [2,2,3,2,5][2,2,3,2,5].
In the second example, the farmer can reduce the power of the 33-rd machine by 22 times, and increase the power of the 22-nd machine by 22times. At the same time, the farmer can leave is be as it is and the total power won't change.
In the third example, it is optimal to leave it be as it is.
题意:找到一个数使得其中两个数变成a[i]/=x,a[j]*=x,使得总和最少
#include<bits/stdc++.h>
using namespace std;
int a[101];
int main(){
int n,sum=0,an=0,x;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&x);
a[x]=1;
sum+=x;
}
for(int x=2;x<=100;x++){
for(int i=1;i<=100;i++){
for(int j=1;j<=100;j++){
if(a[j]&&a[i]&&i-j&&i%x==0){
an=max(an,i-i/x-j*x+j);
}
}
}
}
printf("%d\n",sum-an);
}
C. Sasha and a Bit of Relax
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array aa with nn integers. You need to count the number of funny pairs (l,r)(l,r) (l≤r)(l≤r). To check if a pair (l,r)(l,r) is a funny pair, take mid=l+r−12mid=l+r−12, then if r−l+1r−l+1 is an even number and al⊕al+1⊕…⊕amid=amid+1⊕amid+2⊕…⊕aral⊕al+1⊕…⊕amid=amid+1⊕amid+2⊕…⊕ar, then the pair is funny. In other words, ⊕⊕ of elements of the left half of the subarray from ll to rr should be equal to ⊕⊕ of elements of the right half. Note that ⊕⊕ denotes the bitwise XOR operation.
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer nn (2≤n≤3⋅1052≤n≤3⋅105) — the size of the array.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai<2200≤ai<220) — array itself.
Output
Print one integer — the number of funny pairs. You should consider only pairs where r−l+1r−l+1 is even number.
Examples
input
Copy
5 1 2 3 4 5
output
Copy
1
input
Copy
6 3 2 2 3 7 6
output
Copy
3
input
Copy
3 42 4 2
output
Copy
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2,5)(2,5), as 2⊕3=4⊕5=12⊕3=4⊕5=1.
In the second example, funny pairs are (2,3)(2,3), (1,4)(1,4), and (3,6)(3,6).
In the third example, there are no funny pairs.
求前缀异或和,则题目可以转换成求(r-l+1)是偶数并且f[r]^f[mod]=f[mod]^f[l-1]的数目,又因为f[r]^f[mod]=f[mod]^f[l-1],则可以求f[r]^f[l]=0&&(r-l+1)是偶数的数目。。。
#include<bits/stdc++.h>
using namespace std;
long long f[300005];
typedef pair<int,int>p;
map<p,int>m;
int main(){
int n;
long long ans=0;
m[p(0,0)]=1;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&f[i]);
if(i!=1)
f[i]^=f[i-1];
ans+=m[p(f[i],i&1)];
m[p(f[i],i%2)]++;
}
printf("%lld\n",ans);
}
D. Sasha and One More Name
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Reading books is one of Sasha's passions. Once while he was reading one book, he became acquainted with an unusual character. The character told about himself like that: "Many are my names in many countries. Mithrandir among the Elves, Tharkûn to the Dwarves, Olórin I was in my youth in the West that is forgotten, in the South Incánus, in the North Gandalf; to the East I go not."
And at that moment Sasha thought, how would that character be called in the East? In the East all names are palindromes. A string is a palindrome if it reads the same backward as forward. For example, such strings as "kazak", "oo" and "r" are palindromes, but strings "abb" and "ij" are not.
Sasha believed that the hero would be named after one of the gods of the East. As long as there couldn't be two equal names, so in the East people did the following: they wrote the original name as a string on a piece of paper, then cut the paper minimum number of times kk, so they got k+1k+1 pieces of paper with substrings of the initial string, and then unite those pieces together to get a new string. Pieces couldn't be turned over, they could be shuffled.
In this way, it's possible to achive a string abcdefg from the string f|de|abc|g using 33 cuts (by swapping papers with substrings f and abc). The string cbadefg can't be received using the same cuts.
More formally, Sasha wants for the given palindrome ss find such minimum kk, that you can cut this string into k+1k+1 parts, and then unite them in such a way that the final string will be a palindrome and it won't be equal to the initial string ss. It there is no answer, then print "Impossible" (without quotes).
Input
The first line contains one string ss (1≤|s|≤50001≤|s|≤5000) — the initial name, which consists only of lowercase Latin letters. It is guaranteed that ss is a palindrome.
Output
Print one integer kk — the minimum number of cuts needed to get a new name, or "Impossible" (without quotes).
Examples
input
Copy
nolon
output
Copy
2
input
Copy
otto
output
Copy
1
input
Copy
qqqq
output
Copy
Impossible
input
Copy
kinnikkinnik
output
Copy
1
Note
In the first example, you can cut the string in those positions: no|l|on, and then unite them as follows on|l|no. It can be shown that there is no solution with one cut.
In the second example, you can cut the string right in the middle, and swap peaces, so you get toot.
In the third example, you can't make a string, that won't be equal to the initial one.
In the fourth example, you can cut the suffix nik and add it to the beginning, so you get nikkinnikkin.
如果在一边区间字符都相等则不可能,反之暴力模拟一次后左右区间不是回文串则要切两次,反之切一次
#include<bits/stdc++.h>
using namespace std;
int main(){
string s,t;
cin>>s;
int flag=1;
for(int i=0;i<s.length()/2;i++)
if(s[i]!=s[0]){
flag=0;
break;
}
if(flag){
printf("Impossible\n");
return 0;
}
else{
for(int i=1;i<s.length();i++){
t=s.substr(i)+s.substr(0,i);
if(s!=t){
flag=1;
for(int i=0;i<t.length()/2;i++){
if(t[i]!=t[t.size()-i-1]){
flag=0;
break;
}
}
if(flag==1){
printf("1\n");
return 0;
}
}
}
printf("2\n");
return 0;
}
}