ACdream 1175 Board 对称思想,完全解题过程!

Board

Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)
Problem Description
有一个N * N的棋盘,对于方格上的两个点(x1,y1),(x2,y2),(1 <= x1,y1,x2,y2 <= N) 如果abs(x1 - x2) < abs(y1 - y2),则称(x1,y1)攻击到了(x2,y2),现在想问你每个点能攻击的其他点数目之和 % 1000000007
Input
多组数据,每组数据一个数N(1<=N<=10^9)
Output
每组数据一行,答案
Sample Input
1
2
3
4
Sample Output
0
4
26
92
Source
classical
Manager


又是数学题,好吧,我承认刷ACdream数学题提高了不少。碰到这种题目,第一反应,毫无疑问,打暴力程序找规律。于是就有了最初始的暴力程序:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<string>
#include<cstring>
#include<algorithm>
#include<fstream>
#include<queue>
#include<stack> 
#include<vector>
#include<cmath>
#include<iomanip>
#define rep(i,n) for(i=1;i<=n;i++)
#define MM(a,t) memset(a,t,sizeof(a))
#define INF 1e9
typedef long long ll;
#define mod 1000000007
using namespace std;
int n;
int a[1000][1000];
int cal(int x,int y){
	int i,j;
	int res=0;
	rep(i,n)
	  rep(j,n)
	  if(y!=j && abs(x-i)<abs(y-j)) res++;
    return res;
}
int main()
{
	int i,j;

    while(scanf("%d",&n)!=EOF){
    	MM(a,0);
    	int res=0;
    	rep(i,n)
    	  rep(j,n){
  	    	a[i][j]=cal(i,j);
  	    	res+=a[i][j];
  	    }
      rep(i,n){
      	rep(j,n) cout<<setw(4)<<a[i][j];
      	cout<<'\n';
      }
  	  cout<<"Res= "<<res<<'\n';
    }
	
	return 0;
}

以n=10为例的结果是这样的:

10
  45  37  31  27  25  25  27  31  37  45
  53  44  38  34  32  32  34  38  44  53
  59  50  43  39  37  37  39  43  50  59
  63  54  47  42  40  40  42  47  54  63
  65  56  49  44  41  41  44  49  56  65
  65  56  49  44  41  41  44  49  56  65
  63  54  47  42  40  40  42  47  54  63
  59  50  43  39  37  37  39  43  50  59
  53  44  38  34  32  32  34  38  44  53
  45  37  31  27  25  25  27  31  37  45
Res= 4380


这些数字乍看之下毫无规律可言,但可以发现它整体是中心对称的,再来仔细考虑下条件abs(x1 - x2) < abs(y1 - y2),对于一个点来说,符合条件的点也是中心对称的,具体应该是当x2==x1时的那一条线和这个点的左上左下右上右下分布着符合条件的点,我们不妨把暴力程序改变一下,如:

	  if(y!=j && abs(x-i)<abs(y-j)) res++;if中加个条件为x==i

就会有了美如画的一个结果:


10
   9   9   9   9   9   9   9   9   9   9
   9   9   9   9   9   9   9   9   9   9
   9   9   9   9   9   9   9   9   9   9
   9   9   9   9   9   9   9   9   9   9
   9   9   9   9   9   9   9   9   9   9
   9   9   9   9   9   9   9   9   9   9
   9   9   9   9   9   9   9   9   9   9
   9   9   9   9   9   9   9   9   9   9
   9   9   9   9   9   9   9   9   9   9
   9   9   9   9   9   9   9   9   9   9
Res= 900

这个900在最终结果中是只占一个的,而且可以看出它=n*n*(n-1)

接着在改把x==i && y!=j改为 x>i && y>j

又会发现:

10
   0   0   0   0   0   0   0   0   0   0
   0   0   1   2   3   4   5   6   7   8
   0   0   1   3   5   7   9  11  13  15
   0   0   1   3   6   9  12  15  18  21
   0   0   1   3   6  10  14  18  22  26
   0   0   1   3   6  10  15  20  25  30
   0   0   1   3   6  10  15  21  27  33
   0   0   1   3   6  10  15  21  28  35
   0   0   1   3   6  10  15  21  28  36
   0   0   1   3   6  10  15  21  28  36
Res= 870


可以发现870*4+900=4380 所以本题关键点在于求这个四分之一点的总和,看最后一行的总和是等差数列和的累加和,然后从倒数第三行看起每行比最后一行少个等差数列和的累加和,因为N有1e9之大,所以我们必须求出等差数列和的累加和的通项公式,以及等差数列和的累加和的累加和的通项公式。

a(n)=n(n+1)/2+a(n-1)这是递推公式,发挥你在高中学过的知识把a(n)的通项公式和S(n)的通项公式给求出来吧。

不过在解这个方程前你得先知道平方和公式:n(n+1)(2n+1)/6 立方和公式 n*n(n+1)(n+1)/4;

经过一系列复杂的计算可以得到两个美如画的公式:

等差数列和累加和 a(n)=n(n+1)(n+2)/6 S(n)=n(n+1)(n+2)(n+3)/24 全部总的和的公式也可以得到为:

Res=4*((n-1)*a(n-2)-S(n-3))+(n-1)*n*n 最后结果是要mod 1e9+7的,但是mod的过程中是不能除的,不然就会丢失精确数据,但凡除的必须在原数据中把除法搞定,所以对于a(n)中的n,n+1,n+2来说,能除以2,3(6的约数)的就直接除了,S(n)类似。

总程序:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<string>
#include<cstring>
#include<algorithm>
#include<fstream>
#include<queue>
#include<stack> 
#include<vector>
#include<cmath>
#include<iomanip>
#define rep(i,n) for(i=1;i<=n;i++)
#define MM(a,t) memset(a,t,sizeof(a))
#define INF 1e9
typedef long long ll;
#define mdu 1000000007
using namespace std;
ll n;
ll funa(ll x){
	ll t[4]={x,x+1,x+2,1};
	ll chu[2]={2,3};
	int i=0,j=0;
    while(i<3 && j<1){
 	  if(t[i]%chu[j]==0) {
  	    t[i]/=chu[j];
		j++;	
  	  }
  	  else i++;
    }
    i=0;
    while(i<3 && j<2){
 	  if(t[i]%chu[j]==0) {
  	    t[i]/=chu[j];
		j++;	
  	  }
  	  else i++;
    }
    t[3]=t[0];
    for(i=1;i<3;i++) t[3]=(t[3]*t[i])%mdu;
    
    return t[3];
}
ll funs(ll x){
    ll t[5]={x,x+1,x+2,x+3,1};
	ll chu[4]={2,2,2,3};
	int i=0,j=0;	
    while(i<4 && j<3){
 	  if(t[i]%chu[j]==0) {
  	    t[i]/=chu[j];
		j++;	
  	  }
  	  else i++;
    }
    i=0;
    while(i<4 && j<4){
 	  if(t[i]%chu[j]==0) {
  	    t[i]/=chu[j];
		j++;	
  	  }
  	  else i++;
    }
    t[4]=t[0];
    for(i=1;i<4;i++) t[4]=(t[4]*t[i])%mdu;
    
    return t[4];
}
int main()
{
	int i,j;
	ll res;
	
	while(scanf("%lld",&n)!=EOF){
	  switch(n){
  	    case 1:cout<<0<<'\n'; continue;
		case 2:cout<<4<<'\n'; continue;
		case 3:cout<<26<<'\n'; continue;
	    default:
		  ll t1=funa(n-2);
		  t1=(n-1)*t1%mdu;
		  ll t2=funs(n-3);
		  if(t1>t2) t1-=t2;
		  else      t1=t1+mdu-t2;
		  t1=4*t1%mdu;
		  t1=(t1+(n*n%mdu)*(n-1)%mdu)%mdu;	
		  res=t1;
  	  }	
  	  cout<<res<<'\n';
	}

	
	return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值