UVA 10036 Divisibility

本文探讨了一组整数通过加减运算形成的数学表达式,并着重研究了如何判断这些表达式的结果是否能被某个整数整除。通过实例演示了计算过程,并提供了一个基于动态规划的方法来解决此类问题。

Consider an arbitrary sequence of integers. One can place + or - operators between integers in the sequence, thus deriving different arithmetical expressions that evaluate to different values. Let us, for example, take the sequence: 17, 5, -21, 15. There are eight possible expressions:

17 + 5 + -21 + 15 = 16
17 + 5 + -21 - 15 = -14
17 + 5 - -21 + 15 = 58
17 + 5 - -21 - 15 = 28
17 - 5 + -21 + 15 = 6
17 - 5 + -21 - 15 = -24
17 - 5 - -21 + 15 = 48
17 - 5 - -21 - 15 = 18

We call the sequence of integers divisible by K if + or - operators can be placed between integers in the sequence in such way that resulting value is divisible by K. In the above example, the sequence is divisible by 7 (17+5+-21-15=-14) but is not divisible by 5.

You are to write a program that will determine divisibility of sequence of integers.

Input

The first line of the input file contains a integer M indicating the number of cases to be analyzed. Then M couples of lines follow. 
For each one of this couples, the first line contains two integers, N and K (1 <= N <= 10000, 2 <= K <= 100) separated by a space. The second line contains a sequence of N integers separated by spaces. Each integer is not greater than 10000 by it's absolute value.

Output

For each case in the input file, write to the output file the word "Divisible" if given sequence of integers is divisible by K or "Not divisible" if it's not.

Sample input

2
4 7
17 5 -21 15
4 5
17 5 -21 15

Sample Output

Divisible
Not divisible

dp[i][j]中的i表示加上或者减去第i个数时,余数为j是否为true。最后得到dp[n-1][0]是否为真。


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

int dp[11000][110];

int main(){
//freopen("test.in","rt",stdin);
    int t,n,k,a,i,j;
    cin >>t;
    while (t--){
        cin >>n >>k >>a;
        memset(dp, 0, sizeof(dp));
        dp[0][(a % k + k) % k] = 1;
        for (i = 1; i < n; ++i){
            cin >>a;
        a = abs(a) % k;
        for (j = 0; j < k; ++j)
            if (dp[i - 1][j])
                dp[i][(j + a) % k] = dp[i][(j - a + k) % k] = 1;
        }
        cout <<(dp[n - 1][0] ? "Divisible" : "Not divisible") <<endl;
    }
    return 0;
}




<think>我们正在解决WPF中UI元素Visibility属性设置无效的问题。根据用户提供的引用和之前的问题,我们可以总结出一些常见的原因和解决方案。 引用中提到了绑定失效的问题(引用[3]和[4]),这可能是导致Visibility属性设置无效的一个原因。另外,引用[1]提到了资源字典的问题,引用[2]提到了窗口最大化的问题,虽然与可见性不直接相关,但资源问题也可能影响可见性。 常见原因及解决方案: 1. 绑定失效:如果Visibility是通过绑定设置的,绑定失效会导致属性不更新。 - 检查绑定模式(OneWay, TwoWay等)是否正确。 - 确保源属性实现了属性更改通知(INotifyPropertyChanged)。 - 使用调试输出查看绑定错误(在输出窗口中查看绑定跟踪)。 2. 样式和模板覆盖:可能在样式或模板中重新设置了Visibility属性。 - 检查是否在样式、控件模板或数据模板中设置了Visibility属性,这可能会覆盖你的设置。 3. 布局容器的影响:某些容器(如Grid、StackPanel)可能会折叠或隐藏子元素。 - 确保容器本身是可见的,并且没有其他布局问题(如宽度高度为0)。 4. 资源未正确加载:如果Visibility使用了静态资源(如转换器),资源未加载可能导致转换失败。 - 检查转换器(Converter)是否在资源字典中正确定义,并且资源字典已加载。 5. 元素渲染问题:元素可能在可视树之外,或者被其他元素遮挡。 - 检查元素是否在可视树中(例如,不在当前激活的TabItem中)。 - 检查Z轴顺序,是否被其他元素遮挡。 6. 动画影响:正在进行的动画可能改变了Visibility,但动画结束后可能没有恢复。 - 检查是否有动画在控制Visibility。 7. 线程问题:在非UI线程上修改Visibility会导致无效。 - 确保在UI线程上更新Visibility,使用Dispatcher。 8. 转换器(Converter)错误:如果使用了BooleanToVisibilityConverter,确保转换逻辑正确。 - 检查转换器的转换逻辑,确保输入和输出符合预期。 9. 依赖属性优先级问题:WPF依赖属性有优先级,可能被更高优先级的设置覆盖(如样式触发器)。 - 使用依赖属性调试工具(如Snoop)检查实际应用的属性值来源。 10. 元素未加载:如果尝试在元素未加载时设置Visibility,可能无效。 - 在元素Loaded事件后再设置Visibility。 解决方案步骤: 步骤1:检查绑定是否生效 - 在输出窗口中查看绑定错误信息(在Visual Studio中运行程序,查看“输出”窗口,选择“调试”来源)。 - 如果绑定失败,检查源对象、路径、转换器等。 步骤2:使用Snoop等工具检查 - 使用Snoop工具检查元素的Visibility属性实际值以及它的来源(本地值、样式、模板等)。 步骤3:简化问题 - 尝试直接在XAML中设置Visibility(如Visibility="Visible"或Visibility="Collapsed"),看是否生效。如果直接设置有效,则问题出在绑定或动态设置上。 步骤4:检查转换器 - 如果使用了转换器,检查转换器是否被正确实例化,转换方法是否正确。 - 在转换器中设置断点,确保被调用。 步骤5:检查布局容器 - 确保父容器没有折叠或隐藏子元素(例如,父容器是Collapsed,那么子元素即使Visible也不会显示)。 步骤6:检查线程 - 如果是在后台线程中更新,使用Dispatcher: Application.Current.Dispatcher.Invoke(() => { element.Visibility = Visibility.Visible; }); 步骤7:检查样式和模板 - 检查是否在样式或模板中设置了触发器控制Visibility,可能覆盖了你的设置。 步骤8:检查资源字典 - 如果转换器定义在资源字典中,确保资源字典已合并到当前资源中。 示例代码:使用BooleanToVisibilityConverter 在资源中定义转换器: ```xaml <Window.Resources> <BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter"/> </Window.Resources> ``` 在元素上使用绑定: ```xaml <Button Visibility="{Binding IsButtonVisible, Converter={StaticResource BoolToVisibilityConverter}}"/> ``` 确保IsButtonVisible属性在ViewModel中实现了INotifyPropertyChanged。 如果问题仍然存在,可以尝试不使用绑定,直接设置Visibility,逐步排查。 相关问题: 1. WPF中如何调试绑定失败? 2. 如何正确使用BooleanToVisibilityConverter? 3. WPF中如何确保在UI线程更新界面? 4. 如何检查WPF元素的依赖属性值来源? 5. WPF中样式和模板如何影响属性设置?</think>### WPF Visibility 属性设置无效的解决方案 当 WPF 中 `Visibility` 属性设置无效时,通常由以下原因导致。请按顺序排查: #### 1. 绑定失效问题(最常见) **原因**:数据绑定未正确实现 `INotifyPropertyChanged` 接口,导致 UI 未收到更新通知[^3] **解决方案**: ```csharp // ViewModel 实现通知机制 public class MyViewModel : INotifyPropertyChanged { private Visibility _elementVisibility; public Visibility ElementVisibility { get => _elementVisibility; set { _elementVisibility = value; OnPropertyChanged(); } } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged([CallerMemberName] string name = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); } } ``` **XAML 绑定**: ```xml <Button Visibility="{Binding ElementVisibility, Mode=OneWay}"/> ``` > 使用输出窗口检查绑定错误:在 Visual Studio 中查看 **"输出"** 窗口的绑定跟踪信息 #### 2. 样式/模板覆盖 **原因**:控件模板或样式中的触发器覆盖了显式设置的 Visibility[^2] **解决方案**: ```xml <!-- 在样式中添加基于模板的触发器 --> <Style TargetType="Button"> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Visibility" Value="Visible"/> </Trigger> </Style.Triggers> </Style> ``` > 使用 **Snoop** 工具检查最终应用的属性值 #### 3. 布局容器影响 **原因**:父容器(如 Grid.Row/Column)约束导致元素不可见 **解决方案**: - 检查父容器的 `Visibility` 和布局约束 - 确保元素在可视区域内: ```csharp element.BringIntoView(); // 强制滚动到可视区域 ``` #### 4. 渲染变换问题 **原因**:`RenderTransform` 导致元素移出可视区域 **验证方法**: ```xaml <Border Background="Red" Width="5" Height="5" HorizontalAlignment="Left" VerticalAlignment="Top"/> ``` > 在可疑元素旁添加定位标记,确认布局位置 #### 5. 线程访问问题 **原因**:在非 UI 线程修改 Visibility **解决方案**: ```csharp Application.Current.Dispatcher.Invoke(() => { myElement.Visibility = Visibility.Visible; }); ``` #### 6. 资源未加载 **原因**:静态资源(如转换器)未正确加载[^1] **解决方案**: ```xml <!-- 确保资源字典正确合并 --> <Window.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Styles.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Window.Resources> ``` #### 7. 自定义行为冲突 **原因**:第三方库或附加行为覆盖 Visibility **诊断方法**: ```csharp // 检查附加属性 var descriptor = DependencyPropertyDescriptor.FromProperty( UIElement.VisibilityProperty, typeof(UIElement)); descriptor.AddValueChanged(myElement, (s,e) => { Debug.WriteLine($"Visibility changed to {myElement.Visibility}"); }); ``` #### 高级调试技巧 1. **实时属性跟踪**: ```csharp DependencyPropertyDescriptor .FromProperty(UIElement.VisibilityProperty, typeof(UIElement)) .AddValueChanged(myElement, (s,e) => Debug.WriteLine("Visibility changed")); ``` 2. **可视化树检查**: ```csharp // 输出可视化树结构 static void PrintVisualTree(Visual visual, int level = 0) { Debug.WriteLine(new string(' ', level*2) + visual.GetType().Name); for(int i=0; i<VisualTreeHelper.GetChildrenCount(visual); i++) { PrintVisualTree(VisualTreeHelper.GetChild(visual, i) as Visual, level+1); } } ``` 3. **事件路由监控**: ```csharp myElement.AddHandler(UIElement.VisibilityChangedEvent, new RoutedEventHandler((s,e) => Debug.WriteLine("Visibility routed event"))); ``` > 提示:使用 **Live Visual Tree** (Visual Studio) 或 **Snoop** 实时检查属性值来源 --- ### 相关问题 1. 如何调试 WPF 数据绑定失败问题? 2. WPF 中如何正确实现 INotifyPropertyChanged 接口? 3. 如何使用 Snoop 诊断 WPF 可视化树问题? 4. WPF 多线程更新 UI 的最佳实践是什么? 5. 如何检测 WPF 样式和模板的优先级冲突? [^1]: 资源字典未正确加载会导致转换器等资源失效 [^2]: 窗口最大化问题说明布局约束可能影响元素可见性 [^3]: 绑定失效是 Visibility 属性无效的常见原因 [^4]: 自定义控件行为可能覆盖基础属性设置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值