• Ehab and a 2-operation task【数论思想】


    Ehab and a 2-operation task

    题目链接(点击)

    You're given an array aa of length nn. You can perform the following operations on it:

    • choose an index ii (1≤i≤n)(1≤i≤n), an integer xx (0≤x≤106)(0≤x≤106), and replace ajaj with aj+xaj+x for all (1≤j≤i)(1≤j≤i), which means add xx to all the elements in the prefix ending at ii.
    • choose an index ii (1≤i≤n)(1≤i≤n), an integer xx (1≤x≤106)(1≤x≤106), and replace ajaj with aj%xaj%x for all (1≤j≤i)(1≤j≤i), which means replace every element in the prefix ending at ii with the remainder after dividing it by xx.

    Can you make the array strictly increasing in no more than n+1n+1 operations?

    Input

    The first line contains an integer nn (1≤n≤2000)(1≤n≤2000), the number of elements in the array aa.

    The second line contains nn space-separated integers a1a1, a2a2, ……, anan (0≤ai≤105)(0≤ai≤105), the elements of the array aa.

    Output

    On the first line, print the number of operations you wish to perform. On the next lines, you should print the operations.

    To print an adding operation, use the format "11 ii xx"; to print a modding operation, use the format "22 ii xx". If ii or xx don't satisfy the limitations above, or you use more than n+1n+1 operations, you'll get wrong answer verdict.

    Examples

    Input

    3
    1 2 3
    
    Output
    0
    Input
    3
    7 6 3
    Output
    2
    1 1 1
    2 2 4
    Note
    In the first sample:
        the array is already increasing so we don't need any operations.
    In the second sample:
        In the first step: the array becomes [8,6,3][8,6,3].
        In the second step: the array becomes [0,2,3][0,2,3].
    

    思路:

      先将输入的 a[i] 全部加上 MAX=1e6

      然后从i=1 开始到 i=n 每次将 a[i]%(a[i]-i)

      这样可以保证每次取余前面的每个值都不受影响(怎么也想不到这样……)

    例如:

               a[i]:           3              6            4           9

      a[i]+MAX:     1000003 1000006 1000004 1000009

     a[i]%(a[i]-i) :         1            2            3            4

    AC代码:

    #include<stdio.h>
    const int MAX=1e6;
    int main()
    {
        int a[MAX+5],n;
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        printf("%d
    ",n+1);
        printf("1 %d %d
    ",n,MAX);
        for(int i=0;i<n;i++){
            printf("2 %d %d
    ",i+1,a[i]+MAX-(i+1));
        }
        return 0;
    }
  • 相关阅读:
    链表 | 递归删除不带头结点链表所有x元素
    A1016 | 磨人的大模拟
    Ubuntu 下安装 Qt Designer
    shell札记
    A1102 | 反转二叉树
    顺序表 | 二分查找:两个数组合并后的中位数
    A1113 | Integer Set Partition (25)
    A1128 | 逻辑想象能力、简洁高效美观的代码、memset的使用情景
    05.字符串
    04.序列的应用
  • 原文地址:https://www.cnblogs.com/ldu-xingjiahui/p/12407461.html
Copyright © 2020-2023  润新知