1

$f(x)=\left(\sum_{i=1}^k{a_ix^i}\right) \pmod P$

$P > n \gg k.$

Obviously calculating $f(1),f(2)..f(n)$ takes $O(nk)$.

So is there a faster way to speed up the process? $k$ is not very big. $k$ is only about $30.$

Somos
  • 35,251
  • 3
  • 30
  • 76
  • is $P<k$ ? if so there can be ways to shrink it ... – Roddy MacPhee Mar 22 '21 at 15:11
  • 1
    is your polynomial arbitrary or does it come from somewhere concrete? – Asinomás Mar 22 '21 at 15:21
  • You can use Horner's algorithm. – Bernard Mar 22 '21 at 15:23
  • I vaguely remember seeing in my algorithms textbook that there's an efficient algorithm based partially on FFT which evaluates a polynomial simultaneously at multiple points. Though that would certainly be more for a computer to do than by hand. – Daniel Schepler Mar 22 '21 at 15:42
  • @Jorge, arbitrary. – Reykjavik Mar 23 '21 at 12:32
  • @Bernard, Horner's algorithm takes O(nk). – Reykjavik Mar 23 '21 at 12:33
  • @DanielSchepler, yep. But FFT about multipoint values of polynoimals takes O(nlogn*logn), but k ~ O(logn). It is slower than horner. – Reykjavik Mar 23 '21 at 12:34
  • @RoddyMacPhee, sorry. I forgot to mention that P > n >> k. – Reykjavik Mar 23 '21 at 12:35
  • @Reykjavik: What do you mean with ‘takesO(nk)’? – Bernard Mar 23 '21 at 12:59
  • Hello, this is probably better suited for mathoverflow. I have a very strong hunch there is no known way to do it but it would be much more convincing if someone from over there would say it haha. – Asinomás Mar 23 '21 at 14:07
  • @Bernard I think he means each f(i) is calculated completely independantly and each one takes approximately $2k$ operations or something like that ($k$ additions and $k$ multiplications). So the complexity is something like $nk$. – Asinomás Mar 23 '21 at 14:10
  • Also, I'm assuming this is for some sort of coding competition given the OP wanted to use FFT. Could you perhaps supply the original problem @Reykjavik ? Perhaps there is some sort of alternative trick that we can take for that particular problem? – Asinomás Mar 23 '21 at 14:11
  • @Jorge, aha, you're right.That's basically the original problem. I also think there should be no solution to this problem, otherwise there should be a way to accelerate polynomial multipoint evaluation without FFT. – Reykjavik Mar 23 '21 at 16:07

2 Answers2

2

We are given $$ f(x)=\left(\sum_{i=1}^k{a_ix^i}\right) \pmod P. $$ Since $\,f(x)\,$ is a polynomial mod $\,P\,$, then it is periodic. That is, $\,f(x+P) = f(x) \pmod P.\,$ Thus, all you need to do is compute $\,f(0),f(1),\dots,f(P-1)\,$ and there is no need for any further computation of $\,f(n).\,$ Even though $\,P\,$ may be big, it is a constant $\,O(1)\,$ compared to $\,O(nk).$

Of course, this is assuming that $\,P\,$ is fixed and that $\,n\,$ can be arbitrarily large. If $\,P\,$ depends on $\,n\,$ then that is a very different situation. In that case, $\,P\,$ doesn't even matter. Use a difference table up to $k$th order differences. Each new value of $\,f(n)\,$ takes $\,k\,$ additions for time complexity of $\,O(nk)\,$ additions with no multiplications required. This is very fast in my opinion.

Somos
  • 35,251
  • 3
  • 30
  • 76
  • Thanks for you answer. I forgot to metion that P > n. – Reykjavik Mar 23 '21 at 12:23
  • @Reykjavik I added an alternative fast method – Somos Mar 23 '21 at 12:56
  • OP already said he knew how to do it in $\mathcal O(nk)$ time though – Asinomás Mar 23 '21 at 14:06
  • @Jorge In comparison with addition, multiplications are much more expensive. Consult the Wikipedia article complexity of mathematical operations. The OP did not specify what those $O(nk)$ operations were, and so by default, it is assumed they are multiplications. My method uses no multiplications. – Somos Mar 23 '21 at 14:12
  • multiplications and additions are pretty much the same thing for competitive programming from the little I remember. I remember remainder was a bit slower though, but most problems didn't come down to technicalities like that. – Asinomás Mar 23 '21 at 14:13
  • Although it might be possible to speed it up because the remainder operation when doing an adition can be sped up a bit. Since you only need to substract MOD if a+b > MOD, and the % operation was indeed a bit costly. Although problems that came down to that were generally looked down upon haha – Asinomás Mar 23 '21 at 14:15
  • Your method is very enlightening, because the addition constant is much smaller than the multiplication when we do large scale calculations. Thank you~ @Somos – Reykjavik Mar 23 '21 at 16:09
0

Thanks for your reply.In fact, both n and P are large, P>n, k is small.

I know how to use FFT to speed up multipoint evaluations of polynomials, it takes $O(nlog^2n)$.

Because k is small here, and x is 1,2,3...,n, so I guess there's a faster way to do it.

  • FFT would help if we knew how to factor the polynomial but it could be an irreducible polynomial in Z_p – Asinomás Mar 23 '21 at 14:08