2020 蓝桥杯省赛 B 组模拟赛:寻找重复项

#include<bits/stdc++.h>
using namespace std;
//set<long long> st;
//int flag;
//priority_queue <int ,vector <int >, less <int > > que;
unordered_map<int,bool> mp;
int main(){
    long long a,b,c,i=0;
    cin>>a>>b>>c;
   long long x=1;
   mp[x]=1;
    for(int i=1;i<=2000000;i++)
    {
    	x=(a*x+(x%b))%c;
        if(mp.count(x))
        {
        	cout<<i<<endl;
            return 0;
        }
        mp[x]=1;
    }
    cout<<-1<<endl;
    return 0;
}

因为超过2000000就要输出-1,所以直接扫一遍就行,时间也不会很长,当找到了的时候,就输出i,没找到就输出-1,mp.count()的功能是如果mp中存在所要查找的主键,就返回一。这是新学到的。

发布了12 篇原创文章 · 获赞 0 · 访问量 225

猜你喜欢

转载自blog.csdn.net/weixin_43305294/article/details/104276451