D. Yarik and Musical Notes Codeforces Round 909 (Div. 3) 1899D

Problem - D - Codeforces

The main idea of ​​the question: Given an array a of n numbers, ask how many pairs of i and j satisfy i<j, 2 a[i] to the power 2 a[j] is equal to 2 a[j] a[i] power of 2

1<=n<=1e5;1<=a[i]<=1e9

Idea: Use a fast power table to find a[i], a[j] that meet the conditions, or a[i]=a[j] or one number is equal to 1 and the other is equal to 2, so we only need to , find out how many numbers are equal to it behind it, if it is 1 or 2, then check the number of 2 or 1 behind it.

Then we first find the number of occurrences of each number, and then each time we traverse a number, we make the number of occurrences of that number -1. At this time, the number of occurrences of this number is equal to how many of this number are behind it.

//#include<bits/stdc++.h>
#include<__msvc_all_public_headers.hpp>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
const ll MOD = 1e9 + 7;
ll n;
ll a[N];
ll qpow(ll a, ll b)
{//快速幂
    if (b <= 0)
        return 1;
    ll ret = 1, temp = a;
    while (b)
    {
        if (b & 1)
        {
            ret = ret * a % MOD;
        }
        a = a * a % MOD;
        b >>= 1;
    }
    return ret;
}
void init()
{

}
void solve()
{
    cin >> n;
    init();
    map<ll, ll>cnt;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        cnt[a[i]]++;//求每个数的出现次数
    }
    ll ans = 0;
    for (int i = 1; i <= n; i++)
    {
        cnt[a[i]]--;//当前数出现次数-1
        ans += cnt[a[i]];
        if (a[i] == 1)
        {//特判1和2
            ans += cnt[2];
        }
        if (a[i] == 2)
        {
            ans += cnt[1];
        }
    }
    cout << ans;
    cout << '\n';
}
int main()
{
    /*for (int i = 1; i <= 60; i++)
    {
        for (int j = 1; j <= 60; j++)
        {
            ll temp1 = qpow(2, i), temp2 = qpow(2, j);
            if (qpow(temp1, temp2) == qpow(temp2, temp1))
            {
                cout << i << " " << j << '\n';
            }
        }
    }*/
    //打表找规律
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/ashbringer233/article/details/134477979