USACO 2017 US Open Contest, Platinum Problem 3. COWBASIC

来源:互联网 发布:c语言if语句多个条件 编辑:程序博客网 时间:2024/05/14 20:36

Bessie has invented a new programming language, but since there is no compiler yet, she needs your help to actually run her programs.
COWBASIC is a simple, elegant language. It has two key features: addition and MOO loops. Bessie has devised a clever solution to overflow: all addition is done modulo 109+7109+7. But Bessie’s real achievement is the MOO loop, which runs a block of code a fixed number of times. MOO loops and addition can, of course, be nested.

Given a COWBASIC program, please help Bessie determine what number it returns.

INPUT FORMAT (file cowbasic.in):

You are given a COWBASIC program at most 100 lines long, with each line being at most 350 characters long. A COWBASIC program is a list of statements.
There are three types of statements:

<variable> = <expression><literal> MOO {  <list of statements>}RETURN <variable>

There are three types of expressions:

<literal><variable>( <expression> ) + ( <expression> )

A literal is a positive integer at most 100,000.

A variable is a string of at most 10 lowercase English letters.

It is guaranteed that no variable will be used or RETURNed before it is defined. It is guaranteed that RETURN will happen exactly once, on the last line of the program.

OUTPUT FORMAT (file cowbasic.out):

Output a single positive integer, giving the value of the RETURNed variable.
Scoring

In 20 percent of all test cases - MOO loops are not nested.
In another 20 percent of all test cases - The program only has 1 variable. MOO loops can be nested.
In the remaining test cases, there are no further restrictions.
SAMPLE INPUT:

x = 110 MOO {  x = ( x ) + ( x )}RETURN x

SAMPLE OUTPUT:

1024
This COWBASIC program computes 210210.
SAMPLE INPUT:

n = 1nsq = 1100000 MOO {  100000 MOO {    nsq = ( nsq ) + ( ( n ) + ( ( n ) + ( 1 ) ) )    n = ( n ) + ( 1 )  }}RETURN nsq

SAMPLE OUTPUT:

4761
This COWBASIC program computes (105∗105+1)2(105∗105+1)2 (modulo 109+7109+7).
Problem credits: Jonathan Paulson
矩阵幂
数组用c++容器方便很多,忽略里面的一些坏的写法,开始是用数组在写

#include<iostream>#include<string>#include<sstream>#include<vector>#include<cstring>#include<map>#include<array>#define MOD 1000000007using namespace std;typedef array<array<long long ,105>,105> ar;typedef array<long long,105> ar1;map<string, int> V;//编号ar1 var;//0代表常数1vector<string> all;//所有语句int v=0;stringstream ss;string st,str1;//memsetvoid f(ar &x) {    for (int i = 0; i <= v; ++i)        for (int j = 0; j <= v; ++j)            x[i][j] = 0;}//memset的效果void getarr(string &s,ar &re) {    ss.clear(); ss.str(s);    ss >> str1;    int k=V[str1];     while (ss >> str1) {        if (str1 != "=" && str1 != "(" &&str1 != ")" && str1 != "+") {            if (str1[0] >= '0' && str1[0] <= '9') re[k][0] += stoi(str1);            else re[k][V[str1]]++;        }    }    for (int i = 0; i <= v; ++i)        if (i != k) re[i][i] = 1;}//提要要用的两种矩阵乘法A(v,v)*B(v,v)和A(v,v)*B(v)void mularr1(ar &a) {    ar1 tmp = {};    for (int i = 0; i <= v; ++i) {        tmp[i] = var[i];        var[i] = 0;    }    for (int i = 0; i <= v; ++i)        for (int k = 0; k <= v; ++k)            var[i] = (var[i]+a[i][k] * tmp[k])%MOD;}void mularr(ar a, ar b, ar &re) {    f(re);    for (int i = 0; i <= v; ++i)        for (int j = 0; j <= v; ++j)            for (int k = 0; k <= v; ++k)                re[i][j] = (re[i][j]+a[i][k] * b[k][j])%MOD;}//矩阵幂void poww(ar a, int b, ar &re) {    f(re);    for (int i = 0; i <= v; ++i)        re[i][i] = 1;    while (b ) {        if (b & 1) mularr(a,re,re);        mularr(a, a, a);        b >>=1;    }}//处理循环void MOO(int &b, ar &x) {    ss.clear(); ss.str(all[b]);    int k;    ss >> k;    ar xx;    ar ans = {};    for (int i = 0; i <= v; ++i)        ans[i][i] = 1;    while (1) {        f(xx);        if (all[++b].find("{") != string::npos) MOO(b, xx);        else if (all[b].find("}") != string::npos) break;        else getarr(all[b],xx);        mularr(xx, ans, ans);    }    poww(ans, k, x);}int main() {    freopen("cowbasic.in","r",stdin);    freopen("cowbasic.out", "w", stdout);    getline(cin, st);    //输入,计数变量个数    while (st[0] != 'R') {        all.push_back(st);        if (st.find("=")!=st.npos) {            ss.clear(); ss.str(st);            ss >> str1;            if (!V[str1]) V[str1] = ++v;        }        getline(cin, st);    }    var[0] = 1; ar x;    //处理开始    for (int i = 0; i < all.size(); ++i) {        f(x);        if (all[i].find("{")!=string::npos) MOO(i, x);        else getarr(all[i], x);        mularr1(x);    }    cout << var[V[string(st, 7, st.size())]] << endl;}
阅读全文
0 0
原创粉丝点击