【差分数组】Master of GCD
題目描述
Hakase has n numbers in a line. At fi rst, they are all equal to 1. Besides, Hakase is interested in primes. She will choose a continuous subsequence [l, r] and a prime parameter x each time and for every l≤i≤r, she will change ai into ai*x. To simplify the problem, x will be 2 or 3. After m operations, Hakase wants to know what is the greatest common divisor of all the numbers.
?
輸入
The first line contains an integer T (1≤T≤10) representing the number of test cases.
For each test case, the fi rst line contains two integers n (1≤n≤100000) and m (1≤m≤100000),where n refers to the length of the whole sequence and m means there are m operations.
The following m lines, each line contains three integers li (1≤li≤n), ri (1≤ri≤n), xi (xi∈{2,3} ),which are referred above.
?
輸出
For each test case, print an integer in one line, representing the greatest common divisor of the sequence. Due to the answer might be very large, print the answer modulo 998244353.
?
樣例輸入
復(fù)制樣例數(shù)據(jù)
2 5 3 1 3 2 3 5 2 1 5 3 6 3 1 2 2 5 6 2 1 6 2樣例輸出
6 2?
提示
For the first test case, after all operations, the numbers will be [6,6,12,6,6]. So the greatest common divisor is 6.
?
題目大意:
先輸入一個(gè)整數(shù)t,代表有t組測(cè)試,對(duì)于每組測(cè)試,第一行輸入兩個(gè)整數(shù)n,m,n代表數(shù)組的長(zhǎng)度,m代表下面對(duì)這個(gè)數(shù)組進(jìn)行m此操作,下面m行每行輸入三個(gè)整數(shù)l,r,x,代表將數(shù)組從a[l]到a[r]里面每一個(gè)數(shù)進(jìn)行乘x操作,x只取2,3兩個(gè)值,問(wèn)最后整個(gè)數(shù)組的最大公約數(shù)是多少。
解題思路:
因?yàn)橹皇菍?duì)數(shù)組的某段區(qū)間進(jìn)行乘2或乘3操作,所以可以通過(guò)計(jì)算數(shù)組的每一項(xiàng)元素乘2,乘3的次數(shù),找出最小乘2次數(shù)n1,乘3的次數(shù)n2,最后的結(jié)果就是(2^n1)*(3^n2),所以我們可以通過(guò)差分?jǐn)?shù)組進(jìn)行區(qū)間修改的維護(hù),O(1)修改,O(n)查詢,最后通過(guò)快速冪計(jì)算出乘積即可。
代碼:
#include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #include <cstring> #include <map> #include <stack> #include <queue> #include <vector> #include <bitset> #include <set> #include <utility> #include <sstream> #include <iomanip> using namespace std; typedef long long ll; typedef unsigned long long ull; #define inf 0x3f3f3f3f #define rep(i,l,r) for(int i=l;i<=r;i++) #define lep(i,l,r) for(int i=l;i>=r;i--) #define ms(arr) memset(arr,0,sizeof(arr)) //priority_queue<int,vector<int> ,greater<int> >q; const int maxn = (int)1e5 + 5; const ll mod = 998244353.; int d1[100100],d2[100100]; ll quickpow(ll base,ll x) {ll ans=1;while(x) {if(x&1) ans=(ans*base)%mod;base=(base*base)%mod;x>>=1;}return ans; } int main() {#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);int t;cin>>t;while(t--) {int n,m;cin>>n>>m;fill(d1+1,d1+1+n,0);fill(d2+1,d2+1+n,0);for(int i=1;i<=m;i++) {int a,b,c;cin>>a>>b>>c;if(c==2) d1[a]++,d1[b+1]--;if(c==3) d2[a]++,d2[b+1]--;}ll nape1=0,nape2=0;ll maxl1=inf,maxl2=inf;for(int i=1;i<=n;i++) {nape1+=d1[i];nape2+=d2[i];maxl1=min(maxl1,nape1);maxl2=min(maxl2,nape2);}ll ans=(quickpow(2LL,maxl1)*quickpow(3LL,maxl2))%mod;cout<<ans<<endl;}return 0; }?
總結(jié)
以上是生活随笔為你收集整理的【差分数组】Master of GCD的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: UVA1025——A Spy in th
- 下一篇: MIP开发教程(一) MIP-CLI工