BestCoder

Solved Problems
ZYB’s Biology
ZYB’s Game
ZYB’s Premutation

ZYB’s Biology

平淡无奇的匹配

ZYB’s Game

惊异于“最优策略”的选取,最终结果竟与奇偶性挂钩。

ZYB’s Premutation

常规题型,旧题重演,竟然跑偏了。
半年多前的某基础题的逆向问题。
同样用线段树或树状数组解决,关键是实现查找第k大的数、删除第k大的数并维护。也有基于查询数的更抽象高效的解法。
树状数组:

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
/**
* Dec 5, 2015 8:39:53 PM
* PrjName: 1205-03-2
* @semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
static int[] a,f;
static BIT tr;
static HashSet<Integer> st=new HashSet<Integer>();
static void print(int[] a,PrintWriter out){
int n=a.length-1;
for(int i=1;i<=n;i++)
out.print(a[i]+(i<n?" ":""));
out.println();
}
public static void main(String[] args) throws IOException{
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
int T=in.nextInt();
// tr=new BIT(5);
// out.print(tr.lowbit(2));
while(T-->0){
int n=in.nextInt();
a=new int[n+1];
f=new int[n+1];
tr=new BIT(n);
for(int i=1;i<=n;i++){
f[i]=in.nextInt();
tr.add(i, 1);
}

for(int i=n;i>=1;i--){
f[i]-=f[i-1];
int tmp=tr.find(i-f[i]-1);
// out.println(i+","+f[i]+","+tmp);
a[i]=tmp;
tr.add(tmp, -1);
}
print(a, out);
}
out.flush();
out.close();
}
}
class BIT{
int[] data;
int sz;
BIT(){}
BIT(int _sz){
sz=_sz;
data=new int[sz+1];
}
int lowbit(int x){
return x&(-x);
}
void add(int p,int v){
while(p<=sz){
data[p]+=v;
p+=lowbit(p);
}
}
int sum(int p){
int res=0;
while(p>0){
res+=data[p];
p-=lowbit(p);
}
return res;
}
int find(int p){
int l=1,r=sz;
while(l<r){
int mid=(l+r)>>1;
if (sum(mid)<=p)
l=mid+1;
else
r=mid;
}

return l;
}
}

线段树:

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
/**
* Dec 6, 2015 4:42:31 PM
* PrjName: 1205-03-3
* @semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
static int[] a,f;
static ST tr;
static HashSet<Integer> st=new HashSet<Integer>();
static void print(int[] a,PrintWriter out){
int n=a.length-1;
for(int i=1;i<=n;i++)
out.print(a[i]+(i<n?" ":""));
out.println();
}
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();
a=new int[n+1];
f=new int[n+1];
tr=new ST(n);
tr.build(1, 1, n);
for(int i=1;i<=n;i++)
f[i]=in.nextInt();

for(int i=n;i>=1;i--){
f[i]-=f[i-1];
int tmp=tr.query(1, 1, n, f[i]+1);
// out.println(i+","+f[i]+","+tmp);
a[i]=tmp;

}
print(a, out);
}
out.flush();
out.close();
}
}
class ST{
int[] l,r,m,v;
int sz;
ST(){}
ST(int _sz){
sz=_sz<<2;
l=new int[sz];
r=new int[sz];
m=new int[sz];
v=new int[sz];
}
void build(int k,int x,int y){
l[k]=x;r[k]=y;m[k]=(x+y)>>1;
if (x<y){
build(k<<1,x,m[k]);
build(k<<1|1,m[k]+1,y);
}
v[k]=y-x+1;
}
int query(int k,int x,int y,int q){
if (l[k]==r[k]){
v[k]=0;
return l[k];
}
int res=0;
if (v[k<<1|1]>=q)
res=query(k<<1|1, m[k]+1, y, q);
else
res=query(k<<1,x,m[k],q-v[k<<1|1]);
v[k]=v[k<<1]+v[k<<1|1];
return res;
}

}

EC-final赛前一周

为信仰充值,与神犇赛码!年度最终决战,剑不出鞘更待何时?下周末双日,12.12~13,2015 ACM/ICPC EC-final 上海大学站,蓄势待发!全力备战

wpid-screenshot_2015-12-05-21-55-28.png wpid-230c7f4a3a36cc1f.jpg wpid-351324c2c7cdabb1.jpg wpid-screenshot_2015-12-05-21-55-05.png

wpid-screenshot_2015-12-05-22-02-33.png wpid-img_20151205_220342.jpg

BestCoder

Solved Problems To be solved
Numbers Array
Sum

Numbers

没话可说。

Sum

基础一维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
/**
* Nov 28, 2015 8:04:23 PM
* PrjName: Bc64-02
* @semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
static int[] a,s,f;
static int fn(int x){
return (1890*x+143)%10007;
}
public static void main(String[] args) throws IOException{
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
while(in.nextLine()!=null){
int n=in.nextInt();
a=new int[n+1];
s=new int[n+1];
f=new int[n+1];
for(int i=1;i<=n;i++){
a[i]=in.nextInt();
s[i]=s[i-1]+a[i];
}
int ans=s[n];
for(int i=1;i<=n;i++){
f[i]=Math.max(f[i-1]+fn(a[i]), s[i]);
ans=Math.max(ans, f[i]+s[n]-s[i]);
}
out.println(ans);
}
out.flush();
out.close();
}

}

Bestcoder

Solved Problems
Clarke and food
Clarke and five-pointed star

Clarke and food

非常轻易地完成填充。

Clarke and five-pointed star

辛辛苦苦地敲完了又挂了。。。

我这里独立思考得到的结论是:
以五角星的一个顶点为参考点,则该点必在其他四点所构成的四边形之外,且其他四点相对于该点所成的张角之和是确定的;尽管随着所取的四个点的顺序不同会有不同的值,但也仅有三种情况。
可是测样例数据时不知怎的多加了一个待判值,结果WA了

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
/**
* Nov 14, 2015 7:35:56 PM
* PrjName: Bc62-02
* @semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
static Point[] pt=new Point[5];
static PrintWriter out=new PrintWriter(System.out);
static boolean check1(){
return !Point.isPointInRect(pt[0], pt[1], pt[2], pt[3], pt[4])&&
!Point.isPointInRect(pt[1], pt[2], pt[3], pt[4], pt[0])&&
!Point.isPointInRect(pt[2], pt[3], pt[4], pt[0], pt[1])&&
!Point.isPointInRect(pt[3], pt[4], pt[0], pt[1], pt[2])&&
!Point.isPointInRect(pt[4], pt[0], pt[1], pt[2], pt[3]);
}
static boolean check0(Point p,Point a,Point b,Point c,Point d){
double v1=Math.acos(-1.0)/5.0,v2=v1*2.0,v3=v1*3.0;
double angle=Vector.angle2(p, a, b)+Vector.angle2(p, b, c)+Vector.angle2(p, c, d);
angle=Math.abs(angle);
// out.println(v1+","+v2+","+v3+","+angle);
return Point.dcmp(angle-v1)==0||Point.dcmp(angle-v2)==0||Point.dcmp(angle-v3)==0;
}
static boolean check2(){
return check0(pt[0], pt[1], pt[2], pt[3], pt[4])&&
check0(pt[1], pt[2], pt[3], pt[4], pt[0])&&
check0(pt[2], pt[3], pt[4], pt[0], pt[1])&&
check0(pt[3], pt[4], pt[0], pt[1], pt[2])&&
check0(pt[4], pt[0], pt[1], pt[2], pt[3]);
}
public static void main(String[] args) throws IOException{
InputReader in=new InputReader(System.in);

int T=in.nextInt();
while(T-->0){
for(int i=0;i<5;i++)
pt[i]=new Point(in.nextDouble(), in.nextDouble());
if (pt[0].equals(pt[1])&&pt[1].equals(pt[2])&&pt[2].equals(pt[3])&&pt[3].equals(pt[4])&&pt[4].equals(pt[0])){
out.println("Yes");continue;
}
out.println(check2()?"Yes":"No");
}
out.flush();
out.close();
}
}

Bestcoder

Solved Problems
Numbers
Game
Subtrees

Numbers

可能需要两个特判?

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
/**
* Oct 31, 2015 7:01:33 PM
* PrjName: Bc61-01
* @semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
static int maxl=1001;
static int[] f=new int[maxl];
static Vector<Integer> v=new Vector<Integer>();
public static void main(String[] args) throws IOException{
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
while(in.nextLine()!=null){
int n=in.nextInt();
// if (n<1) continue;
boolean has1=false,has2=false,has=false;;
Arrays.fill(f, 0);
for(int i=1;i<=n;i++){
int k=in.nextInt();
f[k]++;
if (f[k]>1&&k>0) has1=true;
if (f[k]>2) has2=true;
}
v.clear();
for(int i=0;i<maxl;i++)
if (f[i]>0)
v.add(i);
// for(Integer e:v.toArray(new Integer[0]))
// out.print(e+" ");
if (v.get(0)==0&&has1||has2){
out.println("YES");continue;
}
Integer[] a=v.toArray(new Integer[0]);
for(int i=0;i<a.length-1;i++)
if (!has)
for(int j=a.length-1;j>i;j--){
int k=Arrays.binarySearch(a, a[j]-a[i]);
if (k>i&&k<j){
has=true;break;
}

}
out.println(has?"YES":"NO");
}
out.flush();
out.close();
}
}

Game

各种分类讨论,但就是要写好最边缘的特判。。。

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
/**
* Oct 31, 2015 7:28:53 PM
* PrjName: Bc61-02
* @semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException{
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
while(in.nextLine()!=null){
int n=in.nextInt();
int s=in.nextInt();
int t=in.nextInt();
if (s==t){
out.println(n>1?-1:0);continue;
}
if (t==1){
if (s==n)
out.println(0);
else if (s==2)
out.println(1);
else
out.println(2);
}
else if (t==n){
if (s==1)
out.println(0);
else if (s==n-1)
out.println(1);
else
out.println(2);
}
else{
if (s==t+1||s==t-1||s==1||s==n)
out.println(1);
else
out.println(2);
}
}
out.flush();
out.close();
}

}

Subtrees

有些查找行为显然拖慢时间以致TLE。。。
有一个重要优化是从小到大枚举子树大小而不是相反方向;

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
/**
* Nov 7, 2015 10:31:31 PM
* PrjName: hdu5524-3
* @semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
static int maxn=64;
static long[] p2;
static HashSet<Long> st=new HashSet<Long>();
static PrintWriter out=new PrintWriter(System.out);
static void solve(long n){
if (n==0L) return;
st.add(n);
if (n==1L) return;
long f;
for(f=1L;;f=f<<1|1L)
if ((f>>1)<=n-1L-f&&(f<<1)>=n-1L-f)
break;
solve(f);
if ((f<<1|1)!=n)
solve(n-1-f);
}
static void init(){
p2=new long[maxn];
p2[0]=1L;
for(int i=1;i<maxn;i++){
p2[i]=p2[i-1]<<1;
p2[i-1]--;
}
}
public static void main(String[] args) throws IOException{
InputReader in=new InputReader(System.in);

init();
while(in.nextLine()!=null){
long n=in.nextLong();
st.clear();solve(n);
out.println(st.size());
}
out.flush();
out.close();
}

}

BestCoder

Solved Problems
SDOI
Reorder the Books

SDOI

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
/**
* 2015年10月10日 下午6:54:59
* PrjName:Bc59-01
* @ Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
static Person[] ps;
static ArrayList<Person> li=new ArrayList<Person>();
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();
int m=in.nextInt();
float x1=0,x2=0;
int female=0,h=0;
ps=new Person[n];
for(int i=0;i<n;i++){
ps[i]=new Person(in.next(), in.next(), in.nextInt(), in.nextInt());
female+=ps[i].sex;
if (ps[i].sex==1&&h==0) h=i;
x1=Math.max(x1, ps[i].s1);
x2=Math.max(x2, ps[i].s2);
}
li.clear();
for(int i=0;i<n;i++){
ps[i].s1*=300;ps[i].s1/=x1;
ps[i].s2*=300;ps[i].s2/=x2;
ps[i].s=ps[i].s1*(float)0.3+ps[i].s2*(float)0.7;
if (female>0){
if (ps[i].s>ps[h].s&&ps[i].sex==1) h=i;
}
else
if (ps[i].s>ps[h].s) h=i;
//li.add(ps[i]);
}
out.println("The member list of Shandong team is as follows:");
Person p0=new Person();
if (female>0){
// out.println(li.get(h).name);
p0=new Person(ps[h]);
ps[h].s=-1;
m--;
}
Arrays.sort(ps, new Person.comp());
li.clear();

for(int i=0;i<m;i++)
li.add(ps[i]);
if (female>0){
li.add(p0);m++;
}
ps=li.toArray(new Person[0]);
Arrays.sort(ps, new Person.comp());
for(int i=0;i<m;i++)
out.println(ps[i].name);

}
out.flush();
out.close();
}
}
class Person{
String name;
int sex;
float s1,s2,s;
Person(){}
Person(Person p){
name=new String(p.name);
sex=p.sex;
s1=p.s1;
s2=p.s2;
s=p.s;
}
Person(String nm,String sx,int _s1,int _s2){
name=new String(nm);
if (sx.charAt(0)=='m') sex=0;
else sex=1;
s1=_s1;s2=_s2;
}
void debug(PrintWriter out){
out.println(name+"\t"+sex+"\t"+s);
}
static class comp implements Comparator<Person> {
public int compare(Person p1,Person p2){
return -Float.compare(p1.s, p2.s);
}
}
}

Reorder the Books

思索再三,竟然连充分条件、必要条件都没找准,

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
/** Oct 13, 2015 8:55:41 PM
* PrjName:hdu5500
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
static int[] a;
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();
a=new int[n+1];
for(int i=1;i<=n;i++)
a[in.nextInt()]=i;
int ans=0;
// if (a[n]!=n&&a[n]!=1) ans++;
int k=a[n];
for(int i=n;i>1;i--)
if (a[i-1]<k){
k=a[i-1];n--;
}
else
break;
// out.println(n);
for(int i=n-1;i>1;i--){
if (a[i]==1)
continue;
boolean need=false;
for(int j=1;j<i;j++)
if (a[j]>a[i]) need|=true;
for(int j=i+1;j<=n;j++)
if (a[j]<a[i]) need|=true;
if (need){
ans++;
for(int j=1;j<=n;j++)
if (a[j]<a[i])
a[j]++;
a[i]=1;
}
}
if (a[1]!=1) ans++;
out.println(ans);
}
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
public class Main {
static int[] a;
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();
a=new int[n+1];
for(int i=1;i<=n;i++)
a[in.nextInt()]=i;
int ans=0;
int k=a[n],h=0;
for(int i=n;i>1;i--){
if (a[i-1]>k){
ans++;
a[i-1]=h--;
}
k=a[i-1];
}
if (a[1]!=h+1) ans++;
out.println(ans);
}
out.flush();
out.close();
}

}

BestCoder

1001 Clarke and minecraft

玩过MC无压力

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
/** Sep 19, 2015 7:12:36 PM
* PrjName:Bc56-01
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {

/**
* @param args
*/
final static int maxn=110;
static int cnt;
static int[] w;
static HashMap<Integer, Integer> mp=new HashMap<Integer,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();
//h=new int[maxn];
w=new int[maxn];
while(T-->0){
int n=in.nextInt();
cnt=0;
mp.clear();
Arrays.fill(w, 0);
for(int i=1;i<=n;i++){
int k=in.nextInt();
if (mp.containsKey(k))
w[mp.get(k)]+=in.nextInt();
else{
w[cnt]+=in.nextInt();
mp.put(k, cnt++);
}
}
int sum=0;
for(int i=0;i<cnt;i++)
sum+=(w[i]+63)/64;
out.println((sum+35)/36);
}
out.flush();
out.close();
}
}

1002 Clarke and problem

第一时间想到的是容斥,而后才逐渐醒悟过来是DP。。。
另外mod的遗漏又错了一发。。。

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
/** Sep 19, 2015 7:24:54 PM
* PrjName:Bc56-02
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {

/**
* @param args
*/
final static int maxn=1010;
final static int high=1000000000;
final static int mod=1000000007;
//static int[] s=new int[maxn];
static int[][] f=new int[2][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);
int T=in.nextInt();
while(T-->0){
int n=in.nextInt();
int p=in.nextInt();
//Arrays.fill(s, 0, p, 0);
Arrays.fill(f[0], 0, p, 0);
f[0][0]=1;
int cnt=0;
//for(int j=0;j<p;j++)out.print(f[j]+" ");out.println();
for(int i=1;i<=n;i++){
int k=in.nextInt();
while(k<0) k+=p;
k%=p;
Arrays.fill(f[cnt^1], 0,p,0);
for(int j=0;j<p;j++){
f[cnt^1][(k+j)%p]+=f[cnt][j];
f[cnt^1][(k+j)%p]%=mod;
}
for(int j=0;j<p;j++){
f[cnt^1][j]+=f[cnt][j];
f[cnt^1][j]%=mod;
}
cnt^=1;
}
out.println(f[cnt][0]);

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

1003 Clarke and puzzle

没有总结好Nim游戏的规律啊。。。超简单的代码

2015 ACM/ICPC Asia Regional Shenyang Online

Show All Problems

Soluble Problems
Jesus Is Here

Jesus Is Here

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
/** Sep 19, 2015 12:41:25 PM
* PrjName:0919-10
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {

/**
* @param args
*/
static long[] s,l,f,ans;
static String[] sh,sr;
final static int maxn=201315;
final static long mod=530600414L;
static long mul_mod(long n, long m, long mod) {
long ans = 0L;
n %= mod;
while (m > 0L) {
if ((m & 1L) > 0L)
ans = (ans + n) % mod;
m >>= 1;
n = (n + n) % mod;
}
return ans;
}
static void init(){
s=new long[maxn];
l=new long[maxn];
f=new long[maxn];
ans=new long[maxn];
sh=new String[maxn];
sr=new String[maxn];
s[1]=s[2]=0;s[3]=1;s[4]=3;
f[1]=f[2]=0;f[3]=f[4]=1;
l[1]=1;l[2]=2;l[3]=3;l[4]=5;
ans[1]=ans[2]=ans[3]=ans[4]=0;
sh[3]=new String("cf");sr[3]=new String("ff");
sr[4]=new String("ff");sr[4]=new String("ff");
}
static long solve(int n/*,PrintWriter out*/){
long res=0L;
if (n<5) return res;
if (ans[n]>0L) return ans[n];
res+=solve(n-2);
res+=solve(n-1);
//res+=solve(n-2,out);
//res+=solve(n-1,out);
sh[n]=sh[n-2];sr[n]=sr[n-1];
boolean has=false;
if (sr[n-2].compareTo("cf")==0&&sh[n-1].charAt(0)=='f'){
has=true;s[n]+=l[n-2]-1;
res-=s[n-2];
res+=mul_mod((l[n-2]-1),f[n-2],mod);
}
if (sr[n-2].charAt(1)=='c'&&sh[n-1].compareTo("ff")==0){
has=true;s[n]+=l[n-2];
res-=s[n-2];
res+=mul_mod(l[n-2],f[n-2],mod);
}
s[n]+=(s[n-2]+s[n-1]+mul_mod(l[n-2],f[n-1],mod))%mod;s[n]%=mod;
f[n]=has?f[n-2]+f[n-1]+1:f[n-2]+f[n-1];f[n]%=mod;
l[n]=l[n-2]+l[n-1];l[n]%=mod;
//out.print(n+":");out.print(res+" ");
res-=mul_mod(s[n-2],f[n-1],mod);//out.print(res+" ");
//res+=s[n-1]*(has?f[n-2]+1:f[n-2]);out.print(res+" ");
//res+=l[n-2]*f[n-1];out.println(res+" ");
res+=mul_mod(s[n-1]+mul_mod(l[n-2],f[n-1],mod),f[n-2],mod);//out.println(res+" ");
while(res<0L) res+=mod;
//if (has) res+=
res%=mod;
ans[n]=res;
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;
init();
for(int i=5;i<maxn;i++)
solve(i);
while(T-->0){
//long k=solve(in.nextInt(), out);
long k=solve(in.nextInt());
out.println("Case #"+(++cas)+": "+k);
}
out.flush();
out.close();
}
}

就差一点了,只剩减法取模的方法不太科学。
递归形式有利于寻找思路,改写成非递归形式更简练。