LightOJ1213 Fantasy of a Summation


LightOJ1213 Fantasy of a Summation


label


Foreword


Concise meaning of the questions

  • Given n, k, mod, and the size n of the array a [], requirements:
    i = 1 n j = 1 n . . . k = 1 n ( a [ i ] + a [ j ] + . . . + a [ k ] ) \sum_{i=1}^n\sum_{j=1}^n...\sum_{k=1}^n(a[i]+a[j]+...+a[k])

Thinking

  • And the stripper can quickly find regular. Apart like this:
    i = 1 n ( a i + b i ) = i = 1 n a i + i = 1 n b i \sum_{i=1}^n(a_i+b_i)=\sum_{i=1}^na_i+\sum_{i=1}^nb_i

Precautions

  • no

to sum up

  • no

AC Code

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

const int maxn = 1e5 + 10;

int mod;

int ksm(int a, int b)
{
	int ans = 1, base = a;
	while (b)
	{
		if (b & 1)
			ans = 1ll * ans * base % mod;
		b >>= 1;
		base = 1ll * base * base % mod;
	}
	return ans;
}

void solve()
{
	int t;
	scanf("%d", &t);
	for (int i = 1; i <= t; i++)
	{
		int n, k;
		long long sum = 0;
		scanf("%d%d%d", &n, &k, &mod);
		for (int i = 1; i <= n; i++)
		{
			int t;
			scanf("%d", &t);
			sum += t;
			sum %= mod;
		}

		printf("Case %d: %d\n", i, sum * k % mod * ksm(n, k - 1) % mod);
	}
}

int main()
{
	freopen("Testin.txt", "r", stdin);
	solve();
	return 0;
}
发布了109 篇原创文章 · 获赞 33 · 访问量 3523

Guess you like

Origin blog.csdn.net/weixin_42431507/article/details/100134893