Home Web Front-end HTML Tutorial Codeforces Round #258 (Div. 2) B. Jzzhu and Sequences (Quick Power of Matrix)_html/css_WEB-ITnose

Codeforces Round #258 (Div. 2) B. Jzzhu and Sequences (Quick Power of Matrix)_html/css_WEB-ITnose

Jun 24, 2016 pm 12:01 PM
matrix

题目链接:http://codeforces.com/problemset/problem/450/B

----------------------------------------------------------------------------------------------------------------------------------------------------------
Copy after login
Copy after login
欢迎光临天资小屋:http://user.qzone.qq.com/593830943/main
Copy after login
----------------------------------------------------------------------------------------------------------------------------------------------------------
Copy after login
Copy after login


B. Jzzhu and Sequences

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Jzzhu has invented a kind of sequences, they meet the following property:

You are given x and y, please calculate fn modulo 1000000007 (109? ?7).

Input

The first line contains two integers x and y (|x|,?|y|?≤?109). The second line contains a single integer n (1?≤?n?≤?2·109).

Output

Output a single integer representing fn modulo 1000000007 (109? ?7).

Sample test(s)

input

2 33
Copy after login

output

input

0 -12
Copy after login

output

1000000006
Copy after login

Note

In the first sample, f2?=?f1? ?f3, 3?=?2? ?f3, f3?=?1.

In the second sample, f2?=??-?1; ?-?1 modulo (109? ?7) equals (109? ?6).


代码如下:

#include <iostream>#include <cstdio>#include <cstring>using namespace std;struct A{    int mat[2][2];};A d,f;__int64 n,mod;A mul(A a,A b){    A t;    memset(t.mat,0,sizeof(t.mat));    for(int i=0;i<n;i++)    {        for(int k=0;k<n;k++)        {            if(a.mat[i][k])                for(int j=0;j<n;j++)                {                    t.mat[i][j]+=a.mat[i][k]*b.mat[k][j];                    t.mat[i][j]%=mod;                }        }    }    return t;}A quickP(int k){    A p = d ,m;    memset(m.mat,0,sizeof(m.mat));    for(int i=0;i<n;++i)//单位矩阵    {        m.mat[i][i]=1;    }    while(k)    {        if(k & 1)            m=mul(m,p);        p=mul(p,p);        k >>= 1 ;    }    return m;}int main(){    n=2;    int k,t;__int64 x,y,z;    while(scanf("%I64d%I64d",&x,&y)!=EOF)    {        int s=0;        scanf("%I64d",&z);        mod=1000000007;        if(z == 1)        {            if(x < 0)                printf("%I64d\n",x+mod);            else                printf("%I64d\n",x);            continue;        }        d.mat[0][1]=-1;d.mat[1][1] = 0;        d.mat[0][0]=d.mat[1][0]=1;        A ret=quickP(z-2);//z-2 乘的次数        __int64 ans=(ret.mat[0][0]*y%mod+ret.mat[0][1]*x%mod)%mod;        if(ans < 0)            ans+=mod;        printf("%I64d\n",ans);    }    return 0;}
Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Exploring the History and Matrix of Artificial Intelligence: Artificial Intelligence Tutorial (2) Exploring the History and Matrix of Artificial Intelligence: Artificial Intelligence Tutorial (2) Nov 20, 2023 pm 05:25 PM

In the first article of this series, we discussed the connections and differences between artificial intelligence, machine learning, deep learning, data science, and more. We also made some hard choices about the programming languages, tools, and more that the entire series would use. Finally, we also introduced a little bit of matrix knowledge. In this article, we will discuss in depth the matrix, the core of artificial intelligence. But before that, let’s first understand the history of artificial intelligence. Why do we need to understand the history of artificial intelligence? There have been many AI booms in history, but in many cases the huge expectations for AI's potential failed to materialize. Understanding the history of artificial intelligence can help us see whether this wave of artificial intelligence will create miracles or is just another bubble about to burst. us

How to calculate the determinant of a matrix or ndArray using numpy in Python? How to calculate the determinant of a matrix or ndArray using numpy in Python? Aug 18, 2023 pm 11:57 PM

In this article, we will learn how to calculate the determinant of a matrix using the numpy library in Python. The determinant of a matrix is ​​a scalar value that can represent the matrix in compact form. It is a useful quantity in linear algebra and has numerous applications in various fields including physics, engineering, and computer science. In this article, we will first discuss the definition and properties of determinants. We will then learn how to use numpy to calculate the determinant of a matrix and see how it is used in practice through some examples. Thedeterminantofamatrixisascalarvaluethatcanbeusedtodescribethepropertie

Python program to calculate the sum of the right diagonal elements of a matrix Python program to calculate the sum of the right diagonal elements of a matrix Aug 19, 2023 am 11:29 AM

A popular general-purpose programming language is Python. It is used in a variety of industries, including desktop applications, web development, and machine learning. Fortunately, Python has a simple and easy-to-understand syntax that is suitable for beginners. In this article, we will use Python to calculate the sum of the right diagonal of a matrix. What is a matrix? In mathematics, we use a rectangular array or matrix to describe a mathematical object or its properties. It is a rectangular array or table containing numbers, symbols, or expressions arranged in rows and columns. . For example -234512367574 Therefore, this is a matrix with 3 rows and 4 columns, expressed as a 3*4 matrix. Now, there are two diagonals in the matrix, the primary diagonal and the secondary diagonal

Python program to multiply two matrices using multidimensional arrays Python program to multiply two matrices using multidimensional arrays Sep 11, 2023 pm 05:09 PM

A matrix is ​​a set of numbers arranged in rows and columns. A matrix with m rows and n columns is called an mXn matrix, and m and n are called its dimensions. A matrix is ​​a two-dimensional array created in Python using lists or NumPy arrays. In general, matrix multiplication can be done by multiplying the rows of the first matrix by the columns of the second matrix. Here, the number of columns of the first matrix should be equal to the number of rows of the second matrix. Input and output scenario Suppose we have two matrices A and B. The dimensions of these two matrices are 2X3 and 3X2 respectively. The resulting matrix after multiplication will have 2 rows and 1 column. [b1,b2][a1,a2,a3]*[b3,b4]=[a1*b1+a2*b2+a3*a3][a4,a5,a6][b5,b6][a4*b2+a

C program to compare two matrices for equality C program to compare two matrices for equality Aug 31, 2023 pm 01:13 PM

The user must enter the order of the two matrices as well as the elements of both matrices. Then, compare the two matrices. Two matrices are equal if both matrix elements and sizes are equal. If the matrices are equal in size but not equal in elements, then the matrices are shown to be comparable but not equal. If the sizes and elements do not match, the display matrices cannot be compared. The following program is a C program, used to compare whether two matrices are equal-#include<stdio.h>#include<conio.h>main(){ intA[10][10],B[10][10]; in

How to reverse the account in the matrix? What does matrix inversion mean? How to reverse the account in the matrix? What does matrix inversion mean? Mar 27, 2024 pm 12:16 PM

In social media operations, matrix account backflow is a common strategy. By directing traffic between different accounts, fans can complement each other and increase their activity. Backflow between matrix accounts requires careful planning and execution, and is not a simple matter. This article will discuss in detail how to implement reversal between different accounts and the significance of matrix inversion. 1. How to reverse the account in the matrix? Among the matrix accounts, it is crucial to choose a main account, which will become the main source of traffic and the platform for publishing core content. Content planning is to formulate corresponding content plans based on account characteristics and target audiences to ensure consistent content quality and style. 3. Recommend and like each other: promote and like each other between matrix accounts, and guide fans through reasonable layout and arrangements.

How to set up Douyin account matrix? How to solve the account problem when doing matrix? How to set up Douyin account matrix? How to solve the account problem when doing matrix? Mar 25, 2024 pm 11:01 PM

With the rapid development of the short video industry, Douyin has become one of the most popular short video platforms in China. Many companies and self-employed individuals hope to expand their influence by building a Douyin account matrix. So, how to build a good Douyin account matrix? This article will answer this question for you and introduce ways to solve account problems. 1. How to set up Douyin account matrix? When establishing a Douyin account matrix, the first task is to accurately determine the positioning of each account. According to the characteristics of the brand or individual, clarify the theme and style of each account, so as to attract the target audience. Determining your content strategy is key, including content topics, publishing frequency, and shooting techniques. These steps can help account matrices take full advantage of their effectiveness. 3. Interaction between accounts: Establish a good relationship between each account

How to flatten a matrix in Python using numpy? How to flatten a matrix in Python using numpy? Aug 20, 2023 pm 04:37 PM

Inthisarticle,wewillshowyouhowtoflattenamatrixusingtheNumPylibraryinpython.numpy.ndarray.flatten()函数Thenumpymoduleincludesafunctioncallednumpy.ndarray.flatten()thatreturnsaone-dimensionalcopyofthearrayratherthanatwo-dimensionalormulti-dimensionalarra

See all articles