概述
You are given an n-dimensional grid in which the dimensions of the grid are a1 × a2 × ... × an. Each cell in the grid is represented as an n-tuple (x1, x2, ..., xn) (1 ≤ xi ≤ ai).
Two cells are considered to be adjacent if the Manhattan Distance between them is equal to 1. The Manhattan Distance between two cells X(x1, x2, ..., xn) and Y(y1, y2, ..., yn) is equal to: |x1 - y1| + |x2 - y2| + ... + |xn - yn|.
Your task is to count how many pairs of cells are adjacents. Can you? Two pairs of cells are considered the same if they include the same cells, i.e the pair (c1, c2)is the same as (c2, c1).
Input
The first line contains an integer T (1 ≤ T ≤ 100) specifying the number of test cases.
The first line of each test case contains an integer n (1 ≤ n ≤ 105), in which n is the number of dimensions of the grid. Then a line follows containing n integers a1, ..., an (1 ≤ ai ≤ 105), in which ai is the size of the ith dimension.
The sum of n overall test cases does not exceed 6 × 106.
Output
For each test case, print a single line containing the number of pairs of adjacent cells modulo 109 + 7.
Example
Input
1 3 1 2 3
Output
7
Note
The absolute value |x| of a real number x is the non-negative value of x without regard to its sign. Namely, |x| = x for a positive x, |x| = - x for a negative x (in which case - x is positive), and |0| = 0. For example, the absolute value of 3 is 3, and the absolute value of - 3 is also 3. The absolute value of a number may be thought of as its distance from zero.
题目:比赛时根本就没看这一题 比赛完看答案还是懵逼 被自己菜哭了 还摔了键盘,,
题目大意:有N维的空间(改成空间更好理解),给出每一维的大小,计算总的相邻点的个数
思维:代码篇幅中有写。
代码:
#include<bits/stdc++.h>
using namespace std;
int w,n,a[100005];
/*
本题的思路是 N维空间
第一维空间的时候 有sum=a[1]-1个相邻点
第二维空间的时候 有sum = sum*a[2] + a[1]*(a[2]-1) (低维的复制a[i]遍,然后是维维(低)之间的点数*低维一共有多少点)
。。。。
*/
int main(){
scanf("%d",&w);
while(w--){
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
long long sum=a[1]-1,num=a[1];
for(int i=2;i<=n;i++){
sum=sum*a[i];//对数复制(单位内的数量*第I维的size)
sum%=1000000007;
sum+=num*(a[i]-1);//维维之间的相邻点计算
sum%=1000000007;
num=num*a[i];
num%=1000000007;
}
printf("%lldn",sum);//注意在进行int的加减时会出现爆int的情况,wa两发,真菜!
}
return 0;
}
最后
以上就是从容黄蜂为你收集整理的N-Dimensional Grid的全部内容,希望文章能够帮你解决N-Dimensional Grid所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复