当前位置:网站首页>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
边栏推荐
- Floweable source code annotation (40) class delegation
- 穿越派·派盘 + Mountain Duck = 数据本地管理
- excel高级绘图技巧100讲(一)-用甘特图来展示项目进度情况
- 扩展点系列之SmartInstantiationAwareBeanPostProcessor确定执行哪一个构造方法 - 第432篇
- 数据库连接池的简单实现
- 【笔记】电商订单数据分析实战
- 4GB大文件,如何实时远程传输和共享?
- Fiber Bragg grating (FBG) notes [1]: waveguide structure and Bragg wavelength derivation
- boot+jsp的高校社团管理系统(附源码下载链接)
- This is the necessary software for college students 𞓜 knowledge management
猜你喜欢
![[QT] QT after addition, subtraction, multiplication and division, two decimal places are reserved](/img/30/c802ae9b65601832bf52e760e9962d.png)
[QT] QT after addition, subtraction, multiplication and division, two decimal places are reserved

scope 数据导出mat

boot+jsp的高校社團管理系統(附源碼下載鏈接)

Trust guessing numbers game

为什么用葫芦儿派盘取代U盘?

Brief description of activation function

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

Educational administration management system (free source code)

Speed regulation and stroke control based on Ti drv8424 driving stepper motor

【考研高数 武忠祥+880版 自用】高数第二章基础阶段思维导图
随机推荐
这才是大学生必备软件 | 知识管理
Printk debugging summary
QT waiting box production
Idea start view project port
如何添加葫芦儿派盘
It's not that you have a bad mind, but that you haven't found the right tool
Know the future of "edge computing" from the Nobel prize!
What is the at instruction set often used in the development of IOT devices?
【考研高数 武忠祥+880版 自用】高数第二章基础阶段思维导图
What things you didn't understand when you were a child and didn't understand until you grew up?
CentOS 7使用yum安装PHP7.0
Advanced cross platform application development (II): uni app practice
【笔记】电商订单数据分析实战
[QT] QT after addition, subtraction, multiplication and division, two decimal places are reserved
加密狗资料搜集
Debug details under pycharm
C语言初阶——牛客网精选好题
激活函数简述
This is the necessary software for college students 𞓜 knowledge management
OpenGL ES: (5) OpenGL的基本概念、OpenGL ES 在屏幕产生图片的过程、OpenGL管线(pipeline)