2013.7.21 acm_schooltraining解题报告(初)

今日,学习了初等数论。做了几道比较简单的入门题(说来惭愧,本有9道,只做了4道)

0:hdu--1066、3240(卡特兰数)、1370(中国剩余定理)、3579、1573


Romantic

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Problem Description
The Sky is Sprite.
The Birds is Fly in the Sky.
The Wind is Wonderful.
Blew Throw the Trees
Trees are Shaking, Leaves are Falling.
Lovers Walk passing, and so are You.
................................Write in English class by yifenfei



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 X*a + Y*b = 1. If no such answer print "sorry" instead.

Input

The input contains multiple test cases.
Each case two nonnegative integer a,b (0<a, b<=2^31)

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

Author

yifenfei 
分析:题目要求二元一次方程的解,可用扩展欧几里得算法来实现。而当a,b的gcd不为1时,不存在;对于x为负数可用通解公式将其化为正数,而对于大于0的取最小亦可以用通解来控制。在这里因为最底层返回的x=1,y=0觉得可以保证最小(未证明,在最底层b为0,因而y应该可以取任意值,在通解范围内),不想竟真的过了。

way:

#include <iostream>
using namespace std;
long x,y;

long extend_gcd(long a, long b){
   long t,m;
   if (b==0){
         x=1;y=0;
         return a;
   }
   m=extend_gcd(b, a%b);
   t=x;x=y;
   y=t-(a/b)*y;
   return m;
}

int main()
{
    long long a,b;
    int ans;
    while(cin>>a>>b){
        ans=extend_gcd(a,b);
        if(ans!=1)
            cout<<"sorry"<<endl;
        else {
            if(x<0) {x+=b;y-=a;}
            cout<<x<<" "<<y<<endl;
        }
    }
    return 0;

A/B

Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Font: Times New Roman | Verdana | Georgia
Font Size: ← →

Problem Description

要求(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

Author

xhd 
分析:关键是找出b,n的关系,知道关系后便是一道模板题(扩展欧几里得) 
             A=qB=x9973+n; 
要求: A/B%9973=q%9973;
            A% 9973 =  qB% 9973 = (x9973 + n) % 9973= n
所以qB 与 n mod(9973) 同余;
可写为: Bq + 9973y= n; (Bx+ 9973y=n)
先求出    Bx+ 9973y=1; 然后 x*n % 9973 求解

way:
#include <iostream>
using namespace std;

long mod_reverse(long a, long m){
    long y=0,x=1,r=a%m, q, t, mm=m;//初始化
    if(r<0)r=r+m;
    while((m%r) != 0) {
        a=m; m=r;
        q= a/m; r=a % m;
        t=x; x=y-x*q; y=t;
    }
    if(r!=1) return 0;
    if(x<0) x=x+mm;
    return x;
}

int main()
{
    int t,n,b;
    int ans;
    cin>>t;
    while(t--){
        cin>>n>>b;
        ans=mod_reverse(b,9973);
        ans=ans*n%9973;
        cout<<ans<<endl;
    }
    return 0;
}

Wolf and Rabbit

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Font: Times New Roman | Verdana | Georgia
Font Size: ← →

Problem Description

There is a hill with n holes around. The holes are signed from 0 to n-1.



A rabbit must hide in one of the holes. A wolf searches the rabbit in anticlockwise order. The first hole he get into is the one signed with 0. Then he will get into the hole every m holes. For example, m=2 and n=6, the wolf will get into the holes which are signed 0,2,4,0. If the rabbit hides in the hole which signed 1,3 or 5, she will survive. So we call these holes the safe holes.

Input

The input starts with a positive integer P which indicates the number of test cases. Then on the following P lines,each line consists 2 positive integer m and n(0<m,n<2147483648).

Output

For each input m n, if safe holes exist, you should output "YES", else output "NO" in a single line.

Sample Input

2
1 2
2 2

Sample Output

NO
YES

Author

weigang Lee 

分析:找规律(亦可用扩展欧几里得求)
way:
#include <iostream>
#include <string.h>
using namespace std;
long long n,m;

int main()
{
    int p,cnt,i;
    cin>>p;
    while(p--){
        cin>>m>>n;
        if(n==1) {cout<<"NO"<<endl;continue;}
        if(m<n){
           if(m!=1&&(n%m==0))  cout<<"YES"<<endl;
           else              cout<<"NO"<<endl;
        }
        else{
            if(m%n==0)   cout<<"YES"<<endl;
            else         cout<<"NO"<<endl;
        }
    }
    return 0;
}

Rightmost Digit

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Font: Times New Roman | Verdana | Georgia
Font Size: ← →

Problem Description

Given a positive integer N, you should output the most right digit of N^N.

Input

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).

Output

For each test case, you should output the rightmost digit of N^N.

Sample Input

2
3
4

Sample Output

7
6

Hint

In the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7.
In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.

Author

Ignatius.L 

分析:找规律,1——9为各位的数求n^n不超过4个便开始循环,所以打了表,具体见代码
way:
#include <iostream>
using namespace std;
int f[10][4]={0,0,0,0,
              1,1,1,1,
              6,2,4,8,
              1,3,9,7,
              6,4,0,0,
              5,5,5,5,
              6,6,6,6,
              1,7,9,3,
              6,8,4,2,
              1,9,0,0};
int main()
{
    int t;
    long long n,m;
    cin>>t;
    while(t--){
        cin>>n;
        m=n%10;
        if(m==1||m==5||m==6){
            cout<<m<<endl;
            continue;
        }
        if(m==4||m==9)
            cout<<f[m][n%2]<<endl;
        else
            cout<<f[m][n%4]<<endl;
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值