day1 转圈游戏

作为2013年的第一题,要拿到90分还是比较简单的~时间复杂度为O(k),代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,k,x,ans;
int main()
{
freopen("circle.in","r",stdin);
freopen("circle.out","w",stdout);
cin>>n>>m>>k>>x;
ans=10*m%n;
for (int i=2;i<=k;i++)
ans=ans*10%n;
if(ans+x>n-1)
cout<<ans+x-n;
else
cout<<ans+x;
return 0;
}

当然,正解并非如此,10分也是分嘛。正解的话是通过快速幂来求解,复杂度为O(log(k)),代码如下:

#include<cstdio>
#include<cstring>
int k;
long long ans;
int n,m,x;
long long Exp(int y)
{
if(y==1) return 10%n;
long long temp=Exp(y>>1);
if(y&1)
{
return (((temp*temp)%n)*10)%n;
}
else return (temp*temp)%n;
}
int main()
{
freopen("circle.in","r",stdin);
freopen("circle.out","w",stdout);
scanf("%d%d%d%d",&n,&m,&k,&x);
ans=Exp(k);
ans*=m;
ans%=n;
ans+=x;
ans%=n;
printf("%lld",ans);
return 0;
}

清清正正射命丸文是也~

posted @ 2016-07-11 21:10  ShameimaruAya  阅读(194)  评论(0编辑  收藏  举报