对象已移动

可在此处找到该文档 Find product of GCDs of all possible row-column pair of given Matrix – New Self New Life
New Self New Life
No Result
View All Result
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices
New Self New Life
No Result
View All Result
Home Softwares

Find product of GCDs of all possible row-column pair of given Matrix

by admin
3 years ago
in Softwares
Best Coding Practices For Rest API Design
Share on FacebookShare on Twitter


Given a matrix of dimension N*M, the duty is to seek out the product of all attainable pairs of (i, j) the place i and j are the row quantity and column quantity respectively.

Observe: For the reason that reply could be very giant output the reply modulo 1000000007.

Examples:

Enter: N = 5, M = 6
Output: 5760
Rationalization: The values of GCD of every attainable pair

1 1 1 1 1 1
1 2 1 2 1 2
1  1 3 1 1 3
1 2 1 4 1 2
1 1 1 1 5 1

The product of grid = 1*1*1*1*1*1*1*2*1*2*1*2*1*1*3*1*1*3*1*2*1*4*1*2*1*1*1*1*5*1 = 5760

Enter: N = 34, M = 46
Output: 397325354

Naive Strategy: To unravel the issue traverse all of the attainable pairs of row and column and discover the GCD of them and multiply them with the required reply.

Comply with the steps talked about under to implement the thought:

  • Initialize a variable ans = 1 to retailer the product.
  • Iterate from i = 1 to N:
    • For every worth of i traverse from 1 to M.
    • Calculate the GCD of every pair.
    • Multiply this with ans.
  • Return the ultimate worth of ans because the required reply.

Beneath is the implementation of the above method.

C++

  

#embody <bits/stdc++.h>

utilizing namespace std;

  

int M = 1e9 + 7;

  

int gridPower(int n, int m)

{

    lengthy lengthy ans = 1;

    for (int i = 1; i <= n; i++) {

        for (int j = 1; j <= m; j++) {

            ans = (ans * __gcd(i, j)) % M;

        }

    }

    return ans;

}

  

int foremost()

{

    int N = 5, M = 6;

  

    

    cout << gridPower(N, M) << endl;

    return 0;

}

Time Complexity: O(N*M*log(min(N, M)))
Auxiliary House: O(1)

Environment friendly Strategy: To unravel the issue comply with the under thought:

It may be noticed that for each row, a sample is fashioned until the row quantity and after that, the identical sample repeats.

1 1 1 1 1 1
1 2 1 2 1 2
1 1 3 1 1 3
1 2 1 4 1 2
1 1 1 1 5 1

For instance within the above grid of 4 rows and 6 columns

In row 1, all of the values are 1
In row 2, until index 2 a sample is fashioned and after that very same sample repeats
In row 3, until index 3 a sample is fashioned and after that very same sample repeats

Comparable observations could be made for all different rows.

Therefore for each row, we solely want to seek out the sample as soon as and multiply that sample energy the variety of instances it happens. This may be completed utilizing Modular exponentiation technique. And eventually we have to multiply the remaining sample energy that is the same as Mpercenti for ith row.

Additionally, we are able to take into account the row because the minimal of N and M to scale back time complexity additional.

Beneath is the implementation of the above method:

C++

  

#embody <bits/stdc++.h>

utilizing namespace std;

  

int M = 1e9 + 7;

  

int fastpower(lengthy lengthy a, int p)

{

    lengthy lengthy res = 1;

    whereas (p > 0) {

        if (p % 2)

            res = (res * a) % M;

        p /= 2;

        a = (a * a) % M;

    }

    return res;

}

  

int gridPower(int n, int m)

{

    lengthy lengthy res = 1;

    for (int i = 1; i <= min(n, m); i++) {

        lengthy lengthy patternPower = 1;

  

        

        

        lengthy lengthy patternOccurence = max(n, m) / i;

  

        

        

        lengthy lengthy stays = max(n, m) % i;

  

        

        

        lengthy lengthy remainsPower = 1;

  

        

        

        for (int j = 1; j <= i; j++) {

            patternPower = (patternPower * __gcd(i, j)) % M;

  

            if (j == stays)

                remainsPower = patternPower;

        }

  

        res = (res

               * fastpower(patternPower, patternOccurence))

              % M;

        res = (res * remainsPower) % M;

    }

    return res;

}

  

int foremost()

{

    int N = 5, M = 6;

  

    

    cout << gridPower(N, M) << endl;

    return 0;

}

Time Complexity: min(N, M)*min(N, M)*log(min(N, M))
Auxiliary House: O(1)



Source link

Tags: FindGCDsMatrixpairproductrowcolumn
Previous Post

Britney Spears slammed for ‘body shaming’ dancers with ‘offensive’ post. : entertainment

Next Post

The Denim Superlatives: 12 Cuts So Good, They Deserve an Award

Related Posts

Huawei Cloud rolls out Pangu Models 5.5 to cover more industries
Softwares

Huawei Cloud rolls out Pangu Models 5.5 to cover more industries

by admin
June 24, 2025
Minor update(4) for Vivaldi Android Browser 7.4
Softwares

Minor update(4) for Vivaldi Android Browser 7.4

by admin
June 21, 2025
How AI Medical Coding Software Reduces Errors & Accelerates Billing in 2025
Softwares

How AI Medical Coding Software Reduces Errors & Accelerates Billing in 2025

by admin
June 22, 2025
10+ Best Free Portfolio & Lookbook Templates for InDesign in 2025 — Speckyboy
Softwares

10+ Best Free Portfolio & Lookbook Templates for InDesign in 2025 — Speckyboy

by admin
June 20, 2025
User Guide For CS-Cart Product Search By Barcode
Softwares

User Guide For CS-Cart Product Search By Barcode

by admin
June 18, 2025
Next Post
The Denim Superlatives: 12 Cuts So Good, They Deserve an Award

The Denim Superlatives: 12 Cuts So Good, They Deserve an Award

Klarus XT11GT Pro 2.0 flashlight review – Copious photons to vanquish the shadows!

Klarus XT11GT Pro 2.0 flashlight review - Copious photons to vanquish the shadows!

  • Trending
  • Comments
  • Latest
Pamela Anderson raves about new natural, makeup-free look: ‘It’s freedom’

Pamela Anderson raves about new natural, makeup-free look: ‘It’s freedom’

October 8, 2023
Alec Baldwin indicted again for ‘Rust’ shooting that left cinematographer dead – National

Alec Baldwin indicted again for ‘Rust’ shooting that left cinematographer dead – National

January 21, 2024
I Tried Calocurb For 90 Days. Here’s My Review.

I Tried Calocurb For 90 Days. Here’s My Review.

January 8, 2025
A look into CAMPUS, ShopBack’s new Singapore HQ at Pasir Panjang

A look into CAMPUS, ShopBack’s new Singapore HQ at Pasir Panjang

July 2, 2022
Guide for Bagisto Quick Commerce

Guide for Bagisto Quick Commerce

October 16, 2024
User Manual for Odoo Docx Report Builder

User Manual for Odoo Docx Report Builder

November 30, 2024
Aaron Rodgers returns to ‘Pat McAfee Show’ 1 day after being axed by host – National

Aaron Rodgers returns to ‘Pat McAfee Show’ 1 day after being axed by host – National

January 11, 2024
8BitDo Retro Mechanical Keyboard C64 Review

8BitDo Retro Mechanical Keyboard C64 Review

March 24, 2025
Jeff Bezos and Lauren Sanchez wedding live updates: all the guests attending the $10 million ceremony

Jeff Bezos and Lauren Sanchez wedding live updates: all the guests attending the $10 million ceremony

June 25, 2025
Who Is Karmelo Anthony? About His Case & Updates on His Trial – Hollywood Life

Who Is Karmelo Anthony? About His Case & Updates on His Trial – Hollywood Life

June 25, 2025
Over 140 People ‘Pricked’ With Syringes at Music Festival

Over 140 People ‘Pricked’ With Syringes at Music Festival

June 24, 2025
Samsung Galaxy Watch 8 Classic: Battery, Design, and More

Samsung Galaxy Watch 8 Classic: Battery, Design, and More

June 24, 2025
Shane Gillis Set As Host Of The 2025 ESPYS For ABC & ESPN+

Shane Gillis Set As Host Of The 2025 ESPYS For ABC & ESPN+

June 24, 2025
Cobra Kai star Martin Kove ‘kicked out of fan convention’ after allegedly ‘biting’ co-star Alicia Hannah-Kim

Cobra Kai star Martin Kove ‘kicked out of fan convention’ after allegedly ‘biting’ co-star Alicia Hannah-Kim

June 24, 2025
Liam Neeson Drives a Bus to Escape Himalayan Kidnappers in Trailer For ICE ROAD: VENGEANCE — GeekTyrant

Liam Neeson Drives a Bus to Escape Himalayan Kidnappers in Trailer For ICE ROAD: VENGEANCE — GeekTyrant

June 24, 2025
X Adds More Sports Engagement Options To Maintain Community Engagement

X Adds More Sports Engagement Options To Maintain Community Engagement

June 24, 2025
New Self New Life

Your source for entertainment news, celebrities, celebrity news, and Music, Cinema, Digital Lifestyle and Social Media and More !

Categories

  • Celebrity
  • Cinema
  • Devices
  • Digital Lifestyle
  • Entertainment
  • Music
  • Social Media
  • Softwares
  • Uncategorized

Recent Posts

  • Jeff Bezos and Lauren Sanchez wedding live updates: all the guests attending the $10 million ceremony
  • Who Is Karmelo Anthony? About His Case & Updates on His Trial – Hollywood Life
  • Over 140 People ‘Pricked’ With Syringes at Music Festival
  • Home
  • Disclaimer
  • DMCA
  • Privacy Policy
  • Cookie Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2021 New Self New Life.
New Self New Life is not responsible for the content of external sites. slotsfree  creator solana token

No Result
View All Result
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices

Copyright © 2021 New Self New Life.
New Self New Life is not responsible for the content of external sites.

New Self New Life