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++,包括极大和极小 ...
随机推荐
- 通过反射获取java nio Direct Memory 的最大值和已使用值
(ps:jdk1.7及之后可通过MBean获取这两个值)
- REST RPC HTTP vs 高性能二进制协议 序列化和通信协议
edisonchou https://mp.weixin.qq.com/s/-XZXqXawR-NxJMPCeiNsmg .NET Core微服务之服务间的调用方式(REST and RPC) Edi ...
- CH 5402 选课(分组背包+树形DP)
CH 5402 选课 \(solution:\) 很有讨论套路的一道题,利用树的结构来表示出不同课程之间的包含关系(这里还要建一个虚点将森林变成一颗打大树).然后用子树这个概念巧妙的消除了因为这些包含 ...
- CH 5102 Mobile Service(线性DP)
CH 5102 Mobile Service \(solution:\) 这道题很容易想到DP,因为题目里已经说了要按顺序完成这些请求.所以我们可以线性DP,但是这一题的状态不是很好设,因为数据范围有 ...
- HTTP协议六种请求方法,get,head,put,delete,post有什么区别
标准Http协议支持六种请求方法,即: 1.GET 2.POST 3.PUT 4.Delete 5.HEAD 6.Options 但其实我们大部分情况下只用到了GET和POST.如果想设计一个符合RE ...
- (25) java web的struts2框架的使用-基于表单的文件上传
一,首先创建一个表单页面 <body> <form action="uploads" method="post" enctype=" ...
- YTU 2456: 评委打分
2456: 评委打分 时间限制: 1 Sec 内存限制: 128 MB 提交: 283 解决: 52 题目描述 一个歌唱比赛,比赛每次会从观众中随即抽取几名观众给分(观众至少有5个,分数为0~1 ...
- OpenMediaVault Redmine 安装
/******************************************************************** * OpenMediaVault Redmine 安装 * ...
- QTextEdit/QPlainTextEdit添加文字超出视图后,滚动条自动移至最底部
void ThreadExit::onTaskPerformState(const QString& strStatus) { //追加文本(ui.taskStatusTextEdit是一个Q ...
- MDZX——张能传
「你们到底要干什么?!」——8012年7月13日 张能于MDZX ———————————— 序章 ———————————— 话说天下大势,分久必合,合久必分. 他肩扛99米大砍刀,站在MDZX大门对面 ...