Codeforces Round #653 (Div. 3) 题解

A. Required Remainder

You are given three integers x,y and n. Your task is to find the maximum integer kk such that 0kn that mod x=y, where modmod is modulo operation. Many programming languages use percent operator % to implement it.

In other words, with given x,yand n you need to find the maximum possible integer from 0 to n that has the remainder yy modulo xx.

You have to answer tt independent test cases. It is guaranteed that such kk exists for each test case.

Input

The first line of the input contains one integer tt (1t510^4) — the number of test cases. The next tt lines contain test casThe only line of the test case contains three integers x,yx,y and nn (2x109; 0y<x; yn109).

It can be shown that such kk always exists under the given constraints.

Output

For each test case, print the answer — maximum non-negative integer kk such that 0kn0≤k≤n and kmodx=ykmodx=y. It is guaranteed that the answer always exists.

题意:  给定x,y和n,你需要找到从0到n的最大可能整数,它有剩余的y模x。

题解:很水的一道题,很容易得到这样的关系式子,设最大整数是k,则k%x=y,k/x=n/x(整除)

那么k=n/x*x+y;

 1 #include<bits/stdc++.h>
 2 #define  ll long long
 3 using namespace std;
 4 int main(){
 5     ll x,y,k,t,n;
 6     scanf("%lld",&t);
 7     while(t--){
 8         scanf("%lld%lld%lld",&x,&y,&n);
 9         ll res=n-y;
10         ll t=res/x;
11         ll k=t*x+y;
12         printf("%lld\n",k);
13         
14     }
15     return 0;
16 } 
Required Remainder

B. Multiply by 2, divide by 6

You are given an integer n. In one move, you can either multiply n by two or divide n by 6 (if it is divisible by 6 without the remainder).

Your task is to find the minimum number of moves needed to obtain 1 from n or determine if it's impossible to do that.

You have to answer t independent test cases.

Input

The first line of the input contains one integer tt(1t210^4) — the number of test cases. Then tt test cases follow.

The only line of the test case contains one integer nn (1n10^9).

Output

For each test case, print the answer — the minimum number of moves needed to obtain 1 from n if it's possible to do that or -1 if it's impossible to obtain 1 from n.

题意: 给你数字n,可以执行若干次操作,每次操作可以乘以2或整除于6(只有n%6==0是才可整除),问从n到1最少要几次操作,若不能则输出-1

题解: 可以采取倒推法,将题目改成从1到n最少要几次操作,每次乘以6或者除以2(只有可整除2时)。那么n一定是3的倍数,因为从1到n只有两种情况:

1.乘以6

2.乘以6除以2,看做乘以3

于是,我们只要将n整除于6直到n%6!=0,并对剩下的r=n-n/6*6作以下判断:

1.r的因子只有1,3

2.若不满足1,则输出-1

3.若满足1,则答案即为n/6+r/3*2;

 1 #include<bits/stdc++.h>
 2 #define  ll long long
 3 using namespace std;
 4 int main(){
 5     ll t,n;
 6     scanf("%lld",&t);
 7     while(t--){
 8         ll ans=0;
 9         scanf("%lld",&n);
10         while(n%6==0){
11             n/=6;
12             ans++;
13         }
14         while(n%3==0){
15             n/=3;
16             ans+=2;
17         }
18         if(n!=1) ans=-1;
19         printf("%lld\n",ans);
20     }
21     return 0;
22 } 
 Multiply by 2, divide by 6

C. Move Brackets

You are given a bracket sequence ss of length n, where n is even (divisible by two). The string ss consists of n/2 opening brackets '(' and n/2 closing brackets ')'.

In one move, you can choose exactly one bracket and move it to the beginning of the string or to the end of the string (i.e. you choose some index i, remove the i-th character of ss and insert it before or after all remaining characters of s).

Your task is to find the minimum number of moves required to obtain regular bracket sequence from ss. It can be proved that the answer always exists under the given constraints.

Recall what the regular bracket sequence is:

  • "()" is regular bracket sequence;
  • if s is regular bracket sequence then "(" + s + ")" is regular bracket sequence;
  • if s and tt are regular bracket sequences then s + tt is regular bracket sequence.

For example, "()()", "(())()", "(())" and "()" are regular bracket sequences, but ")(", "()(" and ")))" are not.

You have to answer tt independent test cases.

Input

The first line of the input contains one integer t (1t2000) — the number of test cases. Then tt test cases follow.

The first line of the test case contains one integer n (2n50) — the length of s. It is guaranteed that n is even. The second line of the test case containg the string s consisting of n2 opening and n2 closing brackets.

Output

For each test case, print the answer — the minimum number of moves required to obtain regular bracket sequence from ss. It can be proved that the answer always exists under the given constraints.

题意:给你一串字符s,s中只有'('与')',且s的长度为偶数。每次操作,可以将第i个位置的字符移到最前或者最后,问要移动多少次可以使得字符s是bracket sequence.

题解:用栈就可以解决

  先找到第一个'('的位置,期间不断把元素')'push

  然后遇到'('就push;遇到')'且top是'('就pop

  如果最后栈是空的,则s本身就是bracket sequence

  如果栈不是空的,则答案是stack长度的一半(因为')'和'('个数相同,且只要移动其中一个即可)

注意:每次运算的时候记得清空stack,第一发runtime人都傻了

 1 #include<bits/stdc++.h>
 2 #define  ll long long
 3 using namespace std;
 4 const int N=2e5+5;
 5  
 6 int main(){
 7     ll t;
 8     scanf("%lld",&t);
 9     string str;
10     while(t--){
11         ll n;
12         scanf("%lld",&n);
13         stack<char> st;
14         ll ans=0;
15         cin>>str;
16         for(int i=0;i<n;i++){
17             if(st.empty()) st.push(str[i]);
18             else if(str[i]==')' && st.top()=='(')
19                 st.pop();
20             else
21                 st.push(str[i]);
22         }
23         ans=st.size()/2;
24         printf("%lld\n",ans);
25     }
26     return 0;    
27 }
Move Brackets

 

剩下题解慢慢来,先挂着(),反正没人看

posted @ 2020-07-03 16:02  Sothoth  阅读(271)  评论(0)    收藏  举报