CodeForces 567C. Geometric Progression(map 数学啊)

题目链接:http://codeforces.com/problemset/problem/567/C

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.

题意:

给定一个数列,要求所有的子序列(顺序一定),是长度为3的等比数列(公比为K)的个数。

PS:

map!

代码如下:

#include <cstdio>
#include <cstring>
#include <map>
#include <iostream>
using namespace std;
typedef long long LL;

map<int, LL> s1, s2;
int main()
{
    LL n, k;
    LL ans, x;
    cin >> n >> k;
    ans = 0;
    for(int i = 0; i < n; i++)
    {
        scanf("%I64d",&x);
        if(x%k == 0)
        {
            LL t1 = s2[x/k];
            ans += t1;
            LL t2 = s1[x/k];
            s2[x] += t2;
        }
        s1[x]++;
    }
    cout << ans << endl;
    return 0;
}
/*
10 3
1 2 6 2 3 6 9 18 3 9
*/

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-08 22:09:31

CodeForces 567C. Geometric Progression(map 数学啊)的相关文章

Codeforces 567C - Geometric Progression - [map维护]

题目链接:https://codeforces.com/problemset/problem/567/C 题意: 给出长度为 $n$ 的序列 $a[1:n]$,给出公比 $k$,要求你个给出该序列中,长度为 $3$ 的等比子序列的数目. 题解: 首先倒着遍历,用map记录曾经出现过的每个数字的出现次数,然后再用另一个map来记录曾经出现过的所有满足 $(x,kx)$ 的二元组的数目,最后就直接维护答案即可. AC代码: #include<bits/stdc++.h> #define IO (i

CodeForces 567C Geometric Progression

Geometric Progression Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 567C Description Polycarp loves geometric progressions very much. Since he was only three years old, he loves only t

CodeForces 567C Geometric Progression 类似dp的递推统计方案数

input n,k 1<=n,k<=200000 a1 a2 ... an 1<=ai<=1e9 output 数组中选三个数,且三个数的下标严格递增,凑成形如b,b*k,b*k*k的种数 做法:先将可以作为第三个数的数放到map中,然后再扫一遍依次统计map中的数作为第三个数的种数,第二个数的种数,第三个数的种数 1 #include<cstdio> 2 #include<map> 3 struct node 4 { 5 int b;//a[i]作为i1的

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

题目传送门 1 /* 2 题意:问选出3个数成等比数列有多少种选法 3 map:c1记录是第二个数或第三个数的选法,c2表示所有数字出现的次数.别人的代码很短,思维巧妙 4 */ 5 /************************************************ 6 * Author :Running_Time 7 * Created Time :2015-8-6 1:07:18 8 * File Name :C.cpp 9 *************************

Codeforces Round #Pi (Div. 2)——map——Geometric Progression

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

CodeForces 449C Jzzhu and Apples 数学+素数

这道题目晚上本来就花了很多把都××了,着实觉得自己思路没错啊,回顾一下思路,给你n个数,分成两两组合一对,分成最多组如何分,但是组合的两个数 不能互素,所以呢 偶数肯定是好的了,所以先放着,先把素数给搞定,10^5所以枚举所有包含该素数因子的数,如果刚好分组则最好,不然的话其中有偶数的踢掉一个给下面的偶数处理部分,最后再处理偶数的部分,这样肯定满足组数最多,完全没有问题,后来方法确实是没问题啊,只是代码有问题,我靠!真是脑残!,今天看到一位大牛的想法,我跟他是一样的,只是代码写搓了,后来改了又改

Codeforces Round #259(div2)C(数学期望)

数学题. 关键是求最大值为k时有多少种情况,结果是kn-(k-1)n-1.可以这么想:每一次都从1至k里选,共kn种,这里需要再减去每一次都从1至k-1里面选的情况.当然也可以分类计数法:按出现几次k来分类,然后逆着用一下二项式定理得出结论. 整个的期望是Σk(kn-(k-1)n-1)/mn,其中k=1......n. 这里的技巧在于:由于n<=105,   kn显然会RE,那么就先把分母除上,每次算一个小于1的浮点数的n次方,肯定不会RE.C++中乘方用pow函数算是很快的. #include

CodeForces - 670C Cinema (map&amp;模拟)水

CodeForces - 670C Cinema Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description Moscow is hosting a major international conference, which is attended by n scientists from different countries. Each of th

codeforces 616E. Sum of Remainders 数学

题目链接 给两个数n, m. 求n%1+n%2+.......+n%m的值. 首先, n%i = n-n/i*i, 那么原式转化为n*m-sigma(i:1 to m)(n/i*i). 然后我们可以发现  1/4 = 2/4 = 3/4 = 0, 4/4 = 5/4 = 6/4 = 7/4 = 1. 所以可以将这些结果分成很多块, 按块算结果. 注意计算过程中时刻避免爆longlong. #include <iostream> #include <vector> #include