Home Database Mysql Tutorial NOIP 2014 D2T3 解方程 Hash大法好

NOIP 2014 D2T3 解方程 Hash大法好

Jun 07, 2016 pm 03:48 PM
hash big equation

题目大意:给定高次方程an*x^n...a1*x^1a0*x^0=0 求[1,m]区间内有多少个整数根 ai=10^10000,m=100W 懒得高精,考场上写的long double乱搞……30分打底50分顶天QAQ 当我终于搞定了各种非官方数据之后,我只能长跪大地,手捧鲜花,仰望上苍高喊:哈希大法好!

题目大意:给定高次方程an*x^n+...+a1*x^1+a0*x^0=0 求[1,m]区间内有多少个整数根

ai

懒得高精,考场上写的long double乱搞……30分打底50分顶天QAQ

当我终于搞定了各种非官方数据之后,我只能长跪大地,手捧鲜花,仰望上苍高喊:哈希大法好!

首先阿贝尔在200年前告诉我们 五次以上方程没有求根公式 于是我们只能枚举1~m 这个是100W

然后100W再加上1W位的精度 都不用运算直接就是跪…… 怎么办呢QAQ

哈希大法好!

令f(x)=an*x^n+...+a1*x^1+a0*x^0 易知若f(x)=0 则f(x) mod p=0

反之如果f(x) mod p=0 那么我们基本可以得出f(x)=0 p比较靠谱的时候碰撞率极低

所以我们把所有的ai都对p取模 然后对于每个解O(n)验证即可

这样是O(m*n)的 可以拿到70分 p比较靠谱的话不会挂

那么100分怎么办呢?

哈希大法好!

我们很容易就会发现f(x+p) mod p=f(x) mod p

于是我们选择一个小一些的p,预处理出0~p-1所有的f(x),然后超过p的取模即可

但是p不够大会挂啊!

于是我们多选择几个p 分别取一遍mod 只有一个值对所有的p取模之后全都0才算作解

哈希大法好!Hash Killer III至今无人AC就是在证明这个算法的正确性!哈希万岁!哈希赛高!哈希万年推!

时间复杂度O(nΣp+m) 其中Σp是选择的所有质数之和 一般选择1W左右的质数就行了

不知道为什么不管考场上拿了多少分只要回来把题切了就算做精神AC了0.0……

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define M 110
using 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>n>>m;
    for(i=0;i<br>
<br>



</algorithm></iostream></cstring></cstdio>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement Redis Hash operation in php How to implement Redis Hash operation in php May 30, 2023 am 08:58 AM

How to implement Redis Hash operation in php

A brief analysis of machine learning and differential equations A brief analysis of machine learning and differential equations Apr 04, 2023 pm 12:10 PM

A brief analysis of machine learning and differential equations

Laravel development: How to generate password hash using Laravel Hash? Laravel development: How to generate password hash using Laravel Hash? Jun 17, 2023 am 10:59 AM

Laravel development: How to generate password hash using Laravel Hash?

Understand the Hash algorithm and application scenarios in one article Understand the Hash algorithm and application scenarios in one article Apr 13, 2023 am 11:55 AM

Understand the Hash algorithm and application scenarios in one article

Use it every day! Do you know what HASH is? Use it every day! Do you know what HASH is? Jul 26, 2023 pm 02:47 PM

Use it every day! Do you know what HASH is?

Detailed explanation of Redis commands: key, string and hash Detailed explanation of Redis commands: key, string and hash Jun 21, 2023 am 09:21 AM

Detailed explanation of Redis commands: key, string and hash

Is the equation a binary tree forest? Discover unknown governing equations and physical mechanisms directly from data Is the equation a binary tree forest? Discover unknown governing equations and physical mechanisms directly from data Apr 08, 2023 pm 06:11 PM

Is the equation a binary tree forest? Discover unknown governing equations and physical mechanisms directly from data

Revealing the best configuration recommendations for all-round website computers Revealing the best configuration recommendations for all-round website computers Dec 31, 2023 am 09:51 AM

Revealing the best configuration recommendations for all-round website computers

See all articles