历届蓝桥杯真题答案

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

历届试题 核桃的数量

请选择编译语言: C

本题的C参考代码如下:

1.#include
2.#include
3.
4.//最小公倍数
5.int LCM(int num1,int num2,int num3)
6.{
7. int value=num1;
8. while(value%num1!=0||value%num2!=0||value%num3!=0)
9. {
10. value+=num1;
11. }
12. return value;
13.}
14.
15.int main()
16.{
17. int num1,num2,num3;
18. scanf("%d%d%d",&num1,&num2,&num3);
19. printf("%d\n",LCM(num1,num2,num3));
20. return 0;
21.}

历届试题 打印十字图

请选择编译语言: C
本题的C参考代码如下:

1.#include
2.#include
3.#include
4.int main()
5.{
6. int n,w,h,l,i,j,x,y,m;
7. char *arry;
8. scanf("%d",&n);
9. w=h=5+n*4;
10. arry=(char *)malloc(w*h);
11. memset(arry,'.',w*h);
12. /*
13. for(i=n*2;i14. {
15. arry[i+((n+1)*2)*w]='$';
16. arry[i*w+(n+1)*2]='$';
17. }*/
18. for(m=0;m<=n;m++)//画没一层
19. {
20. for(i=(m+1)*2;i21. {
22. x=m*2;
23. y=i;
24. arry[x+y*w]='$';
25. x=w-m*2-1;
26. y=i;
27. arry[x+y*w]='$';
28. x=i;
29. y=m*2;
30. arry[x+y*w]='$';
31. x=i;
32. y=w-m*2-1;
33. arry[x+y*w]='$';
34. }
35. for(i=m*2;i<=(m+1)*2;i++)//角
36. {
37. x=i;
38. y=(m+1)*2;
39. arry[x+y*w]='$';
40. x=(m+1)*2;
41. y=i;
42. arry[x+y*w]='$';
43.
44. x=w-i-1;
45. y=(m+1)*2;
46. arry[x+y*w]='$';
47. x=w-(m+1)*2-1;
48. y=i;
49. arry[x+y*w]='$';
50.
51. x=i;
52. y=h-(m+1)*2-1;
53. arry[x+y*w]='$';
54. x=(m+1)*2;
55. y=h-i-1;
56. arry[x+y*w]='$';
57.
58. x=w-i-1;
59. y=h-(m+1)*2-1;
60. arry[x+y*w]='$';
61. x=w-(m+1)*2-1;
62. y=h-i-1;
63. arry[x+y*w]='$';
64. }
65. }
66. for(i=0;i67. {
68. for(j=0;j69. {
70. printf("%c",arry[i*w+j]);
71. }
72. printf("\n");
73. }
74. return 0;
75.}

历届试题 带分数

本题的C参考代码如下:

1.#include
2.#include
3.
4.typedef struct Interval
5.{
6. int pre;
7. int rear;
8. int satisfy;
9.}Interval;
10.Interval interval[7][5];
11.int count=0;
12.
13.//初始化
14.void Init()
15.{
16. int i,j;
17. int value;
18. for(i=1;i<7;i++)
19. {
20. value=i;
21. for(j=1;j<5;j++)
22. {
23. interval[i][j].pre=value++;
24. interval[i][j

].rear=value;
25. }
26. }
27.}
28.
29.//数组初始化为0
30.void InitZero(int *sign)
31.{
32. int i;
33. sign[0]=1;
34. for(i=1;i<10;i++)
35. {
36. sign[i]=0;
37. }
38.}
39.
40.//将一个数的各个位上拆分,并在相应的位上赋值1
41.int Split(int *sign,int value)
42.{
43. int index;
44. while(value)
45. {
46. index=value%10;
47. if(sign[index]==0) sign[index]=1;
48. else return 1;
49. value/=10;
50. }
51. return 0;
52.}
53.
54.//计算一个数的位数
55.int CountBit(int value)
56.{
57. int n=0;
58. while(value)
59. {
60. n++;
61. value/=10;
62. }
63. return n;
64.}
65.
66.//将一个整型数组转换成一个整数
67.int CreateInteger(int *data,int n)
68.{
69. int i;
70. int value=0;
71. for(i=0;i72. {
73. value=value*10+data[i];
74. }
75. return value;
76.}
77.
78.//检查是否每个数都用到
79.int Check(int *sign)
80.{
81. int i;
82. for(i=1;i<10;i++)
83. {
84. if(sign[i]==0) return 0;
85. }
86. return 1;
87.}
88.
89.//复制
90.void Copy(int *sign,int *temp_sign)
91.{
92. int i;
93. for(i=0;i<10;i++)
94. {
95. temp_sign[i]=sign[i];
96. }
97.}
98.//创建一个n位数的整数
99.void CreateNBitNumber(int *sign,int *data,int n,int m,int value,int value3)
100.{
101. if(n==m)
102. {
103. int value1=CreateInteger(data,n);
104. int value2=value1*value;
105. int temp_sign[10];
106. Copy(sign,temp_sign);
107. if(!Split(temp_sign,value2) && Check(temp_sign))
108. {
109. count++;
110. }
111. }
112. else
113. {
114. int i;
115. for(i=1;i<10;i++)
116. {
117. if(sign[i]==0)
118. {
119. sign[i]=1;
120. data[m]=i;
121. CreateNBitNumber(sign,data,n,m+1,value,value3);
122. sign[i]=0;
123. }
124. }
125. }
126.}
127.
128.//求出解
129.void Create(int value)
130.{
131. int i,j;
132. int sign[10];
133. int result;
134. int result_n;
135. int n;
136. for(i=3;i137. {
138. InitZero(sign);
139. if(Split(sign,i)) continue;
140. result=value-i;
141. result_n=CountBit(result);
142. n=CountBit(i);
143. for(j=1;j<5;j++)
144. {
145. if( ((interval[result_n][j].pre+j)==(9-n)) || ((interval[result_n][j].rear+j)==(9-n)))
146. {
147. int data[5];
148. CreateNBitNumber(sign,data,j,0,result,i);
149. }
150. }
151. }
152.}
153.
154.int main()
155.{
156. int value;
157. scanf("%d",&value);
158. Init();
159. Create(value);
160. printf("%d\n",count);
161. return 0;
162.}

历届

试题 剪格子



本题的C参考代码如下:

1.#include
2.#define N 10
3.int num[N][N];
4.int tag[N][N] = {0};
5.int m, n;
6.int r = 100;
7.int find(int i, int j, int t, int ntag[][N])
8.{
9. int count = 0;
10. if (i < 0 || i >= n || j < 0 || j >= m || ntag[i][j] == 1)
11. return 0;
12. ntag[i][j] = 1;
13. if (tag[i][j] != t)
14. return 0;
15. count++;
16. count += find(i - 1, j, t, ntag);
17. count += find(i + 1, j, t, ntag);
18. count += find(i, j - 1, t, ntag);
19. count += find(i, j + 1, t, ntag);
20. return count;
21.}
22.
23.int isbad()
24.{
25. int i, j, k = 0,ge2;
26. int t = tag[0][0];
27. int ntag1[N][N] = {0};
28. int ntag2[N][N] = {0};
29. int ge1 = find(0, 0, t, ntag1);
30. for (i = 0; i < n; i++)
31. {
32. for (j = 0; j < m; j++)
33. {
34. if (tag[i][j] != t)
35. {
36. k = 1;
37. break;
38. }
39. }
40. if (k == 1)
41. break;
42. }
43.
44. if (i == n && j == m)
45. return 0;
46. ge2 = find(i, j, tag[i][j], ntag2);
47.
48. return ge1 + ge2 != m * n;
49.}
50.int bad(int i, int j)
51.{
52. int b;
53. if (i < 0 || i >= n || j < 0 || j >= m || tag[i][j] == 1)
54. return 1;
55.
56. tag[i][j] = 1;
57. b = isbad();
58. tag[i][j] = 0;
59. return b;
60.}
61.
62.void go(int i, int j, int k, int count)
63.{
64.
65. if (bad(i, j) || count < num[i][j])
66. return;
67. k++;
68.
69. if (count == num[i][j])
70. {
71. if (r > k)
72. r = k;
73. return;
74. }
75.
76.
77. tag[i][j] = 1;
78. count -= num[i][j];
79. go(i - 1, j, k, count);
80. go(i + 1, j, k, count);
81. go(i, j - 1, k, count);
82. go(i, j + 1, k, count);
83. tag[i][j] = 0;
84.}
85.
86.int main()
87.{
88.
89. int i, j;
90. int half = 0;
91. scanf("%d %d", &m, &n);
92. for (i = 0; i < n; i++)
93. for (j = 0; j < m; j++)
94. {
95. scanf("%d", &num[i][j]);
96.
97. half += num[i][j];
98. }
99.
100.
101. if (half % 2 == 0 && half >= num[0][0] * 2)
102. {
103.
104. half /= 2;
105. go(0, 0, 0, half);
106. }
107.
108. if (r == 100)
109. r = 0;
110.
111. printf("%d", r);
112.
113. return 0;

114.}

历届试题 错误票据



本题的C参考代码如下:

1.#include
2.int main()
3.{
4. int a[10001]={0};
5. long m,min=100000,max=0,i,n;
6. char c;
7. scanf("%d",&n);
8. for(i=0;i9. while(1)
10. {
11. scanf("%ld",&m);
12. if(m>max) max=m;
13. if(m14. a[m]++;
15. c=getchar();

16. if(c!=' ') break;
17. }
18.
19. for(i=min;i<=max;i++)
20. {
21. if(a[i]==0) printf("%ld ",i);
22. if(a[i]==2) m=i;
23. }
24. printf("%ld",m);
25.
26. return 0;
27.}

历届试题 连号区间数

本题的C参考代码如下:

1.#include
2.int main()
3.{
4. int s[50005],a,i,min,max,count=0,j;
5. scanf("%d",&a);
6. for( i = 0; i < a; i++) {
7. scanf("%d",&s[i]);
8. }
9. for( i = 0; i 10. min=s[i];
11. max=s[i];
12. for( j = i; j 13. if(min>s[j]){min =s[j];}
14. if(max15. if((max-min)==(j-i)){
16. count++;
17. }
18.
19. }
20. }
21. printf("%d",count);
22. return 0;
23.}


历届试题 翻硬币


本题的C参考代码如下:

1.#include
2.#include
3.
4.int turn(char a[], char b[])
5.{
6. int i;
7. int n = 0;
8. for(i = 0;a[i]!='\0';i++)
9. {
10. if(a[i] == b[i])
11. {
12. continue;
13. }
14. else
15. {
16. b[i+1]=(b[i+1]=='*'?'o':'*');
17. n++;
18. }

历届试题 买不到的数目

请选择编译语言:C++ C JAVA
本题的C参考代码如下:

1.#include
2.#define MAXSIZE 1000
3.int main() {
4. int flag[MAXSIZE] = {0};
5. int maxunuse[MAXSIZE] = {0};
6. int num1, num2, minNum, maxNum, temp, i, count = 0;
7. scanf("%d%d", &num1, &num2);
8. minNum = ((num1 < num2) ? num1 : num2);
9. maxNum = num1 + num2 - minNum;
10. for(i = 1; ;i++){
11. temp = i * maxNum % minNum;
12. if((temp) && flag[temp] == 0){
13. flag[temp] = 1;
14. maxunuse[temp] = i * maxNum -minNum;
15. count++;
16. if(count == minNum - 1){
17. break;
18. }
19. }
20. }
21. printf("%d\n", maxunuse[temp]);
22. return 0;
23.}

19. }
20. return n;
21.}
22.int main(void)
23.{
24. char a[1000];
25. char b[1000];
26. gets(a);
27. gets(b);
28. printf("%d\n", turn(a, b));
29. return 0;
30.}
历届试题 大臣的旅费



本题的C参考代码如下:

1.#include
2.#include
3.#include
4.#include
5.
6.struct node;
7.typedef struct node Node;
8.typedef Node *PtrToNode;
9.typedef PtrToNode List;
10.typedef PtrToNode Position;
11.
12.struct node
13.{
14. int n;
15. int val;
16. Position next;
17.};
18.
19.int count=0;
20.int max=0;
21.int x;
22.int *visit; //是否已遍历
23.
24.Position Last(List l); //找出最后项
25.void Insert(int x,int q,List l,Position p); //在p后插入含x的项
26.void Dfs(int a,List l[]); //深度优先搜索
27.int Num(List l);
28.

29.int main(void)
30.{
31. int n,u,v,q,a,b;
32. int i,j,k;
33. Node *head;
34. List *l,tmp;
35. Position p;
36.
37. fscanf(stdin,"%d",&n);
38. head=(Node *)malloc(sizeof(Node)*(n+1));
39. l=(List *)malloc(sizeof(List)*(n+1));
40. visit=(int *)malloc(sizeof(int)*(n+1));
41.
42. for(i=0;i<=n;i++) //初始化表头及链表
43. {
44. head[i].next=NULL;
45. l[i]=&head[i];
46. }
47.
48. for(i=1;i<=n-1;i++) //建立无向图
49. {
50. fscanf(stdin,"%d%d%d",&u,&v,&q);
51. Insert(v,q,l[u],Last(l[u]));
52. Insert(u,q,l[v],Last(l[v]));
53. }
54.
55. for(j=1;j<=n;j++)
56. visit[j]=0;
57. Dfs(1,l); //第一次遍历,找到点a,用全局变量x保存
58.
59. for(j=1;j<=n;j++)
60. visit[j]=0;
61. count=0;
62. max=0;
63. Dfs(x,l); //第二次遍历,找到点b,用全局变量x保存,此时max为最大距离
64.
65. printf("%d",max*10+(max+1)*max/2);
66.
67. return 0;
68.}
69.
70.Position Last(List l)
71.{
72. Position p;
73. for(p=l;p->next!=NULL;p=p->next);
74. return p;
75.}
76.
77.void Insert(int x,int q,List l,Position p)
78.{
79. Position tmp;
80. tmp=(Position) malloc(sizeof(Node));
81.
82. tmp->n=x;
83. tmp->val=q;
84. tmp->next=p->next;
85. p->next=tmp;
86.}
87.
88.void Dfs(int a,List l[])
89.{
90. Position p;
91.
92. visit[a]=1;
93. for(p=l[a]->next;p!=NULL;p=p->next)
94. if(!(visit[p->n]))
95. {
96. count+=p->val;
97. if(count>max)
98. {
99. max=count;
100. x=p->n;
101. }
102.
103. Dfs(p->n,l);
104. count-=p->val;
105. }
106.}
107.
108.int Num(List l)
109.{
110. int n=0;
111. Position p;
112. for(p=l->next;p!=NULL;p=p->next)
113. n++;
114. return n;
115.}

历届试题 幸运数

本题的C参考代码如下:

1.#include
2.#define MAXN 1000010
3.int flag[MAXN];
4.int m,n,a[MAXN],s[MAXN],size=0;
5.int fa(int k)
6.{
7. if(flag[k])
8. return a[k];
9. return fa(k-1);
10.}
11.int main()
12.{
13. int i, p, k, j;
14. scanf("%d%d",&m,&n);
15. for(i=1;i<=n;i+=2)
16. {
17. s[++size]=i;
18. flag[i]=1;
19. a[i]=size;
20. }
21. for(i=2;i<=size;i++)
22. {
23. int Mod=s[i],d=s[i]-1;
24. if(Mod>size)
25. break;
26. for(p=1,j=Mod;j<=size;j+=Mod,p++)



历届试题 横向打印二叉树

请选择编译语言:C++ C JAVA

本题的C参考代码如下:

1.#include
2.#include
3.#include
4.
5.typedef struct TNode
6.{
7. int key;
8. struct TNode *left;
9. struct TNode *right;
10.}TNode, *Tree;
11.
12.Tree insert(Tree root, Tree src)
13.{
14. if(root == NULL)
15. {
16. root = src;


17. }
18. else if(src->key > root->key)
19. {
20. root->left = insert(root->left, src);
21. }
22. else
23. {
24. root->right = insert(root->right, src);
25. }
26. return root;
27.}
28.
29.char l[1000];
30.
31.#define U 1
32.#define D 2
33.#define S ('.')
34.
35.void print(Tree root, int s, int dir)
36.{
37. if(root != NULL)
38. {
39. int i;
40. char buf[10];
41. sprintf(buf, "|-%d-", root->key);
42. int len = strlen(buf);
43. for(i = 0; i < len; i++)
44. {
45. l[s + i] = S;
46. }
47. if(dir == D)
48. {
49. l[s] = '|';
50. }
51. print(root->left, s + len, U);
52.
53. l[s] = '\0';
54. if(root->left == NULL && root->right == NULL)
55. {
56. buf[len - 1] = '\0';
57. printf("%s%s\n", l, buf);
58. }
59. else
60. {
61. printf("%s%s|\n", l, buf);
62. }
63. l[s] = S;
64.
65. if(dir == U)
66. {
67. l[s] = '|';
68. }
69. print(root->right, s + len, D);
70. l[s] = S;
71. }
72.}
73.
74.void printPre(Tree root, int s)
75.{
76. if(root != NULL)
77. {
78. int i;
79. char buf[10];
80. sprintf(buf, "%d-", root->key);
81. int len = strlen(buf);
82. for(i = 0; i < len; i++)
83. {
84. l[s + i] = S;
85. }
86. print(root->left, s + len, U);
87.
88. printf("%s|\n", buf);
89.
90. print(root->right, s + len, D);
91. }
92.}
93.
94.int main(void)
95.{
96. int n;
97. Tree tree = NULL;
98. while(scanf("%d", &n) > 0)
99. {
100. Tree neo = malloc(sizeof(TNode));
101. neo->key = n;
102. neo->left = neo->right = NULL;
103. tree = insert(tree, neo);
104. }
105. printPre(tree, 0);
106. return 0;
107.}

27. {
28. flag[s[j]]=0;
29. for(k=1;k30. {
31. s[++d]=s[j+k];
32. a[s[j+k]]-=p;
33. }
34. }
35. size=d;
36. }
37. printf("%d\n",fa(n-1)-fa(m));
38. return 0;
39.}



历届试题 危险系数

请选择编译语言:C++ C JAVA

本题的C参考代码如下:

1.#include
2.#include
3.struct Node
4.{
5. int data;
6. struct Node *pNext;
7.};
8.struct Node tab[1001];
9.int visit[1001]={0};
10.int way[1001]={0};
11.int count[1001]={0};
12.int cnt=0;
13.void Insert(int n,int x);
14.void Init(int n);
15.void dfs(int x,int y,int n);
16.int fun(int n);
17.int main()
18.{
19. int x,y,n,m,u,v;
20. scanf("%d%d",&n,&m);
21. Init(n);
22. while(m--)
23. {
24. scanf("%d%d",&u,&v);
25. Insert(u,v);
26. Insert(v,u);
27. }
28.

scanf("%d%d",&x,&y);
29. dfs(x,y,0);
30. int ret=fun(n);
31. printf("%d\n",ret);
32. return 0;
33.}
34.int fun(int n)
35.{
36. int i;
37. int ret=0;
38. for(i=1;i<=n;i++)
39. {
40. if(count[i]==cnt)
41. {
42. ret++;
43. }
44. }
45. return (ret-2);
46.}
47.void dfs(int x,int y,int n)
48.{
49. visit[x]=1;
50. way[n]=x;
51. struct Node *p=&tab[x];
52. if(x==y)
53. {
54. int i;
55. cnt++;
56. for(i=0;i<=n;i++)
57. {
58. count[way[i]]++;
59. }
60. return ;
61. }
62. while((p=p->pNext)!=NULL)
63. {
64. if(visit[p->data]!=1)
65. {
66. dfs(p->data,y,n+1);
67. visit[p->data]=0;
68. }
69. }
70.}
71.void Init(int n)
72.{
73. int i;
74. for(i=1;i<=n;i++)
75. {
76. tab[i].data=i;
77. tab[i].pNext=NULL;
78. }
79.}
80.
81.void Insert(int n,int x)
82.{
83. struct Node *p=&tab[n];
84. while(p->pNext!=NULL)
85. {
86. p=p->pNext;
87. }
88. struct Node *new=(struct Node *)malloc(sizeof(struct Node));
89. p->pNext=new;
90. new->data=x;
91. new->pNext=NULL;
92.}

历届试题 网络寻路

本题的C参考代码如下:

1.#include
2.#include
3.#define MAXN 10010
4.#define MAXM 100010
5.int Du[MAXN],U[MAXM],V[MAXM];
6.int main()
7.{
8. int n,i,m;
9. long long ans=0;
10. scanf("%d%d",&n,&m);
11. memset(Du,0,sizeof(Du));
12. for(i=0;i13. scanf("%d%d",&U[i],&V[i]);
14. Du[U[i]]++;
15. Du[V[i]]++;
16. }
17. for(i=0;i1&&Du[V[i]]>1)ans+=(Du[U[i]]-1)*(Du[V[i]]-1)*2;
18. printf("%I64d\n",ans);
19. return 0;
20.}

历届试题 高僧斗法

请选择编译语言:C++ C JAVA

本题的C参考代码如下:

1.#include //参考蓝桥杯贴吧 dezhonger
2.int main()
3.{
4. int a[105],b[105],i=0,j,k,count,sum;
5. char c;
6. while(1)
7. {
8. scanf("%d%c",&a[i++],&c);
9. if(c=='\n')
10. break;
11. }
12. count=i;
13. for(i = 0;i < count-1;i++)
14. b[i]=a[i+1]-a[i]-1;
15. b[count-1]=0;
16. sum=b[0];
17. for(i = 2;i < count;i = i+2)
18. sum^=b[i];
19. if(sum == 0)
20. printf("-1\n");
21. else
22. {
23. for(i = 0;i < count;i++)
24. for(j = 1;j <= b[i];j++)
25. {
26. b[i] -= j;
27. if(i!=0)
28. b[i-1]+=j;
29. sum = b[0];
30. for(k = 2;k < count;k = k+2)
31. sum ^= b[k];
32. if(sum == 0)
33. {
34. printf("%d %d\n",a[i],a[i]+j);
35. break;
36. }
37. b[i] += j;
38. if(i != 0)
39.

b[i-1] -= j;
40. }
41. }
42. return 0;
43.}
44.
45./*
46.1 3 5 7 12 14 17 26 38 45 66 100
47.66 84
48.*/
历届试题 格子刷油漆

请选择编译语言:C++ C JAVA
本题的C参考代码如下:

1.#include
2.long long a[1001],b[1001],sum;
3.#define NUM 1000000007
4.int main()
5.{
6. int i,n;
7. scanf("%d",&n);
8. b[1]=1;
9. for (i=2;i<=n;i++)
10. b[i]=(b[i-1]*2%NUM);
11. a[1]=1;a[2]=6;
12. for (i=3;i<=n;i++)
13. a[i]=(2*a[i-1]+b[i]+4*a[i-2])%NUM;
14. sum=4*a[n];
15. for (i=2;i16. sum=((sum+8*b[n-i]*a[i-1]%NUM)%NUM+(8*a[n-i]*b[i-1])%NUM)%NUM;
17. printf("%I64d\n",sum);
18. return 0;
19.}

历届试题 公式求值

请选择编译语言:C++ C JAVA

本题的Java参考代码如下:

1.import java.math.BigInteger;
2.import java.util.Scanner;
3.public class Main
4.{
5.public static BigInteger lucas(BigInteger n,BigInteger m,BigInteger p){
6.if(m.equals(BigInteger.ZERO)) return BigInteger.ONE;
7.return BigInteger.valueOf(f(n.mod(p).longValue(),m.mod(p).longValue())).multiply(lucas(n.divide(p),m.divide(p),p)).mod(p);
8.}
9.
10.
11.public static long f(long n,long m){
12.if(m>n) return 1;
13.if(n==m|| m==0) return 1;
14.if(m>n-m) m=n-m;
15.long tmpi=1,tmpn=1,s1=1,s2=1,ans=1;
16.for (int i = 1; i<=m; i++) {
17.tmpi=i;
18.tmpn=n-i+1;
19.s1=s1*tmpi%999101;
20.s2=s2*tmpn%999101;
21.}
22.ans = s2*pow1(s1,999099)%999101;
23.return ans%999101;
24.}
25.public static long pow1(long x,long n) {
26.if(x==1) return 1;
27.if (n==0)
28.return 1;
29.else {
30.while ((n & 1)==0) {
31.n>>=1;
32.x=(x *x)%999101;
33.}
34.}
35.long result = x%999101;
36.n>>=1;
37.while (n!=0) {
38.x=(x *x)%999101;;
39.if ((n & 1)!=0)
40.result =result*x%999101;
41.n>>=1;
42.}
43.return result;
44.}
45.public static void main(String[] args) {
46.Scanner sc = new Scanner(System.in);
47.BigInteger n = new BigInteger(sc.nextLine());
48.BigInteger m = new BigInteger(sc.nextLine());
49.int k = Integer.parseInt(sc.nextLine());
50.long start = System.currentTimeMillis();
51.BigInteger md = new BigInteger("999101");
52.long Cnm=lucas(n, m,md).longValue()%999101;
53.long sum = 0;
54.if(Cnm!=0){
55.int[][] a = new int[k][k];
56.int h = 1;
57.for (int i = 0; i < k; i++) {
58.for (int j = 0; j < k; j++) {
59.if (j >= h)
60.a[i][j] =0;
61.else {
62.if (j == 0 || j == h - 1)
63.a[i][j] = 1;
64.else {
65.a[i][j] = (a[i - 1][j - 1]*(h - j)+a[i - 1][j])%999101;
66.}
67.}
68.}
69.h++;
70.}
71.long m1 = 1,n1 =1;
72.long x=n.subtract(new BigInteger(k+"")).mod(md.subtract(BigInteger.ONE)).longValue();
73.long n3 = pow1(2,x);
74.for (int i = k - 1; i >= 0; i--) {
75.n1=n3*pow1(2,i)%999101;
76.m1 = m1*(n.subtract(new BigInteger((k - 1 - i) + "")).mod(md).longValue())%999101;
77.sum = (sum+m1*a[k - 1][i]*n1)%999101;
78.}
79.sum = sum*Cnm%999101;
80.

}
81.System.out.println(sum);
82.long end = System.currentTimeMillis();
83.}
84.
85.
86.}

历届试题 九宫重排

请选择编译语言:C++ C JAVA
本题的Java参考代码如下:

1.import java.io.BufferedReader;
2.import java.io.IOException;
3.import java.io.InputStreamReader;
4.import java.util.HashMap;
5.import java.util.LinkedList;
6.import java.util.Queue;
7.public class Main{//双向广搜
8. private static HashMap hm1=null,hm2=null;
9. public static void main(String[] args) {
10. BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
11. try {
12. String start=bf.readLine();
13. String end=bf.readLine();
14. char a[][]=new char[3][3];
15. char b[][]=new char[3][3];
16. int c=0;
17. int x1=0,y1=0,x2=0,y2=0;
18. for(int i=0;i<3;i++){
19. for(int j=0;j<3;j++){
20. a[i][j]=start.charAt(c);
21. b[i][j]=end.charAt(c);
22. if(a[i][j]=='.'){
23. x1=i;
24. y1=j;
25. }
26. if(b[i][j]=='.'){
27. x2=i;
28. y2=j;
29. }
30. c++;
31. }
32. }
33. Node node1=new Node(a,0,x1,y1);
34. Node node2=new Node(b,0,x2,y2);
35. Queue q1=new LinkedList();
36. Queue q2=new LinkedList();
37. hm1=new HashMap();
38. hm2=new HashMap();
39. hm1.put(start, 0);
40. hm2.put(end, 0);
41. q1.add(node1);
42. q2.add(node2);
43. System.out.println(bfs(q1,q2));
44. } catch (IOException e) {
45. e.printStackTrace();
46. }
47. }
48. private static int bfs(Queue q1,Queue q2) {
49. while(!q1.isEmpty()||!q2.isEmpty()){
50. if(!q1.isEmpty()){
51.
52. Node node1=q1.poll();
53. // System.out.println(node1.getTuString()+"----1");
54. if(hm2.containsKey(node1.getTuString())){
55. return node1.getSum()+hm2.get(node1.getTuString());
56. }
57. int x=node1.getX();
58. int y=node1.getY();
59. if(x>0){
60. char a[][]=node1.getTuCopy();
61. a[x][y]=a[x-1][y];
62. a[x-1][y]='.';
63. Node n=new Node(a,node1.getSum()+1,x-1,y);
64. String s=n.getTuString();
65. if(hm2.containsKey(s)){
66. return n.getSum()+hm2.get(s);
67. }
68. if(!hm1.containsKey(s)){
69. h

m1.put(s, n.getSum());
70. q1.add(n);
71. }
72. }
73. if(x<2){
74. char a[][]=node1.getTuCopy();
75. a[x][y]=a[x+1][y];
76. a[x+1][y]='.';
77. Node n=new Node(a,node1.getSum()+1,x+1,y);
78. String s=n.getTuString();
79. if(hm2.containsKey(s)){
80. return n.getSum()+hm2.get(s);
81. }
82. if(!hm1.containsKey(s)){
83. hm1.put(s, n.getSum());
84. q1.add(n);
85. }
86. }
87. if(y>0){
88. char a[][]=node1.getTuCopy();
89. a[x][y]=a[x][y-1];
90. a[x][y-1]='.';
91. Node n=new Node(a,node1.getSum()+1,x,y-1);
92. String s=n.getTuString();
93. if(hm2.containsKey(s)){
94. return n.getSum()+hm2.get(s);
95. }
96. if(!hm1.containsKey(s)){
97. hm1.put(s, n.getSum());
98. q1.add(n);
99. }
100. }
101. if(y<2){
102. char a[][]=node1.getTuCopy();
103. a[x][y]=a[x][y+1];
104. a[x][y+1]='.';
105. Node n=new Node(a,node1.getSum()+1,x,y+1);
106. String s=n.getTuString();
107. if(hm2.containsKey(s)){
108. return n.getSum()+hm2.get(s);
109. }
110. if(!hm1.containsKey(s)){
111. hm1.put(s, n.getSum());
112. q1.add(n);
113. }
114. }
115. }
116.
117.
118. if(!q2.isEmpty()){
119.
120. Node node2=q2.poll();
121. // System.out.println(node2.getTuString()+"----2");
122. if(hm1.containsKey(node2.getTuString())){
123. return node2.getSum()+hm1.get(node2.getTuString());
124. }
125. int x=node2.getX();
126. int y=node2.getY();
127. if(x>0){
128. char a[][]=node2.getTuCopy();
129. a[x][y]=a[x-1][y];
130. a[x-1][y]='.';
131. Node n=new Node(a,node2.getSum()+1,x-1,y);
132. String s=n.getTuString();
133. if(hm1.containsKey(s)){
134. return n.getSum()+hm1.get(s);
135. }
136. if(!hm2.containsKey(s)){
137. hm2.put(s, n.getSum());
138. q2.add(n);
139. }
140.

}
141. if(x<2){
142. char a[][]=node2.getTuCopy();
143. a[x][y]=a[x+1][y];
144. a[x+1][y]='.';
145. Node n=new Node(a,node2.getSum()+1,x+1,y);
146. String s=n.getTuString();
147. if(hm1.containsKey(s)){
148. return n.getSum()+hm1.get(s);
149. }
150. if(!hm2.containsKey(s)){
151. hm2.put(s, n.getSum());
152. q2.add(n);
153. }
154. }
155. if(y>0){
156. char a[][]=node2.getTuCopy();
157. a[x][y]=a[x][y-1];
158. a[x][y-1]='.';
159. Node n=new Node(a,node2.getSum()+1,x,y-1);
160. String s=n.getTuString();
161. if(hm1.containsKey(s)){
162. return n.getSum()+hm1.get(s);
163. }
164. if(!hm2.containsKey(s)){
165. hm2.put(s, n.getSum());
166. q2.add(n);
167. }
168. }
169. if(y<2){
170. char a[][]=node2.getTuCopy();
171. a[x][y]=a[x][y+1];
172. a[x][y+1]='.';
173. Node n=new Node(a,node2.getSum()+1,x,y+1);
174. String s=n.getTuString();
175. if(hm1.containsKey(s)){
176. return n.getSum()+hm1.get(s);
177. }
178. if(!hm2.containsKey(s)){
179. hm2.put(s, n.getSum());
180. q2.add(n);
181. }
182. }
183. }
184. }
185. return -1;
186. }
187.}
188.class Node{
189. char tu[][]=new char[3][3];
190. int sum=0;
191. int x=0,y=0;
192. public Node(char[][] tu, int sum, int x, int y) {
193. super();
194. this.tu = tu;
195. this.sum = sum;
196. this.x = x;
197. this.y = y;
198. }
199. public char[][] getTuCopy() {
200. char a[][]=new char[3][3];
201. for(int i=0;i<3;i++)
202. for(int j=0;j<3;j++)
203. a[i][j]=tu[i][j];
204. return a;
205. }
206. public String getTuString() {
207. StringBuffer sb=new StringBuffer("");
208. for(int i=0;i<3;i++)
209. for(int j=0;j<3;j++)
210. sb.append(tu[i][j]);
211. return sb.toString();
212. }
213. public void setTu(char[][] tu) {
214. this.tu = tu;
215. }
216. public int getSum() {
217. return sum;
218. }
219. public void setSum(int sum) {
220. this.sum = sum;
221. }
222. public int getX() {
223. return x;
224. }

225. public void setX(int x) {
226. this.x = x;
227. }
228. public int getY() {
229. return y;
230. }
231. public void setY(int y) {
232. this.y = y;
233. }
234.}

















相关文档
最新文档