集训————数论,扩展欧几里得,高精度求模以及同余定理;

今天真是要死的一天,数论学的头疼,感觉好多东西学起来很生疏,找不到节奏感,算了废话不多说;

扩展欧几里得

首先说扩展欧几里得的作用,可以用来求一个数的逆元,以及解特定的二元一次方程的解;

具体内容:

int exgcd(int a,int b,int &x,int &y)//特别注意x和y一定是参引入,否则在回溯时不能改变,导致不能求解;
{
    if(b==0)
    {
        x=1;
        y=0;
        return    a;
    }
    int r=exgecd(b,a%b,x,y);
    int t=y;
    y=x-(a/b)*y;
    x=t;
    return    r;
}

在经过这一番回溯变化后,得到了x,y的一个特殊解,然后我们可以通过这个特殊解来解除a的逆元

公式是a的逆=(x+m)%m;m就是b、参数里的b;

然后感觉这个就没什么好讲的了,有什么新的知识点,在以后碰见了继续补充;

高精度求模

在处理分子很大时,可以考虑的一个方式,要点是把要处理的分子当成string来处理;

代码

int high(string s,const int &mod)//有关这个const int &mod,在我看的一本书上说,这样可以提高效率,至于原因我也记得不是很清楚;
{
    int ans=0;
    for(int i=0;i,s.size();i++)
    {
        ans=(ans*10+s[i]-'0')%mod;//可以看出,大数取模就是同余定理的推广使用
    }
    return  ans;
}

同余定理

 就是说一个数a和b如果(a-b)%mod==0 ,那么就说a和b对于mod同余记为a≡b(mod m),也就是说,a,b分别对mod的求模值一样。

表述:
(a+b)%c=(a%c+b%c)%c
(a*b)%c=(a%c*b%c)%c

训练题

A题太难,回来再讲,我还没搞懂;

B题:要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1)。

Input

数据的第一行是一个T,表示有T组数据。 
每组数据有两个数n(0 <= n < 9973)和B(1 <= B <= 10^9)。

Output

对应每组数据输出(A/B)%9973。

Sample Input

2
1000 53
87 123456789

Sample Output

7922
6060

在讲题时,学长使用的是费马小定理,我ac的是用的扩展欧几里得

代码

#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
int exgcd(long long a,int b,int&x,long long &y)
{
    if(b==0)
    {
        x=1;
        y=0;return a;
    }
    int r=exgcd(b,a%b,x,y);
    int t=y;
    y=x-(a/b)*y;
    x=t;
    return  r;
}
//int nimo(string )
int main()
{
    int T;
    ios::sync_with_stdio(false);提速,避免了cin,cout占用时间;
    cin>>T;
    while(T--)
    {
        int ans;
        int n;
        long long b,y;
        cin>>n>>b;
        int x;
        exgcd(b,9973,x,y);//使用扩展欧几里得得出x,
        long long bni;
        bni=(x+9973)%9973;//利用求得的x来套公式得出逆元
        ans=((n%9973)*(bni%9973))%9973;在根据同余定理,得出答案
        cout<<ans<<endl;
    }
    return  0;
}

C

给出2个数M和N(M < N),且M与N互质,找出一个数K满足0 < K < N且K * M % N = 1,如果有多个满足条件的,输出最小的。

Input

输入2个数M, N中间用空格分隔(1 <= M < N <= 10^9)

Output

输出一个数K,满足0 < K < N且K * M % N = 1,如果有多个满足条件的,输出最小的。

Sample Input

2 3

Sample Output

2

就是一道很简单的扩展欧几里得应用,无非就是最后会有负值while循环变成正值就好】

代码

#include<iostream>//没错我就是不想用typedef
#include<cstdio>
#include<algorithm>
//typedef long long LL;
using namespace std;
long long exgcd(long long a,long long b,long long &x,long long &y )
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    long long r=exgcd( b,a%b,x,y);
    long long t=y;
    y=x-(a/b)*y;
    x=t;
    return  r;
}
int main()
{
    long long m,n;
    long long x,y;
    cin>>m>>n;
    exgcd(m,n,x,y);
    while(x<0)
    {
        x+=n;
    }
    cout<<x<<endl;
    return  0;
}

D题

求:3^0 + 3^1 +...+ 3^(N) mod 1000000007

Input

输入一个数N(0 <= N <= 10^9)

Output

输出:计算结果

Sample Input

3

Sample Output

40

这道题也没什么难度,主要就是题意感觉有问题,前wA的两边我都以为是对n次方求模,没想到是和的求模,改一下就ok了

主要思路是先利用等比数列求和和快速幂求模运算的复合使用

代码

#include<iostream>
using namespace std;
const int mod=1e9+7;
const int ni=5e8+4;//这个是因为根据等比数列的性质计算后你的分母是2,而计算求模时若分母不是1,那就转换为他的逆元,计算得到逆元位5e8+4;
int qsu(int n)
{
    long long res=1;
    long long a=3;
    while(n>0)
    {
        if(n&1) res=res*a%mod;
        a=a*a%mod;
        n>>=1;
    }
    return  res;//快速幂求模3^n
}
int main()
{
    long long n;
    ios::sync_with_stdio(false);
    cin>>n;
    n++;// 不要忘了这点,因为你是从1开始的
    long long ans=0;
    ans=qsu(n);
    ans=((ans-1)*(ni%mod))%mod;
    cout<<ans<<endl;
    return  0;
}

E

an integer is not divisible by 2 or 5, some multiple of that number in decimal notation is a sequence of only a digit. Now you are given the number and the only allowable digit, you should report the number of digits of such multiple.

For example you have to find a multiple of 3 which contains only 1's. Then the result is 3 because is 111 (3-digit) divisible by 3. Similarly if you are finding some multiple of 7 which contains only 3's then, the result is 6, because 333333 is divisible by 7.

Input
Input starts with an integer T (≤ 300), denoting the number of test cases.

Each case will contain two integers n (0 < n ≤ 106 and n will not be divisible by 2 or 5) and the allowable digit (1 ≤ digit ≤ 9).

Output
For each case, print the case number and the number of digits of such multiple. If several solutions are there; report the minimum one.

Sample Input
3

3 1

7 3

9901 1

Sample Output
Case 1: 3

Case 2: 6

Case 3: 12

大意就是给你两个数a,b。你要算出一个只有b的数列且数列的大小是a的倍数,求数列的长度

没啥好说的就是增加一位b就要判定一下

代码

#include<iostream>//我第一次做时还试图用字符串来储存然后利用stringstream流来存字符,结果就是超时;
#include<stdio.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    int T,p=1;
    cin>>T;
    while(T--)
    {
        int n,dig,ans=0;
        cin>>n>>dig;
        ans=dig%n;
        int cnt=1;
        while(ans!=0)
        {
            ans=(ans*10%n+dig%n)%n;
            cnt++;
        }
        printf("Case %d: %d\n",p++,cnt);
    }
    return  0;
}

F就是大数求模,就是直接套公式,不写了,溜了溜了;回寝室睡觉。

2018/7/24-21:25,院楼611,温度依旧那么热,好烦估计又是凌晨三点才睡着;

猜你喜欢

转载自blog.csdn.net/qq_41670466/article/details/81191829