B - Game of Connections(卡特兰数)

This is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, ... , 2n - 1, 2n consecutively in clockwise order on the ground to form a circle, and then, to draw some straight line segments to connect them into number pairs. Every number must be connected to exactly one another. And, no two segments are allowed to intersect. 

It's still a simple game, isn't it? But after you've written down the 2n numbers, can you tell me in how many different ways can you connect the numbers into pairs? Life is harder, right? 

InputEach line of the input file will be a single positive number n, except the last line, which is a number -1. You may assume that 1 <= n <= 100. 
OutputFor each n, print in a single line the number of ways to connect the 2n numbers into pairs. 
Sample Input

2
3
-1

Sample Output

2
5

解法:
卡特兰数的计算公式:a[n] = a[n-1]*(4*n-1)/n+1;
主要的问题在于大数的处理上
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 
 6 using namespace std;
 7 
 8 const int N = 10000;
 9 int a[101][101];
10 
11 void cheng( int temp ,int i)
12 {
13     int temp1 = 0;
14     for(int j = 100;j >=  0;j--)
15     {
16         temp1 = a[i-1][j] * temp + temp1;
17         a[i][j] = temp1 % N;
18         temp1  = temp1 / N;
19     }
20     
21 }
22 
23 void chu (int temp,int i)
24 {
25     int temp1 = 0;
26     for(int j = 0;j  <= 100;j++)
27     {
28         temp1 = temp1*N + a[i][j];
29         a[i][j] = temp1 / temp;
30         temp1 = temp1 % temp;
31     }
32     
33 }
34 
35 
36 void DP()
37 {
38     memset(a,0,sizeof(a));
39     a[1][100] = 1;
40     for(int i = 2;i <= 100;i++)
41     {
42         int temp;
43         temp = 4*i-2;
44         cheng(temp,i);
45         temp = i + 1;
46         chu(temp,i);
47     }    
48     
49 }
50 
51 int main ()
52 {
53     DP();
54     int t;
55     while(cin>>t)
56     {
57         if(t == -1)
58             break;
59         int temp;
60         for(int i = 0;i <=100;i++)
61             if( a[t][i] )
62                 {
63                     temp = i;
64                     break;
65                 }
66         printf("%d",a[t][temp]);
67         for(int i = temp+1;i <= 100;i++){
68             printf("%04d",a[t][i]);
69         }
70         cout<<endl;
71    }
72 }

 

转载于:https://www.cnblogs.com/a2985812043/p/7220546.html

### MySQL 'Too many connections' 错误解决方案 #### 修改最大连接配置 对于遇到 `Too many connections` 错误的情况,可以通过调整 MySQL 配置中的最大连接来解决问题。具体操作如下: - 对于通过 Docker 创建的 MySQL 容器,在启动容器时可以指定环境变量 `MYSQL_MAX_CONNECTIONS` 来设定最大连接量[^2]。 ```bash docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -e MYSQL_MAX_CONNECTIONS=500 -d mysql:tag ``` - 或者编辑 MySQL 的配置文件 `my.cnf` 或 `my.ini` 文件,找到 `[mysqld]` 节点下增加或修改 `max_connections` 参至合适的值[^5]。 ```ini [mysqld] max_connections = 500 ``` 之后重启 MySQL 服务使更改生效。 #### 查看当前最大连接 为了确认当前系统的最大连接设置情况,可以在 MySQL 命令行工具中执行命令: ```sql SHOW VARIABLES LIKE 'max_connections'; ``` 这有助于评估是否有必要进一步调高此参值[^4]。 #### 断开闲置连接以释放资源 当存在大量未关闭的闲置连接时,这些连接不仅占用服务器资源还可能导致新的合法请求无法建立新连接。因此定期清理不再使用的连接非常重要。可通过以下 SQL 查询获取长时间处于非活动状态的进程列表,并终止它们: ```sql SELECT ID, USER, HOST, DB, COMMAND, TIME FROM information_schema.PROCESSLIST WHERE TIME > 3600; KILL QUERY process_id; -- 替换process_id为实际要结束的任务ID ``` 上述语句用于查找超过一小时无动作的会话并将其杀死,以此方式主动管理连接池大小从而预防因过度累积造成的性能瓶颈问题[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值