UVA10689-Yet another Number Sequence


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>

using namespace std;

struct node
{
    int m[3][3];
    node()
    {
        memset(m,0,sizeof m);
    }
}x,y,z,k;
int mod;

node mul(node p,node q)
{
    node sum;
    for(int i=1;i<=2;i++)
    {
        for(int j=1;j<=2;j++)
        {
            for(int k=1;k<=2;k++)
                sum.m[i][j]=(sum.m[i][j]+p.m[i][k]*q.m[k][j])%mod;
        }
    }
    return sum;
}

node f(int n)
{
    node sum=z,l=y;
    while(n)
    {
        if(n&1) sum=mul(sum,l);
        n>>=1;
        l=mul(l,l);
    }
    return sum;
}

int main()
{
    int t,a,b,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d %d %d",&a,&b,&n,&mod);
        int l=mod;
        mod=1;
        while(l--) mod*=10;
        if(n==0)
        {
            printf("%d\n",a%mod);continue;
        }
        if(n==1)
        {
            printf("%d\n",b%mod);continue;
        }
        x.m[1][1]=b;x.m[1][2]=a;
        y.m[1][1]=y.m[1][2]=y.m[2][1]=1;
        for(int i=0;i<=2;i++)
            z.m[i][i]=1;
        k=f(n-1);
        k=mul(x,k);
        printf("%d\n",k.m[1][1]);
    }
    return 0;
}

Sure! Here is a Python program that can perform the operations you described: ```python from collections import OrderedDict class NumberSequence: def __init__(self): self.sequences = {} def new(self, id): if id >= 200000: return "Error: id must be less than 200000" self.sequences[id] = [] return "Success: created sequence for id {}".format(id) def add(self, id, num): if id not in self.sequences: return "Error: sequence for id {} does not exist".format(id) self.sequences[id].append(num) return "Success: added {} to sequence for id {}".format(num, id) def merge(self, id1, id2): if id1 not in self.sequences: return "Error: sequence for id {} does not exist".format(id1) if id2 not in self.sequences: return "Error: sequence for id {} does not exist".format(id2) merged_sequence = self.sequences[id1] + self.sequences[id2] self.sequences[id1] = merged_sequence self.sequences[id2] = [] return "Success: merged sequence for id {} and id {}, and emptied id {}".format(id1, id2, id2) def unique(self, id): if id not in self.sequences: return "Error: sequence for id {} does not exist".format(id) unique_sequence = list(OrderedDict.fromkeys(self.sequences[id])) self.sequences[id] = unique_sequence return "Success: removed duplicate elements from sequence for id {}".format(id) def out(self, id): if id not in self.sequences: return "Error: sequence for id {} does not exist".format(id) sorted_sequence = sorted(self.sequences[id]) output_str = " ".join(str(num) for num in sorted_sequence) return output_str ``` To use this program, you can create a `NumberSequence` object and call its methods to perform the desired operations. For example: ```python ns = NumberSequence() print(ns.new(1)) # Success: created sequence for id 1 print(ns.add(1, 5)) # Success: added 5 to sequence for id 1 print(ns.add(1, 3)) # Success: added 3 to sequence for id 1 print(ns.add(2, 7)) # Error: sequence for id 2 does not exist print(ns.merge(1, 2)) # Error: sequence for id 2 does not exist print(ns.new(2)) # Success: created sequence for id 2 print(ns.add(2, 7)) # Success: added 7 to sequence for id 2 print(ns.add(2, 3)) # Success: added 3 to sequence for id 2 print(ns.merge(1, 2)) # Success: merged sequence for id 1 and id 2, and emptied id 2 print(ns.out(1)) # 3 5 7 print(ns.unique(1)) # Success: removed duplicate elements from sequence for id 1 print(ns.out(1)) # 3 5 7 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值