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++,包括极大和极小 ...
随机推荐
- android编译遇到问题修改
(注意要确定安装了jdk) 第一步: cd lichee; ./build.sh -p sun5i_elite -k 3.0 (apt-get install uboot-mkimage需要安装 ...
- poj 1180 Batch Scheduling (斜率优化)
Batch Scheduling \(solution:\) 这应该是斜率优化中最经典的一道题目,虽然之前已经写过一道 \(catstransport\) 的题解了,但还是来回顾一下吧,这道题其实较那 ...
- YTU 2422: C语言习题 n个数逆序
2422: C语言习题 n个数逆序 时间限制: 1 Sec 内存限制: 128 MB 提交: 150 解决: 96 题目描述 将n(n<20)个数按输入时顺序的逆序排列,用函数实现. 输入 ...
- express 中文文档
express() 创建一个express应用程序 var express = require('express'); var app = express(); app.get('/', functi ...
- 如何反编译silverlight
@years(945060991) 15:10:28问一下 如何反编译silverlight观,一世沧桑如画♥(752816388) 15:10:46解压就行@years(945060991) ...
- Linux 命令行命令及参数辨异
1. 软链接与硬链接 ln -s 源文件 目标文件 当我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要在某个固定的目录,放上该文件,然后在其它的目 ...
- 「LuoguP3369」 【模板】普通平衡树 (用vector乱搞平衡树
Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入 x 数 删除 x 数(若有多个相同的数,应只删除一个) 查询 x 数的排名(排名定义为比当前 ...
- JSON --- 一种轻量级的数据交换格式
目录 1. 语法 2. 解析与序列化 JSON.stringify( jsData[, filter, indent] ) JSON.parse( jsonData[, reduction]) JSO ...
- Code-NFine:变量修改
ylbtech-Code-NFine:Cookie变量修改 1. NFine.Code返回顶部 1.Operator 1.1.OperatorProvider.cs /**************** ...
- VC++、MFC
VC++.MFC最好的开源项目 介绍:介绍一下用VC++/MFC写的最好的开源项目. Sourceforge.net中有许多高质量的VC++开源项目,我列举了一些可以作为VC++程序员的参考. 正文: ...