• Codeforces Round #Pi (Div. 2) C. Geometric Progression


    C. Geometric Progression
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Polycarp loves geometric progressions very much. Since he was only three years old, he loves only the progressions of length three. He also has a favorite integer k and a sequence a, consisting of n integers.

    He wants to know how many subsequences of length three can be selected from a, so that they form a geometric progression with common ratio k.

    A subsequence of length three is a combination of three such indexes i1, i2, i3, that 1 ≤ i1 < i2 < i3 ≤ n. That is, a subsequence of length three are such groups of three elements that are not necessarily consecutive in the sequence, but their indexes are strictly increasing.

    A geometric progression with common ratio k is a sequence of numbers of the form b·k0, b·k1, ..., b·kr - 1.

    Polycarp is only three years old, so he can not calculate this number himself. Help him to do it.

    Input

    The first line of the input contains two integers, n and k (1 ≤ n, k ≤ 2·105), showing how many numbers Polycarp's sequence has and his favorite number.

    The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — elements of the sequence.

    Output

    Output a single number — the number of ways to choose a subsequence of length three, such that it forms a geometric progression with a common ratio k.

    Sample test(s)
    input
    5 2
    1 1 2 2 4
    output
    4
    input
    3 1
    1 1 1
    output
    1
    input
    10 3
    1 2 6 2 3 6 9 18 3 9
    output
    6
    Note

    In the first sample test the answer is four, as any of the two 1s can be chosen as the first element, the second element can be any of the 2s, and the third element of the subsequence must be equal to 4.

    维护当前数字a[i]的前面a[i]/k的数量

    维护当前数字a[i]的后面a[i]*k的数量

    那么答案就是:

    /* ***********************************************
    Author        : PK29
    Created Time  :2015/8/25 10:52:52
    File Name     :4.cpp
    ************************************************ */
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <stdio.h>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <iomanip>
    #include <list>
    #include <deque>
    #include <stack>
    #define ull unsigned long long
    #define ll long long
    #define mod 90001
    #define INF 0x3f3f3f3f
    #define maxn 200000+10
    #define cle(a) memset(a,0,sizeof(a))
    const ull inf = 1LL << 61;
    const double eps=1e-5;
    using namespace std;
    
    bool cmp(int a,int b){
        return a>b;
    }
    map<ll,ll>m1;
    map<ll,ll>m2;
    map<ll,ll>m3;
    ll a[maxn];
    int main()
    {
        #ifndef ONLINE_JUDGE
        //freopen("in.txt","r",stdin);
        #endif
        //freopen("out.txt","w",stdout);
        int n,k;
        while(cin>>n>>k){
            m1.clear();
            m2.clear();
            m3.clear();
            for(int i=1;i<=n;i++){
                scanf("%I64d",&a[i]);
                if(a[i]%k==0)
                m2[i]+=m1[a[i]/k];
                m1[a[i]]++;
            }
            m1.clear();
            for(int i=n;i>=1;i--){
                m3[i]+=m1[a[i]*k];
                    m1[a[i]]++;
            }
            ll sum=0;
            for(int i=1;i<=n;i++){
                sum+=m2[i]*m3[i];
            }
            printf("%I64d
    ",sum);
        }
        return 0;
    }
  • 相关阅读:
    细说google,baidu引擎收录习惯
    考研机试 11.二叉树遍历
    考研机试 18.特殊乘法
    考研机试 19.今年的第几天
    考研机试 20.完数VS盈数
    考研机试 12.玛雅人的密码
    考研机试 10.球的半径和体积
    考研机试 17.n的阶乘
    考研机试 15.abc
    考研机试 9.成绩排序
  • 原文地址:https://www.cnblogs.com/pk28/p/4757789.html
Copyright © 2020-2023  润新知