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;
}




發佈了41 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章