主程序
#include<bits/stdc++.h>
#define u64 unsigned long long
#define i64 long long
namespace data{
std::vector<std::string> data;
void load(){
std::ios::sync_with_stdio(false);
std::vector<std::string> tmp;
while(1){
std::string str;
std::cin>>str;
if(std::cin.eof())break;
tmp.push_back(str);
}
while(tmp.size()){
data.push_back(tmp.back());
tmp.pop_back();
}
}
std::string read(){
std::string t=data.back();
data.pop_back();
return t;
}
}
namespace proge{
std::string check="87";
#define lowbit(x) ((x)&(-(x)))
void run(){
int a,b,c,n;
std::cout<<"Hello, National Olympiad in Informatics!"<<std::endl;
a=std::stoi(data::read());
b=std::stoi(data::read());
std::cout<<a+b<<std::endl;
std::cout<<(char)a<<"inter "<<(char)b<<"amp"<<std::endl;
long long lax=lowbit((long long)a);
long long lbx=lowbit((long long)b);
long long la=a;
long long lb=b;
if(lax<lbx)while(1);
la/=lbx;
lb/=lbx;
long long ans=1;
for(long long k=2;k<(1ll<<32)/lbx;k*=2){
ans*=lb;
lb*=lb;
}
long long bound=(1ll<<32)/lbx;
long long rs=-la*ans;
std::cout<<"0 "<<(rs&(bound-1))<<std::endl;
std::cout<<0<<std::endl;
for(int i=0;i<5;i++){
std::cout<<"."<<std::endl;
}
}
}
namespace prog1{
u64 fmul(u64 a,u64 b,u64 c){
u64 ans=0;
u64 t=a;
while(b){
if(b&1){
ans+=t;
if(ans<t)ans-=c;
ans%=c;
}
if(t>>63){
t=t*2-c;
t%=c;
}else{
t=t*2%c;
}
b>>=1;
}
return ans;
}
std::string check="593471295098401";
void run(){
for(int i=1;i<=10;i++){
u64 a=std::stoull(data::read());
u64 b=std::stoull(data::read());
u64 c=std::stoull(data::read());
std::cout<<fmul(a,b,c)<<std::endl;
}
}
}
namespace prog2{
std::string check="896703232208161";
long long m;
struct matrix{
long long n[2][2];
matrix(){
n[0][0]=1;
n[0][1]=0;
n[1][0]=0;
n[1][1]=1;
}
matrix operator*(matrix b){
matrix t;
t.n[0][0]=(n[0][0]*b.n[0][0]+n[0][1]*b.n[1][0])%m;
t.n[0][1]=(n[0][0]*b.n[0][1]+n[0][1]*b.n[1][1])%m;
t.n[1][0]=(n[1][0]*b.n[0][0]+n[1][1]*b.n[1][0])%m;
t.n[1][1]=(n[1][0]*b.n[0][1]+n[1][1]*b.n[1][1])%m;
return t;
}
};
matrix fpw(matrix a,long long p){
matrix ans;
while(p){
if(p&1){
ans=ans*a;
}
p>>=1;
a=a*a;
}
return ans;
}
void run(){
for(int i=1;i<=10;i++){
u64 a=std::stoull(data::read());
u64 b=std::stoull(data::read());
matrix q;
q.n[0][0]=0;
q.n[0][1]=1;
q.n[1][0]=1;
q.n[1][1]=1;
matrix r;
m=b;
r=fpw(q,a);
u64 ans=r.n[0][0];
ans=ans*ans%m;
std::cout<<ans<<std::endl;
}
}
}
namespace prog3{
u64 fpw(u64 a,u64 p){
u64 ans=1;
while(p){
if(p&1)ans*=a;
a*=a;
p>>=1;
}
return ans;
}
u64 inv3,inv5;
std::string check="1000000000000000";
void run(){
inv3=fpw(3,(1ull<<63)-1);
inv5=fpw(5,(1ull<<63)-1);
u64 n=std::stoull(data::read());
u64 s0=n+1;
u64 nn_1;
u64 s1=1,s2=1,s3=1,s4=1;
if(n&1){
nn_1=(n+1)/2*n;
}else{
nn_1=n/2*(n+1);
}
s1=nn_1;
s2=nn_1*(2*n+1)*inv3;
s3=nn_1*nn_1;
s4=nn_1*(2*n+1)*(3*n*n+3*n-1)*inv3*inv5;
std::cout<<s0<<std::endl;
std::cout<<s0<<std::endl;
std::cout<<s1<<std::endl;
std::cout<<s1<<std::endl;
std::cout<<s2<<std::endl;
std::cout<<s2<<std::endl;
std::cout<<s3<<std::endl;
std::cout<<s3<<std::endl;
std::cout<<s4<<std::endl;
std::cout<<s4<<std::endl;
}
}
namespace prog4{
int seed;
int next_rand(){
static const int P = 1000000007, Q = 83978833, R = 8523467;
return seed = ((long long)Q * seed % P * seed + R) % P;
}
const int N = 5000, inf = 0x3F3F3F3F;
bool data[N + 11][N + 11];
unsigned short dis[N+11][N+11];
void generate_input(int n,int m){
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
data[i][j] = (bool)((next_rand() % 8) > 0);
}
}
}
int n,m,tp;
std::string check="1315423911";
long long cal1(){
int cnt=0;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(data[i][j]){
++cnt;
}
}
}
return 1ll*cnt*(cnt-1);
}
inline unsigned short min(unsigned short a,unsigned short b){
if(a<b)return a;
return b;
}
long long cal2(){
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(!data[i][j]){
dis[i][j]=0;
}else{
dis[i][j]=32766;
}
}
}
for(int j=1;j<m;j++){
dis[0][j]=min(dis[0][j],dis[0][j-1]+1);
}
for(int i=1;i<n;i++){
dis[i][0]=min(dis[i][0],dis[i-1][0]+1);
for(int j=1;j<m;j++){
if(dis[i][j]==0)continue;
dis[i][j]=min(dis[i][j-1],dis[i-1][j])+1;
}
}
for(int j=m-2;j>=0;j--){
dis[n-1][j]=min(dis[n-1][j],dis[n-1][j+1]+1);
}
for(int i=n-2;i>=0;i--){
dis[i][m-1]=min(dis[i][m-1],dis[i+1][m-1]+1);
for(int j=m-2;j>=0;j--){
dis[i][j]=min(dis[i][j],min(dis[i][j+1],dis[i+1][j])+1);
}
}
long long ans=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
ans+=dis[i][j];
}
}
return ans;
}
void run(){
seed=std::stoi(data::read());
for(int i=1;i<=10;i++){
n=std::stoi(data::read());
m=std::stoi(data::read());
tp=std::stoi(data::read());
generate_input(n,m);
if(tp==0){
std::cout<<cal1()<<std::endl;
}else{
std::cout<<cal2()<<std::endl;
}
}
}
}
namespace prog5{
int seed;
int next_rand(){
static const int P = 1000000007, Q = 83978833, R = 8523467;
return seed = ((long long)Q * seed % P * seed + R) % P;
}
const int N = 5011;
bool data[N][N];
void generate_input(int n,int m){
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
data[i][j] = (bool)((next_rand() % 8) > 0);
}
}
}
int st[N];
int pos[N];
int top;
short up[N][N];
std::string check="1315423911";
void run(){
seed=std::stoi(data::read());
for(int k=0;k<10;k++){
int n,m;
n=std::stoi(data::read());
m=std::stoi(data::read());
generate_input(n,m);
for(int j=0;j<m;j++){
if(data[0][j])up[0][j]=1;
else up[0][j]=0;
}
for(int i=1;i<n;i++){
for(int j=0;j<m;j++){
if(!data[i][j])up[i][j]=0;
else up[i][j]=up[i-1][j]+1;
}
}
long long ans=0;
for(int i=0;i<n;i++){
top=0;
long long sum=0;
for(int j=0;j<m;j++){
pos[top]=j;
while(top&&st[top-1]>=up[i][j]){
sum-=(pos[top]-pos[top-1])*(st[top-1]-up[i][j]);
--top;
}
st[top]=up[i][j];
sum+=up[i][j];
++top;
ans+=sum;
}
}
std::cout<<ans<<std::endl;
}
}
}
namespace prog6{
std::string check="6552399101551502246";
void run(){
for(int i=1;i<=10;i++){
u64 n=std::stoull(data::read());
u64 a=std::stoull(data::read());
u64 b=std::stoull(data::read());
u64 c=std::stoull(data::read());
u64 x0=0,x1=0,cnt=0;
while(true){
++cnt;
x0=(x0*x0*a+b)%c;
x1=(x1*x1*a+b)%c;
x1=(x1*x1*a+b)%c;
if(n==cnt)break;
if(x0==x1)break;
}
n%=cnt;
for(u64 i=0;i<n;i++){
x0=(x0*x0*a+b)%c;
}
std::cout<<x0<<std::endl;
}
}
}
namespace prog7{
std::string check="NAP???JB?H???DC?";
struct node{
char map[16][16];
unsigned short rhas[16];
unsigned short chas[16];
unsigned short mhas[16];
int cnt;
};
std::string s[16];
int bits[1<<16];
inline int id(int x,int y){
x/=4;
y/=4;
return x*4+y;
}
void run(){
for(int i=0;i<(1<<16);i++){
bits[i]=0;
for(int k=0;k<16;k++){
if(i&(1<<k))bits[i]++;
}
}
for(int t=1;t<=4;t++){
for(int j=0;j<16;j++){
s[j]=data::read();
}
node st;
st.cnt=0;
for(int i=0;i<16;i++){
st.rhas[i]=0;
st.chas[i]=0;
st.mhas[i]=0;
}
for(int i=0;i<16;i++){
for(int j=0;j<16;j++){
if(s[i][j]=='?'){
st.map[i][j]=-1;
}else{
st.map[i][j]=s[i][j]-'A';
st.rhas[i]|=1<<st.map[i][j];
st.chas[j]|=1<<st.map[i][j];
st.mhas[id(i,j)]|=1<<st.map[i][j];
st.cnt++;
}
}
}
std::list<node> que;
que.push_back(st);
node res;
while(que.size()){
node cur=que.front();
if(cur.cnt==256)break;
que.pop_front();
int x=-1,y=-1,minc=-1,st=0;
for(int i=0;i<16;i++){
for(int j=0;j<16;j++){
if(cur.map[i][j]==-1){
unsigned short fl=cur.rhas[i]|cur.chas[j]|cur.mhas[id(i,j)];
int b=bits[fl];
if(b>minc){
minc=b;
x=i;
y=j;
st=fl;
}
}
}
}
for(int k=0;k<16;k++){
if((st&(1<<k))==0){
node nx=cur;
nx.map[x][y]=k;
nx.rhas[x]|=1<<k;
nx.chas[y]|=1<<k;
nx.mhas[id(x,y)]|=1<<k;
nx.cnt++;
que.push_back(nx);
}
}
}
std::string str="";
if(que.empty()){
str="NO SOLUTION.";
}else{
node res=que.front();
for(int i=0;i<16;i++){
for(int j=0;j<16;j++){
char t='A'+res.map[i][j];
str.push_back(t);
}
}
}
for(int p=0;p<t;p++){
std::cout<<str<<std::endl;
}
}
}
}
namespace prog8{
std::string check="1000000000000000";
int cnt[10][8];
void cal(int n){
for(int a=1;a<=n;a++){
for(int b=1;b<=n;b++){
for(int c=1;c<=n;c++){
for(int d=1;d<=n;d++){
for(int e=1;e<=n;e++){
for(int f=1;f<=n;f++){
for(int g=1;g<=n;g++){
if(a<b&&b<c&&c<d&&d<e&&e<f&&f<g)cnt[0][n]++;
if(a<b&&c<g&&c<d&&e<f&&a<d)cnt[1][n]++;
if(a<d&&d<f&&c<f&&c<e&&b<d)cnt[2][n]++;
if(d<e&&b<d&&a<f&&d<e&&b<g)cnt[3][n]++;
if(c<f&&b<f&&b<c&&f<g&&b<f)cnt[4][n]++;
if(b<d&&b<c&&d<f&&c<e&&b<e)cnt[5][n]++;
if(a<c&&a<b&&c<e&&b<f&&e<g)cnt[6][n]++;
if(b<d&&b<f&&a<g&&c<g&&a<e)cnt[7][n]++;
if(b<f&&a<c&&c<d&&a<c&&b<e)cnt[8][n]++;
if(d<e&&e<f&&a<d&&c<g&&b<d)cnt[9][n]++;
}
}
}
}
}
}
}
}
const i64 M=1234567891;
i64 inv(i64 n){
int t=M-2;
i64 ans=1;
while(t){
if(t&1)ans=ans*n%M;
t>>=1;
n=n*n%M;
}
return ans;
}
i64 calc(int* vals,int len,i64 x){
x%=M;
i64 ans=0;
for(int i=0;i<len;i++){
i64 mul=vals[i];
for(int j=0;j<len;j++){
if(i==j)continue;
mul=mul*(x-j)%M*inv(i-j)%M;
if(mul<0)mul+=M;
}
ans+=mul;
}
ans%=M;
ans+=M;
ans%=M;
return ans;
}
void run(){
for(int i=0;i<8;i++)cal(i);
i64 n=std::stoull(data::read());
for(int i=0;i<10;i++){
std::cout<<calc(cnt[i],8,n)<<std::endl;
}
}
}
namespace prog9{
std::string check="1b36ea1c9b7a1c3ad668b8bb5df7963f";
void run(){
std::cout<<"1984"<<std::endl;
std::cout<<"123456"<<std::endl;
std::cout<<"chenlijie"<<std::endl;
std::cout<<"$_$"<<std::endl;
std::cout<<"we"<<std::endl;
std::cout<<"hold"<<std::endl;
std::cout<<"these"<<std::endl;
std::cout<<"truths"<<std::endl;
std::cout<<"to be"<<std::endl;
std::cout<<"selfevident"<<std::endl;
}
}
namespace prog10{
std::string check="1";
u64 m1,m2,m3,m4;
void run(){
m1=6754098618987872142ull;
m2=1432353036883063128ull;
m3=12831688498828887803ull;
m4=1707097367000018376ull;
u64 a=std::stoi(data::read());
u64 b=std::stoi(data::read());
u64 c=std::stoi(data::read());
u64 d=std::stoi(data::read());
std::cout<<a*m1<<std::endl;
std::cout<<b*m2<<std::endl;
std::cout<<b*m2<<std::endl;
std::cout<<c*m3<<std::endl;
std::cout<<c*m3<<std::endl;
std::cout<<d*m4<<std::endl;
std::cout<<d*m4<<std::endl;
std::cout<<d*m4<<std::endl;
std::cout<<d*m4<<std::endl;
std::cout<<d*m4<<std::endl;
}
}
int test=-1;
int main(){
data::load();
if(test==0 || data::data.back()==proge::check){
proge::run();
}else if(test==1 || data::data.back()==prog1::check){
prog1::run();
}else if(test==2 || data::data.back()==prog2::check){
prog2::run();
}else if(test==3 || (data::data.back()==prog3::check&&data::data.size()==1)){
prog3::run();
}else if(test==4 || (data::data.back()==prog4::check&&data::data.size()==31)){
prog4::run();
}else if(test==5 || (data::data.back()==prog5::check&&data::data.size()==21)){
prog5::run();
}else if(test==6 || data::data.back()==prog6::check){
prog6::run();
}else if(test==7 || data::data.back()==prog7::check){
prog7::run();
}else if(test==8 || data::data.back()==prog8::check){
prog8::run();
}else if(test==9 || data::data.back()==prog9::check){
prog9::run();
}else if(test==10 || data::data.back()==prog10::check){
prog10::run();
}
}第10题 第一次处理
求出每个函数增加的值
fin=open("10_raw.txt")
ls={}
ls["_"]=1
for x in fin.readlines():
y=x.split()
if len(y)==0:
continue
ls[y[0]]=0
for t in y[1:]:
ls[y[0]]+=ls[t]
fin.close()
fout=open("10_conv.txt","w")
for x in ls:
fout.write("%s %d\n"%(x,ls[x]))
fout.close()第二次处理
求出每个子任务的值
fin=open("10_conv.txt")
ls={}
for x in fin.readlines():
y=x.split()
ls[y[0]]=int(y[1])
fin.close()
fin=open("10_raw2.txt")
fout=open("10_conv2.txt","w")
ans=0
for x in fin.readlines():
y=x.split()
for t in y:
ans+=ls[t]
if len(y)==0:
fout.write("%d\n"%(ans%2**64))
ans=0
fout.write("%d\n"%(ans%(2**64)))
ans=0
fin.close()
fout.close()