Codeforces Round #400 C. Molly ' s chemicals__codeforces

Source: Internet
Author: User

Molly Hooper has n different kinds of chemicals arranged in a line. Each of the chemicals has a affection value, the i-th of them has value AI.

Molly wants Sherlock to fall in love with her. She intends to does this by mixing a contiguous segment of chemicals together to make a love potion with total affection Val UE as a non-negative integer power of K. Total affection value of a continuous segment of chemicals n values of each chemical in that segment.

Help her into finding the total number of such segments. Input

The "I" of input contains two integers, N and K, the number of chemicals and the number, such that total affect Ion value is a non-negative power of this number K. (1≤n≤105, 1≤|k|≤10).

Next line contains n integers a1, a2, ..., an (-109≤ai≤109)-affection values of chemicals. Output

Output a single integer-the number of valid segments. Examples input

4 2
2 2 2 2
Output
8
Input
4-3
3-6-3 12
Output
3
Note

Do keep in mind that K0 = 1.

In the ' the ', Molly can get following different affection values:2: segments [1, 1], [2, 2], [3, 3], [4, 4];

4:segments [1, 2], [2, 3], [3, 4];

6:segments [1, 3], [2, 4];

8:segments [1, 4].

Out of these, 2, 4 and 8 are powers of k = 2. Therefore, the answer is 8.

In the second sample, Molly can choose segments [1, 2], [3, 3], [3, 4].

Given the number of N and K let you find out how many intervals to satisfy k^t.

Orz A group of people in the dormitory to drive for one hours this question did not come up with any good practice, really dish to pick foot.

Thinking is always limited to asking for prefixes and then enumerating all the intervals and verifying whether or not to satisfy the k^t, which is definitely timed out ...

In fact, we should change the way we enumerate the intervals and have sum[i]-sum[j]=k^t, then we enumerate the intervals and too time-consuming

We can change the equation to Sum[i]=k^t+sum[j], J's range we know 0---n-1 so we can save the K^t value first.

Sum[i] is the result of the prefix and array ... and then the record Mp[sum[i]] ... orz ... Take care to deal with K==-1 and K==1.

#include <bits/stdc++.h>
#define LL long-long 
using namespace std;
const int MAXN=1E5+10;
const LL INF=1E16+10;
int a,n,k;
ll sum[maxn],b[70];
map<ll,ll>mp;
void print ()
{
	b[0]=1;
	for (int i=1;i<=64;i++)
	if (b[i-1]<inf)
	b[i]=b[i-1]*k;
	return;
}
int main ()
{
	ll ans=0;
	cin>>n>>k;
	Mp.clear ();
	memset (sum,0,sizeof (sum));
	Print ();
	for (int i=1;i<=n;i++)
	{
		scanf ("%d", &a);
		for (int j=0;j<=64&&b[j]<inf;j++)
		{
			if (k==-1| | K==1)
			{
				if (k==-1)
				mp[sum[i-1]-1]++;
				mp[sum[i-1]+1]++;
				break;
			else
			{
				mp[sum[i-1]+b[j]]++;
			}
		}
		Sum[i]=sum[i-1]+a;
		Ans+=mp[sum[i]];
	}
	printf ("%lld\n", ans);
	return 0;
}

Overall, the problem of reverse thinking has always been its own weakness ... After you write the thinking equation, you should try to convert the equation to reverse thinking 、、、、


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.