J - 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

本题暴力会T

所以简化公式

对于同行/列 须要累加的值为 a1*111+a2*22+a3*3

发现规律sum=∑a(10^(n-i+1)-1)/9*i %F

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define MAXN (1000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
int n;
char a[MAXN][MAXN];
ll p10[MAXN]={0};
ll pow2(ll b)
{
if (b==1) return 10;
if (b==0) return 1;
if (p10[b]) return p10[b];
ll p=pow2(b/2)%F;
p=(p*p)%F;
if (b&1)
{
p=(p*10)%F;
}
p10[b]=p;
return p;
}
ll pow2(ll a,ll b)
{
if (b==1) return a;
if (b==0) return 1;
ll p=pow2(a,b/2)%F;
p=p*p%F;
if (b&1)
{
p=(p*a)%F;
}
return p;
}
ll tot[MAXN]={0};
ll mulinv(ll a)
{
return pow2(a,F-2);
}
int main()
{
// freopen("sum.in","r",stdin);
// freopen("sum.out","w",stdout);
scanf("%d",&n);
For(i,n)
{
scanf("%s",a[i]+1); }
/*
For(i,n)
{
For(j,n) cout<<a[i][j];
cout<<endl;
}
*/
For(i,n)
{
For(j,n) tot[i]+=a[i][j]-'0'+a[j][i]-'0';
} // For(i,n) cout<<tot[i]<<endl; // cout<<mul(pow2(10,1232),mulinv(pow2(10,1232)))<<endl;
// cout<<mulinv(9); ll c9=mulinv(9); For(i,n) p10[i]=pow2(i); ll ans=0;
For(i,n)
{
ll t=sub(p10[n-i+1],1),a=tot[i];
t=mul(t,c9);
t=mul(a,t);
ans=add(ans,mul(t,i));
}
cout<<ans<<endl; return 0;
}

ACdream 1139(Sum-逆元)的更多相关文章

  1. acdream Divide Sum

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

  2. ACdream: Sum

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

  3. acdream 1431 Sum vs Product

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

  4. ACdream 1431——Sum vs Product——————【dfs+剪枝】

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

  5. ACDream - Power Sum

    先上题目: Power Sum Time Limit: 20000/10000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) S ...

  6. ACDream - Lowbit Sum

    先上题目: C - Lowbit Sum Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others ...

  7. Codeforces 963 A. Alternating Sum(快速幂,逆元)

    Codeforces 963 A. Alternating Sum 题目大意:给出一组长度为n+1且元素为1或者-1的数组S(0~n),数组每k个元素为一周期,保证n+1可以被k整除.给a和b,求对1 ...

  8. 2016 Asia Jakarta Regional Contest J - Super Sum UVALive - 7720 【快速幂+逆元】

    J-Super Sum 题目大意就是给定N个三元组<a,b,c>求Σ(a1^k1*a2^k2*...*ai^ki*..an^kn)(bi<=ki<=ci) 唉.其实题目本身不难 ...

  9. Codeforces Round #680 (Div. 2, based on Moscow Team Olympiad) D. Divide and Sum (思维,数学,逆元)

    题意:有一个长度为\(2n\)数组,从中选分别选\(n\)个元素出来组成两个序列\(p\)和\(q\),(\(p\)和\(q\)中只要有任意一个元素在\(a\)的原位置不同,就算一个新的情况),选完后 ...

随机推荐

  1. SpringBoot中使用过滤器Filter

    场景:API的参数都是经过加密的,于是在过滤器中,将获取到的请求的参数先解密再去进行处理 一.实现Filter接口 public class TestFilter implements Filter ...

  2. 【Codeforces Round #499 (Div. 2) E】Border

    [链接] 我是链接,点我呀:) [题意] 给你n个数字,每个数字可以无限用,每种方案可以组成一个和,问你%k的结果有多少种不同的结果. [题解] 相当于给你一个方程 \(x_1*a_1+x_2*a_2 ...

  3. SpringAOP之CGLIB字节码增强

    SpringAOP的基础原理就是动态代理 有两种实现方式:1)jdk动态代理 2)cglib动态代理 jdk动态代理和cglib动态代理的区别在于: cglib没有接口(通过继承父类) 只有实现类.  ...

  4. 关于android的设备管理器-DevicePolicyManager(一)

    在Andorid的设置->安全里面有个设备管理器的选项,相信大部分android用户都不太会去注意这个东西.近期在安装了一个应用之后发现这个里面的东西变了.怎么回事呢,研究研究看看.</s ...

  5. Swift基础--定位

    // // ViewController.swift // JieCoreLocation // // Created by jiezhang on 14-10-4. // Copyright (c) ...

  6. @dynamic与@synthesize的差别

    如今非常多时候我们都已经不再使用@synthesizekeyword了,可是须要了解当中的原理: 一.@dynamic与@synthesize的差别 @property有两个相应的词.一个是@synt ...

  7. 高斯滤波及高斯卷积核C++实现

    高斯滤波是一种线性平滑滤波,适用于消除高斯噪声,在图像处理的降噪.平滑中应用较多,特别是对抑制或消除服从正态分布的噪声非常有效. 高斯滤波的过程其实就是对整幅图像进行加权平均操作的过程.滤波后图像上每 ...

  8. Scalable, Distributed Systems Using Akka, Spring Boot, DDD, and Java--转

    原文地址:https://dzone.com/articles/scalable-distributed-systems-using-akka-spring-boot-ddd-and-java Whe ...

  9. APACHE KYLIN™ 概览(分布式分析引擎)

    Apache Kylin™是一个开源的分布式分析引擎,提供Hadoop/Spark之上的SQL查询接口及多维分析(OLAP)能力以支持超大规模数据,最初由eBay Inc. 开发并贡献至开源社区.它能 ...

  10. jQuery EasyUI 右键菜单--关闭标签/选项卡

    目录结构: noContextMenu.js 文件内容如下: $(function(){ //屏蔽右键菜单 $(document).bind("contextmenu", func ...