CodeForces 489C&466C (小技巧)

489C Given Length and Sum of Digits…

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

You have a positive integer m and a non-negative integer s. Your task is to find the smallest and the largest of the numbers that have length m and sum of digits s. The required numbers should be non-negative integers written in the decimal base without leading zeroes.

Input
The single line of the input contains a pair of integers m, s (1 ≤ m ≤ 100, 0 ≤ s ≤ 900) — the length and the sum of the digits of the required numbers.

Output
In the output print the pair of the required non-negative integer numbers — first the minimum possible number, then — the maximum possible number. If no numbers satisfying conditions required exist, print the pair of numbers “-1 -1” (without the quotes).

Examples
input
2 15
output
69 96

input
3 0
output
-1 -1

#include
#include
#include
using namespace std;

#define inf 0x3f3f3f3f

int a[105],b[105];
int flag;

int m,s;

bool can(int m, int s) {
	return s >= 0 && s <= 9 * m;
}

int main() {

	cin>>m>>s;
	if((s==0&&m!=1)||9*m<s) {
		cout<<"-1 -1"<<endl;
		return 0;
	}

	int sum = s;
	for (int i = 0; i < m; i++) {
		for (int d = 0; d < 10; d++) {
			if ((i > 0 || d > 0 || (m == 1 && d == 0)) && can(m - i - 1, sum - d)) {
				a[i]=d;
				sum -= d;
				break;
			}
		}
	}
	
	for(int i=0; i<m;i++) {
		cout<<a[i];
	}
	cout<<" ";
	sum = s;
	for (int i = 0; i < m; i++) {
		for (int d = 9; d>=0; d--) {
			if ((i > 0 || d > 0 || (m == 1 && d == 0)) && can(m - i - 1, sum - d)) {
				b[i]=d;
				sum -= d;
				break;
			}
		}
	}
	for(int i=0; i<m;i++) {
		cout<<b[i];
	}
	cout<<endl;
	return 0;
}

466C Number of Ways

You’ve got array a[1], a[2], …, a[n], consisting of n integers. Count the number of ways to split all the elements of the array into three contiguous parts so that the sum of elements in each part is the same.

More formally, you need to find the number of such pairs of indices i, j (2 ≤ i ≤ j ≤ n - 1), that .

Input
The first line contains integer n (1 ≤ n ≤ 5·105), showing how many numbers are in the array. The second line contains n integers a[1], a[2], …, a[n] (|a[i]| ≤  109) — the elements of array a.

Output
Print a single integer — the number of ways to split the array into three parts with the same sum.

Examples
Input
5
1 2 3 0 3
Output
2
Input
4
0 1 -1 0
Output
1
Input
2
4 1
Output
0

#include
#include

using namespace std;

int a[500005];
long long cnt[500005];//标记满足(i<=j)sum(j to n)=1/3 *SUM 的j的种数 

int main() {
	ios::sync_with_stdio(0);
	int n;
	cin >> n;
	long long s = 0;
	for(int i = 0 ; i < n ; ++i) {
		cin >> a[i];
		s += a[i];
	}
	if (s % 3 != 0)
		cout << "0\n";
	else {
		s /= 3;
		long long ss = 0;
		for(int i = n-1; i >= 0 ; --i) {
			ss += a[i];
			if (ss == s)
				cnt[i] = 1;
		}
		for(int i = n-2 ; i >= 0 ; --i)
			cnt[i] += cnt[i+1];

		long long ans = 0;
		ss = 0;
		for(int i = 0 ; i+2 < n ; ++i) {
			ss += a[i];
			if (ss == s)
				ans += cnt[i+2];
		}
		cout << ans << "\n";
	}
	return 0;
}


你可能感兴趣的:(ACM训练)