Pagini recente » Cod sursa (job #1129038) | Cod sursa (job #1944468) | Cod sursa (job #2279575) | Cod sursa (job #1118157) | Cod sursa (job #2848352)
#include <fstream>
using namespace std;
ifstream fin("nrcuv.in");
ofstream fout("nrcuv.out");
int n, m, a[1001][1001], suma, total;
int dp[1001][27];
char x, y;
int main()
{
fin>>n>>m;
for(int i=1; i<=m; i++)
{
fin>>x>>y;
if(a[x-'a'][y-'a']==0)
{
a[x-'a'][y-'a']=1;
}
}
///dp[i]=nr de cuvinte de i litere care se
///pot forma cu litera j fiind ultima
for(int i=1; i<=26; i++)
{
dp[1][i]=1;
}
for(int i=2; i<=n; i++)
{
///dp[i] lungimea sirului
suma=0;
for(int j=1; j<=26; j++)
{
///litera in care sa se termine sirul de lungime i
int total=0;
for(int t=1; t<=26; t++)
{
if(a[j][t]==0)
{
total+=dp[i-1][t];
if(total>104659)
{
total=total%104659;
}
}
}
dp[i][j]=total;
}
}
int sol=0;
for(int i=1; i<=26; i++)
{
sol=(sol+dp[n][i])%104659;
}
fout<<sol;
return 0;
}