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次数
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int n,m;
const int Max=1e6;
long long Arr[Max];
long long Pos[Max*];
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
void Build(int rt,int l,int r){
if(l==r){
Pos[rt]=Arr[l]%m;
return;
}
int mid=(l+r)/;
Build(rt*,l,mid);
Build(rt*+,mid+,r);
Pos[rt]=(Pos[rt*]*Pos[rt*+])%m;
}
long long query(int L,int R,int l,int r,int rt){
if(L<=l&&r<=R){
return Pos[rt];
}
long long ans=;
int mid=(l+r)/;
if(mid>=L){
ans*=query(L,R,l,mid,rt*)%m;
}
if(mid<R){
ans*=query(L,R,mid+,r,rt*+)%m;
}
return ans%m;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%lld",&Arr[i]);
}
Build(,,n);
long long Sum=;
for(int i=;i<=n;i++){
int l=i;
int r=n;
int pos=n;
while(l<r){
int mid=(l+r)/;
if(query(i,mid,,n,)%m==){
pos=mid;
r=mid;
}else{
l=mid+;
}
}
if(query(i,pos,,n,)%m==){
Sum+=(n-pos+);
}
}
printf("%lld\n",Sum);
return ;
}
http://codeforces.com/contest/818/submission/28156236
#include <bits/stdc++.h>
#include <ext/hash_map>
#include <ext/numeric> using namespace std;
using namespace __gnu_cxx; #define REP(i,n) for( (i)=0 ; (i)<(n) ; (i)++ )
#define rep(i,x,n) for( (i)=(x) ; (i)<(n) ; (i)++ )
#define REV(i,n) for( (i)=(n) ; (i)>=0 ; (i)-- )
#define FORIT(it,x) for( (it)=(x).begin() ; (it)!=(x).end() ; (it)++ )
#define foreach(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define rforeach(it,c) for(__typeof((c).rbegin()) it=(c).rbegin();it!=(c).rend();++it)
#define foreach2d(i, j, v) foreach(i,v) foreach(j,*i)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define SZ(x) ((int)(x).size())
#define MMS(x,n) memset(x,n,sizeof(x))
#define mms(x,n,s) memset(x,n,sizeof(x)*s)
#define pb push_back
#define mp make_pair
#define NX next_permutation
#define UN(x) sort(all(x)),x.erase(unique(all(x)),x.end())
#define CV(x,n) count(all(x),(n))
#define FIND(x,n) find(all(x),(n))-(x).begin()
#define ACC(x) accumulate(all(x),0)
#define PPC(x) __builtin_popcountll(x)
#define LZ(x) __builtin_clz(x)
#define TZ(x) __builtin_ctz(x)
#define mxe(x) *max_element(all(x))
#define mne(x) *min_element(all(x))
#define low(x,i) lower_bound(all(x),i)
#define upp(x,i) upper_bound(all(x),i)
#define NXPOW2(x) (1ll << ((int)log2(x)+1))
#define PR(x) cout << #x << " = " << (x) << endl ; typedef unsigned long long ull;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef pair<int, int> pii; const int OO = (int) 2e9;
const double eps = 1e-; const int N = ; int n, k;
int a[N]; int main() {
std::ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
// freopen("in.txt", "rt", stdin);
// freopen("out.txt", "wt", stdout);
#endif
cin >> n >> k;
for (int i = ; i < n; i++) {
cin >> a[i];
}
int st = , en = ;
ll res = ;
int prv = -;
while (en < n) {
ll cur = ;
for (int i = st; i < n; i++) {
cur *= a[i];
cur %= k;
if (cur == ) {
en = i;
break;
}
}
if (cur != ) {
break;
}
cur = ;
for (int i = en; i >= ; i--) {
cur *= a[i];
cur %= k;
if (cur == ) {
st = i;
break;
}
}
if (cur == ) {
//cout << st << " " << en << endl;
res += (st - prv) * 1LL * (n - en);
prv = st;
}
st++;
en++;
}
cout << res << endl;
return ;
}
Educational Codeforces Round 24 E的更多相关文章
- Educational Codeforces Round 24 A 水 B stl C 暴力 D stl模拟 E 二分
A. Diplomas and Certificates time limit per test 1 second memory limit per test 256 megabytes input ...
- Educational Codeforces Round 24 CF 818 A-G 补题
6月快要结束了 期末也过去大半了 马上就是大三狗了 取消了小学期后20周的学期真心长, 看着各种北方的学校都放假嗨皮了,我们这个在北回归线的学校,还在忍受酷暑. 过年的时候下定决心要拿块ACM的牌子, ...
- codeforces Educational Codeforces Round 24 (A~F)
题目链接:http://codeforces.com/contest/818 A. Diplomas and Certificates 题解:水题 #include <iostream> ...
- Educational Codeforces Round 24
A. Diplomas and Certificates time limit per test 1 second memory limit per test 256 megabytes input ...
- 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 ...
- Educational Codeforces Round 24 B
n children are standing in a circle and playing a game. Children's numbers in clockwise order form a ...
- 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 o ...
- Educational Codeforces Round 24 题解
A: 考你会不会除法 //By SiriusRen #include <bits/stdc++.h> using namespace std; #define int long long ...
- Educational Codeforces Round 32
http://codeforces.com/contest/888 A Local Extrema[水] [题意]:计算极值点个数 [分析]:除了第一个最后一个外,遇到极值点ans++,包括极大和极小 ...
随机推荐
- dubbo springCloud比较
1.dubbo只是专注于服务之间的治理,配置中心.分布式跟踪等这些内容都需要自己集成 2.dubbo核心功能: a.远程通讯 b.集群容错 c.自动发现 Dubbo SpringCloud 服务注册中 ...
- 安卓动态逆向分析工具--Andbug&Androguard
工具使用方法: 转自: http://bbs.pediy.com/showthread.php?t=183412 https://testerhome.com/topics/3542 安装andbug ...
- 浅谈C#中常见的委托
一提到委托,浮现在我们脑海中的大概是听的最多的就是类似C++的函数指针吧,呵呵,至少我的第一个反应是这样的. 关于委托的定义和使用,已经有诸多的人讲解过,并且讲解细致入微,尤其是张子阳的那一篇.我就不 ...
- 细数AutoLayout以来UIView和UIViewController新增的相关API
本文转载至 http://www.itjhwd.com/autolayout-uiview-uiviewcontroller-api/ 细数AutoLayout以来UIView和UIViewContr ...
- POJ 1703 Find them, Catch them(种类并查集)
题目链接 这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解. #include <cstring> #include <cstdio> #include <s ...
- TGraphiControl响应WM_MOUSEMOVE的过程(以TPaintBox为例)good
起因:非Windows句柄控件也可以处理鼠标消息,我想知道是怎么处理的:并且想知道处理消息的顺序(比如TPaintBox和TForm都响应WM_Mouse消息该怎么办)界面:把TPaintBox放到T ...
- 20170225 ABAP获取字符串长度/字节长度
函数YGET_CHAR_LONG: FUNCTION YGET_CHAR_LONG. *"-------------------------------------------------- ...
- td 中设置超出宽度显示省略号失效
td测试内容超出显示省略号时,结果没有显示省略号,而是一直往后显示,且超出了td大小,强行挤大了table. 原因是因为td默认不换行. 解决方法: 1.强制td换行. IE加上word-break: ...
- SQL查询速度
查询速度 https://www.cnblogs.com/ZaraNet/p/9558272.html 影响你的查询速度的原因是什么? 网速不给力,不稳定. 服务器内存不够,或者SQL 被分配的内存不 ...
- 「LuoguP1145」 约瑟夫(打表
Description n 个人站成一圈,从某个人开始数数,每次数到 m 的人就被杀掉,然后下一个人重新开始数,直到最后只剩一个人.现在有一圈人, k 个好人站在一起, k 个坏人站在一起.从第一个好 ...