Make Square CodeForces - 1028H (dp)
大意: 若一个序列存在两个数的积为完全平方数, 则为好序列. 给定序列$a$, 每次询问求子区间$[l,r]$最少修改多少次可以成为好序列, 每次修改可以任选素数$p$, 任选一个数乘或除$p$.
$dp_{x,y}$表示状态为$x$删除$y$个因子的最大位置
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head const int N = 1e7+10;
int n, q, ans[N];
int ss[20], dp[N][10], L[N];
vector<int> g[N], p[N]; int main() {
scanf("%d%d", &n, &q);
REP(i,1,n) {
int x;
scanf("%d", &x);
int mx = sqrt(x+0.5);
REP(j,2,mx) if (x%j==0) {
int t = 0;
while (x%j==0) t^=1,x/=j;
if (t) p[i].pb(j);
}
if (x) p[i].pb(x);
}
REP(i,1,q) {
int r;
scanf("%d%d",L+i,&r);
g[r].pb(i);
}
REP(i,1,n) {
int sz = p[i].size(), mx = (1<<sz)-1;
REP(s,0,mx) {
int x = 1, y = 0;
REP(j,0,sz-1) {
if (s>>j&1) x*=p[i][j];
else ++y;
}
REP(j,0,15) ss[j+y]=max(ss[j+y],dp[x][j]);
}
for (auto j:g[i]) {
int now = 0;
while (ss[now]<L[j]) ++now;
ans[j] = now;
}
REP(s,0,mx) {
int x = 1, y = 0;
REP(j,0,sz-1) {
if (s>>j&1) x*=p[i][j];
else ++y;
}
dp[x][y] = i;
}
}
REP(i,1,q) printf("%d\n",ans[i]);
}
Make Square CodeForces - 1028H (dp)的更多相关文章
- hdu 1398 Square Coins(简单dp)
Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Pro ...
- Two Melodies CodeForces - 813D (DP,技巧)
https://codeforces.com/problemset/problem/813/D dp[i][j] = 一条链以i结尾, 另一条链以j结尾的最大值 关键要保证转移时两条链不能相交 #in ...
- Consecutive Subsequence CodeForces - 977F(dp)
Consecutive Subsequence CodeForces - 977F 题目大意:输出一序列中的最大的连续数列的长度和与其对应的下标(连续是指 7 8 9这样的数列) 解题思路: 状态:把 ...
- codeforces的dp专题
1.(467C)http://codeforces.com/problemset/problem/467/C 题意:有一个长为n的序列,选取k个长度为m的子序列(子序列中不能有位置重复),求所取的k个 ...
- HDU 6125 Free from square 状态压缩DP + 分组背包
Free from square Problem Description There is a set including all positive integers that are not mor ...
- HDU 5903 Square Distance (贪心+DP)
题意:一个字符串被称为square当且仅当它可以由两个相同的串连接而成. 例如, "abab", "aa"是square, 而"aaa", ...
- HDU1398:Square Coins(DP水题)
Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- Codeforces 721C [dp][拓扑排序]
/* 题意:给你一个有向无环图.给一个限定t. 问从1点到n点,在不超过t的情况下,最多可以拜访几个点. 保证至少有一条路时限不超过t. 思路: 1.由无后向性我们可以知道(取决于该图是一个DAG), ...
- CodeForces 607C (DP) Hard problem
题目:这里 题意:给定n个字符串,每个字符串可以进行一项操作,就是将这个字符串交换,就是该字符串的第一个和最后一个交换,第二个和倒数第二个交换,以此类推,当然可以选择对于 该字符串进行或不进行这项操作 ...
随机推荐
- python,在路径中引用变量的方法
fr = open('E:\\pyCharm\\LogisticRegression\\1\\'+变量+'.txt')
- Nutch、Scrapy、Lucene、Heritrix、Solr、Sphinx
Nutch.Scrapy.Lucene.Heritrix.Solr.Sphinx
- Vuejs函数式组件,你值得拥有(1)
函数式组件在React社区很流行使用,那么在vue里面我们要怎么用呢 下面会涉及到的知识点: 高阶函数.状态.实例.vue组件 什么是函数式组件 我们可以把函数式组件想像成组件里的一个函数,入参是渲染 ...
- PHP 循环输出多重数组元素
<?php $arr = array( array( array( array( array( ,, ) ) ), array( ,,) ) ), array(, , ) ); function ...
- 算法习题---5.1大理石在哪(UVa10474)
一:题目 现有N个大理石,每个大理石上写了一个非负整数.首先把各数从小到大排序,然后回答Q个问题.每个问题问是否有一个大理石写着某个整数x,如果是,还要回答哪个大理石上写着x.排序后的大理石从左到右编 ...
- 【插件】thinkphp5+百度编辑器自定义上传
1 官方下载sdk 2 在引入编辑器页面.写入js // 百度编辑器 UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActi ...
- Python初级 2 记住内存和变量的练习
一.数据类型: 数字:3, 5, 100, 50.35 字符串:"abc","wang" 字符串或数字可以由名字来表示,名字也叫变量 二.算术表达式: 形如3 ...
- Vue cli4.0 代理配置
proxy: { '/service': { target: 'http://192.168.40.243:3000/', //对应自己的接口 changeOrigin: true, ws: true ...
- 什么是 https ?这应该是全网把 https 讲的最好的一篇文章了
https://blog.csdn.net/m0_37907797/article/details/102759257
- 【kubernetes secret 和 aws ecr helper】kubernetes从docker拉取image,kubernetes docker私服认证(argo docker私服认证),no basic auth credentials错误解决
aws ecr helper: https://aws.amazon.com/blogs/compute/authenticating-amazon-ecr-repositories-for-dock ...