AtCoder Beginner Contest 179
程序员文章站
2024-03-19 18:57:16
...
题意很简单:求前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;}
}
推荐阅读
-
AtCoder Beginner Contest 179
-
@UPC @AtCoder Regular Contest 076 E:Connected?(思维)
-
AtCoder Beginner Contest 155 【A - E】
-
AtCoder Beginner Contest 055 D - Menagerie -枚举
-
AtCoder Grand Contest 043--A - Range Flip Find Route
-
AtCoder Beginner Contest 173(E 思维模拟 F 容斥 思维题 )
-
AtCoder Beginner Contest 163 D - Sum of Large Numbers(递推&找规律)
-
AtCoder Beginner Contest 182----C. To 3
-
AtCoder Beginner Contest 182----E. Akari
-
AtCoder Beginner Contest 182 题解