生活随笔
收集整理的這篇文章主要介紹了
Codeforces Round #717 (Div. 2) D(倍增dp)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Codeforces Round #717 (Div. 2) D
題意:n個數 q個詢問,每一個詢問有l和r,問你l到r這段區間中最少能分成幾段,每一段中的數都是互質的。
思路:首先預處理出每一個點向左走最多能走多遠,可以分解質因數來找,記錄質因數最右邊的位置,然后使用倍增dp,dp【i】【j】代表j點向左走i^2步 最遠能走到哪,然后循問的時候 也是倍增處理就好了。
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define re register
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(a) ((a)&-(a))
#define ios std::ios::sync_with_stdio(false);std::cin.tie(0);std::cout.tie(0);
#define fi first
#define rep(i,s,n) for(int i=s;(i)<=(n);i++)
#define rep1(i,s,n) for(int i=s;(i)>=(n);i--)
#define se second
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define ac cout<<ans<<"\n"
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
using namespace std
;
typedef long long ll
;
typedef unsigned long long ull
;
typedef pair
<ll
,ll
> pii
;
int dx
[4]= {-1,0,1,0},dy
[4]= {0,-1,0,1};
const ll mod
=1e9+7;
const ll N
=1e5+10;
const double eps
= 1e-4;
int last
[N
];
int vis
[N
];
ll dp
[22][N
];
int main()
{ios
int n
,q
;cin
>>n
>>q
;rep(i
,1,n
) {int x
;cin
>>x
;last
[i
]=last
[i
-1];for(int j
=2;j
*j
<=x
;j
++){if(x
%j
==0){last
[i
]=max(vis
[j
],last
[i
]);while(x
%j
==0) x
/=j
;vis
[j
]=i
;}}if(x
!=1){last
[i
]=max(vis
[x
],last
[i
]);vis
[x
]=i
;}}for(int i
=1;i
<=n
;i
++) dp
[0][i
]=last
[i
];for(int i
=1;i
<=20;i
++){for(int j
=1;j
<=n
;j
++){dp
[i
][j
]=dp
[i
-1][dp
[i
-1][j
]];}}while(q
--){int l
,r
;cin
>>l
>>r
;ll ans
=0;for(int i
=19;i
>=0;i
--){if(l
<=dp
[i
][r
]){ans
+=(1ll<<i
);r
=dp
[i
][r
];}}cout
<<ans
+1<<endl
;}return 0;
}
總結
以上是生活随笔為你收集整理的Codeforces Round #717 (Div. 2) D(倍增dp)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。