hdu2276 Kiki & Little Kiki 2

本文介绍了一个基于矩阵乘法的灯状态模拟算法,通过快速幂运算高效计算圆形排列灯经过特定秒数后的状态变化。

Kiki & Little Kiki 2

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 551    Accepted Submission(s): 317

Problem Description

There are n lights in a circle numbered from 1 to n. The left of light 1 is light n, and the left of light k (1< k<= n) is the light k-1.At time of 0, some of them turn on, and others turn off.
  Change the state of light i (if it's on, turn off it; if it is not on, turn on it) at t+1 second (t >= 0), if the left of light i is on !!! Given the initiation state, please find all lights’ state after M second. (2<= n <= 100, 1<= M<= 10^8)

Input

The input contains one or more data sets. The first line of each data set is an integer m indicate the time, the second line will be a string T, only contains '0' and '1' , and its length n will not exceed 100. It means all lights in the circle from 1 to n.
If the ith character of T is '1', it means the light i is on, otherwise the light is off.

Output

For each data set, output all lights' state at m seconds in one line. It only contains character '0' and '1.

Sample Input

0101111 

10 

100000001

Sample Output

1111000 

001000010

Source

HDU 8th Programming Contest Site 1

Recommend

lcy

 

依旧是矩阵乘法

问题是给一排灯的开关状态,如果左边的灯前一秒是亮着的,那么该灯下一秒就变换状态,否则不变。问m 秒后的情况。

显然,灯ai 下一秒的状态就是 (ai-1+ai) mod 2 ,所以可以构造如下矩阵

假设有5 盏灯

1 0 0 0 1

1 1 0 0 0

0 1 1 0 0

0 0 1 1 0

0 0 0 1 1

之后直接用快速幂解决。

代码如下


### HDU 3919 Little Sheep 解决方案 HDU 3919 Little Sheep 是一道涉及动态规划的经典题目。该问题的核心在于通过状态转移方程来计算最优解,具体来说是寻找一种策略使得羊群能够最大化收益或者最小化损失。 #### 题目概述 Little Sheep 的问题是关于如何安排一组绵羊跳跃过障碍物,目标是最小化总能量消耗。每只绵羊都有一定的初始高度和跳跃能力,而每个障碍物也有其特定的高度。当一只绵羊跳过某个障碍物时,会根据两者之间的高度差产生相应的能量消耗。 为了求解此问题,可以采用动态规划的方法。以下是详细的解决方法: --- #### 动态规划设计 定义 `dp[i][j]` 表示前 `i` 只绵羊已经成功跨越了前 `j` 个障碍物所需的最少能量消耗[^6]。 ##### 转移方程 假设当前处理第 `k` 只绵羊尝试跨越第 `l` 个障碍物,则有如下关系: \[ dp[k][l] = \min(dp[k-1][m]) + cost(m, l) \] 其中 \( m &lt; l \),\( cost(m, l) \) 表示从第 `m` 个障碍物到第 `l` 个障碍物的能量消耗[^7]。 ##### 初始条件 初始化时设置边界情况为无穷大(即不可达),除了起点外设为零: \[ dp[0][0] = 0 \] ##### 结果获取 最终的结果可以通过遍历所有可能的状态得到: \[ ans = \min_{j} dp[n][j] \] --- #### 实现代码 下面是基于上述分析的 C++ 实现代码: ```cpp #include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;climits&gt; using namespace std; const int INF = INT_MAX / 2; int main() { int n, m; cin &gt;&gt; n &gt;&gt; m; // 输入绵羊数量和障碍物数量 vector&lt;int&gt; sheep(n); for (int i = 0; i &lt; n; ++i) { cin &gt;&gt; sheep[i]; // 输入每只绵羊的能力值 } vector&lt;int&gt; obstacles(m); for (int j = 0; j &lt; m; ++j) { cin &gt;&gt; obstacles[j]; // 输入每个障碍物的高度 } // 初始化 DP 数组 vector&lt;vector&lt;int&gt;&gt; dp(n + 1, vector&lt;int&gt;(m + 1, INF)); dp[0][0] = 0; for (int k = 1; k &lt;= n; ++k) { // 枚举绵羊 for (int l = 1; l &lt;= m; ++l) { // 枚举障碍物 for (int m = 0; m &lt; l; ++m) { // 枚举之前的障碍物 int cost = abs(obstacles[l - 1] - obstacles[m]); dp[k][l] = min(dp[k][l], dp[k - 1][m] + cost); } } } // 计算结果 int answer = INF; for (int j = 0; j &lt;= m; ++j) { answer = min(answer, dp[n][j]); } cout &lt;&lt; answer &lt;&lt; endl; // 输出最小能量消耗 return 0; } ``` --- #### 复杂度分析 时间复杂度主要由三重循环决定: \[ O(n \cdot m^2) \] 空间复杂度则取决于存储的二维数组大小: \[ O(n \cdot m) \] 对于较大的数据规模,这种实现方式可能会超时或占用过多内存。因此,在实际应用中可以根据具体情况优化算法结构,比如引入滚动数组减少空间开销。 --- #### 注意事项 1. **输入范围验证**:确保程序能正确处理极端测试用例,例如只有单个绵羊或多于障碍物的情况。 2. **溢出保护**:由于涉及到大量数值运算,需注意防止整型变量发生溢出现象。 3. **性能调优**:针对大规模数据集考虑进一步剪枝操作或其他高效算法替代传统动态规划。 --- ###
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值