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:
LL a[111111];
LL pow(LL x, LL n){
LL ans = 1;
LL tmp = x;
while(n){
if(n & 1LL) ans = (ans*tmp) % mod;
tmp = (tmp*tmp) % mod;
n >>= 1LL;
}
return ans;
}
int main() {
int n;
LL x, sum = 0;
scanf("%d%I64d", &n, &x);
for(int i = 0; i < n; ++i){
scanf("%I64d", &a[i]);
sum += a[i];
}
for(int i = 0; i < n; ++i) a[i] = sum - a[i];
bool ok = true;
LL cnt = 0;
while(ok){
cnt += a[n-1];
LL num = 0;
LL tmp = a[n-1];
for(int i = n - 1; i >= 0; --i){
a[i] -= tmp;
if(a[i] == 0) ++num;
}
if(num % x == 0){
n = n - num;
num = num / x;
for(LL i = 0; i < num; ++i){
a[n++] = 1LL;
}
}
else ok = false;
}
LL ans = pow(x, min(cnt, sum));
cout<<ans<<endl;
return 0;
}
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:
const int N = 333333;
int a[N];
int L[N];
int R[N];
int vis[N];
int main ()
{
int n;
scanf("%d", &n);
for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for(int i = 1; i <= n; ++i){
L[i] = i;
for(int j = i - 1; j >= 1; ){
if(a[j] % a[i] == 0){
L[i] = L[j];
j = L[j] - 1;
}
else break;
}
}
int mx = 0;
for(int i = n; i >= 1; --i){
R[i] = i;
for(int j = i + 1; j <= n; ){
if(a[j] % a[i] == 0){
R[i] = R[j];
j = R[j] + 1;
}
else break;
}
mx = max(mx, R[i]-L[i]);
}
int num = 0;
for(int i = 1; i <= n; ++i){
if(R[i] - L[i] == mx){
if(vis[ L[i] ] == 0){
++num;
vis[ L[i] ] = 1;
}
}
}
printf("%d %d\n" , num, mx);
for(int i = 1; i <= n; ++i){
if(vis[i] == 1) printf("%d ", i);
}
puts("");
return 0;
}
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; }