You are given a set of 2n+12n+1 integer points on a Cartesian plane. Points are numbered from 00 to 2n2n inclusive. Let PiPi be the ii-th point. The xx-coordinate of the point PiPi equals ii. The yy-coordinate of the point PiPi equals zero (initially). Thus, initially Pi=(i,0)Pi=(i,0).
The given points are vertices of a plot of a piecewise function. The jj-th piece of the function is the segment PjPj+1PjPj+1.
In one move you can increase the yy-coordinate of any point with odd xx-coordinate (i.e. such points are P1,P3,…,P2n−1P1,P3,…,P2n−1) by 11. Note that the corresponding segments also change.
For example, the following plot shows a function for n=3n=3 (i.e. number of points is 2⋅3+1=72⋅3+1=7) in which we increased the yy-coordinate of the point P1P1 three times and yy-coordinate of the point P5P5 one time:
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 yy-coordinate among all initial points in the plot (i.e. points P0,P1,…,P2nP0,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 2n+12n+1 vertices and having an area equal to kk. 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 10181018.
The first line of the input contains two integers nn and kk (1≤n,k≤10181≤n,k≤1018) — the number of vertices in a plot of a piecewise function and the area we need to obtain.
Print one integer — the minimum possible height of a plot consisting of 2n+12n+1 vertices and with an area equals kk. 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 10181018.
4 3
1
4 12
3
999999999999999999 999999999999999986
1
One of the possible answers to the first example:
The area of this plot is 3, the height of this plot is 1.
There is only one possible answer to the second example:
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 }
Mikhail walks on a Cartesian plane. He starts at the point (0,0)(0,0), and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point (0,0)(0,0), he can go to any of the following points in one move:
- (1,0)(1,0);
- (1,1)(1,1);
- (0,1)(0,1);
- (−1,1)(−1,1);
- (−1,0)(−1,0);
- (−1,−1)(−1,−1);
- (0,−1)(0,−1);
- (1,−1)(1,−1).
If Mikhail goes from the point (x1,y1)(x1,y1) to the point (x2,y2)(x2,y2) in one move, and x1≠x2x1≠x2 and y1≠y2y1≠y2, then such a move is called a diagonal move.
Mikhail has qq queries. For the ii-th query Mikhail's target is to go to the point (ni,mi)(ni,mi) from the point (0,0)(0,0) in exactly kiki moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point (0,0)(0,0) to the point (ni,mi)(ni,mi) in kiki moves.
Note that Mikhail can visit any point any number of times (even the destination point!).
The first line of the input contains one integer qq (1≤q≤1041≤q≤104) — the number of queries.
Then qq lines follow. The ii-th of these qq lines contains three integers nini, mimi and kiki (1≤ni,mi,ki≤10181≤ni,mi,ki≤1018) — xx-coordinate of the destination point of the query, yy-coordinate of the destination point of the query and the number of moves in the query, correspondingly.
Print qq integers. The ii-th integer should be equal to -1 if Mikhail cannot go from the point (0,0)(0,0) to the point (ni,mi)(ni,mi) in exactly kiki moves described above. Otherwise the ii-th integer should be equal to the the maximum number of diagonal moves among all possible movements.
3
2 2 3
4 3 7
10 1 9
1
6
-1
One of the possible answers to the first test case: (0,0)→(1,0)→(1,1)→(2,2)(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)(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)(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 }
Let's call some positive integer classy if its decimal representation contains no more than 33 non-zero digits. For example, numbers 44, 200000200000, 1020310203 are classy and numbers 42314231, 102306102306, 72774200007277420000 are not.
You are given a segment [L;R][L;R]. Count the number of classy integers xx such that L≤x≤RL≤x≤R.
Each testcase contains several segments, for each of them you are required to solve the problem separately.
The first line contains a single integer TT (1≤T≤1041≤T≤104) — the number of segments in a testcase.
Each of the next TT lines contains two integers LiLi and RiRi (1≤Li≤Ri≤10181≤Li≤Ri≤1018).
Print TT lines — the ii-th line should contain the number of classy integers on a segment [Li;Ri][Li;Ri].
4
1 1000
1024 1024
65536 65536
999999 1000001
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 ",solve(m),solve(n-1)); 48 printf("%lld ", solve(m)-solve(n-1)); 49 } 50 return 0; 51 }
Vasya has two arrays AA and BB of lengths nn and mm, 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 [1,10,100,1000,10000][1,10,100,1000,10000] Vasya can obtain array [1,1110,10000][1,1110,10000], and from array [1,2,3][1,2,3] Vasya can obtain array [6][6].
Two arrays AA and BB are considered equal if and only if they have the same length and for each valid ii Ai=BiAi=Bi.
Vasya wants to perform some of these operations on array AA, some on array BB, in such a way that arrays AA and BB 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 AA and BBequal.
The first line contains a single integer n (1≤n≤3⋅105)n (1≤n≤3⋅105) — the length of the first array.
The second line contains nn integers a1,a2,⋯,an (1≤ai≤109)a1,a2,⋯,an (1≤ai≤109) — elements of the array AA.
The third line contains a single integer m (1≤m≤3⋅105)m (1≤m≤3⋅105) — the length of the second array.
The fourth line contains mm integers b1,b2,⋯,bm (1≤bi≤109)b1,b2,⋯,bm (1≤bi≤109) - elements of the array BB.
Print a single integer — the maximum length of the resulting arrays after some operations were performed on arrays AA and BB in such a way that they became equal.
If there is no way to make array equal, print "-1".
5
11 2 3 5 7
4
11 7 3 7
3
2
1 2
1
100
-1
3
1 2 3
3
1 2 3
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 "); 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 ", pox); 34 return 0; 35 }