求和

题目链接:

https://www.luogu.org/problemnew/show/P2671

通过分解因式总结规律。可以得到公式如下:

同为奇数偶数的n个同颜色的格子的公式为:

(i1+i2+.....in)(num[1]+num[2]....num[n])+(n-2)( i1*num[1]+i2*num[2]......in*num[n] ).

其中i代表的是第几个,num[i]代表第i个上的数字是多少。

最后还要注意的是一定要对每一步的运算都要取余,尤其是注意在每一步的相加之后的取余。

代码:

#include <iostream> //奇或者是偶用二维数组。
#include <cstring>
#include <stdio.h>
using namespace std;
const int inf=1e5+7;
const int MOD=10007;
int then[2][inf],thei[2][inf],thegeshu[2][inf];   //这里都是通过color来实现的。
int arr[inf];
int thelast[2][inf];

int main()
{
    int n,m;
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&arr[i]);
    for(int i=1;i<=n;i++)       //尝试精简。  
    {
        int color;
        scanf("%d",&color);
        then[i%2][color]+=(arr[i])%MOD;       //精简。这样的效果是最好。
        then[i%2][color]=then[i%2][color]%MOD;
        thei[i%2][color]+=(i)%MOD;
        thei[i%2][color]=thei[i%2][color]%MOD;
        thelast[i%2][color]+=((arr[i]%MOD)*i)%MOD;
        thelast[i%2][color]=thelast[i%2][color]%MOD;
        thegeshu[i%2][color]++;
        thegeshu[i%2][color]=thegeshu[i%2][color]%MOD;
    }
    long long ans=0;        	// 可以向他学习一下,精简。
    for(int i=0;i<2;i++) 
        for(int j=1;j<=m;j++)
        {
            if(thegeshu[i][j]>=2)
            {
                ans+= ((then[i][j]*thei[i][j])%MOD+(((thegeshu[i][j]-2)%MOD)*thelast[i][j])%MOD)%MOD;
            	ans=ans%MOD;
			}
        }
    ans=ans%MOD;
    cout<<ans<<endl;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值