[2017百度之星程序設計大賽 - 複賽]A - hdu6144 B - hdu6145

這兩道題都是屬於模擬題,B題我棄坑了。

B題對於一個沒有表達式的數,找出它的循環節。

對於一個表達式也是找出它展開後重復的部分,用快速冪和快速乘計算,首尾單獨處理。

放一個A題

#include<cstdio>  
#include<algorithm>  
#include<cstring>  
#define fo(i,a,b) for(i=a;i<=b;i++)  
#define fd(i,a,b) for(i=a;i>=b;i--)  
using namespace std;  
const int N=1e5+5;  
const int P=1e9+7;  
int n,m;  
char s[N],s1[N],ch[N],s2[N];  
int main()  
{  
    int T;  
    register int i,j;  
    scanf("%d",&T);  
    while (T--)  
    {  
        scanf("%s",s);  
        int len=strlen(s),m=0,i=0;  
        while (i<=len-1)  
        {  
            if (s[i]=='(')  
            {  
                int cnt=0;  
                i++;  
                while (s[i]!=')')  
                {  
                    ch[++cnt]=s[i];  
                    i++;  
                }  
                i+=3;  
                int cnt1=0;  
                while (s[i]!=')')  
                {  
                    s2[++cnt1]=s[i];  
                    i++;  
                }  
                int tim=0,c=1;  
                fd(j,cnt1,1)tim=(tim+1ll*(s2[j]-'0')*c%P)%P,c=1ll*c*10%P;  
                while (tim--)  
                {  
                    fo(j,1,cnt)s1[++m]=ch[j];  
                }  
                i++;  
            }  
            else  
            {  
                s1[++m]=s[i];  
                i++;  
            }  
        }  
        int ans=0,c=1;  
        fd(i,m,1)  
        {  
            ans=(ans+1ll*(s1[i]-'0')*c%P)%P;  
            c=1ll*c*10%P;  
        }  
        printf("%d\n",ans);  
    }  
}  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章