CF1245 A. Good ol' Numbers Coloring(java与gcd)

题意:给定数字A和数字B,问是否满足gcd(A,B)==1。

思路:可以直接写函数gcd。也可以用大数自带的gcd功能。

代码1:

    /*
    @author nimphy
    @create 2019-11-06-12:07
    about:
    */
    import java.io.*;
    import java.util.*;
    public class CF1245 {
        public static void main(String[] args) {
            Scanner In=new Scanner(System.in);
            int T,A,B,C;
            T=In.nextInt();
            while(T-->0){
                A=In.nextInt();
                B=In.nextInt();
                C=gcd(A,B);
                if(C==1) System.out.println("Finite");
                else System.out.println("Infinite");
            }
        }
        static int gcd(int x,int y)
        {
            int t;
            while(y>0){
                t=x%y;
                x=y;
                y=t;
            }
            return x;
        }
    }

代码2:大数 ~ java有自带进制转化功能,可以参考hdu5050

/*
@author nimphy
@create 2019-11-06-12:07
about:
*/

import java.math.*;
import java.io.*;
import java.util.*;

public class CF1245 {
    public static void main(String[] args) {
        Scanner In = new Scanner(System.in);
        int T;
        T = In.nextInt();
        while (T-- > 0) {
            String s1, s2;
            s1 = In.next();
            s2 = In.next();
            BigInteger A = new BigInteger(s1);//将转换成十进制的数转换成大数
            BigInteger B = new BigInteger(s2);
            BigInteger C = A.gcd(B);
            if (C.equals(BigInteger.ONE)) System.out.println("Finite");
            else System.out.println("Infinite");
        }
    }
}

CF1245 A. Good ol' Numbers Coloring(java与gcd)

原文地址:https://www.cnblogs.com/hua-dong/p/11804483.html

时间: 2025-01-12 08:13:36

CF1245 A. Good ol' Numbers Coloring(java与gcd)的相关文章

Codeforces Round #597 (Div. 2) A. Good ol' Numbers Coloring

链接: https://codeforces.com/contest/1245/problem/A 题意: Consider the set of all nonnegative integers: 0,1,2,-. Given two integers a and b (1≤a,b≤104). We paint all the numbers in increasing number first we paint 0, then we paint 1, then 2 and so on. Ea

Add Two Numbers leetcode java

题目: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (4 -> 6 -&

Sum Root to Leaf Numbers leetcode java

题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2

LeetCode201 Bitwise AND of Numbers Range Java 题解

题目: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return 4. 解答: 假如说5:101  7:111  连续几个数相与的规律:一,只要是相同的位置的数字不相同最后那个位置的结果一定是0 .二,如

201. Bitwise AND of Numbers Range java solutions

Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return 4. Credits:Special thanks to @amrsaqr for adding this problem and creatin

ZOJ 3987 Numbers(Java枚举)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3987 题意:给出一个数n,现在要将它分为m个数,这m个数相加起来必须等于n,并且要使得这m个数的或值最小. 思路: 从二进制的角度分析,如果这m个数中有一个数某一位为1,那么最后或起来这一位肯定是为1的,所以如果某一位为1了,那么我们尽量就让其余位也等于1. 所以我们从最高位开始枚举,看看这一位是否需要为1,如果需要为1的话,那么剩下的几个数也尽量让这一位等于1. 1 i

codeforces - 1245 (div2)

A - Good ol' Numbers Coloring 直接判断两个数是否互质 #include <stdio.h> #include <iostream> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #pragma GCC optimize(2) #define mm(i,v) m

Java性能提示(全)

http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLists and ArrayLists (and Vectors) (Page last updated May 2001, Added 2001-06-18, Author Jack Shirazi, Publisher OnJava). Tips: ArrayList is faster than

转:一个java教程各章节的链接

Java概述,Java是什么? Java环境变量设置 Java基本语法 Java对象和类 Java基本数据类型 Java变量类型 Java修饰符类型 Java基本运算符 Java循环for, while和do...while Java决策制定 Java Numbers类 Java String类 Java数组 Java日期时间(Date/Time) Java正则表达式 Java方法 Java流,文件和I/O Java异常处理 Java继承 Java覆盖/重载 Java多态性 Java抽象 Jav