Sum

Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others)

Problem 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 1,2,3,12,23,123.

In first column, you can get 6 digit strings totally, which are 1,4,7,14,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

3
123
456
789

Sample Output

2784

题目主要是导出公式:

如n行n列的每一行的和sum=1111.....111(n个1)*A1+111...111(n-1个1)*2*A2+.........+11*(n-1)*An-1+1*n*An;

好吧。。这么写果然还是不怎么完整。。如今补充一下。。

拿第一行 1 2 3来说。。

1 仅仅能由他本身, 即 sum+=A1。  2 能有2, 12。则 sum+=A2+10*A1+A2;   3 能有3, 23 ,123,则:

sum+=A3+10*A2+A3+100*A1+10*A2+A3......合算的sum=111*A1+11*2*A2+1*3*A3;也就是上面说的那个公式。

别忘了计算列的sum.



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

ACdream: Sum的更多相关文章

  1. 聚合函数:sum,count,max,avg

    聚合函数:sum,count,max,avg等,一般作用于多条记录上.通过group by可以将数据对属于一组的数据起作用. SELECT region, SUM(population), SUM(a ...

  2. LeetCode129:Sum Root to Leaf Numbers

    题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a nu ...

  3. 每日一九度之 题目1038:Sum of Factorials

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2109 解决:901 题目描述: John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, ...

  4. acdream Divide Sum

    Divide Sum Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitSta ...

  5. acdream 1431 Sum vs Product

    Sum vs Product Time Limit: 4000/2000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) Submi ...

  6. Hive函数:SUM,AVG,MIN,MAX

    转自:http://lxw1234.com/archives/2015/04/176.htm,Hive分析窗口函数(一) SUM,AVG,MIN,MAX 之前看到大数据田地有关于max()over(p ...

  7. LeetCode之“树”:Sum Root to Leaf Numbers

    题目链接 题目要求: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represe ...

  8. ACM1001:Sum Problem

    Problem Description In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.   Input ...

  9. HDU - 4676 :Sum Of Gcd (莫队&区间gcd公式)

    Given you a sequence of number a 1, a 2, ..., a n, which is a permutation of 1...n. You need to answ ...

随机推荐

  1. 《转》OpenStack Live Migration

    This post is based assumption that KVM as hypervisor, and Openstack is running in Grizzly on top of ...

  2. JSP中两种include的区别

    首先说明这两种都是什么: <%@ include file=”relativeURI”%> 可以叫作静态include(静态包含),是jsp指令中的一种,(JSP指令控制JSP编译器如何去 ...

  3. opencv做的美女找茬程序~

    // CMP.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <cv.h> #include <high ...

  4. Java.util.zip adding a new file overwrites entire jar?(转)

    ZIP and TAR fomats (and the old AR format) allow file append without a full rewrite. However: The Ja ...

  5. DMA过程分析

    1.1 当我们在应用程序中编写write系统调用,向磁盘中写入数据时,写入请求会先调用底层写函数,将请求先写入内存中的页快速缓存(page cache)中,写入成功则立马返回,真正的写入磁盘操作会延迟 ...

  6. queue C++

    #include <iostream> using namespace std; class DequeEmptyException { public: DequeEmptyExcepti ...

  7. Selenium: 空指针error

    Error 类型:空指针 可能原因一: 只是引用了该类,但是没有对该类进行实例化(即没有New 一下),即没有给该类分配内存,所以导致空指针: 类调用前注意要实例化,否则会导致空指针错误. 首先声明D ...

  8. poj2826(线段相交)

    传送门:An Easy Problem?! 题意:用两条线段接雨水,雨水是垂直落下的,问我们用给定的两条线段能接到多少水. 分析:看起来很简单,写起来略麻烦,先排除不能接到水的情况: 1. 两条线段不 ...

  9. hdu4289(最小割)

    传送门:Control 题意:有n个城市,有个小偷想从其中一个城市逃到另一个城市,警察想要堵截这个小偷,知道了在每个城市堵截的成本,问如何安排在哪些城市堵截可以使得小偷一定会被抓住,而且成本最低. 分 ...

  10. random_shuffle (stl算法)打乱顺序 - 飞不会的日志 - 网易博客

    random_shuffle (stl算法)打乱顺序 - 飞不会的日志 - 网易博客 random_shuffle (stl算法)打乱顺序 2012-03-31 10:39:11|  分类: 算法 | ...