CF 359C - 359E #209 (Div. 2)

C. Prime Number

problem:

输入 n and x (1 ≤ n ≤ 1052 ≤ 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:

  1. <span style="font-size:14px">LL a[111111];  
  2. LL pow(LL x, LL n){  
  3.     LL ans = 1;  
  4.     LL tmp = x;  
  5.     while(n){  
  6.         if(n & 1LL) ans = (ans*tmp) % mod;  
  7.         tmp = (tmp*tmp) % mod;  
  8.         n >>= 1LL;  
  9.     }  
  10.     return ans;  
  11. }  
  12.   
  13. int main() {  
  14.     int n;  
  15.     LL x, sum = 0;  
  16.     scanf("%d%I64d", &n, &x);  
  17.     for(int i = 0; i < n; ++i){  
  18.         scanf("%I64d", &a[i]);  
  19.         sum += a[i];  
  20.     }  
  21.     for(int i = 0; i < n; ++i) a[i] = sum - a[i];  
  22.     bool ok = true;  
  23.     LL cnt = 0;  
  24.     while(ok){  
  25.         cnt += a[n-1];  
  26.         LL num = 0;  
  27.         LL tmp = a[n-1];  
  28.         for(int i = n - 1; i >= 0; --i){  
  29.             a[i] -= tmp;  
  30.             if(a[i] == 0) ++num;  
  31.         }  
  32.         if(num % x == 0){  
  33.             n = n - num;  
  34.             num = num / x;  
  35.             for(LL i = 0; i < num; ++i){  
  36.                 a[n++] = 1LL;  
  37.             }  
  38.         }  
  39.         else ok = false;  
  40.     }  
  41.   
  42.     LL ans = pow(x, min(cnt, sum));  
  43.     cout<<ans<<endl;  
  44.     return 0;  
  45. }</span>  


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:

  1. const int N = 333333;  
  2. int a[N];  
  3. int L[N];  
  4. int R[N];  
  5. int vis[N];  
  6. int main ()  
  7. {  
  8.     int n;  
  9.     scanf("%d", &n);  
  10.     for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);  
  11.     for(int i = 1; i <= n; ++i){  
  12.         L[i] = i;  
  13.         for(int j = i - 1; j >= 1; ){  
  14.             if(a[j] % a[i] == 0){  
  15.                 L[i] = L[j];  
  16.                 j = L[j] - 1;  
  17.             }  
  18.             else break;  
  19.         }  
  20.     }  
  21.     int mx = 0;  
  22.     for(int i = n; i >= 1; --i){  
  23.         R[i] = i;  
  24.         for(int j = i + 1; j <= n; ){  
  25.             if(a[j] % a[i] == 0){  
  26.                 R[i] = R[j];  
  27.                 j = R[j] + 1;  
  28.             }  
  29.             else break;  
  30.         }  
  31.         mx = max(mx, R[i]-L[i]);  
  32.     }  
  33.     int num = 0;  
  34.     for(int i = 1; i <= n; ++i){  
  35.         if(R[i] - L[i] == mx){  
  36.             if(vis[ L[i] ] == 0){  
  37.                 ++num;  
  38.                 vis[ L[i] ] = 1;  
  39.             }  
  40.         }  
  41.     }  
  42.     printf("%d %d\n" , num, mx);  
  43.     for(int i = 1; i <= n; ++i){  
  44.         if(vis[i] == 1) printf("%d ", i);  
  45.     }  
  46.     puts("");  
  47.     return 0;  
  48. }  


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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值