spoj TBATTLE 质因数分解+二分
题目链接:点击传送
TBATTLE - Thor vs Frost Giants
Thor is caught up in a fierce battle with Loki's army. This army consists of frost giants that have magical powers with them. Their strength levels gets multiplied when they are together. Giants are not highly skilled in the arts of combat, but their sheer size and strength make them formidable opponents even for the Asgardian gods. Thor is no exception. They recover very fast from physical injury but their recovery slows down when they are exposed to extreme heat.
Thor's hammer can generate heat only in multiples of heat quantum N. Frost giants get killed only when their combined strength level is exactly equal to the heat level of the hammer. Thor is interested in killing a continuous stretch of frost enemies with a throw of his hammer with a preference to kill closer enemies first.
Continuous stretch is defined as a set of consecutive elements.
Help Thor to determine the minimum stretch of frost giants that could be killed in a throw. In case of multiple minimal stretches, output the indices of that stretch that has lowest starting index. If there is no such continuous stretch possible then print -1.
Input
The first line will contain N, the number of Frost Giants in Loki's army and the Heat quantum.
The second line will contain N integers (a_0, a_2....., a_n-1) - the strength of each frost giant.
Minimum stretch of the army should be 1.
- 1 ≤ N ≤ 100000
- 1 ≤ a_i ≤ 100000
Output
Output the range of the minimum stretch of frost giants that could be killed in a throw. In case of multiple minimal stretches, output the indices of that stretch that has lowest starting index.
If there is no such continuous stretch possible then print -1.
Example
Input:
3
1 2 9
Output:
2 2 Input:
5
2 3 4 8 9
Output:
-1 Input:
10
2 4 3 5 17 4 7 5 2 15
Output:
7 8
Explanation
Input #1:
Thor can only kill the stretch [2,2] as this is the minimum length range with strength, multiple of 3.
Input #2:
There is no stretch of frost giants that have combined strength as a multiple of 5.
Input #3:
There are many stretches of frost giants that have strength as multiple of 10. But the minimal stretch with the least indices is from [7,8]. Minimum size stretches are [7, 8] and [8, 9]. Out of them we select [7,8].
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=1e6+,inf=;
const ll INF=1e18+,mod=1e9+;
/// 数组大小
vector<int>p;
int n;
int c[];
void init(int n)
{
memset(c,,sizeof(c));
int si=;
for(int i=;i<=n;i++)
{
if(n%i==)p.push_back(i),si++;
while(n%i==)
{
c[si-]++;
n/=i;
}
}
}
int sum[N][];
int check(int l,int r)
{
for(int j=;j<p.size();j++)
{
if(sum[r][j]-sum[l-][j]<c[j])
return ;
}
return ;
}
int main()
{
while(~scanf("%d",&n))
{
memset(sum,,sizeof(sum));
p.clear();
init(n);
for(int i=;i<=n;i++)
{
int x;
scanf("%d",&x);
int num=x;
for(int j=;j<p.size();j++)
{
sum[i][j]=sum[i-][j];
while(num%p[j]==)
{
num/=p[j];
sum[i][j]++;
}
}
}
int s=-,e=1e9;
for(int i=;i<=n;i++)
{
int st=i,en=n,ans=-;
while(st<=en)
{
int mid=(st+en)>>;
if(check(i,mid))
{
ans=mid;
en=mid-;
}
else
st=mid+;
}
if(ans!=-)
{
if(ans-i<e-s)
s=i,e=ans;
}
}
if(s==-)
printf("-1\n");
else
printf("%d %d\n",s-,e-);
}
return ;
}
spoj TBATTLE 质因数分解+二分的更多相关文章
- POJ 1845 Sumdiv#质因数分解+二分
题目链接:http://poj.org/problem?id=1845 关于质因数分解,模板见:http://www.cnblogs.com/atmacmer/p/5285810.html 二分法思想 ...
- 2018.09.11 poj1845Sumdiv(质因数分解+二分求数列和)
传送门 显然需要先求出ab" role="presentation" style="position: relative;">abab的所有质因 ...
- 求n!质因数分解之后素数a的个数
n!质因数分解后P的个数=n/p+n/(p*p)+n/(p*p*p)+......直到n<p*p*p*...*p //主要代码,就这么点东西,数学真是厉害啊!幸亏我早早的就退了数学2333 do ...
- AC日记——质因数分解 1.5 43
43:质因数分解 总时间限制: 1000ms 内存限制: 65536kB 描述 已知正整数 n 是两个不同的质数的乘积,试求出较大的那个质数. 输入 输入只有一行,包含一个正整数 n. 对于60% ...
- 【BZOJ-4514】数字配对 最大费用最大流 + 质因数分解 + 二分图 + 贪心 + 线性筛
4514: [Sdoi2016]数字配对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 726 Solved: 309[Submit][Status ...
- 整数分解 && 质因数分解
输入整数(0-30)分解成所有整数之和.每四行换行一次. 一种方法是通过深度优先枚举出解.通过递归的方式来实现. #include <stdio.h> #include <strin ...
- algorithm@ 大素数判定和大整数质因数分解
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...
- POJ1365 - Prime Land(质因数分解)
题目大意 给定一个数的质因子表达式,要求你计算机它的值,并减一,再对这个值进行质因数分解,输出表达式 题解 预处理一下,线性筛法筛下素数,然后求出值来之后再用筛选出的素数去分解....其实主要就是字符 ...
- 数学概念——J - 数论,质因数分解
J - 数论,质因数分解 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
随机推荐
- Elasticsearch Date类型使用技巧
elasticsearch原生支持date类型.这里简单记录下使用的方法. 使用date类型可以用如下两种方式: 使用毫秒的时间戳,直接将毫秒值传入即可. 传入格式化的字符串,默认是ISO 8601标 ...
- Andrew Ng-ML-第十三章-支持向量机
1.从代价函数谈起SVM 图一 根据将y=0||y=1,得到逻辑回归的代价函数,那么SVM和其代价函数是相似的,只不过是引入了cost0与cost1,并且自变量使用了theta_T*x(i),并且由于 ...
- centos7最小安装初始化脚本
#!/bin/bash #zhangsen #lovexlzs@qq.com if [[ "$(whoami)" != "root" ]]; then exit ...
- 混淆和加密.NET开发工具
.NET开发的工具,可以用ILSpy等很轻松的反编译查看源码,为了保护自己写的软件,一般会对软件进行加密,不仅内部关键数据通过加密,软件开发完毕后,对软件也进行加密,防止别人很轻松的反编译和查看到比较 ...
- jmeter 读取excel数据
jmeter 读取excel数据使用的方法是使用Jmeter CSV Data Set Config参数化 但是将excel文件保存成csv格式后,jmeter读取后返回的数据总是出现乱码问题, 以下 ...
- Oracle获取数据库中的对象创建语句
使用dbms_metadata.get_ddl()函数可以做到. 实验环境:Oracle 11.2.0.4 以获取jingyu用户下的T1表为例: SQL> conn jingyu/jingyu ...
- 深入理解php内核——读书笔记1
第一章 基础准备 宏定义 #字符串化 ##连接符 do{}while(0) 多行 全局宏: EG.PG 第二章 用户代码的执行 php请求的生命周期 SAPI接口 php脚本执行 第三章 变量及数据类 ...
- STA分析(二) multi_cycle and false
multicycle path:当FF之间的组合逻辑path propagate delay大于一个时钟cycle时,这条combinational path能被称为multicycle path. ...
- Manacher 计算最长回文串
转自 http://blog.sina.com.cn/s/blog_3fe961ae0101iwc2.html 寻找字符串中的回文,有特定的算法来解决,也是本文的主题:Manacher算法,其时间复杂 ...
- 消息系统之Apache ActiveMQ
一.下载运行MQ服务 1.下载ActiveMQ :http://activemq.apache.org/ 2.解压缩: 进入bin目录 win32和win64对应不同位的操作系统,选择进入 点击act ...