C. Prime Number
problem:
输入 n and x (1 ≤ n ≤ 105, 2 ≤ x ≤ 109) a1, a2, ..., an (0 ≤ a1 ≤ a2 ≤ ... ≤ an ≤ 109). x是素数
1/(x^a1) + 1/(x^a2) + …… + 1/(x^an) 可以变为 t/s 其中 s = x ^ (a1 + a2 + …… + an)
求 gcd(s, t) % (1e9+7)
think:
另sum = (a1 + a2 + …… + an)
t = x^(sum-a1) + x^(sum-a2) + …… + x^(sum-an)
所以可以把每个 ai 变成 sum - ai
看 t 最多变成 x^ans * k, ans为所求,k为任意正整数
一直搞最小的ai ,所有ai都减去最小的 ai 搞完他就是0了,然后看0的个数是不是x的倍数,不是就搞完了,因为x是素数
注意最后的ans不能大于sum,(如所有a都是0
code:
D. Pair of Numbers
problem:
输入 n (1 ≤ n ≤ 3·105). a1, a2, ..., an (1 ≤ ai ≤ 106).
问最长的区间[l, r]满足存在j (l<=j<=r) 使得每个i (l<=i<=j) a[i]能整除a[j]
输出满足要求的区间个数,区间r-l,每个满足区间的l
think:
处理出每个j最左边连续可以整除他的,和最右边连续可以整除他的
code:
E. Neatness
problem:
输入n, x0, y0 (2 ≤ n ≤ 500, 1 ≤ x0, y0 ≤ n)和一张n*m的01矩阵。
要往某个方向走,正前方必须有1。可以把1变为0可以把0变为1. 问怎么把所有的都变成0 并回到x0 y0
think:
bfs
code:
int a[555][555]; int vis[555][555]; int go[555][555][4]; int dx[] = {0, 1, -1, 0}; int dy[] = {1, 0, 0, -1}; char ans[3333333]; int cnt; int n; inline char to(int i){ if(i == 0) return 'R'; else if(i == 1) return 'D'; else if(i == 2) return 'U'; else return 'L'; } inline bool in(int x){ return x >= 1 && x <= n; } void bfs(int x, int y){ queue<pair<int, int> >Q; Q.push(make_pair(x, y)); vis[x][y] = 1; while(!Q.empty()){ x = Q.front().first; y = Q.front().second; Q.pop(); for(int d = 0; d < 4; ++d){ int xx = x + dx[d]; int yy = y + dy[d]; int last = 0, i = 0; while(in(xx) && in(yy) && vis[xx][yy]==0){ ++i; if(a[xx][yy]==1) last = i; xx += dx[d]; yy += dy[d]; } xx = x; yy = y; for(int i = 1; i <= last; ++i){ xx += dx[d]; yy += dy[d]; Q.push(make_pair(xx, yy)); vis[xx][yy] = 1; go[xx-dx[d]][yy-dy[d]][d] = 1; } } } } void dfs(int x, int y){ if(a[x][y]==0) ans[cnt++] = '1'; for(int d = 0; d < 4; ++d){ if(go[x][y][d]){ ans[cnt++] = to(d); dfs(x + dx[d], y + dy[d]); ans[cnt++] = to(3-d); } } ans[cnt++] = '2'; } int main(){ int x0, y0; scanf("%d%d%d", &n, &x0, &y0); for(int i = 1; i <= n; ++i) for(int j = 1; j <= n; ++j) scanf("%d", &a[i][j]); bfs(x0, y0); bool ok = true; for(int i = 1; i <= n; ++i){ for(int j = 1; j <= n; ++j){ if(a[i][j] == 1 && vis[i][j] == 0){ ok = false; break; } } } if(ok){ puts("YES"); cnt = 0; dfs(x0, y0); ans[cnt] = '\0'; puts(ans); } else { puts("NO"); } return 0; }