日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

牛客NOIP2021提高组OI赛前模拟赛第一场T3——与巨(数学)

發(fā)布時(shí)間:2023/12/3 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 牛客NOIP2021提高组OI赛前模拟赛第一场T3——与巨(数学) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

與巨

  • description
  • solution
  • code

description

【題目描述】
定義無窮序列f:f1=1,fn=fn?1?2+1f:f_1=1,f_n=f_{n-1}*2+1f:f1?=1,fn?=fn?1??2+1

定義函數(shù)G(x)=min?fi≥x(fi)G(x)=\min_{f_i\ge x}(f_i)G(x)=minfi?x?(fi?)

定義dpc,0=0,dpc,i=max?(dpc,i?1,[((i?c)&G(i))=i]?i)dp_{c,0}=0,dp_{c,i}=\max(dp_{c,i-1},\big[\big((i*c)\&G(i)\big)=i\big]*i)dpc,0?=0,dpc,i?=max(dpc,i?1?,[((i?c)&G(i))=i]?i)

∑i=0ndpc,i(mod998244353)\sum_{i=0}^ndp_{c,i}\pmod {998244353}i=0n?dpc,i?(mod998244353)

【輸入格式】
第一行輸入一個(gè)整數(shù)T,表示測試用例的組數(shù)。
每組測試用例輸入一行包含兩個(gè)整數(shù)n, c。
其中𝐧以二進(jìn)制形式表示,且𝐧不含有前導(dǎo) 0。
【輸出格式】
對(duì)于每組測試用例輸出一行一個(gè)整數(shù)表示答案。
【樣例 1 輸入】

5 1001 1 11111 1 101010111101 8999 10100101111010101 799 10010010 233

【樣例 1 輸出】

45 496 2835797 707482963 9940

【數(shù)據(jù)范圍】

1≤T≤10;1≤∣n∣≤107,1≤c≤1018,∑i=1T∣n∣≤1071\le T\le 10;1\le |n|\le 10^7,1\le c\le 10^{18},\sum_{i=1}^T|n|\le 10^71T10;1n107,1c1018,i=1T?n107

solution

觀察到fif_ifi?的生成遞推式,最后結(jié)果一定形如000..00011...111

設(shè)G(i)=2t+1?1G(i)=2^{t+1}-1G(i)=2t+1?1G(i)G(i)G(i)必然是這種形式,ttt為二進(jìn)制最高位

x&G(i)x\&G(i)x&G(i)相當(dāng)于xmod?2t+1x\ \text{mod}\ 2^{t+1}x?mod?2t+1

則,有(i?c)&G(i)=i?(i?c)mod?2t+1=i?i?(c?1)mod?2t+1=0(i*c)\&G(i)=i\Leftrightarrow (i*c)\ \text{mod}\ 2^{t+1}=i\Leftrightarrow i*(c-1)\ \text{mod}\ 2^{t+1}=0(i?c)&G(i)=i?(i?c)?mod?2t+1=i?i?(c?1)?mod?2t+1=0

2p?q=c?12^p*q=c-12p?q=c?1來重新表示,則iii必須含有因子2t+1?p2^{t+1-p}2t+1?p

設(shè)m=∣n∣m=|n|m=n,可以枚舉ttt

  • t<m?1t<m-1t<m?1

    [2t,2t+1?1][2^t,2^{t+1}-1][2t,2t+1?1]內(nèi)的所有數(shù)都在nnn之內(nèi),其最高位均為ttt

    設(shè)g=t+1?pg=t+1-pg=t+1?p,那么iii含有因子2g2^g2g意味著iii的低ggg位全為000

    這樣的數(shù)為2t,2t+2g,...,2t+x?2g2^t,2^t+2^g,...,2^t+x*2^g2t,2t+2g,...,2t+x?2g2t+(x+1)2g=2t+12^t+(x+1)2^g=2^{t+1}2t+(x+1)2g=2t+1

    這是一個(gè)x+1x+1x+1項(xiàng)的等差數(shù)列,利用公式容易求得

    同時(shí)每個(gè)數(shù)都會(huì)對(duì)最終答案貢獻(xiàn)2g2^g2g次【對(duì)于G(i)=2aG(i)=2^aG(i)=2a,那么G(i+k,k∈[1,2a])=2a+1G(i+k,k\in[1,2^a])=2^{a+1}G(i+k,k[1,2a])=2a+12a+12^{a+1}2a+1都會(huì)貢獻(xiàn),一共是2a2^a2a

  • t=m?1t=m-1t=m?1,由于2t+1>n2^{t+1}>n2t+1>n,但需要滿足2t+x?2t≤n?x=?n2g??2t?g2^t+x*2^t\le n\Rightarrow x=\lfloor\frac{n}{2^g}\rfloor-2^{t-g}2t+x?2tn?x=?2gn???2t?g

    對(duì)這個(gè)x+1x+1x+1項(xiàng)的等差數(shù)列同樣方法計(jì)算貢獻(xiàn)后,最后一項(xiàng)被計(jì)算了2g2^g2g次,計(jì)數(shù)范圍為[2t+x?2g,2t+(x+1)?2g?1][2^t+x*2^g,2^t+(x+1)*2^g-1][2t+x?2g,2t+(x+1)?2g?1]

    此時(shí)多計(jì)數(shù)了2t+(x+1)?2g?1?n2^t+(x+1)*2^g-1-n2t+(x+1)?2g?1?n次,減去多的貢獻(xiàn)即可

code

#include <cstdio> #include <cstring> #include <iostream> using namespace std; #define int long long #define mod 998244353 #define maxn 10000005 int mi[maxn]; char s[maxn]; const int inv2 = ( mod + 1 ) >> 1;int calc( int a, int d, int n ) {return ( a * n % mod + n * ( n - 1 ) % mod * d % mod * inv2 % mod ) % mod; }signed main() {freopen( "and.in", "r", stdin );freopen( "and.out", "w", stdout );mi[0] = 1;for( int i = 1;i < maxn;i ++ ) mi[i] = ( mi[i - 1] << 1 ) % mod;int T, n, c;scanf( "%lld", &T );while( T -- ) {scanf( "%s %lld", s + 1, &c );n = strlen( s + 1 );c --;int ans = 0;if( ! c ) {for( int i = 1;i <= n;i ++ )ans = ( ( ans << 1 ) + ( s[i] ^ 48 ) ) % mod;printf( "%lld\n", ans * ( ans + 1 ) % mod * inv2 % mod );}else {if( c & 1 ) { printf( "0\n" ); continue; }else {int p = 0;while( ! ( c & 1 ) ) c >>= 1, p ++;for( int t = 0;t < n;t ++ ) {int g = max( 0ll, t + 1 - p );if( t < n - 1 )ans = ( ans + mi[g] * calc( mi[t], mi[g], mi[t + 1 - g] - mi[t - g] ) ) % mod;else {int k = 0;for( int i = 1;i <= n - g;i ++ ) k = ( ( k << 1 ) + ( s[i] ^ 48 ) ) % mod;//n/2^g下取整 int x = ( k - mi[t - g] ) % mod; ans = ( ans + mi[g] * calc( mi[t], mi[g], x + 1 ) ) % mod;//x=k-2^{t-g}=k-mi[t-g] x+1項(xiàng) int lst = ( mi[t] + x * mi[g] ) % mod;//計(jì)算末項(xiàng) 2^t+x*2^gint l = 0;int r = ( lst + mi[g] - 1 ) % mod;//末項(xiàng)的下一項(xiàng) 2^t+(x+1)*2^g 還有一個(gè)-1 for( int i = 1;i <= n;i ++ ) l = ( ( l << 1 ) + ( s[i] ^ 48 ) ) % mod; //-n ans = ( ans - ( r - l ) * lst ) % mod;}}}printf( "%lld\n", ( ans + mod ) % mod );}}return 0; }

總結(jié)

以上是生活随笔為你收集整理的牛客NOIP2021提高组OI赛前模拟赛第一场T3——与巨(数学)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。