So Easy!

Problem Description
  A sequence S n is defined as:

Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example, ┌3.14┐=4. You are to calculate S n.
  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 < 2 15, (a-1) 2< b < a 2, 0 < b, n < 2 31.The input will finish with the end of file.
Output
  For each the case, output an integer S n.
Sample Input
2 3 1 2013 2 3 2 2013 2 2 1 2013
Sample Output
4 14 4
Source
 1 /*思路:若(a+√b)n == x + y·√b(x、y为整数),则(a-√b)n == x - y·√b。又因为a-1 < √b < a,
 2 所以0 < (a-√b)n < 1,即x-1 < y·√b < x,所以ceil((a+√b)n) == 2x。接下来事情就容易多了,
 3 同时对x、y两部分进行快速幂取模即可*/
 4 #include <cstdio>
 5 typedef long long LL;
 6 LL a,b,n,m;
 7 void solve() {
 8     LL ans1(1),ans2(0),t1(a),t2(1);
 9     LL tmp;
10     while(n) {
11         if(n&1) {
12             tmp = ans1;
13             ans1 = (ans1*t1+ans2*t2*b)%m;
14             ans2 = (tmp*t2+ans2*t1)%m;
15         }
16         tmp = t1;
17         t1 = (t1*t1+t2*t2*b)%m;
18         t2 = 2*tmp*t2%m;
19         n >>= 1;
20     }
21     printf("%lld\n",ans1*2%m);
22 }
23 int main() {
24     while(~scanf("%lld%lld%lld%lld",&a,&b,&n,&m))
25         solve();
26     return 0;
27 }
View Code(转载)

 

转载于:https://www.cnblogs.com/contestant/archive/2013/06/03/3115869.html

<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、付费专栏及课程。

余额充值