A. Function Height
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a set of Pi=(i,0).

The given points are vertices of a plot of a piecewise function. The PjPj+1.

In one move you can increase the 1. Note that the corresponding segments also change.

For example, the following plot shows a function for P5 one time:

Educational Codeforces Round 50 (ABCD)

Let the area of the plot be the area below this plot and above the coordinate axis OX. For example, the area of the plot on the picture above is 4 (the light blue area on the picture above is the area of the plot drawn on it).

Let the height of the plot be the maximum P0,P1,…,P2n). The height of the plot on the picture above is 3.

Your problem is to say which minimum possible height can have the plot consisting of k. Note that it is unnecessary to minimize the number of moves.

It is easy to see that any answer which can be obtained by performing moves described above always exists and is an integer number not exceeding 1018.

Input

The first line of the input contains two integers 1≤n,k≤1018) — the number of vertices in a plot of a piecewise function and the area we need to obtain.

Output

Print one integer — the minimum possible height of a plot consisting of 1018.

Examples
input
Copy
4 3
output
Copy
1
input
Copy
4 12
output
Copy
3
input
Copy
999999999999999999 999999999999999986
output
Copy
1
Note

One of the possible answers to the first example:

Educational Codeforces Round 50 (ABCD)

The area of this plot is 3, the height of this plot is 1.

There is only one possible answer to the second example:

Educational Codeforces Round 50 (ABCD)

The area of this plot is 12, the height of this plot is 3.

 

 1 #include <bits/stdc++.h>
 2 #define ll long long int
 3 using namespace std;
 4 
 5 ll n,m;
 6 int main(){
 7     cin>>n>>m;
 8     ll ans = m/n;
 9     ans += m%n==0?0:1;
10     cout<<ans<<endl;
11     return 0;
12 }

 

B. Diagonal Walking v.2
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Mikhail walks on a Cartesian plane. He starts at the point (0,0), he can go to any of the following points in one move:

  • (1,0);
  • (1,1);
  • (0,1);
  • (−1,1);
  • (−1,0);
  • (−1,−1);
  • (0,−1);
  • (1,−1).

If Mikhail goes from the point diagonal move.

Mikhail has ki moves.

Note that Mikhail can visit any point any number of times (even the destination point!).

Input

The first line of the input contains one integer 1≤q≤104) — the number of queries.

Then y-coordinate of the destination point of the query and the number of moves in the query, correspondingly.

Output

Print diagonal moves among all possible movements.

Example
input
Copy
3
2 2 3
4 3 7
10 1 9
output
Copy
1
6
-1
Note

One of the possible answers to the first test case: (0,0)→(1,0)→(1,1)→(2,2).

One of the possible answers to the second test case: (0,0)→(0,1)→(1,2)→(0,3)→(1,4)→(2,3)→(3,2)→(4,3).

In the third test case Mikhail cannot reach the point (10,1) in 9 moves.

 

仔细分析就出来了。

 

 1 #include <bits/stdc++.h>
 2 #define ll long long int
 3 using namespace std;
 4 
 5 int main(){
 6     ll t,x,y,z;
 7     cin>>t;
 8     while(t--){
 9         cin>>x>>y>>z;
10         ll sum = min(x,y);
11         ll ans = abs(x-y);
12         if(z<max(x,y)){
13             cout<<"-1"<<endl;
14         }else{
15             if(ans%2){
16                 cout<<z-1<<endl;
17             }else{
18                 if(z==sum+ans){
19                     cout<<z<<endl;
20                     continue;
21                 }
22                 ll cnt = z-sum-ans;
23                 if(cnt%2==0){
24                     cout<<z<<endl;
25                 }else{
26                     cout<<z-2<<endl;
27                 }
28             }
29         }
30     }
31     return 0;
32 }

 

 

C. Classy Numbers
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Let's call some positive integer classy if its decimal representation contains no more than 7277420000 are not.

You are given a segment L≤x≤R.

Each testcase contains several segments, for each of them you are required to solve the problem separately.

Input

The first line contains a single integer 1≤T≤104) — the number of segments in a testcase.

Each of the next 1≤Li≤Ri≤1018).

Output

Print [Li;Ri].

Example
input
Copy
4
1 1000
1024 1024
65536 65536
999999 1000001
output
Copy
1000
1
0
2

数位dp (看了好久才学会)
算是模板题吧

 1 #include <bits/stdc++.h>
 2 #define ll long long int
 3 using namespace std;
 4 ll dp[30][4],digth[30];
 5 
 6 
 7 ll dfs(int len, int num,  bool lim){
 8     if(len == 0)
 9         return 1;
10     if(!lim && dp[len][num] != -1)
11         return dp[len][num];
12     ll ans = 0;
13     int up = lim ? digth[len] : 9;
14 
15     for(int i = 0; i <= up; ++i){
16         if(num < 3){
17             if( !i )
18                 ans += dfs(len - 1, num, lim && i == digth[len] );
19             else
20                 ans += dfs(len - 1, num + 1, lim && i == digth[len] );
21         }else if(num == 3){
22             if(!i)
23                 ans += dfs(len -1, num, lim && i == digth[len] );
24         }
25     }
26     if(!lim )
27         dp[len][num] = ans;
28     return ans;
29 }
30 
31 ll solve(ll x){
32     int k = 0;
33     while(x){
34         digth[ ++k ] = x%10;
35         x /= 10;
36     }
37     return dfs(k,0,true);
38 }
39 
40 int t;
41 ll n,m;
42 int main(){
43     scanf("%d", &t);
44     memset( dp , -1 , sizeof(dp) ) ;
45     while(t--){
46         scanf("%lld%lld",&n,&m);
47         // printf("%lld %lld\n",solve(m),solve(n-1));
48         printf("%lld\n", solve(m)-solve(n-1));        
49     }
50     return 0;
51 }

 

 

D. Vasya and Arrays
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya has two arrays m, respectively.

He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array [6].

Two arrays Ai=Bi.

Vasya wants to perform some of these operations on array B become equal. Moreover, the lengths of the resulting arrays should be maximal possible.

Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays Bequal.

Input

The first line contains a single integer n (1≤n≤3⋅105) — the length of the first array.

The second line contains A.

The third line contains a single integer m (1≤m≤3⋅105) — the length of the second array.

The fourth line contains B.

Output

Print a single integer — the maximum length of the resulting arrays after some operations were performed on arrays B in such a way that they became equal.

If there is no way to make array equal, print "-1".

Examples
input
Copy
5
11 2 3 5 7
4
11 7 3 7
output
Copy
3
input
Copy
2
1 2
1
100
output
Copy
-1
input
Copy
3
1 2 3
3
1 2 3
output
Copy
3

指针模拟
 1 #include <bits/stdc++.h>
 2 #define ll long long int
 3 #define N 300005
 4 using namespace std;
 5 int n,m;
 6 ll an[N],bn[N];
 7 int main(){
 8     scanf("%d",&n);
 9     for(int i = 1; i <= n; ++i){
10         scanf("%lld", &an[i]);
11         an[i] += an[i-1];
12     }
13     scanf("%d", &m);
14     for(int i = 1; i <= m; ++i){
15         scanf("%lld", &bn[i]);
16         bn[i] += bn[i-1];
17     }
18     if(an[n]!=bn[m]){
19         printf("-1\n");
20         return 0;
21     }
22     int pox = 0;
23     for(int i = 1,j = 1;i <= n&&j <= m;){
24         if(an[i] == bn[j]){
25             pox++;
26             i++;j++;
27         }else if(an[i] < bn[j]){
28             i++;
29         }else{
30             j++;
31         }
32     }
33     printf("%d\n", pox);
34     return 0;
35 }

 

相关文章:

  • 2021-09-07
  • 2021-06-23
  • 2022-01-27
  • 2022-01-11
  • 2022-01-05
  • 2021-11-09
  • 2021-05-17
猜你喜欢
  • 2021-06-24
  • 2021-11-25
  • 2021-12-05
  • 2021-12-02
  • 2022-12-23
  • 2021-05-21
  • 2021-11-21
相关资源
相似解决方案