Codeforces Global Round 15 (div1+div2)D
题目
You are given a sequence of n integers a1,a2,…,an.(1<=n<=10)
Does there exist a sequence of n integers b1,b2,…,bn such that the following property holds?
(For each 1≤i≤n, there exist two (not necessarily distinct) indices j and k (1≤j,k≤n) such that ai=bj−bk)
翻译
问:n个数a[1...n]有没有可能是n个数b[1...n],经过ai=bj−bk这样的操作得到的?
思路
我们可以把bi看作点,ai=bj-bk看作一条bj到bk的有向边
由题意,n个点产生了n条边,故图中一定存在环(可能是自环)
问题转化为:是否存在几个ai的组合(每个ai的符号可能正可能负)和等于0
by dfs 时间复杂度 O(3^n)
1 1 void dfs(int step,int now,bool flag){ 2 2 //flag的作用是标记ai的个数是不是>0了 3 3 if(f==1) return; 4 4 if(step==n+1){ 5 5 if(now==0&&flag){ 6 6 f=1; 7 7 } 8 8 return; 9 9 } 10 10 dfs(step+1,now-a[step],1); 11 11 dfs(step+1,now+a[step],1); 12 12 dfs(step+1,now,flag); 13 13 } 14 14 15 15 16 16 dfs(1,0,0)

浙公网安备 33010602011771号