2020-10-26训练赛错题集

A

Problem Statement

Given are strings S and T. Consider changing S to T by repeating the operation below. Find the minimum number of operations required to do so.

Operation: Choose one character of S and replace it with a different character.

Constraints

  • S and T have lengths between 11 and 2×10^5 (inclusive).
  • S and T consists of lowercase English letters.
  • S and T have equal lengths.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print the answer.


Sample Input 1 Copy

Copy
cupofcoffee
cupofhottea

Sample Output 1 Copy

Copy
4

We can achieve the objective in four operations, such as the following:

  • First, replace the sixth character c with h.
  • Second, replace the eighth character f with t.
  • Third, replace the ninth character f with t.
  • Fourth, replace the eleventh character e with a.

Sample Input 2 Copy

Copy
abcde
bcdea

Sample Output 2 Copy

Copy
5

Sample Input 3 Copy

Copy
apple
apple

Sample Output 3 Copy

Copy
0

No operations may be needed to achieve the objective.

 

 

即看有几个不一样的字符。

#include<bits/stdc++.h>
using namespace std;

int cnt;

int main()
{
  string a,b;
  int cnt=0;
  cin>>a>>b;
  for(int i=0;i<a.size();i++)
  {
    if(a[i]!=b[i])
    cnt++;
  }
  cout<<cnt;

  return 0;
}

 

 

 

B

Problem Statement

 

We have two desks: A and B. Desk A has a vertical stack of N books on it, and Desk B similarly has M books on it.

It takes us Aminutes to read the i-th book from the top on Desk A (1iN) and Bi minutes to read the i-th book from the top on Desk B (1iM).

Consider the following action:

  • Choose a desk with a book remaining, read the topmost book on that desk, and remove it from the desk.

How many books can we read at most by repeating this action so that it takes us at most K minutes in total? We ignore the time it takes to do anything other than reading.

Constraints

 

  • 1N,M200000   
  • 1K10^9
  • 1Ai,Bi10^9
  • All values in input are integers.

Input

 

Input is given from Standard Input in the following format:

N M K
A1 A2  AN
B1 B2  BM

Output

 

Print an integer representing the maximum number of books that can be read.

Sample Input 1

 

3 4 240
60 90 120
80 150 80 150

Sample Output 1

 

3

In this case, it takes us 6090120 minutes to read the 1-st, 2-nd, 3-rd books from the top on Desk A, and 80, 15080150 minutes to read the 1-st, 2-nd, 3-rd, 4-th books from the top on Desk B, respectively.

We can read three books in 230 minutes, as shown below, and this is the maximum number of books we can read within 240 minutes.

  • Read the topmost book on Desk A in 60 minutes, and remove that book from the desk.
  • Read the topmost book on Desk B in 80 minutes, and remove that book from the desk.
  • Read the topmost book on Desk A in 90 minutes, and remove that book from the desk.

Sample Input 2

 

3 4 730
60 90 120
80 150 80 150

Sample Output 2

 

7

Sample Input 3

 

5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000

Sample Output 3

 

0

Watch out for integer overflows.

 

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N=200010;

ll a[N],b[N];

int n,m;

ll k;

int main()

{

    ios::sync_with_stdio(false);

    cin.tie(0);cout.tie(0);

    int res=0;

    cin>>n>>m>>k;

    for(int i=1;i<=n;i++) 

    {

        cin>>a[i];

        a[i]+=a[i-1];

    }

    for(int i=1;i<=m;i++)

    {

        cin>>b[i];

        b[i]+=b[i-1];

    }

    for(int i=0,j=m;i<=n;i++)//这里i一定要从0开始循环要不然会漏掉不在a数组中选数的情况

    {

        while(j&&a[i]>k-b[j])  j--;

        if(a[i]<=k-b[j])

res=max(res,i+j);

    }

    cout<<res<<endl;

    return 0;

}

 

 

 

 

C

Problem Statement

 

For a positive integer XX, let f(X)f(X) be the number of positive divisors of XX.

Given a positive integer NN, find NK=1K×f(K)∑K=1NK×f(K).

Constraints

 

  • 1N1071≤N≤107
Input

 

Input is given from Standard Input in the following format:

NN
Output

 

Print the value NK=1K×f(K)∑K=1NK×f(K).

Sample Input 1

 

4
Sample Output 1

 

23

We have f(1)=1f(1)=1f(2)=2f(2)=2f(3)=2f(3)=2, and f(4)=3f(4)=3, so the answer is 1×1+2×2+3×2+4×3=231×1+2×2+3×2+4×3=23.

Sample Input 2

 

100
Sample Output 2

 

26879
Sample Input 3

 

10000000
Sample Output 3

 

838627288460105

Watch out for overflows.

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;
int n;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n;
    ll res=0;
    for(int i=1;i<=n;i++)
        for(int j=i;j<=n;j+=i) res+=j;//相当于乘k
    cout<<res<<endl;
    return 0;
}
整理一个找约数个数的函数
int
n; ll d[maxn], minn[maxn], prime[maxn]; bool is[maxn]; int tot; void init() { d[1] = 1; for (int i = 2; i <= n; i++) { if (is[i] == 0) prime[++tot] = i, d[i] = 2, minn[i] = 1; for (re int j = 1; prime[j] * i <= n && j <= tot; j++) { is[i * prime[j]] = 1; if (i % prime[j] == 0) { minn[i * prime[j]] = minn[i] + 1; d[i * prime[j]] = d[i] / (minn[i] + 1) * (minn[prime[j] * i] + 1); break; } minn[i * prime[j]] = 1; d[i * prime[j]] = d[i] * 2; } } } int main() { scanf("%d", &n); init(); ll res = 0; for (int i = 1; i <= n; i++) { res += i * d[i]; } printf("%lld", res); }

 

 

D

Count the pairs of length-NN sequences consisting of integers between 11 and MM (inclusive), A1,A2,,ANA1,A2,⋯,AN and B1,B2,,BNB1,B2,⋯,BN, that satisfy all of the following conditions:

  • AiBiAi≠Bi, for every ii such that 1iN1≤i≤N.
  • AiAjAi≠Aj and BiBjBi≠Bj, for every (i,j)(i,j) such that 1i<jN1≤i<j≤N.

Since the count can be enormous, print it modulo (109+7)(109+7).

Constraints

 

  • 1NM5×1051≤N≤M≤5×105
  • All values in input are integers.
Input

 

Input is given from Standard Input in the following format:

NN MM
Output

 

Print the count modulo (109+7)(109+7).

Sample Input 1

 

2 2
Sample Output 1

 

2

A1=1,A2=2,B1=2,B2=1A1=1,A2=2,B1=2,B2=1 and A1=2,A2=1,B1=1,B2=2A1=2,A2=1,B1=1,B2=2 satisfy the conditions.

Sample Input 2

 

2 3
Sample Output 2

 

18
Sample Input 3

 

141421 356237
Sample Output 3

 

881613484

 

 

 

 

 

E

Problem Statement

There are N piles of stones. The i-th pile has Ai stones.

Aoki and Takahashi are about to use them to play the following game:

  • Starting with Aoki, the two players alternately do the following operation:
    • Operation: Choose one pile of stones, and remove one or more stones from it.
  • When a player is unable to do the operation, he loses, and the other player wins.

When the two players play optimally, there are two possibilities in this game: the player who moves first always wins, or the player who moves second always wins, only depending on the initial number of stones in each pile.

In such a situation, Takahashi, the second player to act, is trying to guarantee his win by moving at least zero and at most (A1−1) stones from the 1-st pile to the 2-nd pile before the game begins.

If this is possible, print the minimum number of stones to move to guarantee his victory; otherwise, print -1 instead.

Constraints

 

  • 2N300
  • 1Ai10^12

Input

 

Input is given from Standard Input in the following format:

N
A1  AN

Output

 

Print the minimum number of stones to move to guarantee Takahashi's win; otherwise, print -1 instead.

Sample Input 1

2
5 3

Sample Output 1

1

Without moving stones, if Aoki first removes 2 stones from the 1-st pile, Takahashi cannot win in any way.

If Takahashi moves 1 stone from the 1-st pile to the 2-nd before the game begins so that both piles have 4 stones, Takahashi can always win by properly choosing his actions.

Sample Input 2

2
3 5

Sample Output 2

-1

It is not allowed to move stones from the 2-nd pile to the 1-st.

Sample Input 3

3
1 1 2

Sample Output 3

-1

It is not allowed to move all stones from the 1-st pile.

Sample Input 4

8
10 9 8 7 6 5 4 3

Sample Output 4

3

Sample Input 5

3
4294967297 8589934593 12884901890

Sample Output 5

1

Watch out for overflows.

 

咕咕咕

 

 

 

 

 

I

Problem Statement

 

Takahashi loves the number 7 and multiples of K.

Where is the first occurrence of a multiple of K in the sequence 7,77,777,7,77,777,…? (Also see Output and Sample Input/Output below.)

If the sequence contains no multiples of K, print -1 instead.

Constraints

 

  • 1K10^6
  • K is an integer.

Input

 

Input is given from Standard Input in the following format:

K

Output

 

Print an integer representing the position of the first occurrence of a multiple of K. (For example, if the first occurrence is the fourth element of the sequence, print 4.)

Sample Input 1

 

101

Sample Output 1

 

4

None of 777, and 777 is a multiple of 101, but 7777 is.

Sample Input 2

 

2

Sample Output 2

 

-1

All elements in the sequence are odd numbers; there are no multiples of 2.

Sample Input 3

 

999983

Sample Output 3

 

999982

#include <cstdio>
 
int k;
bool v[1000010];
 
int main()
{
	scanf("%d",&k);
	int x=7%k;
	for(int i=1;;i++){
		if(v[x])
		{
			printf("-1");
			return 0;
		}
		if(!x)
		{
			printf("%d",i);
			return 0;	
		}
		v[x]=true;
		x=x*10+7;
		x%=k;
	}
}

  

 

Problem Statement

 

An altar enshrines N stones arranged in a row from left to right. The color of the i-th stone from the left (1≤i≤N) is given to you as a character ciR stands for red and W stands for white.

You can do the following two kinds of operations any number of times in any order:

  • Choose two stones (not necessarily adjacent) and swap them.
  • Choose one stone and change its color (from red to white and vice versa).

According to a fortune-teller, a white stone placed to the immediate left of a red stone will bring a disaster. At least how many operations are needed to reach a situation without such a white stone?

Constraints

 

  • 2N200000
  • cici is R or W.

Input

 

Input is given from Standard Input in the following format:

N
c1c2...cN

Output

 

Print an integer representing the minimum number of operations needed.

Sample Input 1

 

4
WWRR

Sample Output 1

 

2

For example, the two operations below will achieve the objective.

  • Swap the 1-st and 3-rd stones from the left, resulting in RWWR.
  • Change the color of the 4-th stone from the left, resulting in RWWW.

Sample Input 2

 

2
RR

Sample Output 2

 

0

It can be the case that no operation is needed.

Sample Input 3

 

8
WRWWRWRR

Sample Output 3

 

3


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
    int n;
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n;
    string a;
    cin>>a;
    ll num1=0,num2=0;
    for(int i=0;i<n;i++){
        if(a[i]=='R') num1++;
    }
    for(int i=num1;i<n;i++){
        if(a[i]=='R') num2++;
    }cout<<num2<<endl;
    return 0;
}

  

K

Problem Statement

 

We have N logs of lengths A1,A2,AN

We can cut these logs at most K times in total. When a log of length L is cut at a point whose distance from an end of the log is t(0<t<L), it becomes two logs of lengths t and Lt

Find the shortest possible length of the longest log after at most K cuts, and print it after rounding up to an integer.

Constraints

 

  • 1N2×10^5
  • 0K10^9
  • 1Ai10^9
  • All values in input are integers.

Input

 

Input is given from Standard Input in the following format:

N K
A1 A2  AN

Output

 

Print an integer representing the answer.

Sample Input 1

2 3
7 9

Sample Output 1

 

4
  • First, we will cut the log of length 7 at a point whose distance from an end of the log is 3.5, resulting in two logs of length 3.5 each.
  • Next, we will cut the log of length 9 at a point whose distance from an end of the log is 3, resulting in two logs of length 3 and 6.
  • Lastly, we will cut the log of length 6 at a point whose distance from an end of the log is 3.3, resulting in two logs of length 3.3 and 2.7.

In this case, the longest length of a log will be 3.5, which is the shortest possible result. After rounding up to an integer, the output should be 4.

Sample Input 2

3 0
3 4 5

Sample Output 2

5

Sample Input 3

10 10
158260522 877914575 602436426 24979445 861648772 623690081 433933447 476190629 262703497 211047202

Sample Output 3

292638192


#include <cstdio>
#define maxn 200010
#define ll long long
 
int n,a[maxn];ll m;
ll check(int x){
    ll re=0;
    for(int i=1;i<=n;i++)re+=a[i]/x+(a[i]%x>0)-1;
    return re;
}
 
int main()
{
    scanf("%d %lld",&n,&m);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    int l=1,r=1e9,mid,ans;
    while(l<=r){
        mid=l+r>>1;
        if(check(mid)<=m)ans=mid,r=mid-1;
        else l=mid+1;
    }
    printf("%d",ans);
}

 





莫队板子
https://gene-liu.blog.csdn.net/article/details/100556943?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param

 

posted @ 2020-10-28 16:06  瓜子NEW-G  阅读(369)  评论(0)    收藏  举报