首页 > 代码库 > Educational Codeforces Round 24 E
Educational Codeforces Round 24 E
Vova again tries to play some computer card game.
The rules of deck creation in this game are simple. Vova is given an existing deck of n cards and a magic number k. The order of the cards in the deck is fixed. Each card has a number written on it; number ai is written on the i-th card in the deck.
After receiving the deck and the magic number, Vova removes x (possibly x?=?0) cards from the top of the deck, y (possibly y?=?0) cards from the bottom of the deck, and the rest of the deck is his new deck (Vova has to leave at least one card in the deck after removing cards). So Vova‘s new deck actually contains cards x?+?1, x?+?2, ... n?-?y?-?1, n?-?y from the original deck.
Vova‘s new deck is considered valid iff the product of all numbers written on the cards in his new deck is divisible by k. So Vova received a deck (possibly not a valid one) and a number k, and now he wonders, how many ways are there to choose x and y so the deck he will get after removing x cards from the top and y cards from the bottom is valid?
The first line contains two integers n and k (1?≤?n?≤?100?000, 1?≤?k?≤?109).
The second line contains n integers a1, a2, ..., an (1?≤?ai?≤?109) — the numbers written on the cards.
Print the number of ways to choose x and y so the resulting deck is valid.
3 4
6 2 8
4
3 6
9 1 14
1
In the first example the possible values of x and y are:
- x?=?0,?y?=?0;
- x?=?1,?y?=?0;
- x?=?2,?y?=?0;
- x?=?0,?y?=?1;
题意:选取一段区间,使得乘积形式%k==0,选法有多少种
解法:
1 确定是否被k整除,可以分解质因数或者选择A1*%k*A2*%k....是不是等于0
2 选择第二种方法,求区间乘积,利用线段树维护(当然如果时间给的短就是另一回事了QUQ 第二个代码给出双指针做法,时间更短)
3 寻找符合条件的区间
3.1 如果i~(l+r)/2区间符合,则r=mid,不符合l=mid+1
3.2 最后得出来的区间也必须符合
4 我们避免计算重复,只计算 n-pos+1次数
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 int n,m; 6 const int Max=1e6; 7 long long Arr[Max]; 8 long long Pos[Max*2]; 9 #define lson l,mid,rt<<1 10 #define rson mid+1,r,rt<<1|1 11 void Build(int rt,int l,int r){ 12 if(l==r){ 13 Pos[rt]=Arr[l]%m; 14 return; 15 } 16 int mid=(l+r)/2; 17 Build(rt*2,l,mid); 18 Build(rt*2+1,mid+1,r); 19 Pos[rt]=(Pos[rt*2]*Pos[rt*2+1])%m; 20 } 21 long long query(int L,int R,int l,int r,int rt){ 22 if(L<=l&&r<=R){ 23 return Pos[rt]; 24 } 25 long long ans=1; 26 int mid=(l+r)/2; 27 if(mid>=L){ 28 ans*=query(L,R,l,mid,rt*2)%m; 29 } 30 if(mid<R){ 31 ans*=query(L,R,mid+1,r,rt*2+1)%m; 32 } 33 return ans%m; 34 } 35 int main() 36 { 37 scanf("%d%d",&n,&m); 38 for(int i=1;i<=n;i++){ 39 scanf("%lld",&Arr[i]); 40 } 41 Build(1,1,n); 42 long long Sum=0; 43 for(int i=1;i<=n;i++){ 44 int l=i; 45 int r=n; 46 int pos=n; 47 while(l<r){ 48 int mid=(l+r)/2; 49 if(query(i,mid,1,n,1)%m==0){ 50 pos=mid; 51 r=mid; 52 }else{ 53 l=mid+1; 54 } 55 } 56 if(query(i,pos,1,n,1)%m==0){ 57 Sum+=(n-pos+1); 58 } 59 } 60 printf("%lld\n",Sum); 61 return 0; 62 }
http://codeforces.com/contest/818/submission/28156236
1 #include <bits/stdc++.h> 2 #include <ext/hash_map> 3 #include <ext/numeric> 4 5 using namespace std; 6 using namespace __gnu_cxx; 7 8 #define REP(i,n) for( (i)=0 ; (i)<(n) ; (i)++ ) 9 #define rep(i,x,n) for( (i)=(x) ; (i)<(n) ; (i)++ ) 10 #define REV(i,n) for( (i)=(n) ; (i)>=0 ; (i)-- ) 11 #define FORIT(it,x) for( (it)=(x).begin() ; (it)!=(x).end() ; (it)++ ) 12 #define foreach(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it) 13 #define rforeach(it,c) for(__typeof((c).rbegin()) it=(c).rbegin();it!=(c).rend();++it) 14 #define foreach2d(i, j, v) foreach(i,v) foreach(j,*i) 15 #define all(x) (x).begin(),(x).end() 16 #define rall(x) (x).rbegin(),(x).rend() 17 #define SZ(x) ((int)(x).size()) 18 #define MMS(x,n) memset(x,n,sizeof(x)) 19 #define mms(x,n,s) memset(x,n,sizeof(x)*s) 20 #define pb push_back 21 #define mp make_pair 22 #define NX next_permutation 23 #define UN(x) sort(all(x)),x.erase(unique(all(x)),x.end()) 24 #define CV(x,n) count(all(x),(n)) 25 #define FIND(x,n) find(all(x),(n))-(x).begin() 26 #define ACC(x) accumulate(all(x),0) 27 #define PPC(x) __builtin_popcountll(x) 28 #define LZ(x) __builtin_clz(x) 29 #define TZ(x) __builtin_ctz(x) 30 #define mxe(x) *max_element(all(x)) 31 #define mne(x) *min_element(all(x)) 32 #define low(x,i) lower_bound(all(x),i) 33 #define upp(x,i) upper_bound(all(x),i) 34 #define NXPOW2(x) (1ll << ((int)log2(x)+1)) 35 #define PR(x) cout << #x << " = " << (x) << endl ; 36 37 typedef unsigned long long ull; 38 typedef long long ll; 39 typedef vector<int> vi; 40 typedef vector<vector<int> > vvi; 41 typedef pair<int, int> pii; 42 43 const int OO = (int) 2e9; 44 const double eps = 1e-9; 45 46 const int N = 100005; 47 48 int n, k; 49 int a[N]; 50 51 int main() { 52 std::ios_base::sync_with_stdio(false); 53 cin.tie(NULL); 54 cout.tie(NULL); 55 #ifndef ONLINE_JUDGE 56 // freopen("in.txt", "rt", stdin); 57 // freopen("out.txt", "wt", stdout); 58 #endif 59 cin >> n >> k; 60 for (int i = 0; i < n; i++) { 61 cin >> a[i]; 62 } 63 int st = 0, en = 0; 64 ll res = 0; 65 int prv = -1; 66 while (en < n) { 67 ll cur = 1; 68 for (int i = st; i < n; i++) { 69 cur *= a[i]; 70 cur %= k; 71 if (cur == 0) { 72 en = i; 73 break; 74 } 75 } 76 if (cur != 0) { 77 break; 78 } 79 cur = 1; 80 for (int i = en; i >= 0; i--) { 81 cur *= a[i]; 82 cur %= k; 83 if (cur == 0) { 84 st = i; 85 break; 86 } 87 } 88 if (cur == 0) { 89 //cout << st << " " << en << endl; 90 res += (st - prv) * 1LL * (n - en); 91 prv = st; 92 } 93 st++; 94 en++; 95 } 96 cout << res << endl; 97 return 0; 98 }
Educational Codeforces Round 24 E