CF 241D Numbers

来源:互联网 发布:程序员述职报告 编辑:程序博客网 时间:2024/06/07 11:02

搜索

奇技淫巧题

我们无视排列中所有>25的元素。因为由1~25组成的异或和为0的方案有1048576个,已经远大于50000。在这么多种方案下,一个序列拼起来模p几乎可以看作是一个随机函数,异或和为0跟拼起来模p等于0之间几乎没有相关性。那也就是说我们在1~25之内找不到合法解的概率是

(4999950000)10485767.81010

推广一下,假设只考虑所有≤2l的数,那么由12l组成的异或和为 0 的方案大约会有 22l2l 个。即总方案数 22l,除以总的结果方案数 2l(答案为0的只有12l 概率)

在如此大概率能找到解的情况下,可以通过本题。

#include<cstdio>#include<cstdlib>#define N 50005#define M 26using namespace std;namespace runzhe2000{    int a[M], n, p, tmp, sta[M], top, base[M], pos[M];    void dfs(int x, int xorsum, int addsum)    {        if(top && !xorsum && ! addsum)        {            printf("Yes\n%d\n",top);            for(int i = 1; i <= top; i++)printf("%d ",sta[i]);            exit(0);        }        if(x > n) return;        dfs(x+1,xorsum,addsum);        sta[++top] = pos[x];        dfs(x+1,xorsum^a[x],(addsum*base[a[x]]+a[x]) % p);        --top;    }     void main()    {        tmp = 0; scanf("%d%d",&n,&p);        for(int i = 1; i <= n; i++)        {            int x; scanf("%d",&x);            if(x < M) a[++tmp] = x, pos[tmp] = i;        }        for(int i = 1; i <= 9; i++) base[i] = 10;        for(int i = 10; i < M; i++) base[i] = 100;        n = tmp;        dfs(1,0,0);         puts("No");    }}int main(){    runzhe2000::main(); } 
0 0
原创粉丝点击