luogu P1762 偶数
打表找規(guī)律吼題哇
首先打出\(1-1000\)內(nèi)的答案的表
0 0 1 1 4 6 9 9 16 ... 448363~~有個(gè)**規(guī)律啊qwq~~
然后想到用\(\frac{n(n+1)}{2}\)(也就是數(shù)字的總數(shù))減去答案,得到另一個(gè)表
1 3 5 9 11 15 19 27 29 ...好像有點(diǎn)規(guī)律啊,,,
發(fā)現(xiàn)第\(2^i\)行的數(shù)為\(3^i\)
然后前后做差,得到
1 2 2 4 2 4 4 8 2 4 4 8 4 8 8 16 ...發(fā)現(xiàn)任取一段\(1-2^i\),然后以\(2^{i-1}\)為界,發(fā)現(xiàn)前面一半數(shù)每個(gè)乘2得到了后面一半數(shù),并且對于前后兩半類似的處理下去也是這個(gè)結(jié)論
于是看一下題解整理一下,我們就能知道答案為
\[\frac{n(n+1)}{2}-\sum_{i=0}^{\lfloor log_2n\rfloor}[n\&2^i]2^o3^i(o\text{為二進(jìn)制第i位往后的(編號更大)二進(jìn)制位上1的個(gè)數(shù)} )\]
然后做完了qwq
代碼中我從高到低地模擬,所以復(fù)雜度好像偏高(霧),但是意思是一樣的.所以看不懂直接套用式子吧
#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 registerusing 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為那個(gè)模數(shù)意義下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; //強(qiáng)行找highbit(滑稽)ans=(ans+c)%mod;n-=i;d<<=1;}printf("%lld\n",((cch(m,m+1)*dv)%mod-ans+mod)%mod);return 0; }至于更好理解的代碼,請右轉(zhuǎn)此題題解區(qū)
轉(zhuǎn)載于:https://www.cnblogs.com/smyjr/p/9419279.html
總結(jié)
以上是生活随笔為你收集整理的luogu P1762 偶数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JMeter 报告监听器导入.jtl结果
- 下一篇: input的type为number