Sumtime limit: 2000/1000 ms (Java/others) memory limit: 128000/64000 KB (Java/others) submitstatisticnext problemproblem description
You are given an N * n digit matrix and you can get several horizontal or vertical digit strings from any position.
For example:
123
456
789
In first row, you can get 6 digit strings totally, which are 23,123.
In first column, you can get 6 digit strings totally, which are 47,147.
We want to get all digit strings from each row and column, and write them on a paper. now I wonder the sum of all number on the paper if we consider a digit string as a complete decimal number.
Input
The first line contains an integer n. (1 <=n <= 1000)
In the next n lines each line contains a string with N digit.
Output
Output the answer after module 1,000,000,007 (1e9 + 7 ).
Sample Input
3123456789
Sample output
2784
The main question is to export the formula:
For example, sum = 1111 for each row of N rows and n columns ..... 111 (N 1) * A1 + 111... 111 (N-1 1) * 2 * A2 + ......... + 11 * (n-1) * an-1 + 1 * n *;
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<queue>#include<vector>#define M 1000000007#define f1(i, n) for(int i=1; i<=n; i++)#define f2(i, n) for(int i=0; i<n; i++)#define f3(j, n) for(int j=0; j<n; j++) using namespace std;char a[1050][1050];long long int b[1050];int main(){ int n; int k=1050; f1(i, k) b[i]=0; f1(i, k) for(int j=1; j<=i; j++) b[i]=(b[i]*10+1)%M; while(~scanf("%d",&n)) { k=n; long long int sum=0; f2(i, k) { scanf("%s",&a[i]); // f3(j, k) for(int j=0; j<n; j++) sum=(sum+((((j+1)*((long long)a[i][j]-'0'))*b[n-j])%M))%M; } //f3(j, k) for(int j=0; j<n; j++) { f2(i, k) sum=(sum+((((i+1)*((long long)a[i][j]-'0'))*b[n-i])%M))%M; } cout<<sum<<endl; } return 0;}