usaco Arithmetic Progressions individual report

来源:互联网 发布:好的淘宝店铺推荐 编辑:程序博客网 时间:2024/06/08 17:36

核心方法暴力搜素

剪枝的关键在于:

1利用空间复杂度换取时间复杂度,查找某个元素是否在数列中时,直接利用总长的数组,该数组已在第一次赋值时进行标记。

2由等差数列的长度要求,减少公差的枚举次数,枚举公差后对应的枚举首项时,去掉公差对应的不可能的首项。

upperdef = upper/(n-1); //第一次剪枝for ( def = 1; def<=upperdef; def++) // array判定数组    {        for ( p = 0; places[p]<=(upper-((n-2)*def)); p++) {//第二次剪枝            bool is;            is = true;            int where;            for (int c = (n-1); c>=0 ; c--)                    if (!array[places[p]+c*def]) {                        is = false;                        where = (p+c*def);                        break;                    }                if (is) {                noneF = false;                out<<places[p]<<" "<<def<<endl;            }        }    }        if (noneF)        out<<"NONE"<<endl;        return 0;}


0 0