数论_CRT(中国剩余定理)& Lucas (卢卡斯定理)

前言

又是一脸懵逼的一天。

正文

按照道理来说,我们应该先做一个介绍。


中国剩余定理

中国剩余定理,Chinese Remainder Theorem,又称孙子定理,给出了一元线性同余方程组的有解判定条件,并用构造法给出了通解的具体形式。

现在有方程组:中国剩余定理指出:

扩展中国剩余定理

在一般情况下,要求任两个数互质这个条件太苛刻了,CRT派不上用场,我们需要一个更具普遍性的结论,这就是EX-CRT。虽然是称为EX-CRT,但这个定理并没有直接用到CRT的结论。

typedef long long ll;
;
​
// m为模数组,a为余数数组,0~n-1
ll m[maxn], a[maxn];
​
ll exgcd(ll a, ll b, ll &x, ll &y) {
    ) {
        x = ; y = ;
        return a;
    }
    ll ans = exgcd(b, a % b, y, x);
    y -= a / b * x;
    return ans;
}
​
ll excrt() {
    ll lcm = m[], last_a = a[];
    ; i < n; i++) {
        ll lcm_a = ((a[i] - last_a) % m[i] + m[i]) % m[i];
        ll k = lcm, x, y;
        ll gcd = exgcd(lcm, m[i], x, y);
        ll mod = m[i] / gcd;
        x = (x * lcm_a / gcd % mod + mod) % mod;
        lcm = lcm / gcd * m[i], last_a = (last_a + k * x) % lcm;
    }
    return (last_a % lcm + lcm) % lcm;
}

卢卡斯定理

卢卡斯定理是关于组合数和同余的定理,它表明当p为素数时:

其中,即为的进制展开中对应次数为的系数

因为当m>n时,二项式系数为0,那么二项式系数即组合数能被p整除等价于在p进制下,存在某一位m的数值大于对应的n的数值。

基于母函数可以简单证明这个定理。

可以用除法和取模方便的在循环中求出各个系数,代码如下:

typedef long long ll;
;
;
​
void init() {
    F[] = ;
    ; i < maxn; i++)
        F[i] = i * F[i - ] % mod;
}
​
ll qpow(ll a, ll b) {
    ll ans = ;
    while(b) {
        ) ans = ans * a % mod;
        b >>= ; a = a * a % mod;
    }
    return ans;
}
​
ll lucas(ll N, ll M) {
    ll ans = ;
    while(N & M) {
        ll n = N % mod, m = M % mod;
        ;
        ans = ans * F[a] % mod * qpow(F[m] * F[n - m] % mod, mod - ) % mod;
        N /= p; M /= p;
    }
    return ans;
}

扩展卢卡斯定理

卢卡斯定理同样不能处理模数不是素数的情况,这时便需要扩展卢卡斯定理。我们一步步分析如何求解模数不是素数的组合数问题。

首先,我们要解决的问题是求,其中不一定是素数。对于非素数,我们首先会联想到质因分解后结合解决问题。假设分解得到个质数,质数对应的个数为,对质因分解有。显然对,,,假设对,我们求出了,那么我们可以得到同余方程组::这时我们便可以套用解决问题,那么问题便转化为如何求解。

现在我们要求的是,其中是素数。又,显然需要求出和关于模的逆元,但考虑到这些项中可能包含(含有则不互质,逆元不存在),所以需要先提取,得到:,这里的阶乘是指提取之后的结果。这时就可以计算和关于的逆元了。这里,为了形式的统一,同时提取了中的。那么,问题又转化为了如何求。

目标:计算,为质数。上一步中提到,我们需要先提取。提取结果为:。第一部分很好理解,对于每一个的倍数,都可以提取出一个,一共有个;第二部分为的倍数被提取之后余下的,是一个阶乘的形式。显然在中,对于的幂,的个数不止个,也就是说第二部分仍然需要提取,这一部分可以递归解决。第三部分是剔除了的倍数之后余下的。对,,都有对,这也就是说,第三部分其实是存在循环节的,一共循环了次。除去循环节的余项长度在之内,直接累乘即可。

完整代码如下:

typedef long long ll;
;
​
ll n, m, p;
​
ll qpow(ll a, ll b, ll mod) {
    ll ans = ;
    while(b) {
        ) ans = ans * a % mod;
        b >>= ; a = a * a % mod;
    }
    return ans;
}
​
ll fac(ll n, ll p, ll pk) {
    ;
    ll ans = ;
    ; i < pk; i++)
        if (i % p) ans = ans * i % pk;
    ans = qpow(ans, n / pk, pk);
    int npk = n % pk;
    ; i <= npk; i++)
        if (i % p) ans = ans * i % pk;
    return ans * fac(n / p, p, pk) % pk;
}
​
ll exgcd(ll a, ll b, ll &x, ll &y) {
    ) {
        x = ; y = ;
        return a;
    }
    ll ans = exgcd(b, a % b, y, x);
    y -= a / b * x;
    return ans;
}
​
ll inv(ll a, ll p) {
    , p);
}
​
ll C(ll n, ll m, ll p, ll pk) {
    ;
    ll fn = fac(n, p, pk),
       fm = fac(m, p, pk),
       fn_m = fac(n - m, p, pk),
       cnt = ;
    for (ll i = n; i; i /= p)
        cnt += i / p;
    for (ll i = m; i; i /= p)
        cnt -= i / p;
    for (ll i = n - m; i; i /= p)
        cnt -= i / p;
    return fn * inv(fm * fn_m % pk, pk) % pk * qpow(p, cnt, pk) % pk;
}
​
ll a[N], mod[N]; // a[]是通过卢卡斯分解出来的组合数值,m[]是对应的模数
int cnt; // 质因数的种数
​
ll CRT() {
    ll M = , ans = ;
    ; i < cnt; i++)
        M *= mod[i];
    ; i < cnt; i++)
        ans = (ans + a[i] * (M / mod[i]) % M * inv(M / mod[i], mod[i]) % M) % M;
    return ans;
}
​
ll exlucas(ll n, ll m, ll p) {
    ll sqrtp = sqrt(p + 0.5);
    ; p >  && i <= sqrtp; i++) {
        ll pk = ;
        )
            p /= i, pk *= i;
        )
            a[cnt] = C(n, m, i, pk), mod[cnt++] = pk;
    }
    )
        a[cnt] = C(n, m, p, p), mod[cnt++] = p;
    return CRT();
}

题目

其实这篇博客到这里几乎就可以没了,因为我。。。爆0了

难啊。。。

A题

Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical, emotional, and intellectual cycles, and they have periods of lengths 23, 28, and 33 days, respectively. There is one peak in each period of a cycle. At the peak of a cycle, a person performs at his or her best in the corresponding field (physical, emotional or mental). For example, if it is the mental curve, thought processes will be sharper and concentration will be easier.

Since the three cycles have different periods, the peaks of the three cycles generally occur at different times. We would like to determine when a triple peak occurs (the peaks of all three cycles occur in the same day) for any person. For each cycle, you will be given the number of days from the beginning of the current year at which one of its peaks (not necessarily the first) occurs. You will also be given a date expressed as the number of days from the beginning of the current year. You task is to determine the number of days from the given date to the next triple peak. The given date is not counted. For example, if the given date is 10 and the next triple peak occurs on day 12, the answer is 2, not 3. If a triple peak occurs on the given date, you should give the number of days to the next occurrence of a triple peak.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

Input

You will be given a number of cases. The input for each case consists of one line of four integers p, e, i, and d. The values p, e, and i are the number of days from the beginning of the current year at which the physical, emotional, and intellectual cycles peak, respectively. The value d is the given date and may be smaller than any of p, e, or i. All values are non-negative and at most 365, and you may assume that a triple peak will occur within 21252 days of the given date. The end of input is indicated by a line in which p = e = i = d = -1.

Output

For each test case, print the case number followed by a message indicating the number of days to the next triple peak, in the form:

Case 1: the next triple peak occurs in 1234 days.

Use the plural form ``days'' even if the answer is 1.

Sample Input

1

0 0 0 0 0 0 0 100 5 20 34 325 4 5 6 7 283 102 23 320 203 301 203 40 -1 -1 -1 -1

Sample Output

Case 1: the next triple peak occurs in 21252 days. Case 2: the next triple peak occurs in 21152 days. Case 3: the next triple peak occurs in 19575 days. Case 4: the next triple peak occurs in 16994 days. Case 5: the next triple peak occurs in 8910 days. Case 6: the next triple peak occurs in 10789 days.

Biorhythms HDU-1370

题意

一个人有三个值(不知道是啥),然后每个值每到一个周期就会到达顶峰,求从d天开始,他三个值都到达顶峰是第几天。

思路

然而,三个周期都是质数,显然用的是中国剩余定理(CRT)

抽象一点来说,就是给你三个同余方程。

代码

 #include <iostream>
 #include <cstdio>
 #include <algorithm>
 #include <cmath>
 #include <cstring>
 using namespace std;
 typedef long long ll;
 ll exgcd(ll a, ll b, ll &x, ll &y)
 {
     if(!b)
     {
         x = ;
         y =  * ;
         return a;
     }
     ll d = exgcd(b, a % b, x, y);
     ll t = x;
     x = y;
     y = t - a / b * y;
 ​
     return d;
 }

 ll inv(ll a,ll n)
 {
     ll y, d, x, fre, pf, qw;
     /*cnt't*/
     fre = pf = qw = ;
     fre++, pf++, qw++;
     /*can't*/
     d = exgcd(a,n,x,y);
      ? (x + n) % n:-;
 }
 ​
 ll CN(ll leo, ll *a, ll *m)
 {
     ll M = , ret = ;
     ; i < leo; i ++)
         M *= m[i];
 ​
     ; i < leo; i ++)
     {
         ll w = M / m[i];
         ret = (ret + w * inv(w, m[i]) * a[i]) % M;
     }
     return (ret + M) % M;
 }
 int main()
 {
     ll t = , d;
     ll a[],m[];
     m[] = ;
     m[] = ;
     m[] = ;
     /*GN*/
     ll tea;
     scanf("%lld", &tea);
     while(true)
     {
         scanf(], &a[], &a[], &d);
 ​
         ] == - && a[] == - && a[] == - && d == -)
             break;
         ll ans = CN(, a, m);
         if(ans <= d)
             ans += ;
         ans -= d;
 ​
         printf("Case %lld: the next triple peak occurs in %lld days.\n", t, ans);
         t++;
     }
     ;
 }

B题

F(x) is a polynomial in x with integer coefficients, here F(x) = (1+x)^a1 + (1+x)^a2 + ... + (1+x)^am. Given a1, a2, ... , am, find number of odd coefficients of F(x).

Input

The first line contains a single positive integer T( T <= 10000 ), indicates the number of test cases. For each test case: First line contains an integer N(1 <= N <= 15). Second line contains N integers a1, a2, ..., am ( 0 <= ai <= 2^45 )

Output

For each test case: output the case number as shown and an the odd coefficients of F(x).

Sample Input

4 1 1 1 3 2 1 3 3 1 2 3

Sample Output

Case #1: 2 Case #2: 4 Case #3: 2 Case #4: 2

Hint

Case #3: (1+x) + (1+x)^3 = 2 + 4x + 3x^2 + x^3. it contains 2 odd coefficients. Case #4: (1+x) + (1+x)^2 + (1+x)^3 = 3 + 6x + 4x^2 + x^3. it contains 2 odd coefficients.

Big Coefficients HDU-3929

题意

思路

显然是用卢卡斯,但我不知道为哈

未完待续

数论 Day 13的更多相关文章

  1. 洛谷P1621 集合 [2017年6月计划 数论13]

    P1621 集合 题目描述 现在给你一些连续的整数,它们是从A到B的整数.一开始每个整数都属于各自的集合,然后你需要进行一下的操作: 每次选择两个属于不同集合的整数,如果这两个整数拥有大于等于P的公共 ...

  2. Educational Codeforces Round 13 D:Iterated Linear Function(数论)

    http://codeforces.com/contest/678/problem/D D. Iterated Linear Function Consider a linear function f ...

  3. CodeForces 300C --数论

    A - A Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  4. [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)

    题目链接:http://acm.swust.edu.cn/problem/1125/ Time limit(ms): 1000 Memory limit(kb): 65535   Descriptio ...

  5. ARZhu的数论初步

    数论 2017年3月4日02:11:35 gcd 1. 原理: gcd( a, b ) = gcd( b, a - b ) -> gcd( a, b ) = gcd( b, b % a ) 2. ...

  6. HDU 1013.Digital Roots【模拟或数论】【8月16】

    Digital Roots Problem Description The digital root of a positive integer is found by summing the dig ...

  7. 2017年浙江理工大学程序设计竞赛校赛 题解&源码(A.水, D. 简单贪心 ,E.数论,I 暴力)

    Problem A: 回文 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 1719  Solved: 528 Description 小王想知道一个字 ...

  8. [自用]数论和组合计数类数学相关(定理&证明&板子)

    0 写在前面 本文受 NaVi_Awson 的启发,甚至一些地方直接引用,在此说明. 1 数论 1.0 gcd 1.0.0 gcd $gcd(a,b) = gcd(b,a\;mod\;b)$ 证明:设 ...

  9. 数论ex

    数论ex 数学学得太差了补补知识点or复习 Miller-Rabin 和 Pollard Rho Miller-Rabin 前置知识: 费马小定理 \[ a^{p-1}\equiv 1\pmod p, ...

随机推荐

  1. Iphone使用过程中遇到的问题

    Q1:同一个Apple ID不同设备之间的通话记录保持同步 解决方法: Step1:"设置"--"电话"--"在其他设备上通话"--选择关闭 ...

  2. 安装yarn实况

    [**前情提要**]最近在gayhub上面得到一个开源项目,遂准备研究一下源码,当然第一步就是要把项目运行起来.然后看了一下技术栈,发现包管理工具是使用yarn,以前也听说过yarn但是也没有具体使用 ...

  3. 对Java中HashCode方法的深入思考

    前言 最近在学习 Go 语言,Go 语言中有指针对象,一个指针变量指向了一个值的内存地址.学习过 C 语言的猿友应该都知道指针的概念.Go 语言语法与 C 相近,可以说是类 C 的编程语言,所以 Go ...

  4. 【0806 | Day 9】异常处理/基本的文件操作

    一.异常处理 异常即报错,可分为语法异常和逻辑异常 1. 语法异常 举个栗子 if #报错 syntaxerror 0 = 1 #报错 syntaxerror ... 正经地举个栗子 print(1) ...

  5. h5微信浏览器复制粘贴--ios兼容问题的解决方法(clipboard.js插件)

    前段时间在做微信h5的时候,遇到了ios兼容,使用clipboard.js插件完美解决 下载地址:下载地址: https://github.com/zenorocha/clipboard.js cnd ...

  6. python调用支付宝支付接口详细示例—附带Django demo代码

    项目演示: 一.输入金额 二.跳转到支付宝付款 三.支付成功 四.跳转回自己网站 在使用支付宝接口的前期准备: 1.支付宝公钥 2.应用公钥 3.应用私钥 4.APPID 5.Django 1.11. ...

  7. Spring Boot Security Oauth2之客户端模式及密码模式实现

    Spring Boot Security Oauth2之客户端模式及密码模式实现 示例主要内容 1.多认证模式(密码模式.客户端模式) 2.token存到redis支持 3.资源保护 4.密码模式用户 ...

  8. 关于sparksql中设置自定义自增列的相关要点(工作共踩过的坑-1)

    小白终于进入了职场,从事大数据方面的工作! 分到项目组了,搬砖的时候遇到了一个这样的问题. 要求:用spark实现oracle的存储过程中计算部分. 坑:由于报表中包含了一个ID字段,其要求是不同的区 ...

  9. 蓝桥杯c语言基础题

    问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少. 输入格式 输入包含一个整数n ...

  10. 解决ionic 中 $ionicHistory.goBack()无法返回

    这种解决方法目前只适合用了 <ion-side-menus>这一组件的<ion-view> 解决方法: 在<ion-view>下一级中包一个div,如下图: 搞了几 ...