zoukankan      html  css  js  c++  java
  • luogu P1762 偶数

    打表找规律吼题哇

    首先打出(1-1000)内的答案的表

    0
    0
    1
    1
    4
    6
    9
    9
    16
    ...
    448363
    
    

    有个**规律啊qwq

    然后想到用(frac{n(n+1)}{2})(也就是数字的总数)减去答案,得到另一个表

    1
    3
    5
    9
    11
    15
    19
    27
    29
    ...
    

    好像有点规律啊,,,

    发现第(2^i)行的数为(3^i)

    然后前后做差,得到

    1
    2
    2
    4
    2
    4
    4
    8
    2
    4
    4
    8
    4
    8
    8
    16
    ...
    

    发现任取一段(1-2^i),然后以(2^{i-1})为界,发现前面一半数每个乘2得到了后面一半数,并且对于前后两半类似的处理下去也是这个结论

    于是看一下题解整理一下,我们就能知道答案为

    [frac{n(n+1)}{2}-sum_{i=0}^{lfloor log_2n floor}[n&2^i]2^o3^i(o ext{为二进制第i位往后的(编号更大)二进制位上1的个数} )]

    然后做完了qwq

    代码中我从高到低地模拟,所以复杂度好像偏高(雾),但是意思是一样的.所以看不懂直接套用式子吧

    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<complex>
    #include<cstdio>
    #include<vector>
    #include<cmath>
    #include<ctime>
    #include<queue>
    #include<map>
    #define LL long long
    #define il inline
    #define re register
    
    using namespace std;
    const LL mod=1000003;
    il LL rd()
    {
        re LL x=0,w=1;re char ch;
        while(ch<'0'||ch>'9') {if(ch=='-') w=-1;ch=getchar();}
        while(ch>='0'&&ch<='9') {x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
        return x*w;
    }
    LL n,m,ans,dv=500002;	//dv为那个模数意义下2的逆元
    il LL cch(LL a,LL b)    //快(gui)速乘,防止爆longlong
    {
      LL an=0;
      while(b)
        {
          if(b&1) an=(an+a)%mod;
          a=(a+a)%mod;
          b>>=1;
        }
      return an;
    }
    
    int main()
    {
      m=n=rd();
      int d=1;
      while(n)
        {
          LL i=1,c=d;
          while((i<<1ll)<=n) c=(c*3)%mod,i<<=1ll;   //强行找highbit(滑稽)
          ans=(ans+c)%mod;
          n-=i;
          d<<=1;
        }
      printf("%lld
    ",((cch(m,m+1)*dv)%mod-ans+mod)%mod);
      return 0;
    }
    
    

    至于更好理解的代码,请右转此题题解区

  • 相关阅读:
    ZeptoLab Code Rush 2015
    UVa 10048 Audiophobia【Floyd】
    POJ 1847 Tram【Floyd】
    UVa 247 Calling Circles【传递闭包】
    UVa 1395 Slim Span【最小生成树】
    HDU 4006 The kth great number【优先队列】
    UVa 674 Coin Change【记忆化搜索】
    UVa 10285 Longest Run on a Snowboard【记忆化搜索】
    【NOIP2016提高A组模拟9.28】求导
    【NOIP2012模拟10.9】电费结算
  • 原文地址:https://www.cnblogs.com/smyjr/p/9419279.html
Copyright © 2011-2022 走看看