一:
Luba is surfing the Internet. She currently has n opened tabs in her browser, indexed from 1 to n from left to right. The mouse cursor is currently located at the pos-th tab. Luba needs to use the tabs with indices from l to r (inclusive) for her studies, and she wants to close all the tabs that don't belong to this segment as fast as possible.
Each second Luba can either try moving the cursor to the left or to the right (if the cursor is currently at the tab i, then she can move it to the tab max(i - 1, a) or to the tab min(i + 1, b)) or try closing all the tabs to the left or to the right of the cursor (if the cursor is currently at the tab i, she can close all the tabs with indices from segment [a, i - 1] or from segment [i + 1, b]). In the aforementioned expressions a and b denote the minimum and maximum index of an unclosed tab, respectively. For example, if there were 7 tabs initially and tabs 1, 2 and 7 are closed, then a = 3, b = 6.
What is the minimum number of seconds Luba has to spend in order to leave only the tabs with initial indices from l to r inclusive opened?
Input
The only line of input contains four integer numbers n, pos, l, r (1 ≤ n ≤ 100, 1 ≤ pos ≤ n, 1 ≤ l ≤ r ≤ n) — the number of the tabs, the cursor position and the segment which Luba needs to leave opened.
Output
Print one integer equal to the minimum number of seconds required to close all the tabs outside the segment [l, r].
Examples
Input
6 3 2 4
Output
5
Input
6 3 1 3
Output
1
Input
5 2 1 5
Output
0
Note
In the first test Luba can do the following operations: shift the mouse cursor to the tab 2, close all the tabs to the left of it, shift the mouse cursor to the tab 3, then to the tab 4, and then close all the tabs to the right of it.
In the second test she only needs to close all the tabs to the right of the current position of the cursor.
In the third test Luba doesn't need to do anything.
//水题不解释
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n,p,l,r;
cin>>n>>p>>l>>r;
if(l==1&&r==n)
printf("0\n");
else
{
if(l==1&&r!=n)printf("%d\n",1+abs(p-r));
if(l!=1&&r==n)printf("%d\n",1+abs(p-l));
int ans=min(abs(p-l),abs(p-r));
if(l!=1&&r!=n) printf("%d\n",ans+2+r-l);
}
return 0;
}
二:
You're given Q queries of the form (L, R).
For each query you have to find the number of such x that L ≤ x ≤ R and there exist integer numbers a > 0, p > 1 such that x = ap.
Input
The first line contains the number of queries Q (1 ≤ Q ≤ 105).
The next Q lines contains two integers L, R each (1 ≤ L ≤ R ≤ 1018).
Output
Output Q lines — the answers to the queries.
Example
Input
6 1 4 9 9 5 7 12 29 137 591 1 1000000
Output
2 1 0 3 17 1111
Note
In query one the suitable numbers are 1 and 4.
//二分+预处理,当指数为2时,底数最多1e9,当>2时,底数1e6,但是等于2和大于2不能一起算,等于2要特别算,在算大于2时要减去重复的(容斥原理)。貌似可以完全用容斥原理做,代码很短,但是没看懂orz。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<vector>
#include<map>
using namespace std;
typedef long long ll;
vector<ll> vec;
map<ll,int>mp;
bool square(ll x)
{
ll t=sqrt((double)x);
if(t*t==x||(t+1)*(t+1)==x||(t-1)*(t-1)==x)return true;
return false;
}
void init()
{
vec.push_back(1);
for(ll i=2;i<=1e6;i++)
{
for(ll t=i*i*i;t<=1e18;t*=i)
{
if(!mp[t]&&!square(t))mp[t]=1,vec.push_back(t);
if(1ll*1e18/i<t)break;
}
}
sort(vec.begin(),vec.end());
}
ll f(ll x)
{
ll l=1,r=1e9;
ll ans=0;
while(l<=r)
{
ll mid=(l+r)/2;
if(mid*mid<=x)ans=mid,l=mid+1;
else r=mid-1;
}
return ans;
}
int main()
{
init();
ll q,l,r;
cin>>q;
while(q--)
{
scanf("%lld%lld",&l,&r);
ll ans=0;
ans+=f(r)-f(l-1);
ans+=upper_bound(vec.begin(),vec.end(),r)-upper_bound(vec.begin(),vec.end(),l-1);
if(l==1)ans--;
printf("%lld\n",ans);
}
return 0;
}
三:
You are given an array of positive integers. While there are at least two equal elements, we will perform the following operation. We choose the smallest value xxthat occurs in the array 22 or more times. Take the first two occurrences of xx in this array (the two leftmost occurrences). Remove the left of these two occurrences, and the right one is replaced by the sum of this two values (that is, 2⋅x2⋅x).
Determine how the array will look after described operations are performed.
For example, consider the given array looks like [3,4,1,2,2,1,1][3,4,1,2,2,1,1]. It will be changed in the following way: [3,4,1,2,2,1,1] → [3,4,2,2,2,1] → [3,4,4,2,1] → [3,8,2,1][3,4,1,2,2,1,1] → [3,4,2,2,2,1] → [3,4,4,2,1] → [3,8,2,1].
If the given array is look like [1,1,3,1,1][1,1,3,1,1] it will be changed in the following way: [1,1,3,1,1] → [2,3,1,1] → [2,3,2] → [3,4][1,1,3,1,1] → [2,3,1,1] → [2,3,2] → [3,4].
Input
The first line contains a single integer nn (2≤n≤1500002≤n≤150000) — the number of elements in the array.
The second line contains a sequence from nn elements a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the elements of the array.
Output
In the first line print an integer kk — the number of elements in the array after all the performed operations. In the second line print kk integers — the elements of the array after all the performed operations.
Examples
Input
7 3 4 1 2 2 1 1
Output
4 3 8 2 1
Input
5 1 1 3 1 1
Output
2 3 4
Input
5 10 40 20 50 30
Output
5 10 40 20 50 30
Note
The first two examples were considered in the statement.
In the third example all integers in the given array are distinct, so it will not change.
//优先队列or map,map的很巧妙!优先队列有点难懂...
stl还要好好加强啊
#include<cstdio>
#include<iostream>
#include<map>
#include<set>
#include<algorithm>
#include<utility>
using namespace std;
typedef long long ll;
map<ll,set<int> >mp;
ll a[150005];
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
mp[a[i]].insert(i);
}
int all=n;
for(map<ll,set<int> >::iterator i=mp.begin();i!=mp.end();i++)
{
while(i->second.size()>1)
{
all--;
set<int>::iterator op1=i->second.begin(),op2;
op2=op1;
op2++;
a[*op1]=-1;
a[*op2]*=2;
mp[a[*op2]].insert(*op2);
i->second.erase(op1);
i->second.erase(op2);
}
}
cout<<all<<endl;
for(int i=1;i<=n;i++)
if(~a[i])printf("%lld ",a[i]);
return 0;
}
四:
String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001".
You are given a correct string s.
You can perform two different operations on this string:
- swap any pair of adjacent characters (for example, "101" "110");
- replace "11" with "1" (for example, "110" "10").
Let val(s) be such a number that s is its binary representation.
Correct string a is less than some other correct string b iff val(a) < val(b).
Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all).
Input
The first line contains integer number n (1 ≤ n ≤ 100) — the length of string s.
The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct.
Output
Print one string — the minimum correct string that you can obtain from the given one.
Examples
Input
4 1001
Output
100
Input
1 1
Output
1
Note
In the first example you can obtain the answer by the following sequence of operations: "1001" "1010" "1100" "100".
In the second example you can't obtain smaller answer no matter what operations you use.
//水题+不解释
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int n;
cin>>n;
int sum=0;
for(int i=0;i<n;i++)
{
char x;
cin>>x;
if(x-'0'==1)sum++;
}
if(sum==0)printf("0\n");
else
{
printf("1");
for(int i=1;i<=n-sum;i++)
printf("0");
}
return 0;
}
五:
A string ss of length nn can be encrypted by the following algorithm:
- iterate over all divisors of nn in decreasing order (i.e. from nn to 11),
- for each divisor dd, reverse the substring s[1…d]s[1…d] (i.e. the substring which starts at position 11 and ends at position dd).
For example, the above algorithm applied to the string ss="codeforces" leads to the following changes: "codeforces" →→ "secrofedoc" →→ "orcesfedoc" →→ "rocesfedoc" →→"rocesfedoc" (obviously, the last reverse operation doesn't change the string because d=1d=1).
You are given the encrypted string tt. Your task is to decrypt this string, i.e., to find a string ss such that the above algorithm results in string tt. It can be proven that this string ss always exists and is unique.
Input
The first line of input consists of a single integer nn (1≤n≤1001≤n≤100) — the length of the string tt. The second line of input consists of the string tt. The length of ttis nn, and it consists only of lowercase Latin letters.
Output
Print a string ss such that the above algorithm results in tt.
Examples
Input
10 rocesfedoc
Output
codeforces
Input
16 plmaetwoxesisiht
Output
thisisexampletwo
Input
1 z
Output
z
Note
The first example is described in the problem statement.
//水题=不解释
#include<cstdio>
#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
char s[105];
int a[50];
int main()
{
int n;
cin>>n>>s+1;
for(int i=1;i<=n;i++)
{
if(n%i==0)
for(int j=1;j<=i/2;j++)
{
char c=s[j];
s[j] =s[i+1-j];
s[i+1-j]=c;
}
}
cout<<s+1<<endl;
return 0;
}
六:
Dumbear likes to play the Chinese Rings (Baguenaudier). It’s a game played with nine rings on a bar. The rules of this game are very simple: At first, the nine rings are all on the bar.
The first ring can be taken off or taken on with one step.
If the first k rings are all off and the (k + 1)th ring is on, then the (k + 2)th ring can be taken off or taken on with one step. (0 ≤ k ≤ 7)
Now consider a game with N (N ≤ 1,000,000,000) rings on a bar, Dumbear wants to make all the rings off the bar with least steps. But Dumbear is very dumb, so he wants you to help him.
Input
Each line of the input file contains a number N indicates the number of the rings on the bar. The last line of the input file contains a number "0".
Output
For each line, output an integer S indicates the least steps. For the integers may be very large, output S mod 200907.
Sample Input
1 4 0
Sample Output
1 10
//矩阵快速幂,专门学了一下,顺便还学了些c++结构体的基础知识(毕竟没有很系统的学过c++嘛)
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<memory.h>
using namespace std;
typedef long long ll;
const int mod=200907;
struct matrix
{
int v[3][3];
matrix(){}
matrix(int x)
{
init();
for(int i=0;i<3;i++)
v[i][i]=x;
}
void init()
{
memset(v,0,sizeof(v));
}
matrix operator *(matrix const &b)const
{
matrix c;
c.init();
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
for(int k=0;k<3;k++)
c.v[i][j]=(c.v[i][j]+(ll)v[i][k]*b.v[k][j])%mod;
return c;
}
matrix operator ^(int b)
{
matrix a=*this,res(1);
while(b)
{
if(b&1)
res=res*a;
a=a*a;
b>>=1;
}
return res;
}
}a,b,tmp;
int main()
{
int n;
while(~scanf("%d",&n)&&n)
{
if(n<3)
{
printf("%d\n",n);
continue;
}
a.init();
a.v[0][0]=a.v[0][2]=a.v[1][0]=a.v[2][2]=1;
a.v[0][1]=2;
tmp=a^(n-2);
printf("%d\n",(tmp.v[0][0]*2+tmp.v[0][1]+tmp.v[0][2])%mod);
}
return 0;
}