AtCoder Beginner Contest 179

在这里插入图片描述
题意很简单:求前n项的和 并且第i项是i-1的平方倍%m。
思路:由于n很大,那么我们可以找循环节,但貌似循环节不一定从第一个位置开始,那么可以找出现两次和出现三次的pos,出现次数用map记录,然后一个一个算就行了。
代码:

#include<iostream>
#include<set>
#include<map>
using namespace std;
const int N=300010;
//const int mod=10000;
typedef long long ll;
ll sum[N];
//int n,m;
ll n,x,m;
//set<int>s;
map<ll,int>mp;
int qsm(int a,int b)
{
    int res=1;
    while(b)
    {
        if(b&1) res=1ll*res*a%m;
        a=a*a%m;
        b>>=1;
    }
    return res;
}
int main()
{
   cin>>n>>x>>m;
   ll xx=x;
   int s,e;
   s=-1;
   //ll sum=0;
   //cout<<xx<<endl;
   ll rap=0;
   bool f1=false ,f2=false;
   for(int i=1;;i++)
   {
        sum[i]=sum[i-1]+x;
        if(!mp[x])
        {
            mp[x]++;
        }
        else if(mp[x]==1 && !f1)
        {
            f1=true;
            rap=x;
            s=i;
            mp[x]++;
        }
        else if(mp[x]==2 && !f2)
        {
            f2=true;
            e=i-1;
            break;
        }
        x=(x*x)%m;
   }
   ll len=e-s+1;
   ll res=0;
   if(n<=e)
    cout<<sum[n]<<endl;
    else
   {
        n-=s-1;
        ll cnt=n/len;
      res=sum[s-1]+cnt*(sum[e]-sum[s-1]);   
      ll remain=n%len;
      for(ll i=s;i<=s+remain-1;i++)
      {
       res = (res+rap);
       rap=(rap*rap)%m;
      }
      cout<<res<<endl;}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值