【Codeforces 364A】Matrix
【链接】 我是链接,点我呀:)
【题意】
让你求出b[i][j]=s[i]*s[j]规则构成的矩阵
的所有子矩阵中子矩阵的和为a的子矩阵的个数
【题解】
(x,y,z,t)
会发现它的和就是sum(x,y)*sum(z,t)
sum(x,y)=a[x]+a[x+1]+...+a[y]
因为这个子矩阵的和就是
a[x][z]+a[x][z+1]+...+a[x][t] +
a[x+1][z] + a[x+1][z+1]....+a[x+1][t]
....
而a[i][j] = a[i]*a[j]
下次遇到这样的就动手算一算和是什么
看看能不能找出规律
注意a=0的时候的情况分类讨论一波
sum(x,y)最多就为9*|s|
因此如果a/i >9*|s|就不用管它
【代码】
import java.io.*;
import java.util.*;
public class Main {
static InputReader in;
static PrintWriter out;
public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
}
static int N = 4000;
static class Task{
int cnt[] = new int[N*10+100];
int s[] = new int[N+10];
String S;
int a,n;
public void solve(InputReader in,PrintWriter out) {
a = in.nextInt();
S = in.next();
for (int i = 0;i < (int)S.length();i++) {
s[i+1] = S.charAt(i)-'0';
}
n = S.length();
for (int i = 1;i <= n;i++) {
int temp = 0;
for (int j = i;j <= n;j++) {
temp = temp + s[j];
cnt[temp]++;
}
}
long ans = 0;
if (a==0) {
//a==0
//sum(x,y)*sum(z,t)==0
//sum(x,y)==0 sum(z,t)!=0
//or sum(x,y)!=0 sum(z,t)==0
for (int i = 1;i <=N*10;i++) {
ans = ans + 1l*2*cnt[0]*cnt[i];
}
//sum(x,y)==0 sum(z,t)==0
ans = ans + 1l*cnt[0]*cnt[0];
}else {
for (int i = 1;i <= N*10;i++) {
if (a%i==0 && a/i<=(N*10)) {
ans = ans + 1l*cnt[i]*cnt[a/i];
}
}
}
out.println(ans);
}
}
static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer;
public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
}
public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
【Codeforces 364A】Matrix的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【NOIP模拟】matrix(简化矩阵)
题目背景 SOURCE:NOIP2016-RZZ-1 题目描述 给出两个 N×N 的矩阵 A.B,矩阵每行每列标号 0-N-1 .定义这两个矩阵的乘积 AB 为
- 【POJ 3233】Matrix Power Series
[题目链接] 点击打开链接 [算法] 要求 A^1 + A^2 + A^3 + ... + A^k 考虑通过二分来计算这个式子 : 令f(k) = A^1 + A^2 + A ^ 3 + ... + ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
随机推荐
- jQuery中contains和has的区别
jQuery中contains和has的区别 根据不同的内容和属性可以准确定位到需要找的属性 如何根据内容筛选标签?:contains 匹配包含给定的文本元素$("div:co ...
- 通过CSS控制页面中的内容垂直居中的方法
方法一:通过行高(line-height)定位 line-height通常是用于调节一段文字的行与行之间的距离,或者说两行文字之间的距离,如果行高是500px,那么每一行中的文字距离本行的顶部就是25 ...
- Android 在eclipse中没有出现AVD的解决方法(转载)
转自:http://frabbit2013.blog.51cto.com/1067958/1243549 本文主要介绍在系统中成功配置好Android开发环境(即SDK is ok and ADT o ...
- E20171212-hm
odd adj. 古怪的; 奇数的; 剩余的; 临时的; odd number 奇数 even adj. 偶数的 even number 偶数
- bzoj 1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路【Floyd】
弗洛伊德之后按序列加起来即可 #include<iostream> #include<cstdio> #include<algorithm> using names ...
- json和Jsonp 使用总结(1)
1.Json的使用 $.getJSON("subPreview", { jsonDatas: JSON.stringify(jsonData) }, function(data) ...
- 题解报告:poj 2386 Lake Counting(dfs求最大连通块的个数)
Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...
- SQL server存储过程及触发器基础
存储过程:就像函数一样的会保存在数据库中-->可编程性 --> 存储过程-----------------------------------------------------创建存储过 ...
- RabbitMQ指南之四:路由(Routing)和直连交换机(Direct Exchange)
在上一章中,我们构建了一个简单的日志系统,我们可以把消息广播给很多的消费者.在本章中我们将增加一个特性:我们可以订阅这些信息中的一些信息.例如,我们希望只将error级别的错误存储到硬盘中,同时可以将 ...
- 盒子模型,top和margin-top
1. 标准盒子模型: width只是内容的宽度. 元素的总宽度=width + padding*2 +border*2 +margin*2. IE盒子模型: width=内容的宽度 + padding ...