hdu 1204 糖果大战 概率过程

hdu 1204

通过本题接触到了存在于《概率论与数理统计》教材,但是不在学校教学范围内的概率过程知识。
接受这种新的认知,才能避免落入高中局限的概率观的窠臼。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/** Sep 5, 2015 9:45:04 PM
* PrjName:hdu1204
* @author Semprathlon
*/
import java.awt.Toolkit;
import java.io.*;
import java.util.*;
public class Main {
/**
* @param args
*/
static double pow(double n,int m){
double res=1;
while(m>0){
if ((m&1)>0) res=res*n;
n=n*n;
m>>=1;
}
return res;
}
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
while(in.nextLine()!=null){
int n=in.nextInt();
int m=in.nextInt();
double p=in.nextDouble();
double q=in.nextDouble();
if (n==0)
out.println("0.00");
else if (m==0)
out.println("1.00");
else if (p==0||q==1)
out.println("0.00");
else if (p==1||q==0)
out.println("1.00");
else{
double k=q*(1.0-p)/p/(1.0-q);
double ans=p==q?n*1.0/(m+n):(1.0-pow(k, n))/(1.0-pow(k, n+m));
out.println(String.format("%.2f", ans));
}
}
out.flush();
out.close();
}
}

Bestcoder

1001 A problem of sorting

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/** Sep 5, 2015 7:07:52 PM
* PrjName:Bc54-01
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
final static int maxn=116;
static String s;
static Vector<String>[] vec=new Vector[maxn];
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
for(int i=0;i<maxn;i++)
vec[i]=new Vector<String>();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int T=Integer.parseInt(br.readLine());
while(T-->0){
for(int i=0;i<maxn;i++)
vec[i].clear();
int n=Integer.parseInt(br.readLine());
for(int i=1;i<=n;i++){
s=new String(br.readLine());
int y=0,tmp=1,j;
for(j=s.length()-1;j>0;j--)
if (s.charAt(j)>='0'&&s.charAt(j)<='9'){
y+=tmp*(s.charAt(j)-'0');
tmp*=10;
}
else
break;
s=s.substring(0, j);
vec[y-1900].add(s);
}
for(int i=maxn-1;i>=0;i--)
for(int j=0;j<vec[i].size();j++)
out.println(vec[i].get(j));
}
out.flush();
out.close();
}
}

1002 The Factor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/** Sep 5, 2015 7:30:23 PM
* PrjName:Bc54-02
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
final static int maxn=109000;
static int[] a;
static int[] pri,phi,fstp;
static void get_prime(){
pri=new int[maxn];
fstp=new int[maxn];
phi=new int[maxn];
phi[1]=1;
for(int i=2;i<maxn;i++){
if (fstp[i]==0){
pri[++pri[0]]=i;
phi[i]=i-1;
}
for(int j=1;j<=pri[0]&&i*pri[j]<maxn;j++){
int k=i*pri[j];
fstp[k]=pri[j];
//if (fstp[i]==pri[j]){
if (i%pri[j]==0){
phi[k]=phi[i]*pri[j];
break;
}
else
phi[k]=phi[i]*(pri[j]-1);
}
}
}
static int get_fstp(int n){
if (n<maxn)
return fstp[n]>0?fstp[n]:n;
for(int i=2;i<maxn;i++)
if (n%i==0)
return fstp[i]>0?fstp[i]:i;
return 0;
}
static Vector<Integer> get_prime_factor(int n){
Vector<Integer> res=new Vector<Integer>();
while(n>1&&fstp[n]>0){
res.add(fstp[n]);
n/=fstp[n];
}
if (n>1) res.add(n);
return res;
}
static Vector<Integer> v=new Vector<Integer>();
static Integer[] f;
public static void main(String[] args) throws IOException{
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
get_prime();
int T=in.nextInt();
while(T-->0){
int n=in.nextInt();
v.clear();
a=new int[n+1];
for(int i=1;i<=n;i++){
a[i]=in.nextInt();
if (a[i]==1) continue;
int tmp=get_fstp(a[i]);
v.add(tmp);
if (a[i]/tmp>1) v.add(get_fstp(a[i]/tmp));
}
/*v.sort(new Comparator<Integer>() {
public int compare(Integer o1,Integer o2){
return Integer.compare(o1, o2);
}
});*/
f=v.toArray(new Integer[0]);
Arrays.sort(f);
if (v.size()<2)
out.println(-1);
else
out.println(f[0]*f[1]);
}
out.flush();
out.close();
}
}

⬆️当时想太多了啊,何必把找到的因子全存进vector啊,白白吃了个RE……
(测试还发现HDU尚不支持Vector的sort方法,还未支持到Java7?)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/** Sep 8, 2015 16:02:23 PM
* PrjName:Bc54-02
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
final static int maxn=45000;
static int[] a;
static int[] pri,fstp;
static void get_prime(){
pri=new int[maxn];
fstp=new int[maxn];
for(int i=2;i<maxn;i++){
if (fstp[i]==0){
pri[++pri[0]]=i;
}
for(int j=1;j<=pri[0]&&i*pri[j]<maxn;j++){
int k=i*pri[j];
fstp[k]=pri[j];
if (i%pri[j]==0)
break;
}
}
}
static int get_fstp(int n){
if (n<maxn)
return fstp[n]>0?fstp[n]:n;
for(int i=2;i<maxn;i++)
if (n%i==0)
return fstp[i]>0?fstp[i]:i;
return n;
}
static int max1,max2,sum;
static void init(){
max1=max2=0x3fffffff;sum=0;
}
static void update(int s){
if (s<max1){
max2=max1;max1=s;
}
else
max2=Math.min(max2, s);
sum++;
}
public static void main(String[] args) throws IOException{
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
get_prime();
int T=in.nextInt();
while(T-->0){
init();
int n=in.nextInt();
a=new int[n+1];
for(int i=1;i<=n;i++){
a[i]=in.nextInt();
if (a[i]==1) continue;
int tmp=get_fstp(a[i]);
update(tmp);
if (a[i]/tmp>1) update(get_fstp(a[i]/tmp));
}
if (sum<2)
out.println(-1);
else
out.println(max1*(long)max2);
}
out.flush();
out.close();
}
}

1003 Geometric Progression

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/** Sep 5, 2015 8:05:33 PM
* PrjName:Bc54-03
* @author Semprathlon
*/
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
final static int maxn=101;
static BigInteger a[]=new BigInteger[maxn];
public static void main(String[] args) throws IOException{
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
int T=in.nextInt();
while(T-->0){
int n=in.nextInt();
boolean zero=false,nonzero=false;
for(int i=1;i<=n;i++){
a[i]=new BigInteger(in.next());
if (a[i].equals(BigInteger.ZERO))
zero|=true;
else
nonzero|=true;
}
boolean ans=(zero&&nonzero)?false:true;
if (ans)
for(int i=2;i<n;i++)
if (!a[i].pow(2).equals(a[i-1].multiply(a[i+1]))){
ans=false;
break;
}
out.println(ans?"Yes":"No");
}
out.flush();
out.close();
}
}

不明不白地送了个WA,没有考虑好数列中值为0的项。
这题不是暑假多校才做过的么?

2014 ACM/ICPC Asia Regional Anshan Online

hdu 5001 Walk 概率dp
其实就是这么直白,正难则反

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/** Sep 2, 2015 8:29:38 PM
* PrjName:hdu5001
* @author Semprathlon
*/
import java.io.*;
import java.util.*;

public class Main {

/**
* @param args
*/
static int n, m, d;
static int[] deg;
static int[][] u;
static double[][] f;

static double solve(int x) {
f = new double[d + 1][n + 1];
for (int i = 1; i <= n; i++)
f[0][i] = 1.0 / n;
for (int i = 1; i <= d; i++)
for (int j = 1; j <= n; j++) {
if (j == x)
continue;
for (int k = 1; k <= u[j][0]; k++)
f[i][u[j][k]] += f[i - 1][j] / u[j][0];
}
double ans = 0;
for (int i = 1; i <= n; i++)
if (i != x)
ans += f[d][i];
return ans;
}

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int T = in.nextInt();
while (T-- > 0) {
n = in.nextInt();
m = in.nextInt();
d = in.nextInt();
u = new int[n + 1][m + 1];
deg = new int[n + 1];
for (int i = 1; i <= m; i++) {
int x = in.nextInt();
deg[x]++;
int y = in.nextInt();
deg[y]++;
u[x][++u[x][0]] = y;
u[y][++u[y][0]] = x;
}

for (int i = 1; i <= n; i++)
out.println(String.format("%.6f", solve(i)));
}
out.flush();
out.close();
}

}

不知为何,printf+格式字符串会出现莫名的PE

hdu 4998 Rotate

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/** Sep 2, 2015 5:58:24 PM
* PrjName:hdu4998
* @author Semprathlon
*/
import java.io.*;
import java.util.*;

public class Main {

/**
* @param args
*/
final static double eps = 1e-5;
final static double pi = Math.acos(-1.0);

static int dcmp(double d) {
if (Math.abs(d) < eps)
return 0;
return d > 0 ? 1 : -1;
}

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int T = in.nextInt();
while (T-- > 0) {
int n = in.nextInt();
Point p1 = new Point(0, 0), q1 = new Point(p1);
Point p2 = new Point(0, 1), q2 = new Point(p2);
for (int i = 1; i <= n; i++) {
double x = in.nextDouble();
double y = in.nextDouble();
double a = in.nextDouble();
q1 = q1.rotate(x, y, a);
q2 = q2.rotate(x, y, a);
}
Vector v1 = new Vector(p1, q1).normal();
Vector v2 = new Vector(p2, q2).normal();
Point o = Vector.GetLineIntersection(Point.mid(p1, q1), v1, Point.mid(p2, q2), v2);
double ans = Vector.angle(o, p1, q1);
if (Vector.cross(new Vector(o, p1), new Vector(o, q1)) < 0)
ans = 2 * pi - ans;
out.printf("%.5f %.5f %.5f\n", o.x, o.y, ans);
}
out.flush();
out.close();
}

static class Point {
double x, y;

Point() {
}

Point(double _x, double _y) {
x = _x;
y = _y;
}

Point(Point p) {
this(p.x, p.y);
}

boolean equals(Point p) {
return dcmp(x - p.x) == 0 && dcmp(y - p.y) == 0;
}

Point add(Point r) {
return new Point(x + r.x, y + r.y);
}

Point sub(Point r) {
return new Point(x - r.x, y - r.y);
}

Point mul(double r) {
return new Point(x * r, y * r);
}

Point move(double dx, double dy) {
return new Point(x + dx, y + dy);
}

Point rotate(double a) {
return new Point(x * Math.cos(a) - y * Math.sin(a), x * Math.sin(a) + y * Math.cos(a));
}

Point rotate(double dx, double dy, double a) {
return this.move(-dx, -dy).rotate(a).move(dx, dy);
}

static Point mid(Point a, Point b) {
return new Point((a.x + b.x) / 2.0, (a.y + b.y) / 2.0);
}
}

static class Vector extends Point {
Vector(double _x, double _y) {
x = _x;
y = _y;
}

Vector(Point a, Point b) {
this(b.x - a.x, b.y - a.y);
}

Vector(Point p) {
this(p.x, p.y);
}

static double angle(Vector a, Vector b) {
return Math.acos(dot(a, b) / a.length() / b.length());
}

static double angle(Point o, Point a, Point b) {
return angle(new Vector(o, a), new Vector(o, b));
}

double dot(Vector r) {
return x * r.x + y * r.y;
}

double cross(Vector r) {
return x * r.y - y * r.x;
}

double length() {
return Math.sqrt(this.dot(this));
}

Vector normal() {
double len = this.length();
return new Vector(-y / len, x / len);
}

static Point GetLineIntersection(Point p, Vector v, Point q, Vector w) {// 求直线交点
Vector u = new Vector(p.sub(q));
double t = cross(w, u) / cross(v, w);
return p.add(v.mul(t));
}

static Point GetLineIntersection(Point p, Point v, Point q, Point w) {
return GetLineIntersection(p, new Vector(v), q, new Vector(w));
}

static Vector add(Vector a, Vector b) {
return new Vector(a.add(b));
}

static double dot(Vector a, Vector b) {
return a.dot(b);
}

static double cross(Vector a, Vector b) {
return a.cross(b);
}

static double cross(Point o, Point a, Point b) {
return cross(new Vector(o, a), new Vector(o, b));
}

static double length(Vector r) {
return r.length();
}
}
}

说得好像很繁的样子

hdu 5000

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/** Sep 3, 2015 9:43:43 PM
* PrjName:hdu5000
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {

/**
* @param args
*/
final static int mod=1000000007;
static int[] a,f;
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
int T=in.nextInt();
while(T-->0){
int n=in.nextInt();
int sum=0;
a=new int[n+1];
for(int i=1;i<=n;i++){
a[i]=in.nextInt();
sum+=a[i];
}
sum>>=1;
f=new int[sum+1];
f[0]=1;
for(int i=1;i<=n;i++)
for(int j=sum;j>=0;j--)
for(int k=1;k<=j&&k<=a[i];k++){
f[j]+=f[j-k];
f[j]%=mod;
}
out.println(f[sum]);
}
out.flush();
out.close();
}

}
class InputReader{
public BufferedReader reader;
public StringTokenizer tokenizer;

public InputReader(InputStream stream){
reader = new BufferedReader(
new InputStreamReader(stream), 32768);
tokenizer = null;
}

public String next(){
while(tokenizer == null || !tokenizer.hasMoreTokens()){
try{
tokenizer = new StringTokenizer(
reader.readLine());
}catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}

public int nextInt() {
return Integer.parseInt(next());
}

public long nextLong() {
return Long.parseLong(next());
}

}

非常奇怪,找到sum/2可得最优解这个规律就算是成功了一半了

hdu 2089 数位dp

不要62

具有教科书性质的数位dp应用实例。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/** Aug 31, 2015 9:57:30 PM
* PrjName:hdu2089
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
/**
* @param args
*/
final static int maxl=7;
static int[][] f;
static int[] digit;
static void init(){
f=new int[maxl+1][10];
f[0][0]=1;
for(int i=1;i<=maxl;i++)//从低位到高位!
for(int j=0;j<10;j++)
for(int k=0;k<10;k++)
if (k!=4&&!(j==6&&k==2))
f[i][j]+=f[i-1][k];
}
static int solve(int n){
//if (n==0) return 1;
digit=new int[maxl+1];
while(n>0){
digit[++digit[0]]=n%10;
n/=10;
}
int res=0;
for(int i=digit[0];i>0;i--){//从高位到低位!
for(int j=0;j<digit[i];j++)
if(j!=4&&!(j==2&&digit[i+1]==6))//限制前j位,枚举后digit[0]-j位
res+=f[i][j];
if(digit[i]==4||digit[i]==2&&digit[i+1]==6) break;
}
return res;
}
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
StreamTokenizer in=new StreamTokenizer(new BufferedInputStream(System.in));
PrintWriter out=new PrintWriter(System.out);
init();
while(in.nextToken()!=StreamTokenizer.TT_EOF){
int n=(int)in.nval;
in.nextToken();
int m=(int)in.nval;
if (n==0&&m==0) break;
//out.println(solve(n)+" "+solve(m+1));
out.println(solve(m+1)-solve(n));
}
out.flush();
out.close();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/** Sep 1, 2015 8:57:53 PM
* PrjName:hdu2089-2
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
/**
* @param args
*/
final static int maxl = 8;
static int[][] f, g;
static int[] digit;
static void init() {
f = new int[maxl + 1][10];
f[0][0] = 1;
g = new int[maxl + 1][10];
g[0][0] = 1;
}
static void getd(int n) {
digit = new int[maxl + 1];
while (n > 0) {
digit[++digit[0]] = n % 10;
n /= 10;
}
}
static int dfs(int d, int r, boolean c) {
if (d == 0) {
if (c)
f[d + 1][r]++;
return 0;
}
if (!(f[d + 1][r] > 0)) {
int add = 0;
int up = c ? 9 : digit[d];
for (int i = 0; i <= up; i++)
if (!(r == 6 && i == 2)) {
if (!(f[d][i] > 0))
dfs(d - 1, i, c || i != up);
if (i != 4)
add += f[d][i];
}
f[d + 1][r] += add;
}
int res = digit[d] == 4 || digit[d] == 2 && digit[d + 1] == 6 ? 0 : dfs(d - 1, 0, false);
for (int i = 0; i < digit[d]; i++)
if (i != 4 && !(i == 2 && digit[d + 1] == 6))
res += f[d][i];
return res;
}
static int solve(int n) {
int res = 0;
getd(n);
return dfs(digit[0], 0, true);
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
StreamTokenizer in = new StreamTokenizer(new BufferedInputStream(System.in));
PrintWriter out = new PrintWriter(System.out);
init();

while (in.nextToken() != StreamTokenizer.TT_EOF) {
int n = (int) in.nval;
in.nextToken();
int m = (int) in.nval;
if (n == 0 && m == 0)
break;
out.println(solve(m + 1) - solve(n));
}
out.flush();
out.close();
}
}
  • 为何生成与统计分两步走?
    因为前i+1位的各种情况以前i位为依据生成,不能打断这个递推的连续性。
    待统计时再筛选所需。
  • 为何solve(m+1)?
    便捷化处理,搜集<m+1的答案即≤m的答案。
  • 为何这个记忆化搜索极易写挂?
    许多人根据数字的可选或不可选来划分状态,而此处为了清晰的体现数位的筛选辄以数字为状态。
    状态的更新与统计仍是分离的。

Bestcoder

迟到了半小时开打,不然rank还可以更好看点……

1001 Rikka with Graph

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/** Aug 29, 2015 7:39:29 PM
* PrjName:Bc53-01
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {

/**
* @param args
*/
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
StreamTokenizer in=new StreamTokenizer(new BufferedInputStream(System.in));
PrintWriter out=new PrintWriter(System.out);
while(in.nextToken()!=StreamTokenizer.TT_EOF){
int n=(int)in.nval;
in.nextToken();
int m=(int)in.nval;
boolean has=false;
//out.println(n+"-"+m);
for(int i=1;i<=m;i++){
in.nextToken();
int u=(int)in.nval;
in.nextToken();
int v=(int)in.nval;
if (u==1&&v==n||u==n&&v==1)
has=true;
}
if (has)
out.println(1+" "+n*(n-1)/2);
else
out.println(1+" "+1);
}
out.flush();
out.close();
}
}

如果点1与点n直接相连,那么就是距离最短了。

1002 Rikka with Tree

要同时满足不同与相似,就是要求结点数相等,各结点到树根1的距离分别相等(即深度分别相同),且存在父节点不同的结点。
可以说就是判断同一深度上的结点互换后是否能得到新的结构。
若有某一深度上的结点数多于一个,那么大概可以通过同层互换获得新的结构。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/** Aug 29, 2015 7:59:14 PM
* PrjName:Bc53-02
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {

/**
* @param args
*/
final static int maxn=2010;
static Graph G=new Graph(maxn);
static int[] dep;
static HashSet<Integer> st=new HashSet<Integer>();
static void bfs(int st){
Queue<Integer> que=new LinkedList<Integer>();
que.add(st);
while(!que.isEmpty()){
int u=que.poll();
for(int i=G.h[u];i>-1;i=G.edge[i].next){
int v=G.edge[i].to;
if (dep[v]>0) continue;
dep[v]=dep[u]+1;
que.add(v);
}
}
}
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
StreamTokenizer in=new StreamTokenizer(new BufferedInputStream(System.in));
PrintWriter out=new PrintWriter(System.out);
while(in.nextToken()!=StreamTokenizer.TT_EOF){
int n=(int)in.nval;
G.clear();
dep=new int[n+1];
dep[1]=1;
for(int i=1;i<n;i++){
in.nextToken();
int u=(int)in.nval;
in.nextToken();
int v=(int)in.nval;
G.add(u, v);
G.add(v, u);
}
bfs(1);
boolean ans=true;
st.clear();
for(int i=1;i<=n;i++)
if (st.contains(dep[i])){
ans=false;
break;
}
else
st.add(dep[i]);
out.println(ans?"YES":"NO");
}
out.flush();
out.close();
}

}
class Edge{
int to,next;
Edge(int _u,int _v){
to=_u;next=_v;
}
}
class Graph{
int[] h;
int sz;
Edge[] edge;
Graph(int size){
sz=size;
h=new int[sz+1];
edge=new Edge[sz+1];
Arrays.fill(h, -1);
h[0]=0;
}
void clear(){
h=new int[sz+1];
edge=new Edge[sz+1];
Arrays.fill(h, -1);
h[0]=0;
}
void add(int u,int v){
edge[h[0]]=new Edge(v,h[u]);
h[u]=h[0]++;
}
}

然而错了。
适当地列举一些情况,发现若树的深度仅有2,那么它始终是特殊的。
可是加上这一判断仍不够。
进一步的举例发现,若树上非最底层有某一层结点多于一个,就能变换形成新结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/** Aug 29, 2015 7:59:14 PM
* PrjName:Bc53-02
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {

/**
* @param args
*/
final static int maxn=2010;
static Graph G=new Graph(maxn);
static int[] dep;
static HashSet<Integer> st=new HashSet<Integer>();
static void bfs(int st){
Queue<Integer> que=new LinkedList<Integer>();
que.add(st);
while(!que.isEmpty()){
int u=que.poll();
for(int i=G.h[u];i>-1;i=G.edge[i].next){
int v=G.edge[i].to;
if (dep[v]>0) continue;
dep[v]=dep[u]+1;
que.add(v);
}
}
}
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
StreamTokenizer in=new StreamTokenizer(new BufferedInputStream(System.in));
PrintWriter out=new PrintWriter(System.out);
while(in.nextToken()!=StreamTokenizer.TT_EOF){
int n=(int)in.nval;
G.clear();
dep=new int[n+1];
dep[1]=1;
for(int i=1;i<n;i++){
in.nextToken();
int u=(int)in.nval;
in.nextToken();
int v=(int)in.nval;
G.add(u, v);
G.add(v, u);
}
bfs(1);
boolean ans=true;
st.clear();
int maxd=0;
for(int i=1;i<=n;i++)
maxd=Math.max(dep[i], maxd);
for(int i=1;i<=n;i++)
if (st.contains(dep[i])){
if (dep[i]<maxd){
ans=false;
break;
}
}
else
st.add(dep[i]);
if (maxd<3)
ans=true;
out.println(ans?"YES":"NO");
}
out.flush();
out.close();
}

}
class Edge{
int to,next;
Edge(int _u,int _v){
to=_u;next=_v;
}
}
class Graph{
int[] h;
int sz;
Edge[] edge;
Graph(int size){
sz=size;
h=new int[sz+1];
edge=new Edge[sz+1];
Arrays.fill(h, -1);
h[0]=0;
}
void clear(){
h=new int[sz+1];
edge=new Edge[sz+1];
Arrays.fill(h, -1);
h[0]=0;
}
void add(int u,int v){
edge[h[0]]=new Edge(v,h[u]);
h[u]=h[0]++;
}
}

这与题解中所说的“一棵树是独特的当且仅当任意处于每一个深度的点数是1 1 1 1 … 1 1 x”相符(我个人将这种形态称作“呈扫帚状”)

hdu 3388 Coprime 容斥原理 二分查找

Coprime

刚开始不知道用二分,因为没有发现序列单调不下降的性质;
留意二分查找需找到下界。
二分查找起点的右边界取m*n是不够的,实际查找到的结果可能远大于该值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/** Aug 27, 2015 9:25:32 PM
* PrjName:hdu3388
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {

/**
* @param args
*/
static Vector<Integer> v1=new Vector<Integer>();
static Vector<Integer> v2=new Vector<Integer>();
static HashSet<Integer> st=new HashSet<Integer>();
static Integer[] fac=new Integer[0];
static Vector<Integer> get_prime_factor(int n){
Vector<Integer> res=new Vector<Integer>();
res.clear();
for(int i=2;i*i<=n;i++)
if (n%i==0){
res.add(i);
while(n%i==0)
n/=i;
}
if (n>1) res.add(n);
return res;
}
static long check(long n){
long res=0L;
int m=fac.length;
for(int i=1;i<(1L<<m);i++){
long tmp=1L;
boolean tag=false;
for(int j=0;j<m;j++)
if (((1L<<j)&i)>0L){
tmp*=fac[j].longValue();
tag^=true;
}
res+=tag?n/tmp:-n/tmp;
}
return n-res;
}
static long bisearch(long low,long high,long key){
long l=low,r=high,mid;
while(l<r){
mid=(l+r)>>1;
if (check(mid)>=key)
r=mid;
else
l=mid+1;
}
return l;
}
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
int T=in.nextInt(),cas=0;
while(T-->0){
int m=in.nextInt();
int n=in.nextInt();
int k=in.nextInt();
v1=get_prime_factor(m);
v2=get_prime_factor(n);
st.clear();
for(Integer e:v1.toArray(new Integer[0]))
st.add(e);
for(Integer e:v2.toArray(new Integer[0]))
st.add(e);
fac=st.toArray(new Integer[0]);
out.println("Case "+(++cas)+": "+bisearch(1, 0x3f3f3f3f3f3f3f3fL, k));
}
out.flush();
out.close();
}

}

较为神奇的是,把以上代码的check()函数(实现容斥原理的计算)替换为以下实现后,效率大有提升。

1
2
3
4
5
6
static long check(long n,int low){
long sum=0L;
for(int i=low;i<fac.length;i++)
sum+=n/fac[i].longValue()-check(n/fac[i].longValue(),i+1);
return sum;
}

hdu 4059 The Boss on Mars 容斥原理 数列通项公式

The Boss on Mars

为了高效求出数列的项 $ a_i=\sum\limits^n_{i=1} i^4 $,不使用通项公式是无法实现的。
另外实际分解所得的质因数的种类并不多,无需浪费大量时间、空间筛选大质数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/** Aug 27, 2015 2:45:37 PM
* PrjName:hdu4059
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
static PrintWriter out=new PrintWriter(System.out);
static int maxn=105;
static Vector<Integer> vec=new Vector<Integer>();
static Vector<Integer> get_prime_factor(int n){
Vector<Integer> res=new Vector<Integer>();
for(int i=2;i*i<=n;i++)
if (n%i==0){
res.add(i);
while(n%i==0)
n/=i;
}
if (n>1) res.add(n);
return res;
}
final static long mod=1000000007L;
static long pow(long n,long m,long mod){
long res=1L;
while(m>0L){
if ((m&1L)>0L) res=res*n%mod;
n=n*n%mod;
m>>=1;
}
return res;
}
static long div(long a,long b,long mod){
return a*pow(b,mod-2,mod)%mod;
}
static long sum(int n,long mod){
long res=n;
res*=2*n+1;res%=mod;
res*=n+1;res%=mod;
res*=(3L*n*n+3*n-1)%mod;res%=mod;
return div(res,30,mod);
}
static long solve(Vector<Integer> vec,int n){
long res=0L;
int m=vec.size();
for(int i=1;i<(1L<<m);i++){
boolean tag=false;
int tmp=1;
for(int j=0;j<m;j++)
if (((1L<<j)&i)>0){
tmp*=vec.get(j);
tmp%=mod;
tag^=true;
}
res=res+(tag?1:-1)*(pow(tmp,4,mod)*sum(n/tmp,mod)%mod);
res%=mod;
}
return res;
}
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
InputReader in=new InputReader(System.in);
int T=in.nextInt();
while(T-->0){
int n=in.nextInt();
Vector<Integer> v=get_prime_factor(n);
out.println((sum(n,mod)-solve(v,n)+mod)%mod);
}
out.flush();
out.close();
}

}

另有一个未知的通过矩阵快速幂计算该数列项的方法(求以下矩阵的n次方)。

1 1 4 6 4 1
0 1 4 6 4 1
0 0 1 3 3 1
0 0 0 1 2 1
0 0 0 0 1 1
0 0 0 0 0 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void init()  
{
int i,j;
//构造矩阵
ONE.mat[1][1]=1;ONE.mat[1][2]=1;ONE.mat[1][3]=4;
ONE.mat[1][4]=6;ONE.mat[1][5]=4;ONE.mat[1][6]=1;
ONE.mat[2][1]=0;ONE.mat[2][2]=1;ONE.mat[2][3]=4;
ONE.mat[2][4]=6;ONE.mat[2][5]=4;ONE.mat[2][6]=1;
ONE.mat[3][1]=0;ONE.mat[3][2]=0;ONE.mat[3][3]=1;
ONE.mat[3][4]=3;ONE.mat[3][5]=3;ONE.mat[3][6]=1;
ONE.mat[4][4]=1;ONE.mat[4][5]=2;ONE.mat[4][6]=1;
ONE.mat[5][4]=0;ONE.mat[5][5]=1;ONE.mat[5][6]=1;
ONE.mat[6][6]=1;
yu[0]=0;
yu[1]=1;
LL x;
for(i=2;i<maxn;i++)//预处理1~200W的sum[x]
{
x=i%MOD;
x=x*i;
x=x%MOD;
x=x*i;
x=x%MOD;
x=x*i;
x=x%MOD;
yu[i]=yu[i-1]+x;
yu[i]=yu[i]%MOD;
}
AA[1]=ONE;
for(i=2;i<30;i++)AA[i]=AA[i-1]*AA[i-1];
}

最后以这种方式取得计算结果(再乘上以下矩阵):

1 0 0 0 0 0
1 0 0 0 0 0
1 0 0 0 0 0
1 0 0 0 0 0
1 0 0 0 0 0
1 0 0 0 0 0
1
2
3
4
5
6
7
8
9
10
matrix A;  
LL kan(LL x)//求sum[x]
{
if(x<maxn)return yu[x];
matrix OP;
for(int i=1;i<=6;i++)OP.mat[i][1]=1;
A=powmul(ONE,x-1);
OP=A*OP;
return OP.mat[1][1];
}

hdu 4135 Co-prime 容斥原理

hdu 4135

具有教科书性质的容斥原理应用实例。
能不重复、不遗漏地选出所有合数,也就能得到质数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/** Aug 26, 2015 9:40:09 PM
* PrjName:hdu4135
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
static int maxn=1001;
static int[] pri,fstp;
static Vector<Integer> vec=new Vector<Integer>();
static void get_prime(){
pri=new int[maxn];
fstp=new int[maxn];
for(int i=2;i<maxn;i++){
if (fstp[i]==0){
pri[++pri[0]]=i;
}
for(int j=1;j<=pri[0]&&i*pri[j]<maxn;j++){
int k=i*pri[j];
fstp[k]=pri[j];
if (i%pri[j]==0)
break;
}
}
}
static Vector<Integer> get_prime_factor(int n){
Vector<Integer> res=new Vector<Integer>();
res.clear();
for(int i=1;i<=pri[0]&&pri[i]*pri[i]<=n;i++)
if (n%pri[i]==0){
res.add(pri[i]);
while(n%pri[i]==0)
n/=pri[i];
}
if (n>1) res.add(n);
return res;
}
static long solve(long n,Vector<Integer> vec){
long res=0L;
final int m=vec.size();
for(long i=1L;i<(1L<<m);i++){
boolean tag=false;
long tmp=1L;
for(int j=0;j<m;j++)
if (((1L<<j)&i)>0){
tag^=true;
tmp*=vec.get(j).longValue();
}
res+=tag?n/tmp:-n/tmp;
}
return n-res;
}
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
get_prime();
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
int T=in.nextInt(),cas=0;
while(T-->0){
long a=in.nextLong();
long b=in.nextLong();
int n=in.nextInt();
vec=get_prime_factor(n);
out.println("Case #"+(++cas)+": "+(solve(b,vec)-solve(a-1,vec)));
}
out.flush();
out.close();
}
}

2013 ACM/ICPC Asia Regional Chengdu Online

1007 F(x)数位DP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/** Aug 22, 2015 5:23:42 PM
* PrjName:hdu4734
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {

/**
* @param args
*/
static int[][] f;
static int[] digit;
final static int maxm=4600;
static int fun(int n){
int res=0,tmp=1;
while(n>0){
res+=n%10*tmp;
tmp<<=1;
n/=10;
}
return res;
}
static void init(){
int tmp=1;
f=new int[10][maxm+1];
f[0][0]=1;
for(int i=1;i<10;i++){
for(int j=0;j<=maxm;j++)
for(int k=0;k<=9;k++)
if (j+tmp*k<=maxm)
f[i][j+tmp*k]+=f[i-1][j];
tmp<<=1;
}
for(int i=0;i<10;i++)
for(int j=1;j<=maxm;j++) f[i][j]+=f[i][j-1];
}
static int[] getdg(int n){
int[] res=new int[10];
while(n>0){
res[++res[0]]=n%10;
n/=10;
}
return res;
}
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
init();
int T=in.nextInt(),cas=0;
while(T-->0){
int A=in.nextInt();
int B=in.nextInt();
int m=fun(A);
digit=getdg(B);
int ans=0,tmp=1<<digit[0];
for(int i=digit[0];i>=1;i--){
tmp>>=1;
for(int k=0;k<digit[i];k++){

if (m-k*tmp>=0){
ans+=f[i-1][m-k*tmp];
}
}
m-=digit[i]*tmp;
if (m<0) break;

}
if (m>=0) ans++;
out.println("Case #"+(++cas)+": "+ans);
}
out.flush();
out.close();
}

}

磕磕绊绊的,到底有几大难?
期初是有过转化为背包问题的尝试,但是忽视了数位DP的处理手段;可先扩大枚举范围,再从中筛选。
预处理的两套循环不能杂糅在一起,因为先通过递推求了第i位、限制F(x)==j的解,然后才相加得到第i位、限制F(x)≤j的解。
筛选时注意“小于”的枚举方式。
最后还应留意B为可行解的条件。

1010 A Bit Fun

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/** Aug 22, 2015 4:28:18 PM
* PrjName:hdu4737
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {

/**
* @param args
*/
static int[] a;
static Queue<Integer> que=new LinkedList<Integer>();
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
int T=in.nextInt(),cas=0;
while(T-->0){
int n=in.nextInt();
int m=in.nextInt();
a=new int[n+1];
que.clear();
int ans=0;
for(int i=1;i<=n;i++){
a[i]=in.nextInt();
int k=que.size();
for(int j=0;j<k;j++){
int tmp=que.poll()|a[i];
if (tmp<m) que.add(tmp);
}
if (a[i]<m) que.add(a[i]);
ans+=que.size();
}
out.println("Case #"+(++cas)+": "+ans);
}
out.flush();
out.close();
}

}

并没有实现有些题解中所说的O(nlogn)或O(30n)的算法,但总的运行时间还是较短的?

OS X下的一些个性化设置

defaults write com.apple.screencapture location /path/

killall SystemUIServer

  • Finder显示隐藏文件

defaults write com.apple.finder AppleShowAllFiles -bool true

  • Finder显示全部驱动器
Screen Shot 2015-08-23 at 11.15.30
  • Dashboard不显示为单独页面
Screen Shot 2015-08-23 at 11.17.10
  • 调整屏幕亮度等常用功能快捷键
Screen Shot 2015-08-23 at 11.18.34
  • 允许未知来源Application的运行
Screen Shot 2015-08-23 at 11.20.43 Screen Shot 2015-08-23 at 11.20.59
  • 管理开机启动项

Snip20151103_3