【编程受害者实录*数论】

总结:在下数学白痴,告辞。
目录:
Modular Inverse (求最小乘法逆元–扩展欧几里得)
Reduced ID Numbers (同余定理)
Romantic (线性同余–扩展欧几里得)

欧几里得核心代码:

int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}

扩展欧几里得核心代码

ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }

    int r=exgcd(b,a%b,x,y);
    int temp;  //    将gcd(a,b)==gcd(b,a%b)代入ax+by==gcd(a,b)
    temp=x;  //       ax+by==a*y1+b*(x1-(a/b)*y1)
    x=y;   //       上一深度的x等于下一深度的y1
    y=temp - a/b*y;  //上一深度的y等于下一深度的x1-(a/b)*y1
    return r;

}

Modular Inverse

The modular modular multiplicative inverse of an integer a modulo m is an integer x such that a-1≡x (modm). This is equivalent toax≡1 (modm).

Input
There are multiple test cases. The first line of input is an integer T ≈ 2000 indicating the number of test cases.

Each test case contains two integers 0 < a ≤ 1000 and 0 < m ≤ 1000.

Output
For each test case, output the smallest positive x. If such x doesn’t exist, output “Not Exist”.

Sample Input
3
3 11
4 12
5 13
Sample Output
4
Not Exist
8

思路:借他人笔记:
ax≡1 (mod m) 等价于 ax % m = 1 ,求x的最小值,
这个式子我们可以化为 ax + my = 1 当gcd(a,m) != 1时无解
ax + my = gcd(a,m),根据欧几里得定理 gcd(a,m) = gcd(m,a%m)
所以 ax + my = mx1 + (a%m)y1
右面式子 = mx1 + (a - a/m * m)y1
继续化简得 ay1 + (x1 - a/m y1)m;
ax + my =ay1 + (x1- a/my1)m

x = y1;
*y = x1 - a/m y1;
我们可以得到 ax + my = gcd(a,m) 的一组解 (x,y);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define INF 0x3f3f3f3f
#define ll long long
using namespace std;
int n;
ll a,m,x,y;
ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }

    int r=exgcd(b,a%b,x,y);
    int temp;//    将gcd(a,b)==gcd(b,a%b)代入ax+by==gcd(a,b)
    temp=x;//       ax+by==a*y1+b*(x1-(a/b)*y1)
    x=y;//       上一深度的x等于下一深度的y1
    y=temp - a/b*y;//上一深度的y等于下一深度的x1-(a/b)*y1
    return r;


}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        while(n--)
        {
            scanf("%lld %lld",&a,&m);
            int flag=exgcd(a,m,x,y);
            if(flag==1)
            {
                while(x<=0)
                {
                    x = x+m;
                    y = y-a;
                }
                printf("%lld\n",x);
            }
            else
            {
                printf("Not Exist\n");
            }

        }
    }
}

Reduced ID Numbers

Description

T. Chur teaches various groups of students at university U. Every U-student has a unique Student Identification Number (SIN). A SIN s is an integer in the range 0 ≤ s ≤ MaxSIN with MaxSIN = 106-1. T. Chur finds this range of SINs too large for identification within her groups. For each group, she wants to find the smallest positive integer m, such that within the group all SINs reduced modulo m are unique.
Input

On the first line of the input is a single positive integer N, telling the number of test cases (groups) to follow. Each case starts with one line containing the integer G (1 ≤ G ≤ 300): the number of students in the group. The following G lines each contain one SIN. The SINs within a group are distinct, though not necessarily sorted.
Output

For each test case, output one line containing the smallest modulus m, such that all SINs reduced modulo m are distinct.

Sample Input
2
1
124866
3
124866
111111
987651

Sample Output
1
8

思路:求一个最小正整数m,满足数组中所有数mod m的结果都不一样。暴力枚举,每次都将一个表示余数的数组初始化。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define INF 0x3f3f3f3f
using namespace std;
int p[1000001];
int  d[1000001];
int main()
{
    int n,m;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d",&m);
        for(int i=1; i<=m; i++)
        {
            scanf("%d",&d[i]);
        }
        int flag,sum;
        for(int i=1;; i++)//枚举
        {
            flag=1;
            for(int j=0; j

Romantic

Girls are clever and bright. In HDU every girl like math. Every girl like to solve math problem!
Now tell you two nonnegative integer a and b. Find the nonnegative integer X and integer Y to satisfy Xa + Yb = 1. If no such answer print “sorry” instead.

Input
The input contains multiple test cases.
Each case two nonnegative integer a,b (0

Output
output nonnegative integer X and integer Y, if there are more answers than the X smaller one will be choosed. If no answer put “sorry” instead.

Sample Input
77 51
10 44
34 79

Sample Output
2 -3
sorry
7 -3

思路;我没啥思路,我数学是真的不行(不对,我什么都不行…)
看答案吧ε=(´ο`*)))唉

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define INF 0x3f3f3f3f
#define ll long long
using namespace std;
ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }

    int r=exgcd(b,a%b,x,y);
    int temp;//    将gcd(a,b)==gcd(b,a%b)代入ax+by==gcd(a,b)
    temp=x;//       ax+by==a*y1+b*(x1-(a/b)*y1)
    x=y;//       上一深度的x等于下一深度的y1
    y=temp - a/b*y;//上一深度的y等于下一深度的x1-(a/b)*y1
    return r;


}
int main()
{
    ll a,b;
    while(scanf("%lld %lld",&a,&b)!=EOF)
    {
        ll d,x,y;
        d=exgcd(a,b,x,y);
        if(d==1)
        {
            while(x<0)//注意
            {
                //a * x + b * y = 1
                //->  a*(x+b) + b*(y-a) = a*x + a*b + b*y - a*b = 1
                x = x+b;
                y = y-a;
            }
            printf("%lld %lld\n",x,y);
        }
        else
        {
            printf("sorry\n");
        }

    }
    return 0;
}

你可能感兴趣的:(acm)