zoukankan      html  css  js  c++  java
  • Codeforces 567C:Geometric Progression(DP)

    time limit per test : 1 second
    memory limit per test : 256 megabytes
    input : standard input
    output : standard output

    Problem Description

     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 (i_1, i_2, i_3), that (1 ≤ i_1 < i_2 < i_3 ≤ 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·k^0, b·k^1, ..., b·k^{r - 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·10^5)), showing how many numbers Polycarp's sequence has and his favorite number.

    The second line contains (n) integers (a_1, a_2, ..., a_n ( - 10^9 ≤ a_i ≤ 10^9)) — 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).

    Examples

    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.

    题意

    (n)个数中选出三个数,要求组成的序列公比为(k),并且不改变这三个数的前后关系,问有多少种选择方案

    思路

    (DP),因为(- 10^9 ≤ a_i ≤ 10^9) ,所以还需要用到(map)

    定义(dp[i][j])表示把数字(j)放在等比数列第(i)个位置的方案数

    (dp[i][j]+=dp[i-1][j/k])

    将所有长度为(3)的方案数加起来即可

    代码

    #include <bits/stdc++.h>
    #define ll long long
    #define ull unsigned long long
    #define ms(a,b) memset(a,b,sizeof(a))
    const int inf=0x3f3f3f3f;
    const ll INF=0x3f3f3f3f3f3f3f3f;
    const int maxn=1e6+10;
    const int mod=1e9+7;
    const int maxm=1e3+10;
    using namespace std;
    ll a[maxn];
    int main(int argc, char const *argv[])
    {
        #ifndef ONLINE_JUDGE
            freopen("/home/wzy/in", "r", stdin);
            freopen("/home/wzy/out", "w", stdout);
            srand((unsigned int)time(NULL));
        #endif
        ios::sync_with_stdio(false);
        cin.tie(0);
        int n,k;
        cin>>n>>k;
        map<int,ll>dp[4];
        ll ans=0;
        for(int i=1;i<=n;i++)
        {
        	cin>>a[i];
        	if(a[i]%k==0)
        	{
        		ans+=dp[2][a[i]/k];
        		dp[2][a[i]]+=dp[1][a[i]/k];
        	}
        	dp[1][a[i]]++;
        }
        cout<<ans<<endl;
        #ifndef ONLINE_JUDGE
            cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
        #endif
        return 0;
    }
    
  • 相关阅读:
    k-d tree
    K近邻算法-KNN
    ORB特征提取与匹配
    ZeroMQ一个更小、更快、更简单的智能传输层协议
    ROS导航之参数配置和自适应蒙特卡罗定位
    cmake实战第二篇:让我们的代码更像个工程
    gcc/g++实战之动态链接库与静态链接库编写
    gcc/g++ 实战之编译的四个过程
    通过 LPeg 介绍解析表达式语法(Parsing Expression Grammars)
    Forth 语言概要
  • 原文地址:https://www.cnblogs.com/Friends-A/p/11686277.html
Copyright © 2011-2022 走看看