当前位置:网站首页>Code shoe set - mt3149 · and - the data is not very strong. Violent pruning can deceive AC
Code shoe set - mt3149 · and - the data is not very strong. Violent pruning can deceive AC
2022-07-01 05:45:00 【Tisfy】
Portal
AND
The time limit :1 second
Space restriction :64M
Title Description
Give a long n Sequence of numbers a, set up p i , j = a i p_{i,j}=a_i pi,j=ai and a i + 1 a_{i+1} ai+1 and ……and a j a_j aj,
seek ∑ i = 1 n ∑ j = i n p i , j \sum_{i=1}^n\sum_{j=i}^np_{i,j} ∑i=1n∑j=inpi,j
Input description
The first line is an integer n, Represents the length of a sequence .
The second line n An integer represents a sequence of numbers a.
Data range
Data range
1 ≤ n ≤ 100000 , 0 ≤ a i ≤ 1 0 9 1\le n \le100000,0\le a_i \le 10^9 1≤n≤100000,0≤ai≤109
Output description
An integer represents the answer .
Example 1
Input
5
5 8 4 5 6
Output
40
Preface
The data of this question is weak , I was thinking of violence first to see how many groups I could pass , The result is pure violence 7 7 7 Group .
Then I want to see how much I can get with pruning , The result is AC 了 .
Then I didn't think about any more methods with lower complexity .
The complexity of the method used in this blog is O ( n 2 ) O(n^2) O(n2), By the end of 2022 year 6 month 30 Japan 18:38 You can pass this question .
I wonder if there will be new test data in the future .
Topic analysis
First, let's think about how violence . The initial violence complexity is O ( n 3 ) O(n^3) O(n3):
from 1 To n enumeration i
from i To n enumeration j
from i AND To j
But it's easy to think of , stay j j j from i i i Enumerate to n n n When , The new result can be used on the basis of the old one O ( 1 ) O(1) O(1) The time complexity of .
Speak in a professional language , Namely :
In known p i , j p_{i,j} pi,j Under the premise of , It can be used O ( 1 ) O(1) O(1) The time complexity of p i , j + 1 p_{i,j+1} pi,j+1
The formula is : p i , j + 1 = p i , j & a [ j + 1 ] p_{i,j+1}=p_{i,j} \& a[j+1] pi,j+1=pi,j&a[j+1]
such , The time complexity will be for O ( n 2 ) O(n^2) O(n2)
int a[100010];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
ll ans = 0;
for (int i = 0; i < n; i++) {
ll k = a[i];
for (int j = i; j < n; j++) {
k &= a[j]; // p{i,j+1}=p{i,j} & a[j+1]
ans += k;
}
}
cout << ans << endl;
return 0;
}
In this way, you can pass 3 3 3 Group data .
prune
It's not hard to find out , If p i , j = 0 p_{i,j}=0 pi,j=0, that p i , j + 1 p_{i,j+1} pi,j+1、 p i , j + 2 p_{i,j+2} pi,j+2、 ⋯ \cdots ⋯、 p i , n p_{i,n} pi,n All for 0 0 0
because 0 A N D ren What Count all by 0 0\ AND\ Any number \ All for 0 0 AND ren What Count all by 0 .
Therefore, the above code is in k = 0 k=0 k=0 When , We directly b r e a k break break that will do .
I'll just try , Just AC 了 .
But watch out , Whenever there is a group that is all 1 1 1 Of 1 e 5 1e5 1e5 The data of , The above method will not pass
AC Code
#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
int a[100010];
int main() {
int n;
cin >> n;
fi (i, 0, n) // for (int i = 0; i < n; i++)
cd(a[i]); // scanf a[i]
ll ans = 0;
for (int i = 0; i < n; i++) {
ll k = a[i];
for (int j = i; j < n; j++) {
k &= a[j];
if (!k)
break; // prune
ans += k;
}
}
cout << ans << endl;
return 0;
}
Update the weekly competition solution of elite class in advance every week , Focus , Neverlost
Originality is not easy. , Reprint please attach Link to the original text Oh ~
Tisfy:https://letmefly.blog.csdn.net/article/details/125546150
边栏推荐
- Data governance: metadata management implementation (Part IV)
- 加密狗资料搜集
- What things you didn't understand when you were a child and didn't understand until you grew up?
- HDU - 1024 Max Sum Plus Plus(DP)
- In depth understanding of condition source code interpretation and analysis of concurrent programming
- mysql 将毫秒数转为时间字符串
- CentOS 7使用yum安装PHP7.0
- 码蹄集 - MT3149 · AND - 数据不是很强,暴力剪枝就能骗AC
- Basic electrician knowledge 100 questions
- 分片上传与断点续传
猜你喜欢

Dear pie users, I want to confess to you!

教务管理系统(免费源码获取)

Brief description of activation function

【考研高数 自用】高数第一章基础阶段思维导图

How to create a progress bar that changes color according to progress

In depth understanding of condition source code interpretation and analysis of concurrent programming

葫芦儿 APP 使用帮助

Know the future of "edge computing" from the Nobel prize!
SSM的教务管理系统(免费源码获取)

What is the at instruction set often used in the development of IOT devices?
随机推荐
Trust guessing numbers game
What things you didn't understand when you were a child and didn't understand until you grew up?
vsCode函数注解/文件头部注解快捷键
论文学习记录随笔 多标签之LIFT
SSM的教务管理系统(免费源码获取)
excel高级绘图技巧100讲(一)-用甘特图来展示项目进度情况
Mongodb學習篇:安裝後的入門第一課
Crossing sect · paipan + Siyuan notes = private notebook
新手在挖财开通证券账户安全吗?
In depth understanding of condition source code interpretation and analysis of concurrent programming
bat操作ftp上传下载命令
Ssm+mysql second-hand trading website (thesis + source code access link)
C语言初阶——实现扫雷游戏
College community management system based on boot+jsp (with source code download link)
Advanced cross platform application development (II): uni app practice
Mongodb学习篇:安装后的入门第一课
Chapitre d'apprentissage mongodb: Introduction à la première leçon après l'installation
mysql 将毫秒数转为时间字符串
千万不要把笔记视频乱放!
POL8901 LVDS转MIPI DSI 支持旋转图像处理芯片