Untitled
raw download clone
TEXT
views 51
,
size 875 b
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MOD 1000000007



long long int answer(int x, char s[2000008]){
	long long int add = 0, s_len, t_len, l;
	int left;
	int i, j;
	char temp[2000008];
	memset(temp, '\0', strlen(temp));
	s_len = strlen(s);
	l = s_len;

	for (i=1; i<=x; i++){

		left = s[i-1] - '0';
		add = (l - i)%MOD * (left - 1)%MOD;
		l = (l + add)%MOD;

		if (s_len <= x && left > 1) {	
			strncpy(temp, s + i, s_len - i);	
			t_len = strlen(temp);
			for (j=0; j < left-1 && s_len <= x; j++){
				strcat(s, temp);
				s_len += t_len;
			}
		}

		if (l < 0) l += MOD;
	}
	return l;
}


int main()
{
	int t, x, i;
	scanf("%d", &t);
	char s[1000];
	long long int ans;
	for (i=0; i<t; i++){
		scanf("%d%s", &x, s);
		ans = answer(x, s);
		printf("%lld\n", ans);
	}
    return 0;
}
close fullscreen
Login or Register to edit or fork this paste. It's free.