CQUPT 2018 寒假训练 DIV2 (2)数学基础题解

Problem A:

One day Vasya was sitting on a not so interesting Maths lesson and making an origami from a rectangular a mm  ×  b mm sheet of paper (a > b). Usually the first step in making an origami is making a square piece of paper from the rectangular sheet by folding the sheet along the bisector of the right angle, and cutting the excess part.

After making a paper ship from the square piece, Vasya looked on the remaining (a - b) mm  ×  b mm strip of paper. He got the idea to use this strip of paper in the same way to make an origami, and then use the remainder (if it exists) and so on. At the moment when he is left with a square piece of paper, he will make the last ship from it and stop.

Can you determine how many ships Vasya will make during the lesson?

题目大意:

一个矩形,截取一个以短边为边长的正方形,一直重复此过程,直到最后剩下的都是正方形。问最后有多少个正方形。

用欧几里得求最大公约数。看进行几次操作,即可求得最后得到多少个正方形。

代码:



#include<cstring>
#include<iostream>
#include<algorithm>
#include<stdio.h>


using namespace std;
long long num = 0;


void Fact(long long max1,long long min1)
{
    if(max1%min1==0)
    {
        num += max1/min1;
        return;
    }
    else{
        long long x = max1;
        max1 = min1;
        min1 = x%max1;
        num += x/max1;
        Fact(max1,min1);
    }
    return;
}


int main()
{
    long long max1,min1;
    long long x,y;


    scanf("%lld%lld",&x,&y);
    max1 = x>y?x:y;
    min1 = x<y?x:y;
    Fact(max1,min1);
    cout << num;
    return 0;
}



Problem B:

Amakusa, the evil spiritual leader has captured the beautiful princess Nakururu. The reason behind this is he had a little problem with Hanzo Hattori, the best ninja and the love of Nakururu. After hearing the news Hanzo got extremely angry. But he is clever and smart, so, he kept himself cool and made a plan to face Amakusa.

Before reaching Amakusa's castle, Hanzo has to pass some territories. The territories are numbered as a, a+1, a+2, a+3 ... b. But not all the territories are safe for Hanzo because there can be other fighters waiting for him. Actually he is not afraid of them, but as he is facing Amakusa, he has to save his stamina as much as possible.

He calculated that the territories which are primes are safe for him. Now given a and b he needs to know how many territories are safe for him. But he is busy with other plans, so he hired you to solve this small problem!


题目大意:

给定一个区间,找其中的素数个数。用埃式筛先筛出根号b内的素数,再用这些数来筛a,b间的素数。


代码:



#include<iostream>
#include<cstring>
#include<algorithm>
#include<stdio.h>
#include<set>
#include<math.h>


using namespace std;


bool prime[100000];
int pi[100000];






long Fact(long  a,long  b)
{
    long  num = 0;
    for(long i=2;i*i <= b;i ++)
    {
        if(!prime[i]) pi[num++] = i;
        for(long j = 0;j <num;j ++)
        {
            if(i*pi[j]>sqrt(b))
                break;
            prime[i*pi[j]] = true;
            if(i%pi[j]==0)
                break;
        }
    }


    memset(prime,false,sizeof(prime));
    long x  = num;
    num = 0;
    for(long i = 0;i < x;i ++)
    {
        for(long j = a/pi[i];;j ++)
        {
            if(j<2)
            {
                j = 1;
                continue;
            }
            if(j*pi[i]<a)
                continue;
            else if(j*pi[i]>b)
                break;
            else
            {
                prime[j*pi[i]-a] = true;
            }
        }


    }
    for(long i = a;i <= b;i ++ )
    {
        if(!prime[i-a])
            num ++;
    }
    if(a==1)
        num --;
    return num;


}


int main()
{
    int n;
    long a,b;
    scanf("%d",&n);






    for(int i = 0;i < n;i ++)
    {
        memset(prime,false,sizeof(prime));
        scanf("%ld%ld",&a,&b);
        long x = Fact(a,b);
        cout <<"Case "<<i+1<<": "<< x<<endl;
    }
    return 0;


}



Problem C:

People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, others like using Windows, and some like difficult mathematical games. Latest marketing research shows, that this market segment was so far underestimated and that there is lack of such games. This kind of game was thus included into the KOKODáKH. The rules follow: 

Each player chooses two numbers Ai and Bi and writes them on a slip of paper. Others cannot see the numbers. In a given moment all players show their numbers to the others. The goal is to determine the sum of all expressions Ai  Bi  from all players including oneself and determine the remainder after division by a given number M. The winner is the one who first determines the correct result. According to the players' experience it is possible to increase the difficulty by choosing higher numbers. 

You should write a program that calculates the result and is able to find out who won the game. 

题目大意:

求ai 的 bi 次幂 (1 <= i <= h)的和与m求膜。

利用快速幂和求膜运算的运算律求解即可。


代码:



#include<stdio.h>
#include<cstring>
#include<iostream>


using namespace std;




long Mod(long long a,long long b,int mod);




int main()
{
    int n;


    scanf("%d",&n);
    for(int i = 0;i < n;i++)
    {
        int a;
        scanf("%d",&a);


        int b;
        scanf("%d",&b);
        int x = 0;


        for(int j=0;j < b;j ++)
        {
            long u,h;
            scanf("%ld%ld",&u,&h);
            x += Mod(u,h,a);


        }
        x %= a;
        cout << x <<endl;
    }
    return 0;
}
long Mod(long long a,long long b,int mod)
{
    if(b == 0)
        return 1;


    int x = b;
    long long f = 1;


        long long c = 1;
        int flag = x;
        for(int i = 0;i < x;i ++)
        {
            c *= mod;
            if(c >4294967296)
            {
                flag = i+1;
                break;
            }
        }
        f = f%mod;
        for(int i = 0;x!=0;i ++)
        {
            c = 1;
            for(int j = 0;j<x%flag;j ++)
            {
                 c *= a%mod;
            }
            c %= mod;
            f = c*f;
            f %= mod;
            c = 1;
            for(int j = 0;j < flag;j ++)
            {
                c *= a%mod;
            }
            a = c;
            x /= flag;
        }


    f = f%mod;
    return f;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值