A题,bfs会爆队列,我用的dp
建一个二维数组,表示到dp[i][j]有多少种路径
dp[1][1] = 1;
之后只需要双重循环进行遍历:
1.一个点如果可以从上面下来,就需要加上上面那个点的所有路径
2.一个点如果可以从左边过来,就需要加上左边那个点的所有路径
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 55;
char a[maxn][maxn];
const int mod = 1e9 + 7;
long long dp[maxn][maxn];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
cin >> a[i][j];
}
}
dp[1][1] = 1;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
if(i != 1 && a[i-1][j] != 'R')//能从上边下来
{
dp[i][j] += dp[i-1][j];
dp[i][j] %= mod;
}
if(j != 1 && a[i][j-1] != 'D')//能从左边过来
{
dp[i][j] += dp[i][j-1];
dp[i][j] %= mod;
}
}
}
cout << dp[n][m] % mod << endl;
return 0;
}
CD模拟,水题
H题有点意思:
算术基本定理这几天用了好多次…
题目要求不是质数的因数个数,即对1~n每个数使用算术基本定理计算因数个数,然后分解质因数,用所有因数个数减去质数因数个数即可。
一开始我t了,后来在计算质数因数个数的时候,特判一下就可以了。
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e5 + 50;
int su[N] = {0};
long long zhi[N];
int z = 0;
int ans[N] = {0};
int aishai()
{
su[1] = 1;
for(int i = 2; i <= 1e5 + 5; i++)
{
if(su[i] == 0)
{
zhi[z] = i;
z++;
//num[i] = num[i-1] + 1;
for(int j = 2; j * i <= 1e5 + 5; j++)
{
su[i*j] = 1;
}
}
// else
// num[i] = num[i-1];
}
//cout << z << endl;
}
long long hanshu(long long a)
{
long long ans = 1;
long long n = a;
long long num = 0;
for(int i = 0; i < z && zhi[i] <= a; i++)
{
if(a % zhi[i] == 0)
{
num++;
long long cnt = 0;
while(a % zhi[i] == 0)
{
a /= zhi[i];
cnt++;
}
ans *= (1 + cnt);
}
}
if(a > 1)
ans *= 2;
return ans-num;//因数个数
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
aishai();
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
long long cnt;
if(su[i] == 0)
cnt = 1;
else
{
cnt = hanshu(i);
}
ans[--cnt]++;
}
for(int i = 0; i < m; i++)
{
int k;
cin >> k;
cout << ans[k] << endl;
}
return 0;
}