第一次还是第二次打CF,太有趣了!
切水题的能力太弱,暴力过题,暴力是美丽的艺术~
AB暴力过,想的太慢。
C递归递推莽就完了,然而我一直在想数学统计的方法orz…
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Ayoub had an array aa of integers of size nn and this array had two interesting properties:
- All the integers in the array were between ll and rr (inclusive).
- The sum of all the elements was divisible by 33.
Unfortunately, Ayoub has lost his array, but he remembers the size of the array nn and the numbers ll and rr, so he asked you to find the number of ways to restore the array.
Since the answer could be very large, print it modulo 109+7109+7 (i.e. the remainder when dividing by 109+7109+7). In case there are no satisfying arrays (Ayoub has a wrong memory), print 00.
Input
The first and only line contains three integers nn, ll and rr (1≤n≤2⋅105,1≤l≤r≤1091≤n≤2⋅105,1≤l≤r≤109) — the size of the lost array and the range of numbers in the array.
Output
Print the remainder when dividing by 109+7109+7 the number of ways to restore the array.
Examples
input
Copy
2 1 3
output
Copy
3
input
Copy
3 2 2
output
Copy
1
input
Copy
9 9 99
output
Copy
711426616
Note
In the first example, the possible arrays are : [1,2],[2,1],[3,3][1,2],[2,1],[3,3].
In the second example, the only possible array is [2,2,2][2,2,2].
递推n每位数字,加到这位余数为0/1/2的个数,即可。
#include <cstdio>
#include <cstring>
const int maxn=2e5+10;
const int mod=1e9+7;
int n;
long long l,r,len;
long long ans;
long long num[3];
long long dp[maxn][3];
int main()
{
while(~scanf("%d%lld%lld",&n,&l,&r)){
memset(dp,0,sizeof(dp));
ans=0;len=r-l+1;
num[0]=num[1]=num[2]=len/3;
if(len%3==1){
num[r%3]++;
}
if(len%3==2){
num[r%3]++;num[l%3]++;
}
for(int i=0;i<3;++i) dp[0][i]=num[i];
for(int i=1;i<n;++i){
for(int j=0;j<3;++j){
for(int k=0;k<3;++k){
dp[i][j]+=(dp[i-1][k]*num[(j-k+3)%3])%mod;
dp[i][j]%=mod;
}
}
}
printf("%lld\n",dp[n-1][0]);
}
return 0;
}
D是一道有趣又有毒的BFS
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Kilani is playing a game with his friends. This game can be represented as a grid of size n×mn×m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player ii can expand from a cell with his castle to the empty cell if it's possible to reach it in at most sisi (where sisi is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers nn, mm and pp (1≤n,m≤10001≤n,m≤1000, 1≤p≤91≤p≤9) — the size of the grid and the number of players.
The second line contains pp integers sisi (1≤s≤1091≤s≤109) — the speed of the expansion for every player.
The following nn lines describe the game grid. Each of them consists of mm symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit xx (1≤x≤p1≤x≤p) denotes the castle owned by player xx.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print pp integers — the number of cells controlled by each player after the game ends.
Examples
input
Copy
3 3 2 1 1 1.. ... ..2
output
Copy
6 3
input
Copy
3 4 4 1 1 1 1 .... #... 1234
output
Copy
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
多起点轮流开始每次扩展s步。
写也不是很难,用bool变量和queue数组控制while循环,bfs就完事了orz
话说过来,BFS真容易写错小细节,什么变量名打错了啊之类的我怎么感觉总发生在bfsorz…
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=1010;
int n,m,p;
long long s[10];
char g[maxn][maxn];
bool vst[maxn][maxn];
int ans[10];
int dx[4]={1,0,-1,0},
dy[4]={0,1,0,-1};
struct step{
int x,y;
long long dis;
step(int a,int b,long long c):x(a),y(b),dis(c){}
};
queue <step> q[10];
void bfs()
{
bool flag=1;
memset(vst,0,sizeof(vst));
while(flag){
flag=0;
for(int i=1;i<=p;++i){
if(!q[i].empty()) flag=1;
long long cnt=q[i].front().dis;
while(!q[i].empty()){
step u=q[i].front();
if(u.dis>=cnt+s[i]) break;
q[i].pop();
for(int j=0;j<4;++j){
int nxtx=u.x+dx[j];
int nxty=u.y+dy[j];
long long nxts=u.dis+1;
if(!vst[nxtx][nxty]&&g[nxtx][nxty]=='.'){
vst[nxtx][nxty]=1;
q[i].push(step(nxtx,nxty,nxts));
ans[i]++;
}
}
}
}
}
}
int main()
{
//初始化
scanf("%d%d%d",&n,&m,&p);
for(int i=1;i<=p;++i){
scanf("%lld",&s[i]);
ans[i]=0;
}
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
scanf(" %c",&g[i][j]);
for(int k=1;k<=p;++k){
if(g[i][j]=='0'+k){
q[k].push(step(i,j,0));
ans[k]++;
}
}
}
}
for(int i=0;i<=n+1;++i){
g[i][0]=g[i][m+1]='#';
}
for(int i=0;i<=m+1;++i){
g[0][i]=g[n+1][i]='#';
}
bfs();
for(int i=1;i<=p;++i){
printf("%d%c",ans[i],i==p?'\n':' ');
}
return 0;
}