Untitled
raw download clone
TEXT
views 22
,
size 1144 b
# include <stdio.h>
# include <string.h>

char a[3000000];

int main(void)
{
    int i, j, l, t, x, times;
    long long int a_len, tlen, add;
    scanf("%d", &t);
    for(i=0;i<t;i++)
    {
        scanf("%d", &x);
        scanf("%s", a);
        a_len = strlen(a);
        for(j=1;j<=x;j++)  
        {
            times = a[j-1]-'0';  //重複的次數
            if(a_len<=x && times>1)
            {
                tlen = a_len;
                for(l=0;l<times-1 && tlen<=x;l++)  //需要更新字串的部分
                {
                    if(1000000-tlen < a_len-j)  //防止溢位
                        strncat(a, &a[j], 1000000-tlen);
                    else
                        strncat(a, &a[j], a_len-j);
                    tlen += (a_len-j);  //記錄更新後的字串長度
                }
            }
            add = (times-1)*(a_len-j)%1000000007;  
            a_len = (a_len+add)%1000000007;  //計算實際的長度
        }  
        if(a_len <= 0)  //防止mod後結果為0
            a_len += 1000000007;
        printf("%lld\n", a_len);
    }
    return 0;
}
close fullscreen
Login or Register to edit or fork this paste. It's free.