
%\usepackage{xparse}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath, amssymb}
\usepackage{stmaryrd}
\renewcommand\i{\ensuremath{\boldsymbol{i}}}
\newcommand\C{\ensuremath{\mathbb{C}}}
\newcommand\R{\ensuremath{\mathbb{R}}}
\newcommand\Q{\ensuremath{\mathbb{Q}}}
\newcommand\D{\ensuremath{\mathbb{D}}}
\newcommand\Z{\ensuremath{\mathbb{Z}}}
\newcommand\N{\ensuremath{\mathbb{N}}}
\newcommand\Ne{\ensuremath{\mathbb{N}^*}}
\newcommand{\abs}[1]{\left\lvert #1 \right\rvert}
\newcommand{\tq}{tel que}
\newcommand\e{\ensuremath{\mathrm{e}}}
\newcommand\dt[1][t]{\ensuremath{\,\mathrm{d}#1}}

\usepackage[francais,bloc,completemulti]{automultiplechoice}
\usepackage[french]{babel}

\usepackage[locale = FR, detect-all, parse-numbers = true]{siunitx}
\selectlanguage{french}%
% siunitx's \ang does not work
\renewcommand\ang[2][]{%
	\SI[#1]{#2}{\degree}
}


\NewEnviron{qcm}[1]{%
	\def\arg{#1}%
	\expandafter\element\expandafter\arg\expandafter{\BODY\par}}

\usepackage{needspace}
\usepackage{multicol}
\setlength\columnsep{1em}
\setlength\columnseprule{1pt}

\usepackage{tkz-fct}
\usepackage{tkz-graph}
\usetikzlibrary{babel}

\newcommand{\logn}[1]{\ensuremath{\ln \left( #1 \right)}}
\usepackage{fp}
\def\AMCdecimalPoint{\raisebox{1ex}{\bf ,}}


\makeatletter
\renewcommand{\element}[2]{%
	\AMC@prepare@element{#1}%
	\global\csname #1@\romannumeral\AMCtok@k\endcsname={#2}%
}

\newif\if@tierstemps

\newcommand\tierstemps[1][4/3]{
	\@tierstempstrue
	\def\ttcoeff{#1}
}

\newcommand{\tierstempsbox}[1][4/3]{
	\if@tierstemps
	\begingroup
	\fbox{%
		\def\AMCbeginQuestion##1##2{}%
		\begin{question}{0} 
			\vspace{-\parskip}
			\vspace{-\abovedisplayskip}
			\QuestionIndicative
			\bareme{MAX=0, default.TIERSTEMPS=1, setglobal.TIERSTEMPS= TIERSTEMPS,
				formula=0}
			\hfill PAP avec tiers-temps\hfill %\hspace{-3em}
			\AMCOpen{lineup=true,backgroundcol=white!0,  lineuptext=\relax}{\correctchoice[T]{}\scoring{set.TIERSTEMPS=\ttcoeff, set.QNTT=0}}
		\end{question}
	} % fin de la fbox
	\endgroup
	\addtocounter{AMCquestionaff}{-1}
	\fi
}

\newif\ifqntt

\NewDocumentCommand{\QuestionSansTiersTemps}{m}{
	\textbf{Sauf en cas de tiers-temps.}\par\qntttrue\bareme{#1}\qnttfalse
}


\def\NoMC#1{
	set.MX=#1, MAX=#1, formula= NMC   > 0 ? 0 : (NB == 0 ? MX : NBC*MX/NB), e=0, v=NB==0?MX:0 }

\def\NoMCdefault{\NoMC{2}}
\def\AllOK#1{set.MX=#1, MAX=#1,
	formula= ( (NBC == NB)  && (NMC ==0 ) ) ? MX : 0  ,e=0,v=0}
\def\AllOKdefault{\AllOK{2}}

\def\MZ#1{\AllOK{#1}}
\def\MZdefault{\MZ{2}}

\baremeDefautM{\NoMCdefault}
\baremeDefautS{mz=2}

\def\OpenDefault#1{MAX=#1, set.MAX=#1, default.T=0, default.D=0, default.P=0, default.A=0, default.E=0, default.H=0, default.Q=0, default.Z=0, formula=(T+D+P+A+E+H+Q+Z) > MAX ? MAX : (T+D+P+A+E+H+Q+Z)}

\def\OpenBoxes{
	\correctchoice[T]{16}\scoring{set.E=16}
	\correctchoice[D]{8}\scoring{set.A=8}
	\correctchoice[P]{4}\scoring{set.P=4}
	\correctchoice[A]{2}\scoring{set.D=2}
	\correctchoice[E]{1}\scoring{set.T=1}
	\correctchoice[d]{$\frac{1}{2}$}\scoring{set.H=0.5}
	\correctchoice[q]{$\frac{1}{4}$}\scoring{set.Q=0.25}
	\correctchoice[z]{0}\scoring{set.Z=0}
}
\def\OpenBoxesShort{
	\correctchoice[P]{4}\scoring{set.P=4}
	\correctchoice[A]{2}\scoring{set.D=2}
	\correctchoice[E]{1}\scoring{set.T=1}
	\correctchoice[d]{$\frac{1}{2}$}\scoring{set.H=0.5}
	\correctchoice[z]{0}\scoring{set.Z=0}
}

\newcommand{\pie}[1]{%
	\begin{tikzpicture}
		\draw (0,0) circle (1ex);
		\fill (0,0) -- (0, 1ex)  arc [start angle = 90, delta angle = -#1, radius = 1ex] -- cycle;
	\end{tikzpicture}%
}

\AMCsetScoreZone{width=1.5em,height=1.5ex,depth=.5ex,position=question}

\usepackage[useregional]{datetime2}
\makeatletter
\def\@AouB{}
\NewDocumentCommand{\AouB}{o}{\IfNoValueTF{#1}
	{\def\@AouB{}}
	{\def\@AouB{#1 -- }}
}

\long\def\@intro{}
\NewDocumentCommand{\intro}{+m}{
	\long\def\@intro{\medskip #1\medskip\hrule}
}

\NewDocumentCommand{\entete}{}{
	{\noindent
		\setlength{\parindent}{0pt}
		%\fbox{
			\begin{minipage}[t]{0cm}
				\AMCcodeGrid[h]{etu}{ABCDEFGH,IJKLMNOP,QRSTUVWX,YZ123456}
			\end{minipage}
		%}
		\hspace{0.01\textwidth}
		\begin{minipage}[t]{0.475\linewidth}
			$\longleftarrow{}$\hspace{0pt plus 1cm} Quatre premières lettres distinctes de votre nom (tous si noms composés). Exemples:
			
			\centering{
			\begin{tabular}{r@{ $\rightarrow$ }l}
				POM\,-\,POKO & POMK \\
				TOTORO & TOR
			\end{tabular}
			}
			
			\champnom{\fbox{\begin{minipage}{\linewidth}
			\@AouB{}Nom et prénom :\par
			\vspace*{1em}\dotfill
			%\vspace*{1mm}
			\par
			\vspace*{1em}\dotfill
			\vspace*{1mm}
			\end{minipage}}}
			
		\end{minipage}
		\vspace{3ex}
		
		\begin{minipage}{\linewidth}
			\centering\large\bf  Mathématiques
			\medskip \hfill \@date\\ \@title
			\medskip
		\end{minipage}\hfill
		\hrule
		\@intro
		%\bigskip
	}	
}

%  #1 function name $f$ as default
%  #2  function algebric formula 
%  #3 list of xmin=-2, xmax=4, ymin=-1, ymax=5
%  #4  x position for function name, near f curve
\edef\grphscale{0.8}
\newcommand{\grphfct}[4][$f$]{
	\begin{tikzpicture}[scale=\grphscale, every node/.style={scale=\grphscale}]
		\pgfkeys{/yc/.cd,
			xmin/.store in=\xmin,
			xmin/.initial=0,
			xmax/.store in=\xmax,
			xmax/.initial=10,
			%			ymin/.store in=\ymin,
			%			ymin/.initial=0,
			%			ymax/.store in=\ymax,
			%			ymax/.initial=10,
		}
		\pgfkeys{/yc/.unknown/.code={}}
		\pgfkeys{/yc/.cd, #3}
		\tkzInit[#3] 
		\tkzGrid[step=5mm, line width=0.25pt]
		\tkzAxeXY
		\tkzFct[samples=100,domain=(\xmin):(\xmax)]{#2}
		\message{ FFFFF #4 FFFFFFFF}
		\tkzDefPointByFct(#4)
		\tkzText[below left](tkzPointResult){#1}
	\end{tikzpicture}
}
\newcommand{\grphfctcode}[5][$f$]{
	\begin{tikzpicture}[scale=\grphscale, every node/.style={scale=\grphscale}]
		\pgfkeys{/yc/.cd,
			xmin/.store in=\xmin,
			xmin/.initial=0,
			xmax/.store in=\xmax,
			xmax/.initial=10,
		}
		\pgfkeys{/yc/.unknown/.code={}}
		\pgfkeys{/yc/.cd, #3}
		\tkzInit[#3] 
		\tkzGrid[step=5mm, line width=0.25pt]
		\tkzAxeXY
		\tkzFct[samples=100,domain=(\xmin):(\xmax)]{#2}
		\message{ FFFFF #4 FFFFFFFF}
		\tkzDefPointByFct(#4)
		\tkzText[below left](tkzPointResult){#1}
		#5
	\end{tikzpicture}
}

\AMCinterBquest=2ex
\AMCinterBrep=0.8ex
\AMCinterIquest=10pt
\AMCinterIrep=0.4ex
\usepackage{rsfso}

\newcommand{\FPevalint}[2]{%
	\FPeval{#1}{clip(trunc((#2),0))}%
}

\newcommand{\FPrandbounds}[3]{%
	% This to make sure negative numbers will work
	\FPset\amin{#2}%
	\FPset\bmax{#3}%
	\FPeval{#1}{random*(bmax-amin+1) + amin}%
}

% bornes incluses
\newcommand{\FPrandint}[3]{%
	\FPrandbounds{#1}{#2}{#3}%
	\FPeval{#1}{clip(trunc(#1,0))}
	\ifnum #1=-0 \FPeval{#1}{0}\fi%
}

% bornes incluses
\newcommand\fproundNC[2]{%
	\fpeval{0+round(#1,#2,sign(#1))}
}

\edef\defaultRounding{2}
\NewDocumentCommand\fpround { m } {%
	\fpeval{0+round(#1,\defaultRounding,sign(#1))}
}

\NewDocumentCommand{\suite}
	{ m O{n} O{\N}}
	{\ensuremath{\left({#1}_{#2}\right)_{#2 \in #3}}}

\NewDocumentCommand{\limsuite}
	{m O{n}}
	{\ensuremath{\lim_{#2\rightarrow +\infty}#1_{#2}}}

\newcommand{\annoncegroupe}[1][2]{
	\hrule
	\medskip
	\centering{\bfseries Questions groupées }
	Les #1 questions suivantes portent sur la situation ci-dessous.
	\medskip
	\hrule
	\medskip
}
\newcommand{\vcoord}[2]{\ensuremath{%
		\begin{pmatrix}
			#1 \\ #2 %
\end{pmatrix}}}
\newcommand{\hcoord}[2]{\ensuremath{%
		\left(#1 ; #2\right)}}
\newcommand{\vect}[1]{\ensuremath{\overrightarrow{#1}}}

\newcommand{\csns}[1]{\ensuremath{\cos\left(#1\right)}}
\newcommand{\sns}[1]{\ensuremath{\sin\left(#1\right)}}
\newcommand{\tng}[1]{\ensuremath{\tan\left(#1\right)}}

\ExplSyntaxOn
%%% BUG FIX
\edef\Seed{42}

\NewDocumentCommand \fixSeed {O{\Seed}} {
	\FPseed\Seed
	\AMCrandomseed{\Seed}
	\sys_gset_rand_seed:n {\Seed}
 }

\NewExpandableDocumentCommand \randomint { m m } { \fp_eval:n { randint( #1, #2) }}


\NewExpandableDocumentCommand \rand {} { \fp_eval:n { rand() }}

\def\probacorrect{3/4}
% if #1 == n, will have 1/n chance to have not the ggod ansver
\NewExpandableDocumentCommand \RandomCorrectWrong { O{\probacorrect} +m +m } {
	\ifthenelse{\lengthtest{\rand pt<\fpeval{(#1)} pt}}
	{\bonne{#2}}
	{\mauvaise{#3}}
}

\NewExpandableDocumentCommand 
\RandomWrongWrong { O{\probacorrect} +m +m } {
	\ifthenelse{\lengthtest{\rand pt<\fpeval{(#1)} pt}}
	{\mauvaise{#2}}
	{\mauvaise{#3}}
}

\NewExpandableDocumentCommand 
\RandomCorrectCorrect { O{\probacorrect} +m +m } {
	\ifthenelse{\lengthtest{\rand pt<\fpeval{(#1)} pt}}
	{\bonne{#2}}
	{\bonne{#3}}
}

\ExplSyntaxOff	


\fixSeed

\makeatother
