So Easy!

本文介绍了一种高效的方法来解决复杂数学序列计算问题,通过矩阵快速计算技术简化了求解过程,避免了精度损失,并提供了实例代码演示。
部署运行你感兴趣的模型镜像

 A sequence Sn is defined as:

Sn = (a+b)n%m
Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example, ┌3.14┐=4. You are to calculate Sn.
  You, a top coder, say: So easy!

Input
  There are several test cases, each test case in one line contains four positive integers: a, b, n, m. Where 0< a, m < 215, (a-1)2< b < a2, 0 < b, n < 231.The input will finish with the end of file.
Output
  For each the case, output an integer Sn.
Sample Input

2 3 1 2013
2 3 2 2013
2 2 1 2013

Sample Output

4
14
4

题目就是问给出a,b,n,m,按公式求解Sn。

直接求精度肯定有误差,我们可以发现(一般发现不了)
(a+b)n(ab)n 加起来就是答案。因为(ab)n 是一个小数(a - 1 < b2 < a)。
sn=(a+b)n+(ab)n
c=a+b
d=ab
sn=cn+dn
则可以得到
Sn+1=(cn+dn)(c+d)cd(cn1+dn1)
Sn+1=(c+d)SncdSn1

Sn+1=2aSn(a2b)Sn1

看到这个形式就知道接下来就可以构造矩阵

 (Sn+1Sn)=(2a  a2+b1      0)(SnSn1)

下面就是矩阵的快速计算了。
注意:结果可能为负的,就是说输出结果+mod再取mod。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<utility>
#include<sstream>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
ll m;
struct mat
{
    int n,m;
    ll a[4][4];
    void clr()
    {
        memset(a,0,sizeof(a));
    }
}ini;
mat mul(mat &a,mat &b)
{
    mat c;
    c.n = c.m = 2;
    c.clr();
    for(int i = 0;i < a.n;i++)
    {
        for(int k = 0;k < b.n;k++)
        {
            for(int j = 0;j < b.m;j++)
                c.a[i][j] = (c.a[i][j] + a.a[i][k]*b.a[k][j])%m;
        }
    }
    return c;
}
mat pow_mod(mat& a,ll n)
{
    if(n == 0)return ini;
    if(n == 1)return a;
    mat temp = pow_mod(a,n/2);
    temp = mul(temp,temp);
    if(n%2)
        temp = mul(temp,a);
    return temp;
}
int main()
{
    #ifdef LOCAL
    freopen("C:\\Users\\巍巍\\Desktop\\in.txt","r",stdin);
    //freopen("C:\\Users\\巍巍\\Desktop\\out.txt","w",stdout);
    #endif // LOCAL
    ll a,b,n;
    ini.n = ini.m = 2;
    ini.clr();
    ini.a[0][0] = ini.a[1][1] = 1;
    while(scanf("%lld%lld%lld%lld",&a,&b,&n,&m) != EOF)
    {
        if(n == 1)
        {
            printf("%lld\n",(2*a + m)%m);
            continue;
        }
        mat t;
        t.n = t.m = 2;
        t.a[0][0] = 2*a;
        t.a[0][1] = -a*a + b;
        t.a[1][0] = 1;
        t.a[1][1] = 0;
        t = pow_mod(t,n - 1);
        mat ans;
        ans.n = 2;ans.m = 1;
        ans.a[0][0] = 2*a;
        ans.a[1][0] = 2;
        ans = mul(t,ans);
        printf("%lld\n",(ans.a[0][0]%m + m)%m);
    }
    return 0;
}

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

<think>我们使用C语言来完成这个文件操作任务。 步骤: 1. 使用fopen打开文件,模式为"w+",这表示读写模式,如果文件不存在则创建。 但是注意:使用"w+"会先清空文件内容。所以我们可以先写入,然后读取。 2. 写入字符串"linux so easy!"。 3. 为了读取,我们需要将文件指针重置到文件开头,使用fseek或rewind。 4. 读取文件内容,并打印到标准输出。 5. 关闭文件。 注意:在写入后,文件指针位于文件末尾,直接读取会读不到内容,所以需要用fseek或rewind将指针移到开头。 代码实现如下:</think>### C文件操作实现 以下是完整的C语言代码实现,包含文件操作的所有步骤: ```c #include <stdio.h> #include <string.h> int main() { FILE *fp = NULL; char write_data[] = "linux so easy!"; char read_data[100] = {0}; size_t len = strlen(write_data); // 1. 打开/创建文件 fp = fopen("bite", "w+"); if (fp == NULL) { perror("fopen failed"); return 1; } // 2. 写入数据 size_t written = fwrite(write_data, 1, len, fp); if (written != len) { perror("fwrite failed"); fclose(fp); return 1; } // 3. 重置文件指针到开头 fseek(fp, 0, SEEK_SET); // 4. 读取数据 size_t read = fread(read_data, 1, len, fp); if (read != len) { perror("fread failed"); fclose(fp); return 1; } // 5. 打印读取的内容 printf("File content: %s\n", read_data); // 6. 关闭文件 fclose(fp); return 0; } ``` ### 代码解释 1. **文件打开 (`fopen`)**: ```c fp = fopen("bite", "w+"); ``` - `"w+"` 模式:打开文件用于读写 - 文件不存在时自动创建 - 文件存在时清空内容 2. **数据写入 (`fwrite`)**: ```c fwrite(write_data, 1, len, fp); ``` - 将字符串 `"linux so easy!"` 写入文件 - 参数说明: - `write_data`:写入数据的缓冲区 - `1`:每个元素的大小(字节) - `len`:元素数量(字符串长度) - `fp`:文件指针 3. **文件指针重置 (`fseek`)**: ```c fseek(fp, 0, SEEK_SET); ``` - 将文件指针移动到文件开头 - 参数说明: - `0`:偏移量(字节) - `SEEK_SET`:从文件起始位置计算偏移 4. **数据读取 (`fread`)**: ```c fread(read_data, 1, len, fp); ``` - 从文件中读取相同长度的数据 - 读取结果存入 `read_data` 缓冲区 5. **输出结果 (`printf`)**: ```c printf("File content: %s\n", read_data); ``` - 将读取的内容打印到标准输出 6. **资源清理 (`fclose`)**: ```c fclose(fp); ``` - 关闭文件流并释放资源 - 防止内存泄漏和文件损坏 ### 执行结果 程序执行后会在终端显示: ``` File content: linux so easy! ``` 同时会在当前目录创建/更新 `bite` 文件,内容为 `"linux so easy!"`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值