前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Fantasy of a Summation (LightOJ - 1213)(快速幂+简单思维)

Fantasy of a Summation (LightOJ - 1213)(快速幂+简单思维)

作者头像
Lokinli
发布2023-03-09 16:35:53
1270
发布2023-03-09 16:35:53
举报
文章被收录于专栏:以终为始以终为始

题解:根据题目给的程序,就是计算给的这个序列,进行k次到n的循环,每个数需要加的次数是k*n^(k-1),所以快速幂取模,算计一下就可以了。

代码语言:javascript
复制
#include <bits/stdc++.h>

using  namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f3f;
long long pow_mod(ll a, ll k, ll mod)
{
    ll ans = 1;
    while(k)
    {
        if(k%2)
            ans *= a;
        ans %= mod;
        a = a * a;
        a %= mod;
        k /=2;
    }
    return ans;
}
int main()
{
    int T;
    ll n,k,mod,x,sum;
    while(~scanf("%d",&T))
    {
        int cas = 1;
        while(T--)
        {
            sum = 0;
            scanf("%lld%lld%lld",&n,&k,&mod);
            for(ll i = 0; i < n; i ++)
            {
                scanf("%lld",&x);
                sum += (x * (k  * pow_mod(n,k-1,mod)%mod)%mod);
                sum %= mod;
            }
            printf("Case %d: %lld\n",cas++, sum);
        }
    }
    return 0;
}

Problem: If you think codes, eat codes then sometimes you may get stressed. In your dreams you may see huge codes, as I have seen once. Here is the code I saw in my dream. #include <stdio.h> int cases, caseno; int n, K, MOD; int A[1001]; int main() { scanf("%d", &cases); while( cases-- ) { scanf("%d %d %d", &n, &K, &MOD); int i, i1, i2, i3, ... , iK; for( i = 0; i < n; i++ ) scanf("%d", &A[i]); int res = 0; for( i1 = 0; i1 < n; i1++ ) { for( i2 = 0; i2 < n; i2++ ) { for( i3 = 0; i3 < n; i3++ ) {                     ... for( iK = 0; iK < n; iK++ ) {                         res = ( res + A[i1] + A[i2] + ... + A[iK] ) % MOD; }                     ... } } } printf("Case %d: %d\n", ++caseno, res); } return 0; } Actually the code was about: 'You are given three integers n, K, MOD and n integers: A0, A1, A2 ... An-1, you have to write K nested loops and calculate the summation of all Ai where i is the value of any nested loop variable.' Input Input starts with an integer T (≤ 100), denoting the number of test cases. Each case starts with three integers: n (1 ≤ n ≤ 1000), K (1 ≤ K < 231), MOD (1 ≤ MOD ≤ 35000). The next line contains n non-negative integers denoting A0, A1, A2 ... An-1. Each of these integers will be fit into a 32 bit signed integer. Output For each case, print the case number and result of the code. Sample Input 2 3 1 35000 1 2 3 2 3 35000 1 2 Sample Output Case 1: 6 Case 2: 36

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-09-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档