当前位置:网站首页>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
边栏推荐
- [QT] QT after addition, subtraction, multiplication and division, two decimal places are reserved
- Why use huluer pie disk instead of U disk?
- Advanced cross platform application development (II): uni app practice
- 从MLPerf谈起:如何引领AI加速器的下一波浪潮
- 码蹄集 - MT3149 · AND - 数据不是很强,暴力剪枝就能骗AC
- 数据治理:数据治理管理(第五篇)
- Idea start view project port
- Learn the customization and testing of fpga---ram IP from the bottom structure
- Crossing pie · pie pan + Mountain duck = local data management
- Is it safe for a novice to open a securities account?
猜你喜欢

What is the at instruction set often used in the development of IOT devices?
![[excel] column operation, which performs specific column for data in a cell, such as text division by comma, colon, space, etc](/img/c8/e3e31ad9ef214d97228cb501dd752f.jpg)
[excel] column operation, which performs specific column for data in a cell, such as text division by comma, colon, space, etc

A little assistant for teenagers' physiological health knowledge based on wechat applet (free source code + project introduction + operation introduction + operation screenshot + Thesis)

不是你脑子不好用,而是因为你没有找到对的工具

Build 2022 上开发者最应关注的七大方向主要技术更新

Learn the customization and testing of fpga---ram IP from the bottom structure

Basic electrician knowledge 100 questions

Boot + jsp University Community Management System (with source Download Link)

Chip, an empire built on sand!

OpenGL ES: (5) OpenGL的基本概念、OpenGL ES 在屏幕产生图片的过程、OpenGL管线(pipeline)
随机推荐
为什么用葫芦儿派盘取代U盘?
Leetcode top 100 questions 1 Sum of two numbers
HCM 初学 ( 二 ) - 信息类型
Educational administration management system of SSM (free source code)
[SRS] use of Vhost isolated stream: push / pull Stream Address
Why use huluer pie disk instead of U disk?
JDBC common interview questions
C语言初阶——牛客网精选好题
Printk debugging summary
mysql 将毫秒数转为时间字符串
新手在挖财开通证券账户安全吗?
论文学习记录随笔 多标签之GLOCAL
It's not that you have a bad mind, but that you haven't found the right tool
如何添加葫芦儿派盘
Learn the customization and testing of fpga---ram IP from the bottom structure
Trust guessing numbers game
Leetcode top 100 question 2 Add two numbers
Web Security (IX) what is JWT?
【问题思考总结】为什么寄存器清零是在用户态进行的?
SSGSSRCSR区别