2020.1.4 freshman winter training five (GCD && fast power)

First of all introduce a user-friendly IDE invincible - vs code, the Black well-written tutorials, thanks thanks, attach a link portal .

Problem: A and the least common multiple of the greatest common divisor (NEFU OJ 1077)

Template title.

#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    int a,b;
    while(cin >> a >> b)
    {
        int ans;
        ans=__gcd(a,b);            //内置gcd真香
        cout << ans << " " << a/ans*b << endl;

    }
    return 0;
}

Problem: B See GCD (NEFU OJ 992)

The core idea: violence, b is the gcd, and b = c, c must be greater than b!.

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
int main()
{
    ios::sync_with_stdio(false);
    int a,b;
    while(cin >> a >> b)
    {
        for(int i=b+1;;i++)
        {
            if(__gcd(i,a)==b)
            {
                cout << i << endl;
                break;
            }
        }
    }
    return 0;
}

Problem: the greatest common divisor of the number of the plurality of C (NEFU OJ 764)

The core idea: two two numbers gcd, and the results over a set number of the next gcd.

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
int main()
{
    ios::sync_with_stdio(false);
    LL a[15],n;
    while(cin >> n)
    {
        LL ans;
        for(int i=1;i<=n;i++)
            cin >> a[i];
        ans=__gcd(a[1],a[2]);
        for(int i=3;i<=n;i++)
            ans=__gcd(ans,a[i]);
        cout << ans << endl;
    }
    return 0;
}

Problem: least common multiple of the number of the plurality of D (NEFU OJ 765)

The core idea: with ProbleC, find lcm (a, b) = a / gcd (a, b) * b, burst values ​​can be avoided.

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
int main()
{
    ios::sync_with_stdio(false);
    LL a[15],n;
    while(cin >> n)
    {
        LL ans;
        for(int i=1;i<=n;i++)
            cin >> a[i];
        ans=a[1]/__gcd(a[1],a[2])*a[2];
        for(int i=3;i<=n;i++)
            ans=ans/__gcd(ans,a[i])*a[i];
        cout << ans << endl;
    }
    return 0;
}

Problem:E    LCM&GCD(NEFU OJ 1411)

The core ideas: violence, (I wrote a classmate ran 7ms, I wrote 500ms, follow I will try to think of QAQ !!), I had also thought that violence will time out, he has been to gcd, lcm want to pay union, then wa, and I will continue to follow-up verification.

My idea of violence: the use of property lcm(a,b)*gcd(a,b)=a*b;from a to b loop, and add the judgment condition.

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;

int main()
{
    ios::sync_with_stdio(0);
    int t;
    while(cin >> t)
    {
        LL x,y;
        while(t--)
        {
            cin >> x >> y;
            LL ans=0,tmp,tmp2;
            tmp=x*y;                //lcm*gcd的性质
            for(LL i=x; i<=y; i++)
            {
                if(tmp%i==0)            //判定是否能整除
                {
                    tmp2=__gcd(i,tmp/i);
                    if(tmp2==x&&tmp/i<=y)//二次判断,是否符合gcd==x,lcm不用进行判断,因为tmp是x*y的乘积,可以自己去带入lcm的公式验证一下
                        ans++;
                }
            }
            cout << ans << endl;
        }
    }

    return 0;
}

Problem: F cute gcd (NEFU OJ 1221) (but I really do not love him ah QAQ !!)

The core ideas: mathematical derivation, as shown visible.

#include<bits/stdc++.h>
using namespace std;
int t, a, b;
int main()
{
	ios::sync_with_stdio(0);
	while(scanf("%d", &t) != EOF)
	{
		while(t--)
		{
			scanf("%d%d", &a, &b);
			printf("%d\n", a*a-2*__gcd(a, b)*b);
		}
	}
	return 0;
}

Problem: G Takagi students factor (NEFU OJ 1669)

The core ideas: factoring, specific issues in the code comments.

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
int main()
{
    LL a,b;
    while(~scanf("%lld%lld",&a,&b))
    {
        LL sum=0;
        int ans=__gcd(a,b);            //计算他是因为gcd是取a,b的公共因子的
        for(int i=1;i*i<=ans;i++)      //计算一半就行
            if(ans%i==0)
                sum++;
        if(sqrt(ans)*sqrt(ans)==ans)    //中间的因子是不是sqrt()为整数        
            printf("%lld\n",2*sum-1);   //若是整数,×2就会多计算一次,例子在代码的最后面
        else                            
            printf("%lld\n",2*sum);
    }

    return 0;
}
//比如16 32的公共因子={1,2,4,8,16},sqrt(gcd(16,32))==4,他只能算一次,不能重复计算。

Problem: H Fast modulo exponentiation (NEFU OJ 601)

The core idea: template title.

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

LL fastPow(LL a,LL n,LL c)
{
    LL base=a;
    LL res=1;
    const LL mod=c;
    while(n)
    {
        if(n&1)
            res=(res*base)%mod;
        base=(base*base)%mod;
        n>>=1;
    }
    return res;
}

int main()
{
    ios::sync_with_stdio(false);
    LL a,b,c;
    while(cin>> a >> b >> c)
    {
        LL ans;
        ans=fastPow(a,b,c);
        cout << ans << endl;
    }
    return 0;
}

Problem: I Kurt math (NEFU OJ 1666)

The core idea: You can write about their first violence, and played several successive answers that he will be able to find the law.

Let us talk about how I think of: casual working 1 <= n <= 1e18, if you write recursive, apparently arrays can not be open to 1e18, so you're looking for the law.

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
const LL M=1e9+7;
LL fastPow(LL a,LL n)            //快速幂取模的模板
{
    LL base=a;
    LL res=1;
    const LL mod=M;
    while(n)
    {
        if(n&1)
            res=(res*base)%mod;
        base=(base*base)%mod;
        n>>=1;
    }
    return res;
}

int main()
{
    ios::sync_with_stdio(false);
    LL n;
    while(cin >> n)
    {
        LL ans;
        ans=fastPow(3,n);
        cout << (2*ans)%M << endl;//别忘了再一次取模
    }
    return 0;
}

Problem: The number of XOR J solutions of the equations (NEFU OJ 1834)

This question was originally ljw card giant guy out of our AK, Xing'an family let him down, QAQ.

The core idea: lunch with the students think whim, I'll follow-up to prove it, and update.

Konjac write a proof, I do not know right or wrong, please correct me if wrong.

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

int fastPow(int a,int n)
{
    int base=a;
    int res=1;
    while(n)
    {
        if(n&1)
            res*=base;
        base*=base;
        n>>=1;
    }
    return res;
}

int main()
{
    ios::sync_with_stdio(0);
    LL n;
    while(cin >> n)
    {
        int sum=0,ans;
        while(n)
        {
            if(n&1)
                sum++;
                n>>=1;
         }
        ans=fastPow(2,sum);
        cout << ans << endl;
    }
    return 0;
}

 

 

Published 11 original articles · won praise 18 · views 1940

Guess you like

Origin blog.csdn.net/acm_durante/article/details/103832677