450B Jzzhu and Sequences 我考研之后做的第一道题

来源:互联网 发布:mac向上箭头怎么打 编辑:程序博客网 时间:2024/06/02 23:59
从考研到现在就没做过题了,一转眼一年半过去了,自己的编程能力真的是飞速下降,如此简单的一道题我却做得如此费劲如此麻烦,哎,以后每天做题,九月之前一定codeforces至少要紫色吧,说正题:
因为计算失误,一开始以为不是从头就是一流为周期的,要自己寻找周期开始的位置,还意淫的用kmp找起始位置(真是好久没做题了,思维僵化啊)
最蛋疼的是算mod的时候,我真是一点编程的思维都没有了,完全模拟自我脑海里的手动计算的方法进行,所以我的代码写的非常湿,非常麻烦,看完网上一些人写的代码我惭愧的不行,好了,忏悔结束。
我还寻找其实点,取mod的模拟手动计算的模式,所以代码很麻烦!
#include<iostream>#include<stdio.h>using namespace std;#define mod 1000000007int abs(int i){    if(i<0)        return i*(-1);    return i;}int a[100];int main(){//  cout<<-200%107<<endl;//    freopen("in.txt","r",stdin);    int x,y,n;    while(scanf("%d %d",&x,&y)!=EOF){        scanf("%d",&n);        int temp;        a[0]=x;a[1]=y;        for(int i=0;i<2;i++){            if(a[i]<0 && a[i]>=(-1)*mod)                a[i]+=mod;            else{                int t,t1;                t=a[i]/mod;                t1=a[i]%mod;                if(t<0) {                    t=t*(-1);                    a[i]=abs((t+1)*mod+a[i])>abs(t1)? abs(t1):abs((t+1)*mod+a[i]);                }                else                    a[i]=a[i]%mod;            }        }        for(int i=2;i<100;i++){            a[i]=a[i-1]-a[i-2];            if(a[i]<0 && a[i]>=(-1)*mod)                a[i]+=mod;            else{                int t,t1;                t=a[i]/mod;                t1=a[i]%mod;                if(t<0) {                    t=t*(-1);                    a[i]=abs((t+1)*mod+a[i])>abs(t1)? abs(t1):abs((t+1)*mod+a[i]);                }                else                    a[i]=a[i]%mod;            }        }        int st=0,qt=0;        int count=0;        for(int i=0,j=6;j<100;i++,j++){            if(a[i]==a[j])                count++;            else count=0;            if(count==6){                st=i+1-6;                break;            }        }        if(n<=st)            cout<<a[n-1]<<endl;        else{            temp=(n-st)%6-1;            if(temp==-1) temp=5;            temp+=st;            printf("%d\n",a[temp]);        }    }}

0 0
原创粉丝点击