[LightOJ] [1213] Fantasy of a Summation

Problem Description

Loading...

Problem Analysis

A somewhat difficult problem
but careful derivation reveals
$ res = sum * k * n^{(k-1)} $

So modular exponentiation is required

Also watch out for intermediate overflow in the final computation

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<stdio.h>
int f(int x,int n,int mod) {
	int res=1;
	while(n>0) {
		if(n&1)
			res=(res*x)%mod;
		x=(x*x)%mod;
		n>>=1;
	}
	return res;
}
int main() {
	int T,kase=0;
	scanf("%d",&T);
	while(T--) {
		int n,K,MOD;;
		scanf("%d %d %d",&n,&K,&MOD);
		int sum=0;
		for(int i=0; i<n; i++) {
			int t;
			scanf("%d",&t);
			sum=sum+t%MOD;
		}
		sum%=MOD;
		int k=K%MOD;
		int res=f(n,K-1,MOD);
		printf("Case %d: ",++kase);
		printf("%d\n",(long long)((long long)sum*(long long)k*(long long)res)%MOD);
	}
	return 0;
}

Problem link: LightOJ 1213 Fantasy of a Summation

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...