NOIP2014D2T3 is good at solving the equation Hash big method.

Source: Internet
Author: User
Given the high-order equation an * x ^ n... a1 * x ^ 1a0 * x ^ 00 calculate the number of integers in the range [1, m] ai10 ^ 10000, And the m100W is too lazy to be precise. The longdouble written in the test room is messy ...... 30 minutes, 50 minutes, and a day, QAQ. After I finally got all sorts of unofficial data, I had to kneel down on the earth, hold flowers, and look up at the sky and shout: hash!

Given the high-order equation an * x ^ n... a1 * x ^ 1a0 * x ^ 0 = 0 how many integers are there in the range [1, m]? ai = 10 ^ 10000, m = 100 W too lazy and high-precision, long double in the test room ...... 30 minutes, 50 minutes, and a day, QAQ. After I finally got all sorts of unofficial data, I had to kneel down on the earth, hold flowers, and look up at the sky and shout: hash!

Given the high-order equation an * x ^ n +... + a1 * x ^ 1 + a0 * x ^ 0 = 0 evaluate the number of integer roots in the range [1, m]

Ai <= 10 ^ 10000, m <= 100 W

Too lazy and high-precision, long double in the test room ...... 30 minutes base, 50 minutes top day, QAQ

After I finally got all sorts of unofficial data, I had to kneel down on the earth, hold flowers in my hand, and look up at the sky and shout: Hash big way!

First, Abel told us five or more times before June 200 that there was no root equation, so we could only enumerate 1 ~ M: 100 W.

Then the accuracy of plus bits does not need to be calculated ...... What should we do? QAQ

Hash is good!

Make f (x) = an * x ^ n +... + a1 * x ^ 1 + a0 * x ^ 0 it is easy to know if f (x) = 0 then f (x) mod p = 0

If f (x) mod p = 0, then we can basically conclude that f (x) = 0 p is very reliable, and the collision rate is very low.

So we can modularize all the ai and then verify each solution O (n ).

In this case, if the O (m * n) can get 70 points p, it will not fail.

So what about 100 points?

Hash is good!

We can easily find f (x + p) mod p = f (x) mod p

Therefore, we select a smaller p and pre-process 0 ~ All the f (x) values of the p-1st, And the modulo exceeding the p value can be obtained.

But p is not enough for the conference!

So we select a few more p to get them one time. mod. Only one value is counted as the solution after all p modulo values are 0.

Hash is good! Hash Killer III is proving the correctness of this algorithm! Long live hash! Hash competition height! Hash!

Time complexity O (n Σ p + m) Where Σ p is the sum of all the selected prime numbers. Generally, we can choose a prime number of about 1 W.

I don't know why, no matter how much points I got in the test room, I just need to come back and cut the question. Even if I am mentally AC, I am 0.0 ......

#include
 
  #include
  
   #include
   
    #include#define M 110using namespace std;typedef long long ll;const int prime[]={10007,11261,14843,19997,21893};int n,m,stack[1001001],top;ll a[M][5],f[21893][5];inline ll F(int x,int j){    int i;    ll re=0;    for(i=n;~i;i--)        re=(re*x+a[i][j])%prime[j];    return re;}inline void Input(int x){    static char s[10100];    int i,j;    bool flag=false;    scanf("%s",s+1);    for(i=1;s[i];i++)    {        if(s[i]=='-')            flag=true;        else            for(j=0;j<5;j++)                a[x][j]=( (a[x][j]<<1) + (a[x][j]<<3) + s[i]-'0' )%prime[j];    }    if(flag)         for(j=0;j<5;j++)            a[x][j]=prime[j]-a[x][j];}int main(){    int i,j;    cin>>n>>m;    for(i=0;i<=n;i++)        Input(i);         for(j=0;j<5;j++)        for(i=0;i
    
   
  
 

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.