矩阵快速幂公式还没推完,不如来打一个表!

题目来源:http://swjtuoj.cn/problem/2434/

题意:要求输出T组,每组各四个int类型数(设为a,b,c,d),使得a*b=c*d 且a!=b!=c!=d。 并且这4T个数都不相等。
分析:这道题思路清奇,在int类型相乘超过2^31-1的时候,程序会将结果减去2^32,就是4294967296. 那么我们可以利用给出的样例,打表给出题目所需数据。需要注意的点是,输出的a,b,c,d范围都应该在int以内。
AC代码:

#include<bits/stdc++.h>
#define maxn 4294967296
using namespace std;
set<int> s; //用set记录出现过的数字
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int T;
    cin>>T;
    int a,b,c,d,aa,bb,cc,dd;
    a=aa=1;
    b=bb=4;
    c=cc=325130;
    d=dd=13210;
    s.clear();
    while(T--){
        while(s.find(a)!=s.end() || s.find(c)!=s.end()){
            a+=aa,c+=cc;  //翻倍有越界的风险,所以我用了累加。
        }
        while(s.find(b)!=s.end() || s.find(d)!=s.end()){
            b+=bb,d+=dd;
        }
        cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;
        s.insert(a);
        s.insert(b);
        s.insert(c);
        s.insert(d);
    }
    return 0;
}

原本想贴一道矩阵快速幂的,奈何公式太难推……

猜你喜欢

转载自blog.csdn.net/zhubozhen/article/details/79955534
今日推荐