想了一下决定还是把代码贴出来,毕竟是自己的第一份ACM解题:
#include<iostream>
#include<cmath>
using namespace std;
const int Y=1000000007;
int main()
{
long int T, N, i, j, k, p, sum=0, total=0;
cin >> T;
for (p = 1; p <= T; p++)
{
sum = 0;
cin >> N;
for (i = 1; i <= N; i++)
{
for (j = 1; j <= N; j++)
{
if (i == j);
else for (k = 1; k <= N; k++)
{
if (i == k || j == k);
else
{
sum = (sum + ((N + 1 - i)%1000000007)*((N + 1 - j)%1000000007)*((N + 1 - k)%1000000007))%1000000007;
}
}
}
}
sum = (sum + total)%1000000007;
for (i = 1; i <= N; i++)
{
sum = (sum + (i % 1000000007)*(i % 1000000007)*(i % 1000000007)) % 1000000007;
}
cout <<"Case "<<p <<": "<<sum % (1000000007)<<endl;
}
return 0;
}
虽说不是很成熟,但交的就是这个,留作纪念吧。
再贴一个:http://blog.csdn.net/jiangshibiao/article/details/23437025
简洁的暴力破解:
#include<cstdio>
using namespace std;
const int mo=1000000007;
int T,n,a,b,c,p;
typedef long long ll;
ll ans;
int main()
{
scanf("%d",&T);
for (p=1;p<=T;p++)
{
ans=0;
scanf("%d",&n);
for (a=1;a<=n;a++)
for (b=1;b<=n;b++)
for (c=1;c<=n;c++)
{
if (a==b||a==c||b==c)
{
if (a!=b||a!=c||b!=c) continue;
}
ans=(ans+((n-a+1)%mo)*((n-b+1)%mo)*((n-c+1)%mo)%mo)%mo;
}
printf("Case %d: %d\n",p,ans);
}
return 0;
}
另外一个:
更简洁的 逆元
#include<cstdio>
using namespace std;
typedef long long ll;
ll now,test,p=1000000007,N=500000004,JSB,ans,n,t;
int main()
{
scanf("%I64d",&test);
for (now=1;now<=test;now++)
{
scanf("%I64d",&n);
JSB=(1ll*(n%p)*((n+1)%p)*N%p)%p;
if (JSB+2-2*n<0)
{
t=(-JSB-2+2*n)/p+1;
}
ans=(1ll*JSB*JSB)%p*((JSB+2-2*n+t*p)%p)%p;
printf("Case %I64d: %I64d\n",now,ans);
}
return 0;
}