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 ...
随机推荐
- Kylin构建cube时状态一直处于pending
在安装好kylin之后我直接去访问web监控页面发现能够进去,也没有去看日志.然后在运行官方带的例子去bulid cube时去发现状态一直是pending而不是runing.这个时候才去查看日志: 2 ...
- 用lua扩展你的Nginx(整理)
首先得声明.这不是我的原创,是在网上搜索到的一篇文章,原著是谁也搞不清楚了.按风格应该是属于章亦春的文章. 整理花了不少时间,所以就暂写成原创吧. 一. 概述 Nginx是一个高性能.支持高并发的,轻 ...
- 【Cocos2dx 3.3 Lua】定时器事件
Cocos2dx 3.x Lua 中使用定时器有两种方式: (1)self:scheduleUpdateWithPriorityLua(update, priority) > 参数一:刷新函数 ...
- [LeetCode] 697. Degree of an Array_Easy tag: Hash Table
Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...
- Pycharm上python3运行unittest无法生成测试报告
原文地址https://www.cnblogs.com/yoyoketang/p/7523409.html 前言 经常有人在群里反馈,明明代码一样的啊,为什么别人的能出报告,我的出不了报告:为什么别人 ...
- Lower Power with CPF(三)
常用的一些Lower Power的策略: 1)Clock tree optimization and clock gating:在正常情况下clock信号会一直toggle at the maximu ...
- 《Convolutional Neural Networks for Sentence Classification》 文本分类
文本分类任务中可以利用CNN来提取句子中类似 n-gram 的关键信息. TextCNN的详细过程原理图见下: keras 代码: def convs_block(data, convs=[3, 3, ...
- 2018-2019-2 20165209 《网络对抗技术》 Kali安装
2018-2019-2 20165209 <网络对抗技术> Kali安装 目录内容 下载 安装 网络 共享 软件源 下载kali kali下载官网地址 我下载的版本(如下图所示) 安装 打 ...
- linux服务器---squid缓存
Squid缓存 代理服务器会在本地硬盘设置缓存,这样可以提高网络效率 1修改squid配置文件“/etc/squid/squid.conf”,参数“cache_dir_ufs”就是设置缓存目录的 [r ...
- cocoapod 快速更新,加载
pod install --verbose --no-repo-update pod update --verbose --no-repo-update