2015 ACM-ICPC 上海邀请赛 迭代解决

B.Base64

  • Problem Description
    Mike does not want others to view his messages, so he find a encode method Base64.
    Here is an example of the note in Chinese Passport.
    The Ministry of Foreign Affairs of the People’s Republic of China requests all civil and military authorities of foreign countries to allow the bearer of this passport to pass freely and afford assistance in case of need.

When encoded by Base64, it looks as follows

VGhlIE1pbmlzdHJ5IG9mIEZvcmVpZ24gQWZmYWlycyBvZiB0aGUgUGVvcGxlJ3MgUmVwdWJsaWMgb2Yg
Q2hpbmEgcmVxdWVzdHMgYWxsIGNpdmlsIGFuZCBtaWxpdGFyeSBhdXRob3JpdGllcyBvZiBmb3JlaWdu
IGNvdW50cmllcyB0byBhbGxvdyB0aGUgYmVhcmVyIG9mIHRoaXMgcGFzc3BvcnQgdG8gcGFzcyBmcmVl
bHkgYW5kIGFmZm9yZCBhc3Npc3RhbmNlIGluIGNhc2Ugb2YgbmVlZC4=

In the above text, the encoded result of The is VGhl. Encoded in ASCII, the characters T, h, and e are stored as the bytes 84, 104, and 101, which are the 8-bit binary values 01010100, 01101000, and 01100101. These three values are joined together into a 24-bit string, producing 010101000110100001100101.
Groups of 6 bits (6 bits have a maximum of 2^6 = 64 different binary values) are converted into individual numbers from left to right (in this case, there are four numbers in a 24-bit string), which are then converted into their corresponding Base64 encoded characters.
The Base64 index table is

0123456789012345678901234567890123456789012345678901234567890123
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/

In the above example, the string 010101000110100001100101 is divided into four parts 010101, 000110, 100001 and 100101, and converted into integers 21, 6, 33 and 37. Then we find them in the table, and get V, G, h, l.
When the number of bytes to encode is not divisible by three (that is, if there are only one or two bytes of input for the last 24-bit block), then the following action is performed:
Add extra bytes with value zero so there are three bytes, and perform the conversion to base64. If there was only one significant input byte, only the first two base64 digits are picked (12 bits), and if there were two significant input bytes, the first three base64 digits are picked (18 bits). ‘=’ characters are added to make the last block contain four base64 characters.
As a result, when the last group contains one bytes, the four least significant bits of the final 6-bit block are set to zero; and when the last group contains two bytes, the two least significant bits of the final 6-bit block are set to zero.
For example, base64(A) = QQ==, base64(AA) = QUE=.
Now, Mike want you to help him encode a string for k times. Can you help him?
For example, when we encode A for two times, we will get base64(base64(A)) = UVE9PQ==.

  • Input
    The first line contains an integer T(T≤20) denoting the number of test cases.
    In the following T lines, each line contains a case. In each case, there is a number k(1≤k≤5) and a string s. s only contains characters whose ASCII value are from 33 to 126(all visible characters). The length of s is no larger than 100.

  • Output
    For each test case, output Case #t:, to represent this is t-th case. And then output the encoded string.

  • Sample Input
    2
    1 Mike
    4 Mike

  • Sample Output
    Case #1: TWlrZQ==
    Case #2: Vmtaa2MyTnNjRkpRVkRBOQ==

粗暴的模拟题,但是赛场上的做法简直一根筋;
修正……

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
/**
* 2015年7月14日 下午2:56:01
* PrjName:hdu5237
* @ Semprathlon
*/
import java.io.*;
import java.util.*;

public class Main {

final static String code = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

static String encode(String str) {
String res = new String();
int ch1 = 0, ch0, pos = 0;
for (int i = 0; i < str.length(); i++) {
ch0 = ch1;
ch1 = str.charAt(i);

switch (i % 3) {
case 0:
pos = ch1 >> 2;
res += code.charAt(pos);
break;
case 1:
pos = ((ch0 & 3) << 4) + ((ch1 & ~3) >> 4);
res += code.charAt(pos);
break;
case 2:
pos = ((ch0 & 15) << 2) + ((ch1 & ~15) >> 6);
res += code.charAt(pos);
pos = ch1 & 63;
res += code.charAt(pos);
break;
}
}
if (str.length() % 3 == 2) {
ch1 = str.charAt(str.length() - 1);
pos = (ch1 & 15) << 2;
res += code.charAt(pos);
res += '=';
} else if (str.length() % 3 == 1) {
ch1 = str.charAt(str.length() - 1);
pos = (ch1 & 3) << 4;
res += code.charAt(pos);
res += "==";
}
return res;
}

public static void main(String[] args) throws IOException {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
int T = in.nextInt(), cas = 0;
while (T-- > 0) {
int k = in.nextInt();
String s = new String(in.next());
for (int i = 1; i <= k; i++)
s = encode(s);
out.println("Case #" + (++cas) + ": " + s);

}
out.flush();
out.close();
}

}

#D.Doom
本题的唯一(赛场上难以发现的)突破口就是,输入数据任给的正整数,经过不超过30次“平方再取模”操作后都成为某一定值。
也有特别的卡long long边界而不卡unsigned long long的现象,Java中不存在无符号类型因而无法实现。

#E.Exam
贪心。。。

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
/** Sep 6, 2015 8:40:50 PM
* PrjName:hdu5240
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
/**
* @param args
*/
static ArrayList<Data> vec=new ArrayList<Data>();
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 s=0,last=0;
boolean ans=true;
vec.clear();
for(int i=1;i<=n;i++){
int r=in.nextInt();
int e=in.nextInt();
int l=in.nextInt();
vec.add(new Data(r, e, l));
}
vec.sort(new DataComp());
for(int i=0;i<vec.size();i++){
int r=vec.get(i).r;
int e=vec.get(i).e;
int l=vec.get(i).l;
//out.println(r+" "+e+" "+l);
s+=r;
if (s>e){
ans=false;break;
}
s+=l;
}
out.println("Case #"+(++cas)+": "+(ans?"YES":"NO"));
}
out.flush();
out.close();
}
}
class Data{
int r,e,l;
Data(int _r,int _e,int _l){
r=_r;
e=_e;
l=_l;
}
}
class DataComp implements Comparator<Data>{
@Override
public int compare(Data d1,Data d2){
return Integer.compare(d1.e, d2.e);
}
}

#F.Friends
必须及时通过必要的暴力模拟来找出这个千呼万唤不出来的规律

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* 2015年7月27日 上午11:25:38
* PrjName:hdu5241
* @ Semprathlon
*/
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
static BigInteger TWO=new BigInteger("2");
public static void main(String[] args) {
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();
out.println("Case #"+(++cas)+": "+TWO.pow(n*5));
}
out.flush();
out.close();
}

}

#J.Joyful
赛场上耗费不少精力的题。。。
我无论是赛中还是赛后都没明白一点:对一个矩形区域涂色次数的期望,就是对该区域各点涂色期望之和。
写代码时轻视了多个int类型值相乘越界的问题

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
/** Sep 6, 2015 9:26:29 PM
* PrjName:hdu5245
* @author Semprathlon
*/
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);
int T=in.nextInt(),cas=0;
while(T-->0){
int m=in.nextInt();
int n=in.nextInt();
int k=in.nextInt();
double ans=0;
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++){
double s=0;
s+=(double)(i-1)*(i-1)*n*n;
s+=(double)(m-i)*(m-i)*n*n;
s+=(double)m*m*(j-1)*(j-1);
s+=(double)m*m*(n-j)*(n-j);
s-=(double)(i-1)*(i-1)*(j-1)*(j-1);
s-=(double)(i-1)*(i-1)*(n-j)*(n-j);
s-=(double)(m-i)*(m-i)*(j-1)*(j-1);
s-=(double)(m-i)*(m-i)*(n-j)*(n-j);
ans+=1.0-pow(s/n/n/m/m, k);
}
out.println("Case #"+(++cas)+": "+Math.round(ans));
}
out.flush();
out.close();
}
}

#A.Article
赛中没有耐心读完的题。。。赛后还读错了题意……
原来按“保存”键是不存在失败率的,这大大简化了问题模型(即可列出状态转移方程)。

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 7, 2015 6:37:43 PM
* PrjName:hdu5236
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {

/**
* @param args
*/
static double[] f;
static int n,x;
static double solve(int k){
double res=k*x;
if (n%k>0)
//res+=f[n/k]*(k-1)+f[n%k];
res+=f[n/k+1]*(n%k)+f[n/k]*(k-n%k);
else
res+=f[n/k]*k;
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);
int T=in.nextInt(),cas=0;
while(T-->0){
n=in.nextInt();
double p=in.nextDouble();
x=in.nextInt();
f=new double[n+1];
for(int i=1;i<=n;i++)
f[i]=(f[i-1]+1)/(1-p);
double ans=f[n]+x;
for(int i=2;i<=n;i++)
ans=Math.min(ans, solve(i));
out.println("Case #"+(++cas)+": "+String.format("%.6f", ans));
}
out.flush();
out.close();
}
}

Java io StreamTokenizer的使用小结

StreamTokenizer
double navl ——> 如果当前标记是一个数字,则此字段将包含该数字的值。
String sval ——> 如果当前标记是一个文字标记,则此字段包含一个给出该文字标记的字符的字符串。
static int TT_EOF ——>指示已读到流末尾的常量。
static int TT_EOL ——->指示已读到行末尾的常量。
static int TT_NUMBER——->指示已读到一个数字标记的常量。
static int TT_WORD ——-> 指示已读到一个文字标记的常量。
int ttype ——–> 在调用 nextToken() 方法之后,此字段将包含刚读取的标记的类型。
The StreamTokenizer class takes an input stream and parses it into “tokens”, allowing the tokens to be read one at a time,这些符号的拆分是按照空格来确定的。

查阅官方文档得知,它是不把数字作为字符处理的。它能取得的字符串是以大小写字母为内容的。
换行符也会被视作空格。

hdu 2894 DFS 欧拉回路

  • 这应该是《离散数学》教材中关于欧拉回路的一道例题,模型很经典。

不要拘泥于过去邻接矩阵存储形式的做法!

从效率角度上着想,能得到更佳的解决方案

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
struct Edge
{
int to,next,ve;
Edge(){}
Edge(int v,int w,int x):to(v),next(w),ve(x){}
} edge[maxn];
int head[maxn],cnt;

void addedge(int u,int v)
{
if (u==v) return;
edge[cnt]=Edge(v,head[u],v);
head[u]=cnt++;

/*edge[head[0]]=Edge(u,head[v]);
head[v]=head[0]++;*/
}

bool vis[maxn];
int n,N;
vector<int> vec;

void init()
{
memset(head,-1,sizeof(head));
cnt=0;

for(int i=0;i<N;i++)
{
//cout<<i<<':'<<(i<<1)%N<<' '<<((i<<1)|1)%N<<' '<<((N|i)>>1)<<' '<<(i>>1)<<endl;
/*mp[i][(i<<1)%N]++;
mp[i][((i<<1)|1)%N]++;
mp[i][(N|i)>>1]++;
mp[i][i>>1]++;
mp[i][i]=0;*/
addedge(i+1,(i<<1)%N+1);
addedge(i+1,((i<<1)|1)%N+1);
//addedge(i,(N|i)>>1);
//addedge(i,i>>1);
}
}

void dfs(int u)
{

for(int i=head[u];i>-1;i=edge[i].next)
{
int v=edge[i].to;
//cout<<u<<' '<<v<<endl;
if (vis[edge[i].ve]) continue;
vis[edge[i].ve]=1;
vec.push_back(edge[i].ve);
dfs(v);
}
}

int main()
{
while(~scanf("%d",&n))
{
N=1<<n;
init();
vec.clear();

CLEAR(vis,maxn);
dfs(1);
cout<<N<<' ';
//cout<<vec[0];
for(int i=0;i<vec.size();i++) cout<<(vec[i]&1);//cout<<vec[i]<<' ';
cout<<endl;
}
return 0;
}

(迟到的)湖南湘潭程序设计邀请赛后感想

来到如此遥远的地域挑战未知,少有惊喜却多有险阻。
第一个签到题D题的疏忽几乎引起我的疯狂WA。实际上只是简单的边界问题的处理(分数值等于0)胡牛和张学长的仔细列举检验奏效了。
我个人花大量精力为F题写的线段树惨烈地挂了。Lazy标记未能体现出应有的使用价值。多种不同的修改操作与统计操作的优先级,必是平时练习时漏洞密布之处。
E题简单的计算几何遭遇了一些磕绊。张学长所归纳的方程是正确的,只是计算过程中变量范围的设置不甚合理。
相比之下,A题递推的实现较为顺利。
I题和J题是既面熟又陌生的题型。尽管全队坚持到了最后一刻,却未能再争取关键的突破。
赛后听解题报告会,发现原题无不存在破解的可能性?

【150707】Blog主题更换 - Amativeness(改) → Clearision

感谢不知名的作者提供的Amativeness主题,附带了华丽的特效,更给我一个精美的CSS调试用样例。
可是面对多样化的文章、页面类型,背景有待完善。
QQ截图20150707201448
WP-Touch插件未能及时更新,失去了移动端的前台支持。
所以下定决心,寻找自适应的主题,迎来了本blog第一次真正的主题更替。
钉子菊苣设计的Clearision主题就是这样的出彩而不浮华。
QQ截图20150707202342
向着网页前台的无限设计机遇的原始森林探险。

改良Eclipse Java EE IDE,使它的界面更顺眼、更酷炫

  • 当前使用的版本是 Luna Service Release 2 (4.4.2)

从所给的【链接】 下载Dark Juno主题,它修改了Eclipse所有的视图的颜色,工具栏也变成了暗黑主题。另外还需要修改编辑器的颜色主题。
下载Eclipse的颜色主题(Color Themes)插件,可通过Eclipse的Marketplace找到Eclipse Color Theme插件。

主题包下载完成后,请解压到Eclipse安装目录的dropins子目录下。
重启Eclipse,并选择菜单”Preferences”->”General”->”Appearance”,并选择Dark Juno主题。
代码高亮的颜色主题在它的下级选项Color Theme中选择。
QQ截图20150707135444
体验到如VS一般的酷黑风格了吧?
QQ截图20150707140352

=================================================
其实本该是这个样子的,可是总有什么不一样的地方?
http://blog.csdn.net/chszs/article/details/8301884

===========2015-07补充============================
进入Settings->General->Keys,修改Context Assist项目,可提供代码补全的快捷键支持。

6月底7月初兴风作浪折腾UEFI+GPT+Clover+Mac

太久没更新博客了,原因之一是此番折腾的曲折历程!

Stage 1

在硬盘前部空余空间(上次折腾后的预留空间)建立了200MB的fat32分区,并用bcdboot命令写入了win7引导文件,炮制了一个有“水分”的EFI分区。
主板早已启用UEFI,自然重启操作系统成功。接着心一急,手一横,没切换到PE系统就动用DG专业版转换分区表格式。结果出乎意料,却合情合理,win 7直接蓝屏。
心急火燎地喊来PE,把分区表格式从GPT转回MBR,可是DG不配合工作,在即将转成时报错终止。
直到此时,看似MBR,实则硬盘分区表出了隐性故障。win7启动卡在飘动的徽标处,再也进不去了。

Stage 2

三番五次倒腾分区与分区表,多次取用先前的C盘的ghost备份镜像,没有实际作用。

Stage 3

经百度经验查实,win7及其安装光盘所带的diskpart能够安全实现MBR与GPT互转。可是代价是丢失所有分区。
赶紧备份所有必要资料,把win7安装镜像搬到U盘。较为耗时的流程。
这下分区表重建了,而后手动建立了各个所需的分区方休。
还通过电话激活解决了不少工具束手无策的额外的win7 UEFI激活问题。

Cylinder Candy - The 15th Zhejiang University Programming Contest 求积分

原题地址
比赛时卡在这种题上,很是要命

Edward the confectioner is making a new batch of chocolate covered candy. Each candy center is shaped as a cylinder with radius r mm and height h mm.

The candy center needs to be covered with a uniform coat of chocolate. The uniform coat of chocolate is d mm thick.

You are asked to calcualte the volume and the surface of the chocolate covered candy.

Input

There are multiple test cases. The first line of input contains an integer T(1≤ T≤ 1000) indicating the number of test cases. For each test case:

There are three integers r, h, d in one line. (1≤ r, h, d ≤ 100)

Output

For each case, print the volume and surface area of the candy in one line. The relative error should be less than 10^-8.

Sample Input

2
1 1 1
1 3 5

Sample Output

32.907950527415 51.155135338077
1141.046818749128 532.235830206285

Author: ZHOU, Yuchen Source: The 15th Zhejiang University Programming Contest

在底面半径r mm,高h mm的圆柱体糖块外裹上一层厚度均匀的壳,求它的体积和表面积。
设外壳厚度为$ a $ mm(只是为了避免与微分符号混淆,原题中用d表示),

求体积

  • (1)糖果顶上和底下的各一个侧面圆滑的类台体
    $ z^2+{(y-r)}^2=a^2(y\ge r),y=\sqrt {a^2-z^2} +r.$
    $$
    \begin{array}
    \newline
    V_{台体}=\int_0^a{\pi (\sqrt {a^2-z^2} +r)^2 {\rm d}z}
    \newline\left( 或V_{台体}=\int_0^a {\rm d}z\int_0^{2\pi}{\rm d}\theta \int_0^{r+a\sqrt{1-\frac{z^2}{a^2}}}r{\rm d}r \right)
    \newline=\int_0^a \pi \left(r+a\sqrt{1-\frac{z^2}{a^2}}\right)^2 {\rm d}z
    \newline=\int_0^a \left( \pi r^2+\pi a^2\left(1-\frac{z^2}{a^2}\right)+2\pi ar\sqrt{1-\frac{z^2}{a^2}} \right) {\rm d}z
    \newline=\int_0^a \left( \pi r^2+\pi a^2 \right) {\rm d}z-\int_0^a \pi z^2{\rm d}z + \int_0^a 2\pi r\sqrt{a^2-z^2}{\rm d}z
    \newline=\pi a^3+\pi ar^2-\frac{\pi a^3}{3}+\frac{\pi^2 a^2r}{2}\newline
    \end{array}
    $$

  • (2)糖果中部的圆柱体
    $ V_{圆柱}=Sh=\pi r^2h=\pi(r+a)^2h$

  • (3)总体积
    $ V=2V_{台体}+V_{圆柱}=\frac{4\pi a^3}{3}+2\pi ar^2+\pi^2a^2r+\pi(r+a)^2h $

求表面积

  • (1)求上下各一个环状曲面的面积
    这个曲面(轮胎面)的参数方程为
    $$
    \begin{cases}
    x = (r+a {\rm cos}\varphi) {\rm cos}\theta \newline
    y = (r+a {\rm cos}\varphi) {\rm sin}\theta \newline
    z = a {\rm sin}\varphi
    \end{cases}
    $$
    用向量值函数表示为:$ \vec{f}(\theta,\varphi)=x(\theta,\varphi)\vec{i}+y(\theta,\varphi)\vec{j}+z(\theta,\varphi)\vec{k}$
    该曲面上某一点(x,y,z)处的基本法向量
    $$
    \vec{n}=(-\frac{\partial z}{\partial x},-\frac{\partial z}{\partial y},1)=\frac{\partial \vec{f}}{\partial \theta}\times\frac{\partial \vec{f}}{\partial \varphi}
    $$
    积分区域$ \Sigma=$ { $ \left(\theta,\varphi \right) | 0\le \theta \le 2\pi,0\le \varphi \le\frac{\pi}{2} $ }
    $$
    \begin{array}
    \newline
    S_{环}=\iint_\Sigma {\rm d}S=\iint_\Sigma |\vec{n}| {\rm d}x{\rm d}y\newline
    =\iint_\Sigma \sqrt{\left(-\frac{\partial z}{\partial x}\right)^2+\left(-\frac{\partial z}{\partial y}\right)^2+1}{\rm d}x{\rm d}y\newline
    =\iint_\Sigma \left|\frac{\partial \vec{f}}{\partial \theta}\times\frac{\partial \vec{f}}{\partial \varphi}\right| {\rm d}x{\rm d}y\newline
    =\int_0^\frac{\pi}{2} {\rm d}\varphi \int_0^{2\pi} a(r+a {\rm cos}\varphi) {\rm d}\theta\newline
    =\int_0^\frac{\pi}{2} 2\pi a(r+a {\rm cos}\varphi){\rm d}\varphi\newline
    =2\pi a\left.(r+a {\rm cos}\varphi)\right|_0^\frac{\pi}{2}=\pi^2 ar+2\pi a^2\newline
    \end{array}
    $$

  • (2)求侧面积
    $$
    S_{侧}=2 \pi(r+a)h
    $$

  • (3)求底面积
    $$
    S_{底}=\pi r^2
    $$

  • (4)总表面积
    $$
    S=S_{环}+S_{侧}+2S_{底}=\pi^2 ar+2\pi a^2+2 \pi(r+a)h+2 \pi r^2
    $$