【TOJ 1723】Just calculate it!

描述

Given n positive numbers a1, a2, ..., an,two number m and b,please calculate the following expression:

Here "x mod y" is the positive remainder when x is divided by y. For example, 3 mod 7 = 3

输入

There are multiple test cases. Each teat case has two lines, the first line contains three positve integers n, m and b, the second line has n positive integers

a1, a2, ..., an a integer n, all positve numbers in this problem are not larger than 1000.

The end of input is marked by end of file.

输出

For each input, you should calculate value of the expression and output it. 

样例输入

扫描二维码关注公众号,回复: 114124 查看本文章

1 1 2
1
3 2 5
2 2 2

样例输出

1
2

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,b,i,j,a[1005];  //cifang-m   mod-b
    while(scanf("%d%d%d",&n,&m,&b)!=EOF)
    {
        long long s=0;
        
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            long long x=1;
            for(j=1;j<=m;j++)
            {
                x=x*a[i];
                if(x>=b)
                    x=x%b;
            }
            s=s+x;
            if(s>=b)
            s=s%b;
        }
        printf("%lld\n",s);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/kannyi/p/8996444.html
今日推荐