2020/6/7东华大学2020年程序设计竞赛

A.Shooting Game

#include<iostream>
#include<cstring>
using namespace std;
int main(){
    int n;
    cin>>n;    
    int id,red,white,black,score;
    int maxx=0,flag=0;
    for(int i=1;i<=n;i++){    
        cin>>id>>red>>white>>black;
        score=1*red+2*white+3*black;
        if(score>maxx){
            maxx=score;
            flag=i;
        }
    }
    cout<<flag<<" "<<maxx<<endl;
    return 0;
} 
View Code

B.A Number Theoretical Problem

题意:给定正整数y和质数p,要求找到一个整数x使(x*y) mod p =1。如果存在这样的x,输出任意xmod p,否则输出-1。提示:对于任意整数a和正整数b, a对b取余等于a除以b的余数。

题解:实际上就是解一个二元一次方程,k1*y-k2*p=1,我最后输出的时候没有判断小于不小于零,就一直没做出正确答案(太伤心了,/(ㄒoㄒ)/~~)

//这是解ax+by=m,
void
exgcd(ll a,ll b,ll &c,ll &x,ll &y){//c是a,b的最大公约数,如果m%c==0,就有解,在这道题就是1%c==0,所以c=1的时候有解 if(b==0){ c=a; x=1; y=0; }else{ exgcd(b,a%b,c,y,x); y-=x*(a/b); } }

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
void exgcd(ll a,ll b,ll &c,ll &x,ll &y){
     if(b==0){
         c=a;
         x=1;
         y=0;
     }else{
         exgcd(b,a%b,c,y,x);
         y-=x*(a/b);
     }

}
int main(){
    int t;
    cin>>t;
    while(t--){
        ll y,p,gcd,m,n,c;
        cin>>y>>p;
        exgcd(y,p,c,m,n);
        if(y%p==0||c!=1){
            cout<<"-1"<<endl;
        }else{
            if(m<0){
                m=m+p;
            }
            cout<<m<<endl;
        }
        
    }
    return 0;
}
View Code

 

posted @ 2020-06-07 20:27  Endeavo_r  阅读(239)  评论(0)    收藏  举报