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++,包括极大和极小 ...
随机推荐
- 项目Beta冲刺(团队4/7)
项目Beta冲刺(团队4/7) 团队名称: 云打印 作业要求: 项目Beta冲刺(团队) 作业目标: 完成项目Beta版本 团队队员 队员学号 队员姓名 个人博客地址 备注 221600412 陈宇 ...
- 在MVC中使用泛型仓储模式和工作单元来进行增删查改
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...
- LeetCode(67)题解: Add Binary
https://leetcode.com/problems/add-binary/ 题目: Given two binary strings, return their sum (also a bin ...
- SD_WebImage之相同url图片不更新问题
最近做项目,服务器返回的头像url雷打不动的不变,命名方式是用户id与日期的组合,与后台沟通无果,于是把sdwebimage看了一圈后发现了这个选项,它的原理是如果沙盒中有相同的url,则会把原来的删 ...
- Intellij IDEA debug jar包
打成jar包 mvn clean install -Dmaven.test.skip=true jar包保存在target目录下 启动jar Terminal控制台执行下面的命令,启动jar java ...
- Node中的promise简说及入门
Node的特色之一就是异步回调,可是回调过多,就会形成著名的回调金字塔. 直接上例子,我要读取1.txt里的内容,然后在这个内容上加上'test'并重新写入文件,如下代码所示: var fs = re ...
- p1694猴子 并查集
有n只猴子,第一只尾巴挂在树上,剩下的n-1只,要么被其他的猴子抓住,要么抓住了其他的猴子,要么两者均有. 当然一只猴子最多抓两只另外的猴子,因为只有两只猴爪子嘛.现在给出这n只猴子抓与被抓的信息,并 ...
- POJ2243 Knight Moves —— A*算法
题目链接:http://poj.org/problem?id=2243 Knight Moves Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- set built-in function
集合类型 集合对象是一组无序排列的可哈希的值,集合可以作为字典的键.因为集合是无序的,不可以为集合创建索引或执行切片操作,也没有键可以用来获取元素的值. 集合有两种不同的类型,可变集合和不可变集合.可 ...
- 转:Apache-Tomcat各发布包说明
下载地址:http://tomcat.apache.org/发布包说明: apache-tomcat-[version].zip---------------------------基本发布包.这些发 ...