C - たくさんの数式 / Many Formulas
Time Limit: 2 sec / Memory Limit: 256 MB
Score : 300300 points
Problem Statement
You are given a string SS consisting of digits between 1
and 9
, inclusive. You can insert the letter +
into some of the positions (possibly none) between two letters in this string. Here, +
must not occur consecutively after insertion.
All strings that can be obtained in this way can be evaluated as formulas.
Evaluate all possible formulas, and print the sum of the results.
Constraints
- 1≤|S|≤101≤|S|≤10
- All letters in SS are digits between
1
and 9
, inclusive.
Input
The input is given from Standard Input in the following format:
SS
Output
Print the sum of the evaluated value over all possible formulas.
Sample Input 1 Copy
Copy
125
Sample Output 1 Copy
Copy
176
There are 44 formulas that can be obtained: 125
, 1+25
, 12+5
and 1+2+5
. When each formula is evaluated,
- 125125
- 1+25=261+25=26
- 12+5=1712+5=17
- 1+2+5=81+2+5=8
Thus, the sum is 125+26+17+8=176125+26+17+8=176.
Sample Input 2 Copy
Copy
9999999999
Sample Output 2 Copy
Copy
12656242944
题意:
n=125,ans=176
分析:
二进制枚举即可
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.TreeMap;
public class Main {
static int MOD=1000000007;
static long p[][]=new long[405][405];
static long sum[][]=new long[405][405];
static long dp[][]=new long[35][100005];
static long a[]=new long[100005];
static long b[]=new long[405];
public static void main(String[] args) {
Scanner in=new Scanner( new BufferedReader(new InputStreamReader(System.in))) ;
String s=in.next();
int len=s.length();
long sum=0;
for(int i=0;i<(1<<(len-1));i++)
{
//System.out.println("i:"+i);
int pos=0;
for(int j=0;j<len-1;j++)
{
if((i&(1<<j))!=0)
{
//System.out.println(s.substring(pos,j+1));
sum+=Long.parseLong(s.substring(pos, j+1));
pos=j+1;
}
}
//System.out.println(s.substring(pos,len));
sum+=Long.parseLong(s.substring(pos, len));
}
System.out.println(sum);
}
}
D - すぬけ君の塗り絵 / Snuke's Coloring
Time Limit: 3 sec / Memory Limit: 256 MB
Score : 400400 points
Problem Statement
We have a grid with HH rows and WW columns. At first, all cells were painted white.
Snuke painted NN of these cells. The ii-th ( 1≤i≤N1≤i≤N ) cell he painted is the cell at the aiai-th row and bibi-th column.
Compute the following:
- For each integer jj ( 0≤j≤90≤j≤9 ), how many subrectangles of size 3×33×3 of the grid contains exactly jj black cells, after Snuke painted NN cells?
Constraints
- 3≤H≤1093≤H≤109
- 3≤W≤1093≤W≤109
- 0≤N≤min(105,H×W)0≤N≤min(105,H×W)
- 1≤ai≤H1≤ai≤H (1≤i≤N)(1≤i≤N)
- 1≤bi≤W1≤bi≤W (1≤i≤N)(1≤i≤N)
- (ai,bi)≠(aj,bj)(ai,bi)≠(aj,bj) (i≠j)(i≠j)
Input
The input is given from Standard Input in the following format:
HH WW NN
a1a1 b1b1
:
aNaN bNbN
Output
Print 1010 lines. The (j+1)(j+1)-th ( 0≤j≤90≤j≤9 ) line should contain the number of the subrectangles of size 3×33×3 of the grid that contains exactly jj black cells.
Sample Input 1 Copy
Copy
4 5 8
1 1
1 4
1 5
2 3
3 1
3 2
3 4
4 4
Sample Output 1 Copy
Copy
0
0
0
2
4
0
0
0
0
0
There are six subrectangles of size 3×33×3. Two of them contain three black cells each, and the remaining four contain four black cells each.
Sample Input 2 Copy
Copy
10 10 20
1 1
1 4
1 9
2 5
3 10
4 2
4 7
5 9
6 4
6 6
6 7
7 1
7 3
7 7
8 1
8 5
8 10
9 2
10 4
10 9
Sample Output 2 Copy
Copy
4
26
22
10
2
0
0
0
0
0
Sample Input 3 Copy
Copy
1000000000 1000000000 0
Sample Output 3 Copy
Copy
999999996000000004
0
0
0
0
0
0
0
0
题意:
n * m个格子 ,k个格子是黑色,问你含有0~9块黑色格子的3*3的格子的数量。
分析:
因为最后要求多少个3*3,我们可以把每一个3*3格子的左上角的格子看作这个的代表这个矩形,转移便可以压缩了,然后用map保存这个点对其他3*3的矩形影响,注意有坑点,画一下图就明白了
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner( System.in) ;
long n=in.nextLong(),m=in.nextLong(),k=in.nextLong();
//HashMap<pointt,Long> map=new HashMap<pointt,Long>();
TreeMap<pointt,Long> map=new TreeMap<pointt,Long>();
for(int i=1;i<=k;i++)
{
int a=in.nextInt();
int b=in.nextInt();
for(int x=Math.max(1, a-2);x<=Math.min(n-2, a);x++)
for(int y=Math.max(1, b-2);y<=Math.min(m-2, b);y++)
{
pointt p=new pointt(x, y);
Long v=map.get(p);
// System.out.println(v);
if(v==null)
{
v=(long) 1;
}
else v++;
map.put(p, v);
// System.out.println(x+" "+y);
}
}
long[] ans=new long[15];
ans[0]=(n-2)*(m-2);
for(long value:map.values())
{
ans[(int) value]++;
ans[0]--;
}
for(int i=0;i<=9;i++)
{
System.out.println(ans[i]);
}
}
}
class pointt {
int x,y;
public pointt(int x, int y) {
super();
this.x = x;
this.y = y;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
pointt other = (pointt) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
public int compareTo(pointt p) {
if (this.x != p.x) {
return this.x - p.x;
}
return this.y - p.y;
}
}