nefu 630 Min Chain

题目:大意是说给定两个数,让你用这两个数,随意地进行+或者-两种操作,求出最小操作数使得结果为1,当不可能达到1的时候,输出-1.

方法:明显的数论题目,相当于求出ax+by=1的解。

当两个数不互素时,得不到1的结果;

当两个数互素时,使用拓展欧几里德来求得x和y,输出abs(x)+abs(y)-1即可。

注意:这道题目的数据涉及0、1,这些数据需要单独处理。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

long long gcd(long long a,long long b)
{
    long long r=a%b;
    while(r)
    {
        a=b;
        b=r;
        r=a%b;
    }
    return b;
}

void exgcd(long long m,long long n,long long  &x,long long &y)
{
    if(n==0)
    {
        x=1;
        y=0;
        return;
    }
    else exgcd(n,m%n,x,y);
    long long t=x;
    x=y;
    y=t-m/n*y;
}
int main()
{
    long long da,db;
    long long n;
    cin>>n;
    while(n--)
    {
        cin>>da>>db;
        if(da==0&&db==0||da==0&&db!=1||db==0&&da!=1)
        {
            cout<<-1<<endl;
            continue;
        }
        if(da==0&&db==1||db==0&&da==1)
        {
             cout<<1<<endl;
             continue;
        }
        if(da==1||db==1)
        {
            if(da==2||db==2)
                cout<<1<<endl;
            else
            cout<<2<<endl;
            continue;
        }
        long long r=gcd(da,db);
        if(r!=1) printf("-1\n");
        else
        {
            long long X,Y;
            exgcd(da,db,X,Y);
            if(X<0) X=-X;
            if(Y<0) Y=-Y;
            printf("%lld\n",X+Y-1);
        }
    }
    return 0;
}

nefu 630 Min Chain

时间: 2024-11-04 21:21:51

nefu 630 Min Chain的相关文章

Project Euler 95:Amicable chains 亲和数链

Amicable chains The proper divisors of a number are all the divisors excluding the number itself. For example, the proper divisors of 28 are 1, 2, 4, 7, and 14. As the sum of these divisors is equal to 28, we call it a perfect number. Interestingly t

hdu 5293 Tree chain problem(树链剖分+树形dp)

题目链接:hdu 5293 Tree chain problem 维护dp[u], sum[u],dp[u]表示以u为根节点的子树的最优值.sum[u]表示以u节点的所有子节点的dp[v]之和.对于边a,b,w,在LCA(a,b)节点的时候进行考虑.dp[u] = min{dp[u], Sum(a,b) - Dp(a,b) + sum[u] | (ab链上的点,不包括u } #pragma comment(linker, "/STACK:1024000000,1024000000")

HDU 5293(Tree chain problem-树链剖分)

Tree chain problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 940    Accepted Submission(s): 248 Problem Description Coco has a tree, whose vertices are conveniently labeled by 1,2,-,n. Th

MCMC(Markov Chain Monte Carlo) and Gibbs Sampling

MCMC(Markov Chain Monte Carlo) and Gibbs Sampling 1.   随机模拟 随机模拟(或者统计模拟)方法有一个很酷的别名是蒙特卡罗方法(Monte Carlo Simulation).这个方法的发展始于20世纪40年代,和原子弹制造的曼哈顿计划密切相关,当时的几个大牛,包括乌拉姆.冯.诺依曼.费米.费曼.Nicholas Metropolis, 在美国洛斯阿拉莫斯国家实验室研究裂变物质的中子连锁反应的时候,开始使用统计模拟的方法,并在最早的计算机上进行

【Poj1090】Chain

Chain Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3414   Accepted: 1126 Description Byteland had not always been a democratic country. There were also black pages in its book of history. One lovely day general Bytel − commander of th

1106. Lowest Price in Supply Chain (25)

A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer. Starting from one root supplier, everyone on the chain buys products from one's supplier in a pric

SRM 630 DIV2

SRM 630 DIV2 第一次TC,本来以为AK了,结果1000分还是被系统cha掉了,不过倒是也cha掉了房间其他人赚了不少 A:字符串长度才50,直接简单的模拟即可 B:结点个数才10,先做一边floyd,找出两两之间路径,然后暴力枚举选哪些点,判断可不可以,如果可以的话,记录下最大个数 C:一开始的做法是,构造出rank数组后,对于连续的一段,都放a,然后最后一个放b即可,以为这样构造出来的肯定是字典序最小的,结果被系统cha掉了. 正确做法:一开始先构造出sa数组,暴力枚举每个位置,非

arm,iptables: No chain/target/match by that name.

最近由于项目需要,需要打开防火墙功能. 公司有 arm linux 3.0x86 linux 3.2x86 linux 2.4 的三个嵌入式.都需要打开防火墙功能. 执行“whereis iptables”命令,如果结果不为空,则说明防火墙软件已安装 # whereis iptables iptables: /sbin/iptables /usr/share/iptables /usr/share/man/man8/iptables.8.gz [email protected]:~ 9:26:5

[Coding Made Simple] Matrix Chain Multiplication

Given some matrices, in what order you would multiply them to minimize cost of multiplication. The following problem formulation is extracted from this link. Problem Formulation Note that although we can use any legal parenthesization, which will lea