HackerRank - Sherlock and Queries

All about pruning and duplication removal. Took me several submissions to get it AC:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;

#define BOUND (1000000007)
int main()
{
    //    Get input
    int n, m; cin >> n >> m;
    vector<long long> A(n);
    for(int i = 0; i < n; i ++)        cin >> A[i];
    vector<long long> B(m);
    for(int i = 0; i < m; i ++)        cin >> B[i];
    vector<long long> C(m);
    for(int i = 0; i < m; i ++)        cin >> C[i];

    //
    unordered_map<int, long long> hm;
    for(int i = 0; i < m; i ++)
    {
        if(hm.find(B[i]) == hm.end())
        {
            hm[B[i]] = C[i];
        }
        else
        {
            hm[B[i]] *= C[i];
            hm[B[i]] %= BOUND;
        }
    }

    //
    long long facOne = 1;
    for(auto &e: hm)
    {
        if(e.first == 1)
        {
            facOne *= e.second;
            facOne %= BOUND;
            continue;
        }

        int j = e.first - 1;
        while(j < n)
        {
            A[j] *= e.second;
            A[j] %= BOUND;
            j += e.first;
        }
    }

    //
    for_each(A.begin(), A.end(), [&](long long v){
        v = (v * facOne) % BOUND;
        cout << v << " ";});
    return 0;
}
时间: 2024-12-28 20:53:25

HackerRank - Sherlock and Queries的相关文章

HackerRank - &quot;Sherlock and GCD&quot;

This is a very smart observation:http://www.martinkysel.com/hackerrank-sherlock-and-gcd-solution/ # http://www.martinkysel.com/hackerrank-sherlock-and-gcd-solution/ # from functools import reduce def gcd(a, b): if a == 1 or b == 1: return 1 if a % b

HackerRank - Sherlock and The Beast

Greedy beats DP this time... I tried several DP solutions first, but all failed with RE\TLE. If you 'feel' the problem, Greedy should be working: (A solution from discussion) def getPivot(n): while n > 0: if n % 3 == 0: break; else: n -= 5 return n T

HackerRank - Sherlock and Anagram

Please note input constraints. String length will not exceed 100, which means, we can use relatively naive representation\calculation for anagrams: sorting. #include <cmath> #include <cstdio> #include <vector> #include <iostream> #

【HackerRank】Sherlock and MiniMax

题目连接:Sherlock and MiniMax Watson gives Sherlock an array A1,A2...AN. He asks him to find an integer M between P and Q(both inclusive), such that, min {|Ai-M|, 1 ≤ i ≤ N} is maximised. If there are multiple solutions, print the smallest one. Input For

【HackerRank】Sherlock and Array

Watson gives an array A1,A2...AN to Sherlock. Then he asks him to find if there exists an element in the array, such that, the sum of elements on its left is equal to the sum of elements on its right. If there are no elements to left/right, then sum

【HackerRank】 Sherlock and The Beast

Sherlock and The Beast Sherlock Holmes is getting paranoid about Professor Moriarty, his archenemy. All his efforts to subdue Moriarty have been in vain. These days Sherlock is working on a problem with Dr. Watson. Watson mentioned that the CIA has b

HackerRank &quot;Array and simple queries&quot; !

The most interesting, flexible and juicy binary tree problem I have ever seen. I learnt it from here: https://codepair.hackerrank.com/paper/5fIoGg74?b=eyJyb2xlIjoiY2FuZGlkYXRlIiwibmFtZSI6IkJsdWVCaXJkMjI0IiwiZW1haWwiOiJoZWFsdGh5dG9ueUBnbWFpbC5jb20ifQ%

HDU4027 Can you answer these queries 线段树区间求和+剪枝

给了你n,然后n个数字在一个数组中,接下来m个询问,每个询问三个数字 t,x,y,若t==0,那么修改区间[x,y]的每一个值,变为原来每个位置上的数 开根号取整,若t==1,那么对区间[x,y]求和 由于n,m,很大,所以树状数组铁定超时,若直接用线段树来做区间修改,那么也是超时,这类题目没别的方法了,静心剪枝,发现题目给的数据范围为2^63,有没有发现,2^63开根号 绝对不需要开10次,就能到1,到1以后就不需要再开了,意思就是若有某个区间[x,y]每一个点的值都为1时,这一段区间事实上是

[CodeChef - GERALD07 ] Chef and Graph Queries

Read problems statements in Mandarin Chineseand Russian. Problem Statement Chef has a undirected graph G. This graph consists of N vertices and M edges. Each vertex of the graph has an unique index from 1 to N, also each edge of the graph has an uniq