static Node SwapFirst(Node c) {
if(c==null || c.next==null) return c;
Node result=c.next;
c.next=result.next;
result.next=c;
return result;
}
public static Node reverseMyList (Node c,int n) {
if(n==0) return SwapFirst(c);
Node d=c;
for(;n>1 && d.next!=null;n--) d=d.next;
if(n==1) d.next=SwapFirst(d.next);
return c;
}
void Check(int M[5][5]){
for(int a=0;a<5;a++){
for(int b=0;b<5;b++){
if(M[a][b]!=0) goto L1;
}
goto L2;
L1: continue;
}
printf("No\n"); return;
L2:
printf("Yes\n");
}
void Check(int M[5][5]){
int a,b;
a=0;
goto Loop1_Check;
Loop1_Body:
b=0;
goto Loop2_Check;
Loop2_Body:
if(M[a][b]!=0) goto L1;
b++;
Loop2_Check:
if(b<5) goto Loop2_Body;
goto L2;
L1:
Loop1_End:
a++;
Loop1_Check:
if(a<5) goto Loop1_Body;
printf("No\n"); goto Return;
L2:
printf("Yes\n");
Return: ;
}
void Check(int M[5][5]){
const int Return=0;
const int Loop1_Check=1;
const int Loop1_Body=2;
const int Loop2_Check=3;
const int Loop2_Body=4;
const int L1=5;
const int L2=6;
int state;
int a,b;
a=0;
state=Loop1_Check;
Loop1_Body:
b=0;
state=Loop2_Check;
Loop2_Body:
if(M[a][b]!=0) state=L1;
else{
b++;
state=Loop2_Check;
}
Loop2_Check:
if(b<5) state=Loop2_Body;
else state=L2;
L1:
a++;
state=Loop1_Check;
Loop1_Check:
if(a<5) state=Loop1_Body;
else{
printf("No\n");
state=Return;
}
L2:
printf("Yes\n");
state=Return;
Return: ;
}
void Check(int M[5][5]){
const int Return=0;
const int Loop1_Check=1;
const int Loop1_Body=2;
const int Loop2_Check=3;
const int Loop2_Body=4;
const int L1=5;
const int L2=6;
int state;
int a,b;
a=0;
state=Loop1_Check;
while(state!=Return){
switch(state){
case Loop1_Body:
b=0;
state=Loop2_Check;
break;
case Loop2_Body:
if(M[a][b]!=0) state=L1;
else{
b++;
state=Loop2_Check;
}
break;
case Loop2_Check:
if(b<5) state=Loop2_Body;
else state=L2;
break;
case L1:
a++;
state=Loop1_Check;
break;
case Loop1_Check:
if(a<5) state=Loop1_Body;
else{
printf("No\n");
state=Return;
}
break;
case L2:
printf("Yes\n");
state=Return;
break;
}
}
;
}
class Program{
static IEnumerable<int[]> Permutations(int N,Func<int,int,bool>Cond1,Func<int,int,int,int,bool> Cond2) {
int[] res=new int[N];
int k=0;
res[0]=0;
while(k>=0){
if(++res[k]<=N){
if(Cond1(k+1,res[k]) && GoodValue(res,k,Cond2)){
if(k==N-1) yield return res;
else res[++k]=0;
}
}else k--;
}
}
static private bool GoodValue(int[] res,int k,Func<int,int,int,int,bool> Cond) {
for(int i=0;i<k;i++) {
if(res[i]==res[k] || !Cond(i+1,res[i],k+1,res[k])) return false;
}
return true;
}
static int N=6;
static bool Cond1(int a,int va) {
if(a==1) return va==1;
if(a==N) return va==N;
return true;
}
static bool Cond2(int a,int va,int b,int vb) {
return Math.Abs(a-b)>1 || Math.Abs(va-vb)<=3;
}
static void Main(string[] args) {
foreach(int[] perm in Permutations(N,Cond1,Cond2))
Console.WriteLine(String.Join(",",perm));
}
}
1,2,3,4,5,6
1,2,3,5,4,6
1,2,4,3,5,6
1,2,4,5,3,6
1,2,5,3,4,6
1,2,5,4,3,6
1,3,2,4,5,6
1,3,2,5,4,6
1,3,4,2,5,6
1,3,5,2,4,6
1,4,2,3,5,6
1,4,2,5,3,6
1,4,3,2,5,6
1,4,5,2,3,6
if ((i < leftLen && left[i] < right[j]) || j >= rightLen) {
if ( j >= rightLen || (i < leftLen && left[i] < right[j])) {
mergeSort(array, mid, end);
if (start < end-1) {
int leftLen = mid - start,
left[leftLen];
int S0[37],S1[37],S2[37],S3[37],S4[37];
void Expand(int *a,int *b,int n){
int s0=0;
for(int i=0;i<n+10;i++){
if(i<n) s0+=a[i];
if(i>=10) s0-=a[i-10];
b[i]=s0;
}
}
int Conv(int *S,int balance,int m){
int res=0;
while(--m>=0) res+=S[m]*S4[balance+m];
return res;
}
// number of tickets in 0..u-1
int NHappy(int u){
int res=0;
int balance=0;
int digit;
digit=u/10000000; u%=10000000;
for(int i=0;i<digit;i++) res+=Conv(S3,balance++,28);
digit=u/1000000; u%=1000000;
for(int i=0;i<digit;i++) res+=Conv(S2,balance++,19);
digit=u/100000; u%=100000;
for(int i=0;i<digit;i++) res+=Conv(S1,balance++,10);
digit=u/10000; u%=10000;
for(int i=0;i<digit;i++) res+=S4[balance++];
digit=u/1000; u%=1000;
for(int i=0;i<digit && balance>=0;i++) res+=S3[balance--];
digit=u/100; u%=100;
for(int i=0;i<digit && balance>=0;i++) res+=S2[balance--];
digit=u/10; u%=10;
for(int i=0;i<digit && balance>=0;i++) res+=S1[balance--];
if(balance>=0 && balance<u) res++;
return res;
}
void __cdecl main(){
S0[0]=1;
Expand(S0,S1,10);
Expand(S1,S2,19);
Expand(S2,S3,28);
Expand(S3,S4,37);
int m,n;
for(;;){
printf("m,n: ");
scanf("%d%d",&m,&n);
printf("Result: %d\n",NHappy(99999999)+1-NHappy(m)-NHappy(99999999-n));
}
}
int S[8][38];
void InitS(){
for(int i=1;i<38;i++) S[0][i]=1;
for(int a=1;a<9;a++){
int s0=0;
for(int i=1;i<=37;i++){
s0+=S[a-1][i];
if(i>10) s0-=S[a-1][i-10];
S[a][i]=s0;
}
}
}
// number of tickets in 0..u-1
int NHappy(int u){
int res=0;
int balance=36;
for(int a=7,b=10000000;a>=0;a--,b/=10){
int digit=u/b;
u%=b;
int b1;
if(a>3){
balance-=9;
res+=S[a][b1=balance+digit]-S[a][balance];
} else{
b1=balance-digit; if(b1<0) b1=-1;
res+=S[a][balance+1]-S[a][b1+1];
}
balance=b1;
}
return res;
}
void __cdecl main(){
InitS();
int m,n;
for(;;){
printf("m,n: ");
scanf("%d%d",&m,&n);
printf("Result: %d\n",NHappy(99999999)+1-NHappy(m)-NHappy(99999999-n));
}
}