ACdream: Sum
Sum
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的更多相关文章
- 聚合函数:sum,count,max,avg
聚合函数:sum,count,max,avg等,一般作用于多条记录上.通过group by可以将数据对属于一组的数据起作用. SELECT region, SUM(population), SUM(a ...
- 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 ...
- 每日一九度之 题目1038:Sum of Factorials
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2109 解决:901 题目描述: John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, ...
- acdream Divide Sum
Divide Sum Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitSta ...
- acdream 1431 Sum vs Product
Sum vs Product Time Limit: 4000/2000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) Submi ...
- Hive函数:SUM,AVG,MIN,MAX
转自:http://lxw1234.com/archives/2015/04/176.htm,Hive分析窗口函数(一) SUM,AVG,MIN,MAX 之前看到大数据田地有关于max()over(p ...
- LeetCode之“树”:Sum Root to Leaf Numbers
题目链接 题目要求: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represe ...
- ACM1001:Sum Problem
Problem Description In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n. Input ...
- 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 ...
随机推荐
- disruptor流程
这里先不提那些编译器方面的优化.只看一下基于无锁环形队列的生产者消费者模型的工作流程.上一个图先: 当中,buffer是一个数组,用来模拟环形队列. slowest_reader记录最慢的reader ...
- 【MongoDB】学习MongoDB推荐三本书
近期学习mongodb,感觉这三本书写得不错.非常大家分享一下:
- Delphi中获取Unix时间戳及注意事项(c语言中time()是按格林威治时间计算的,比北京时间多了8小时)
uses DateUtils;DateTimeToUnix(Now) 可以转换到unix时间,但是注意的是,它得到的时间比c语言中time()得到的时间大了8*60*60这是因为Now是当前时区的时间 ...
- python 内存泄露的诊断 - 独立思考 - ITeye技术网站
python 内存泄露的诊断 - 独立思考 - ITeye技术网站 python 内存泄露的诊断 博客分类: 编程语言: Python Python多线程Blog.net 对于一个用 python ...
- Ubuntu下is not in the sudoers file 问题解决
在Ubuntu12.04 下,使用sudo apt-get install XXX 时,突然跳出 username is not in the sudoers file的问题 然后我一查此userna ...
- 解决xShell4某些情况下按删除键会输出^H的问题
当我们用Xshell登录进入linux后,在普通模式下,对输入进行删除等操作没有问题. 而在执行中,按delete,backspace键时会产生^H等乱码问题. 这是由于编码不匹配的问题. 解决方法: ...
- Android Fragment的介绍与使用(案例Demo)
应用场景: 众所了解Android上的界面展示都是通过Activity实现的,可是Activity也有它的局限性,相同的界面在手机上显示可能非常好看,在平板上就未必了.为了让界面能够在平板上更好地展示 ...
- Iterator 和 Iterable 差别和联系
用Iterator模式实现遍历集合 Iterator模式是用于遍历集合类的标准訪问方法.它能够把訪问逻辑从不同类型的集合类中抽象出来,从而避免向client暴露集合的内部结构. 比如,假设没有使用I ...
- Linux pipe功能
1. 功能说明 pipe(管道建设): 1) 头 #include<unistd.h> 2) 定义函数: int pipe(int filedes[2]); 3) 函数说明: pipe() ...
- TR90眼镜_百度百科
TR90眼镜_百度百科 TR90眼镜