zoukankan      html  css  js  c++  java
  • luogu2312 解方程 (数论,hash)

    luogu2312 解方程 (数论,hash)

    第一次外出学习讲过的题目,然后被讲课人的一番话惊呆了.
    这个题,我想着当年全国只有十几个满分.....然后他又说了句我考场A这道题时,用了5个模数
    确实不好做想不到.
    由于(a)非常大.转为以下思路.

    (f(x) = a_0+a_1x+a_2x^2+cdots+a_nx^n)
    对于(f(x) = 0)(f(x)\%p = 0)
    (f(x \% p) = 0)
    然后这里最好是选择素数.由于luogu数据较水,可以直接选择(1e9 + 7)水过.
    判断会有误,所以这里选择两个数.
    bzoj也有这道题,但是极其考验卡常技巧......
    卡到吐血.还是没A,算了,不卡了....
    记录:

    #pragma GCC optimize(2)
    #include <iostream>
    #include <cstdio>
    #define ll long long
    const int maxN = 100 + 7;
    const int maxM = 1e6 + 7;
    const int p = 1e9 + 7; 
    const int p1 = 20030327;
    
    ll a[maxN],b[maxN];
    ll n,m;
    bool vis[maxM];
    
    inline bool calc(int x)
    {
        long long sum = 0;
        for(int i = n;i >= 1;i --)
        {
            sum = ( (long long) ( a[i] + sum ) * x ) % p;
        }
        sum = ( sum + a[0] ) % p;
        return !sum;
    }
    
    inline void read(ll &x1,ll &x2) {
        x1 = 0,x2 = 0;
        int f = 1;
    	char c = getchar();
        while(c < '0' || c > '9') {if(c == '-')f = -1;c = getchar();}
        while(c >= '0' && c <= '9') {
            x1 = ( x1 * 10 + c - '0' ) % p;
            x2 = ( x2 * 10 + c - '0') % p1;
            c = getchar();
    	}
    	x1 *= f;
    	x2 *= f;
    }
    
    inline bool calc1(int x)
    {
        long long sum = 0;
        for(int i = n;i >= 1;i --)
        {
            sum = ( (long long) ( b[i] + sum ) * x ) % p1;
        }
        sum = ( sum + b[0] ) % p1;
        return !sum;
    }
    
    void print(int x)
    {
        if(x < 0)
        {
            putchar('-');
            x = -x;
        }
        if(x > 9)
        {
            print(x / 10);
        }
        putchar(x % 10 + '0');
    }
    
    int main() {
        scanf("%d%d",&n,&m);
        for(int i = 0;i <= n;++ i)
            read(a[i],b[i]);
        int cnt = 0;
        for(int i = 1;i <= m;++ i) 
            if(calc(i) && calc1(i)) ++ cnt,vis[i] = true;
        printf("%d
    ",cnt);
        for(int i = 1;i <= m;++ i)
            if(vis[i]) print(i),puts("");
        return 0;
    }
    
  • 相关阅读:
    Codeforces Round #344 (Div. 2) C. Report 其他
    Codeforces Round #344 (Div. 2) B. Print Check 水题
    Codeforces Round #344 (Div. 2) A. Interview 水题
    8VC Venture Cup 2016
    CDOJ 1280 772002画马尾 每周一题 div1 矩阵快速幂 中二版
    CDOJ 1280 772002画马尾 每周一题 div1 矩阵快速幂
    CDOJ 1279 班委选举 每周一题 div2 暴力
    每周算法讲堂 快速幂
    8VC Venture Cup 2016
    Educational Codeforces Round 9 F. Magic Matrix 最小生成树
  • 原文地址:https://www.cnblogs.com/tpgzy/p/9715362.html
Copyright © 2011-2022 走看看