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?

Input

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.

Output

Print the number of ways to choose x and y so the resulting deck is valid.

Examples

input

3 46 2 8

output

4

input

3 69 1 14

output

1

Note

In the first example the possible values of x and y are:

  1. x?=?0,?y?=?0;
  2. x?=?1,?y?=?0;
  3. x?=?2,?y?=?0;
  4. 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 }
时间: 2024-11-05 15:52:43

Educational Codeforces Round 24 E的相关文章

Educational Codeforces Round 24 F. Level Generation(三分)

题目链接:Educational Codeforces Round 24 F. Level Generation 题意: 给你n个点,让你构造ans条边,使得这ans条边中至少有一半是桥. 让你求ans的最大值. 题解: 首先我们将每一个点按顺序连起来,那么可以构成n-1个桥. 然后我们可以把其中的x个点拿出来连边,这些边都不是桥. x个点最多能连x*(x-1)条边,然后剩下的n-x个点连的边将会构成桥. 然后就可以构造一个函数关系,详见check函数,然后三分一下就行了. 1 #include

Educational Codeforces Round 24 D

Alice and Bob got very bored during a long car trip so they decided to play a game. From the window they can see cars of different colors running past them. Cars are going one after another. The game rules are like this. Firstly Alice chooses some co

Educational Codeforces Round 24 CF 818 A-G 补题

6月快要结束了 期末也过去大半了 马上就是大三狗了 取消了小学期后20周的学期真心长, 看着各种北方的学校都放假嗨皮了,我们这个在北回归线的学校,还在忍受酷暑. 过年的时候下定决心要拿块ACM的牌子,一直坚持刷题,这一个学期刷了200道吧,感觉还是小有收获.特别是Ural和Codeforces上的题,质量很高. 然后4月的校赛,5月的省赛,发挥的也一般,不过也没有太失常. 希望暑假的选拔赛能碰到有趣的队友 蛤蛤. 这两天各种考试,实在是太忙,看了一下edu24的题目,不是很容易,做了一道水题,以

Educational Codeforces Round 24 A

There are n students who have taken part in an olympiad. Now it's time to award the students. Some of them will receive diplomas, some wiil get certificates, and others won't receive anything. Students with diplomas and certificates are called winner

Educational Codeforces Round 24 B

n children are standing in a circle and playing a game. Children's numbers in clockwise order form a permutation a1,?a2,?...,?an of length n. It is an integer sequence such that each integer from 1 to n appears exactly once in it. The game consists o

Educational Codeforces Round 24

陷入了一种每场比赛打完都不想改题的虚无状态,不能这样,改题改题改题. 昨晚只写了三道题意即题解的题…感觉意识模糊,看了看是unrated就睡了 CF已经连续三场unrated了qwq,我一共就没打过几场 A. Diplomas and Certificates 题意:拿到certificate的人数将会是拿到diploma人数的k倍,但拿到他们的总人数不能超过n/2 把n/2向下取底分成k+1分 #include<iostream> #include<cstdio> #includ

Educational Codeforces Round 21 G. Anthem of Berland(dp+kmp)

题目链接:Educational Codeforces Round 21 G. Anthem of Berland 题意: 给你两个字符串,第一个字符串包含问号,问号可以变成任意字符串. 问你第一个字符串最多包含多少个第二个字符串. 题解: 考虑dp[i][j],表示当前考虑到第一个串的第i位,已经匹配到第二个字符串的第j位. 这样的话复杂度为26*n*m*O(fail). fail可以用kmp进行预处理,将26个字母全部处理出来,这样复杂度就变成了26*n*m. 状态转移看代码(就是一个kmp

Educational Codeforces Round 26 D. Round Subset(dp)

题目链接:Educational Codeforces Round 26 D. Round Subset 题意: 给你n个数,让你选其中的k个数,使得这k个数的乘积的末尾的0的个数最大. 题解: 显然,末尾乘积0的个数和因子2和因子5的个数有关. 然后考虑dp[i][j]表示选i个数,当前因子5的个数为j时,能得到因子2最多的为多少. 那么对于每个数,记录一下因子2和5的个数,做一些01背包就行了. 1 #include<bits/stdc++.h> 2 #define mst(a,b) me

Educational Codeforces Round 23 F. MEX Queries(线段树)

题目链接:Educational Codeforces Round 23 F. MEX Queries 题意: 一共有n个操作. 1.  将[l,r]区间的数标记为1. 2.  将[l,r]区间的数标记为0. 3.  将[l,r]区间取反. 对每个操作,输出标记为0的最小正整数. 题解: hash后,用线段树xjb标记一下就行了. 1 #include<bits/stdc++.h> 2 #define ls l,m,rt<<1 3 #define rs m+1,r,rt<&l