Codeforces 1195D1-Submarine in the Rybinsk Sea (easy edition)

题目链接
题意就不讲了,挺容易理解的

思路

找规律看出来的,就这组样例来说
3
12 33 45
总共九种组合,分别是
f(12, 12) = 1122
f(12, 33) = 1323
f(12, 45) = 1425
f(33, 12) = 3132
f(33, 33) = 3333
f(33, 45) = 3435
f(45, 12) = 4152
f(45, 33) = 4353
f(45, 45) = 4455
就12来说,12的个位,也就是2在个位上贡献了3次在十位贡献了3次,12的十位也就是1,在百位贡献了3次,在千位贡献了3次;因为D1比较简单,每个数的长度都是一样的,所以我们把每个数的每一位用cnt数组加起来,因为n个数就会贡献n次,所以要乘个n,在从个位上开始累加,每个元素在两个位上贡献一次,每次要往高位乘两次

代码

#include 
using namespace std;

typedef long long ll;

ll n, a, cnt[100];
ll mod = 998244353;
int main(){
	scanf("%lld", &n);
	ll temp;
	for(int i = 0; i < n; i++){
		scanf("%lld", &a);
		temp = a;
		int j = 0;
		while(temp){
			cnt[j] = cnt[j] + (temp % 10) * n;
			temp /= 10;
			j++;
		}
	}
	long long ans = 0, q = 1;
	for(int i = 0; i <= 9; i++){
		ans = ((ans % mod) + (((cnt[i] % mod) * (q % mod)) % mod)) % mod;
		q = ((q % mod) * 10) % mod;
		ans = ((ans % mod) + (((cnt[i] % mod) * (q % mod)) % mod)) % mod;
		q = ((q % mod) * 10) % mod;
	}
	printf("%lld\n", ans);
}

你可能感兴趣的:(Codeforces 1195D1-Submarine in the Rybinsk Sea (easy edition))