题目链接:
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;
}