freopen

noip2013

无人久伴 提交于 2019-11-28 18:32:16
D1: T1:快速幂 #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<cstdlib> #include<cmath> #define LL long long using namespace std; LL n,m,k,x; inline LL quickpow(LL a,LL b){//a^b %n LL ans=1ll; while(b){ if(b&1)ans=ans*a%n; a=a*a%n; b>>=1; } return ans; } int main(){ //freopen("circle.in","r",stdin); //freopen("circle.out","w",stdout); scanf("%lld%lld%lld%lld",&n,&m,&k,&x); printf("%lld",(x%n+m*quickpow(10ll,k)%n)%n); return 0; } T2: #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<cstdlib> #include<cmath> #include<map> #define LL

Create a file if one doesn't exist - C

和自甴很熟 提交于 2019-11-28 16:26:38
问题 I want my program to open a file if it exists, or else create the file. I'm trying the following code but I'm getting a debug assertion at freopen.c. Would I be better off using fclose and then fopen immediately afterward? FILE *fptr; fptr = fopen("scores.dat", "rb+"); if(fptr == NULL) //if file does not exist, create it { freopen("scores.dat", "wb", fptr); } 回答1: You typically have to do this in a single syscall, or else you will get a race condition. This will open for reading and writing,

暑假D22

北慕城南 提交于 2019-11-28 13:26:01
星际旅行 有n个行星由m个双向虫洞连接,不同行星间最多有1个虫洞直接相连,但一个虫洞的两端可能连接同一行星。一条星际旅行的航线满足:从任意一颗行星出发,在任意一颗行星上结束,总共经过m-2个虫洞恰2次,经过两个虫洞恰1次。两条航线不同,当且仅当至少存在一个虫洞在两条航线中经过次数不同。询问本质不同的航线有多少条。 1<=n,m<=1e5 题解 先不考虑自环,如果将边拆成两条,每个点的度数都是偶数,相当于删去两条之后能够走完所有边,这貌似就是欧拉路径,所以需要2个奇数度数的点,那么删去的边一定有且只有一个公共点,不然有4个奇数度数的点或者没有。 那么就可以找到一种答案:枚举每个点作为公共点,那么就需要选出两条边删去,方案数就是$\sum_{i=1}^{n} c(du[i],2) $,du[i]是i在原图上的度数(不算自环); 现在考虑自环,他可以作为删去的边,因为走一遍位置没变没有影响,另外一条删去的边如果也是自环的话,又有一种答案就是c(num2,2),num2为自环个数;如果另外一条边是普通边的话,在2倍边的图上随便删一条边都是欧拉路径(图上边是普通边),方案就是从技术点出发,到达自环时走一遍就继续走欧拉路径,所以又有一种答案就是 num1*num2,num1是普通边个数。 如果自环不作删去的边也没有影响,路过时走两边就好。 原图也可能不连通,不过是对于边来说

20190822考试

半世苍凉 提交于 2019-11-28 07:30:43
期望:80 + 30 + 10 = 120.实际: 50 + 30 + 10 = 90. T1:小P写出二叉树的中序遍历,但他没判断左右儿子是否存在就 遍历 ,我们设访问到虚点时输出'#',给定数字(存在点)与'#'(虚点),要求你判断是否存在这样的二叉树? S1:蒟蒻理解题意错了,以为只要是给定的条件能组成二叉树即可(条件给多少个虚点就访问多少个虚点),立马想到了区间DP,类似于加分二叉树的做法.设f[ i ][ j ]表示区间[ i , j ]区间能否组成二叉树,f[ i ][ j ]唯一为true的情况为f[ i ][ k -1 ] = true,f[ k + 1 ][ j ] = true,s[ k ][ 0 ] ! =' # ',同时注意枚举的根为i/j的情况,还想了一个剪枝,就是我们判断f[ i ][ j ]为true后,只要找到中间点 k = i-1,对称区间 j1 = i -2,i1 = j1 -len + 1.只要f[ i1 ][ j1 ]&&f[ i ][ j ]&&s[ k ][ 0 ] ! = '#',我们就就能判断f[ i1 ][ j ]区间合法.这样我们就能O(n^3)过1000的点(80分),但测出来前3个点WA了,与dalao交流后发现自己理解错了,可能存在虚点就一定要访问,并不是给定多少个就访问多少个就可以了.那么唯一合法形式就为"# 数字 #

对拍过程讲解

删除回忆录丶 提交于 2019-11-28 07:28:09
我们机房有的大佬成天沉迷于学习无法自拔,导致考试时不会对拍, 虽然还是$\mathcal{AK}$了 。 对拍有什么用呢?让我们来看看蓝书是怎么说的: $\mathcal{1. }$ 当你在打比赛(或考试)时,想出了一个高分代码,但是不保证正确性。 $\mathcal{2. }$ 当你在某OJ上做某题$\mathcal{WA}$了,但是你死活调试不出来,而你又没有数据或者可以下载数据但是数据过大无法找出原因 那么对拍就可以助你一臂之力了~ 它可以快速通过你的程序制造的数据,对比你的程序与标程的答案。 如果不同,那么就会停止对拍,这时你就可以通过这组数据找错误了。 如果我们主要是为了找数据,那么一般数据强度小;如果你只想看看你的程序能否$\mathcal{AC}$, 那么就往死里开大数据吧 。 那么这里就讲一下对拍的具体过程~(本过程很大一部分是来自蓝书的) $\mathcal{Part} \mathcal{1}$对拍具体过程$ $\mathcal{P.S: }这一部分只对具体过程做一个讲解,代码见\mathcal{Part} \mathcal{2}$ $\mathcal{1. }$对你的程序进行操作。 你已经写好了你的程序了,那么现在你要在你的主函数开头加这些话: freopen("data.in", "r", stdin); freopen("data.ans", "w",

C和C++的文件操作

三世轮回 提交于 2019-11-28 05:12:13
首先来说C;   开启重定向语句:     freopen("input.txt", "r", stdin); //将之后的读入都从input.txt中读入     freopen("output.txt", "w", stdout); //将之后的写入都写到output.txt中   关闭重定向语句:     关闭读入:freopen("CON", "r", stdin);     关闭写入写文件后:freopen("CON", "w", stdout);   fopen版输入输出:     FILE *fin, *fout;     fin=fopen("input.txt", "rb");     fout=fopen("output.txt", "wb");     然后后面scanf("%d", &n)之类的全改成fscanf(fin, "%d", &n)         printf("%d", n)之类的全改成fprintf(fout, "%d", n)   fopen版关闭输入输出:     fclose(fin);     fclose(fout); 然后我们说C++;   文件流输入输出:     ifstream fin("input.txt");     ofstream fout("output.txt");//可以使用fin.is_open()和fout

codeforces Technocup 2017 - Elimination Round 2/Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 2) 题解

痴心易碎 提交于 2019-11-28 04:17:59
久违的下午场,打了一场状态不错一下rank12涨了207~~~ A. Interview with Oleg 题意: 给你一个长度不超过100的串,把其中ago开头后面不接或者接gogogo。。。的部分全部变成*** 思路: 水水,扫 /* *********************************************** Author :devil ************************************************ */ #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <stack> #include <map> #include <string> #include <time.h> #include <cmath> #include <stdlib.h> #define LL long long #define rep(i,a,b) for(int i=a;i<=b;i++) #define dep(i,a,b) for(int i=a;i>=b;i--) #define ou(a) printf("%d\n",a

Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3)

安稳与你 提交于 2019-11-28 00:40:38
A.Math Problem(CF 1262 A) 题目大意:给定n条线段,求一条线段,使得这个线段能够跟所有给定的线段都相交(端点值一样也算相交),最小化它的长度,可以是0. 很显然找出这n条线段的左端点最大值和右端点的最小值,它们的差和0的最大值即为答案。 1 #include <bits/stdc++.h> 2 #define MIN(a,b) (((a)<(b)?(a):(b))) 3 #define MAX(a,b) (((a)>(b)?(a):(b))) 4 using namespace std; 5 6 template <typename T> 7 void read(T &x) { 8 int s = 0, c = getchar(); 9 x = 0; 10 while (isspace(c)) c = getchar(); 11 if (c == 45) s = 1, c = getchar(); 12 while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); 13 if (s) x = -x; 14 } 15 16 template <typename T> 17 void write(T x, char c = ' ') { 18 int b[40], l = 0; 19

HPU积分赛 2019.8.18

有些话、适合烂在心里 提交于 2019-11-27 19:00:18
A题 给出n个数,问这n个数能不能分成奇数个连续的长度为奇数并且首尾均为奇数的序列 Codeforces849A 题解传送门 代码 1 #include <bits/stdc++.h> 2 #define ll long long 3 #define ull unsigned long long 4 #define ms(a,b) memset(a,b,sizeof(a)) 5 const int inf=0x3f3f3f3f; 6 const ll INF=0x3f3f3f3f3f3f3f3f; 7 const int maxn=1e6+10; 8 const int mod=1e9+7; 9 const int maxm=1e3+10; 10 using namespace std; 11 int a[maxm]; 12 int main(int argc, char const *argv[]) 13 { 14 #ifndef ONLINE_JUDGE 15 freopen("/home/wzy/in.txt", "r", stdin); 16 freopen("/home/wzy/out.txt", "w", stdout); 17 srand((unsigned int)time(NULL)); 18 #endif 19 ios::sync_with_stdio

2019年百度之星·程序设计大赛 初赛一 题解

孤人 提交于 2019-11-27 18:30:34
1001 Polynomial:若第一个多项式的次数大于第二个,就是1/0,若小于就是0/1,若等于就是第一个多项式最高次项系数/第二个多项式最高次项系数。 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define rep(i,l,r) for (int i=(l); i<=(r); i++) 5 typedef long long ll; 6 using namespace std; 7 8 const int N=10010; 9 int T,n,f[N],g[N]; 10 11 int main(){ 12 freopen("a.in","r",stdin); 13 freopen("a.out","w",stdout); 14 for (scanf("%d",&T); T--; ){ 15 scanf("%d",&n); int s1=n-1,s2=n-1; 16 rep(i,0,n) f[i]=g[i]=0; 17 rep(i,0,n-1) scanf("%d",&f[i]); 18 rep(i,0,n-1) scanf("%d",&g[i]); 19 while (!f[s1]) s1--; 20 while (!g[s2]) s2--; 21 if (s1>s2){ puts("1/0