Good ol' Numbers Coloring

根据给定的规则对所有非负整数进行染色,判断黑色非负整数的数量是否无限。如果存在无限多个非负整数被染成黑色,则输出"Infinite",否则输出"Finite"。染色规则涉及两个参数a和b,染色过程考虑a和b的最小公倍数。
摘要由CSDN通过智能技术生成

Consider the set of all nonnegative integers: 0,1,2,…0,1,2,…. Given two integers aa and bb (1≤a,b≤1041≤a,b≤104). We paint all the numbers in increasing number first we paint 00, then we paint 11, then 22 and so on.

Each number is painted white or black. We paint a number ii according to the following rules:

  • if i=0i=0, it is colored white;
  • if i≥ai≥a and i−ai−a is colored white, ii is also colored white;
  • if i≥bi≥b and i−bi−b is colored white, ii is also colored white;
  • if ii is still not colored white, it is colored black.

In this way, each nonnegative integer gets one of two colors.

For example, if a=3a=3, b=5b=5, then the colors of the numbers (in the order from 00) are: white (00), black (11), black (22), white (33), black (44), white (55), white (66), black (77), white (88), white (99), ...

Note that:

  • It is possible that there are infinitely many nonnegative integers colored black. For example, if a=10a=10 and b=10b=10, then only 0,10,20,300,10,20,30 and any other nonnegative integers that end in 00 when written in base 10 are white. The other integers are colored black.
  • It is also possible that there are only finitely many nonnegative integers colored black. For example, when a=1a=1 and b=10b=10, then there is no nonnegative integer colored black at all.

Your task is to determine whether or not the number of nonnegative integers colored black is infinite.

If there are infinitely many nonnegative integers colored black, simply print a line containing "Infinite" (without the quotes). Otherwise, print "Finite" (without the quotes).

Input

The first line of input contains a single integer tt (1≤t≤1001≤t≤100) — the number of test cases in the input. Then tt lines follow, each line contains two space-separated integers aa and bb (1≤a,b≤1041≤a,b≤104).

Output

For each test case, print one line containing either "Infinite" or "Finite" (without the quotes). Output is case-insensitive (i.e. "infinite", "inFiNite" or "finiTE" are all valid answers).

Example

input

Copy

4
10 10
1 10
6 9
7 3

output

Copy

Infinite
Finite
Infinite
Finite

思路:根据题目描述,设当前位置之前的所有白色数中任意两个为x和y只要当前位置的数可以由ax+by表示,那就是白色的(a,b为任意常数)       

结论:ax+by=k*gcd(a,b);k号位置是白色的就是ax+by有整数解k,如果有有限个黑色的,只要gcd(a,b)=1,那从某个位置开始之后的所有位置能满足当前位置编号是整数解,即白色,black就是有限的;否则就是无限的。

完整代码:

#include <bits/stdc++.h>
#define int long long
using namespace std;
int gcd(int x,int y){return x%y?gcd(y,x%y):y;}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t,a,b;
    cin>>t;
    while(t--)
    {
        cin>>a>>b;
        if(gcd(a,b)==1){
            cout<<"Finite"<<endl;
        }
        else{
            cout<<"Infinite"<<endl;
        }
    }
    return 0;
}

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值